diff --git a/.coveragerc b/.coveragerc index df371e1b683..9f6bb0d1b95 100644 --- a/.coveragerc +++ b/.coveragerc @@ -47,6 +47,7 @@ omit = homeassistant/components/august/* homeassistant/components/automatic/device_tracker.py homeassistant/components/avion/light.py + homeassistant/components/azure_event_hub/* homeassistant/components/baidu/tts.py homeassistant/components/bbb_gpio/* homeassistant/components/bbox/device_tracker.py diff --git a/CODEOWNERS b/CODEOWNERS index a1581d4720c..1e7c3c87a07 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -32,6 +32,7 @@ homeassistant/components/automatic/* @armills homeassistant/components/automation/* @home-assistant/core homeassistant/components/aws/* @awarecan @robbiet480 homeassistant/components/axis/* @kane610 +homeassistant/components/azure_event_hub/* @eavanvalkenburg homeassistant/components/bitcoin/* @fabaff homeassistant/components/bizkaibus/* @UgaitzEtxebarria homeassistant/components/blink/* @fronzbot diff --git a/homeassistant/components/azure_event_hub/__init__.py b/homeassistant/components/azure_event_hub/__init__.py new file mode 100644 index 00000000000..c5362fe1821 --- /dev/null +++ b/homeassistant/components/azure_event_hub/__init__.py @@ -0,0 +1,80 @@ +"""Support for Azure Event Hubs.""" +import json +import logging +from typing import Any, Dict + +import voluptuous as vol +from azure.eventhub import EventData, EventHubClientAsync + +from homeassistant.const import ( + EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED, STATE_UNAVAILABLE, + STATE_UNKNOWN) +from homeassistant.core import Event, HomeAssistant +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entityfilter import FILTER_SCHEMA +from homeassistant.helpers.json import JSONEncoder + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'azure_event_hub' + +CONF_EVENT_HUB_NAMESPACE = 'event_hub_namespace' +CONF_EVENT_HUB_INSTANCE_NAME = 'event_hub_instance_name' +CONF_EVENT_HUB_SAS_POLICY = 'event_hub_sas_policy' +CONF_EVENT_HUB_SAS_KEY = 'event_hub_sas_key' +CONF_FILTER = 'filter' + +CONFIG_SCHEMA = vol.Schema({ + DOMAIN: vol.Schema({ + vol.Required(CONF_EVENT_HUB_NAMESPACE): cv.string, + vol.Required(CONF_EVENT_HUB_INSTANCE_NAME): cv.string, + vol.Required(CONF_EVENT_HUB_SAS_POLICY): cv.string, + vol.Required(CONF_EVENT_HUB_SAS_KEY): cv.string, + vol.Required(CONF_FILTER): FILTER_SCHEMA, + }), +}, extra=vol.ALLOW_EXTRA) + + +async def async_setup(hass: HomeAssistant, yaml_config: Dict[str, Any]): + """Activate Azure EH component.""" + config = yaml_config[DOMAIN] + + event_hub_address = "amqps://{}.servicebus.windows.net/{}".format( + config[CONF_EVENT_HUB_NAMESPACE], + config[CONF_EVENT_HUB_INSTANCE_NAME]) + entities_filter = config[CONF_FILTER] + + client = EventHubClientAsync( + event_hub_address, + debug=True, + username=config[CONF_EVENT_HUB_SAS_POLICY], + password=config[CONF_EVENT_HUB_SAS_KEY]) + async_sender = client.add_async_sender() + await client.run_async() + + encoder = JSONEncoder() + + async def async_send_to_event_hub(event: Event): + """Send states to Event Hub.""" + 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 + + event_data = EventData( + json.dumps( + obj=state.as_dict(), + default=encoder.encode + ).encode('utf-8') + ) + await async_sender.send(event_data) + + async def async_shutdown(event: Event): + """Shut down the client.""" + await client.stop_async() + + hass.bus.async_listen(EVENT_STATE_CHANGED, async_send_to_event_hub) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_shutdown) + + return True diff --git a/homeassistant/components/azure_event_hub/manifest.json b/homeassistant/components/azure_event_hub/manifest.json new file mode 100644 index 00000000000..e2223fc97c3 --- /dev/null +++ b/homeassistant/components/azure_event_hub/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "azure_event_hub", + "name": "Azure Event Hub", + "documentation": "https://www.home-assistant.io/components/azure_event_hub", + "requirements": ["azure-eventhub==1.3.1"], + "dependencies": [], + "codeowners": ["@eavanvalkenburg"] + } \ No newline at end of file diff --git a/requirements_all.txt b/requirements_all.txt index 545ff132bed..3287c6ef46e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -216,6 +216,9 @@ av==6.1.2 # homeassistant.components.axis axis==23 +# homeassistant.components.azure_event_hub +azure-eventhub==1.3.1 + # homeassistant.components.baidu baidu-aip==1.6.6