-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Add Google pubsub component #20049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Google pubsub component #20049
Changes from 5 commits
4ce0813
c6dd89a
f8f3d6f
cb66716
a85faf6
3a6fdf3
e968410
20022ca
81d7b55
c08a13a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import datetime | ||
| import json | ||
| import logging | ||
| import os | ||
| from typing import Dict, Any | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.const import ( | ||
| EVENT_STATE_CHANGED, STATE_UNKNOWN, STATE_UNAVAILABLE) | ||
| from homeassistant.core import HomeAssistant, Event | ||
| from homeassistant.helpers.entityfilter import FILTER_SCHEMA | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| REQUIREMENTS = ['google-cloud-pubsub==0.39.1'] | ||
|
|
||
| DOMAIN = 'google_pubsub' | ||
|
|
||
| CONF_PROJECT_ID = 'project_id' | ||
| CONF_TOPIC_NAME = 'topic_name' | ||
| CONF_SERVICE_PRINCIPAL = 'credentials_json' | ||
| CONF_FILTER = 'filter' | ||
|
|
||
| CONFIG_SCHEMA = vol.Schema({ | ||
| DOMAIN: vol.Schema({ | ||
| vol.Required(CONF_PROJECT_ID): cv.string, | ||
| vol.Required(CONF_TOPIC_NAME): cv.string, | ||
| vol.Required(CONF_SERVICE_PRINCIPAL): cv.string, | ||
| vol.Required(CONF_FILTER): FILTER_SCHEMA | ||
| }), | ||
| }, extra=vol.ALLOW_EXTRA) | ||
|
|
||
|
|
||
| def setup(hass: HomeAssistant, yaml_config: Dict[str, Any]): | ||
|
timvancann marked this conversation as resolved.
|
||
| from google.cloud import pubsub_v1 | ||
|
|
||
| config = yaml_config.get(DOMAIN, {}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be missing. Changed it to |
||
| project_id = config[CONF_PROJECT_ID] | ||
| topic_name = config[CONF_TOPIC_NAME] | ||
| service_principal_path = os.path.join(hass.config.config_dir, | ||
| config[CONF_SERVICE_PRINCIPAL]) | ||
|
|
||
| os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = service_principal_path | ||
|
timvancann marked this conversation as resolved.
Outdated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some way to pass this without modifying the global environment?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did some digging, and yes, there is 😄. Fixed it |
||
|
|
||
| entities_filter = config[CONF_FILTER] | ||
|
|
||
| publisher = pubsub_v1.PublisherClient() | ||
| topic_path = publisher.topic_path(project_id, topic_name) | ||
|
timvancann marked this conversation as resolved.
Outdated
|
||
|
|
||
| encoder = DateTimeJSONEncoder() | ||
|
|
||
| def send_to_pubsub(event: Event): | ||
|
timvancann marked this conversation as resolved.
|
||
| state = event.data.get('new_state') | ||
| if (state is None | ||
| or state.state in (STATE_UNKNOWN, '', STATE_UNAVAILABLE) | ||
| or not entities_filter(state.entity_id)): | ||
| return | ||
|
|
||
| as_dict = state.as_dict() | ||
| data = json.dumps( | ||
| obj=as_dict, | ||
| default=encoder.encode | ||
| ).encode('utf-8') | ||
|
|
||
| publisher.publish(topic_path, data=data) | ||
|
timvancann marked this conversation as resolved.
|
||
|
|
||
| hass.bus.listen(EVENT_STATE_CHANGED, send_to_pubsub) | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| class DateTimeJSONEncoder(json.JSONEncoder): | ||
|
timvancann marked this conversation as resolved.
|
||
| def default(self, obj): | ||
| if isinstance(obj, datetime.datetime): | ||
| return obj.isoformat() | ||
| else: | ||
| return super(DateTimeJSONEncoder, self).default(obj) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from datetime import datetime | ||
|
|
||
| from homeassistant.components.google_pubsub import ( | ||
| DateTimeJSONEncoder as victim) | ||
|
|
||
|
|
||
| class TestDateTimeJSONEncoder(object): | ||
|
|
||
| def test_datetime(self): | ||
| time = datetime(2019, 1, 13, 12, 30, 5) | ||
| assert victim().encode(time) == '"2019-01-13T12:30:05"' | ||
|
|
||
| def test_no_datetime(self): | ||
| assert victim().encode(42) == '42' | ||
|
|
||
| def test_nested(self): | ||
| assert victim().encode({'foo': 'bar'}) == '{"foo": "bar"}' |
Uh oh!
There was an error while loading. Please reload this page.