2017-04-25 05:24:57 +00:00
|
|
|
"""
|
2018-10-24 12:12:30 +00:00
|
|
|
Support for Zigbee Home Automation devices.
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/zha/
|
|
|
|
"""
|
2019-02-06 18:33:21 +00:00
|
|
|
import asyncio
|
2017-04-25 05:24:57 +00:00
|
|
|
import logging
|
2018-11-27 20:21:25 +00:00
|
|
|
import os
|
2018-12-10 15:59:50 +00:00
|
|
|
import types
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
from homeassistant import config_entries, const as ha_const
|
2018-12-04 10:38:57 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-12-01 09:31:49 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
# Loading the config flow file will register the flow
|
|
|
|
from . import config_flow # noqa # pylint: disable=unused-import
|
2019-01-11 19:34:29 +00:00
|
|
|
from . import api
|
2019-02-06 18:33:21 +00:00
|
|
|
from .core import ZHAGateway
|
|
|
|
from .core.const import (
|
2018-12-04 10:38:57 +00:00
|
|
|
COMPONENTS, CONF_BAUDRATE, CONF_DATABASE, CONF_DEVICE_CONFIG,
|
|
|
|
CONF_RADIO_TYPE, CONF_USB_PATH, DATA_ZHA, DATA_ZHA_BRIDGE_ID,
|
|
|
|
DATA_ZHA_CONFIG, DATA_ZHA_CORE_COMPONENT, DATA_ZHA_DISPATCHERS,
|
|
|
|
DATA_ZHA_RADIO, DEFAULT_BAUDRATE, DEFAULT_DATABASE_NAME,
|
2019-02-06 18:33:21 +00:00
|
|
|
DEFAULT_RADIO_TYPE, DOMAIN, RadioType, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS)
|
|
|
|
from .core.gateway import establish_device_mappings
|
2019-02-19 17:58:22 +00:00
|
|
|
from .core.channels.registry import populate_channel_registry
|
2018-11-27 20:21:25 +00:00
|
|
|
|
2018-02-06 00:05:19 +00:00
|
|
|
REQUIREMENTS = [
|
2019-02-20 15:34:59 +00:00
|
|
|
'bellows-homeassistant==0.7.1',
|
|
|
|
'zigpy-homeassistant==0.3.0',
|
|
|
|
'zigpy-xbee-homeassistant==0.1.2',
|
2019-03-09 20:14:58 +00:00
|
|
|
'zha-quirks==0.0.7',
|
2019-03-01 18:47:20 +00:00
|
|
|
'zigpy-deconz==0.1.2'
|
2018-02-06 00:05:19 +00:00
|
|
|
]
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
DEVICE_CONFIG_SCHEMA_ENTRY = vol.Schema({
|
|
|
|
vol.Optional(ha_const.CONF_TYPE): cv.string,
|
|
|
|
})
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
2018-11-27 20:21:25 +00:00
|
|
|
vol.Optional(
|
|
|
|
CONF_RADIO_TYPE,
|
|
|
|
default=DEFAULT_RADIO_TYPE
|
|
|
|
): cv.enum(RadioType),
|
2017-04-25 05:24:57 +00:00
|
|
|
CONF_USB_PATH: cv.string,
|
2018-11-27 20:21:25 +00:00
|
|
|
vol.Optional(CONF_BAUDRATE, default=DEFAULT_BAUDRATE): cv.positive_int,
|
|
|
|
vol.Optional(CONF_DATABASE): cv.string,
|
2017-04-25 05:24:57 +00:00
|
|
|
vol.Optional(CONF_DEVICE_CONFIG, default={}):
|
|
|
|
vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
|
2018-12-12 16:06:22 +00:00
|
|
|
vol.Optional(ENABLE_QUIRKS, default=True): cv.boolean,
|
2017-04-25 05:24:57 +00:00
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2018-10-24 12:12:30 +00:00
|
|
|
# Zigbee definitions
|
2017-04-25 05:24:57 +00:00
|
|
|
CENTICELSIUS = 'C-100'
|
|
|
|
|
|
|
|
# Internal definitions
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def async_setup(hass, config):
|
2018-11-27 20:21:25 +00:00
|
|
|
"""Set up ZHA from config."""
|
|
|
|
hass.data[DATA_ZHA] = {}
|
|
|
|
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_CONFIG] = conf
|
|
|
|
|
|
|
|
if not hass.config_entries.async_entries(DOMAIN):
|
|
|
|
hass.async_create_task(hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={'source': config_entries.SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
CONF_USB_PATH: conf[CONF_USB_PATH],
|
2019-01-04 19:00:26 +00:00
|
|
|
CONF_RADIO_TYPE: conf.get(CONF_RADIO_TYPE).value
|
2018-11-27 20:21:25 +00:00
|
|
|
}
|
|
|
|
))
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up ZHA.
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
Will automatically load components to support devices found on the network.
|
|
|
|
"""
|
2019-02-06 18:33:21 +00:00
|
|
|
establish_device_mappings()
|
2019-02-19 17:58:22 +00:00
|
|
|
populate_channel_registry()
|
2019-02-06 18:33:21 +00:00
|
|
|
|
|
|
|
for component in COMPONENTS:
|
|
|
|
hass.data[DATA_ZHA][component] = (
|
|
|
|
hass.data[DATA_ZHA].get(component, {})
|
|
|
|
)
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
hass.data[DATA_ZHA] = hass.data.get(DATA_ZHA, {})
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_DISPATCHERS] = []
|
|
|
|
config = hass.data[DATA_ZHA].get(DATA_ZHA_CONFIG, {})
|
|
|
|
|
2019-01-04 19:00:26 +00:00
|
|
|
if config.get(ENABLE_QUIRKS, True):
|
|
|
|
# needs to be done here so that the ZHA module is finished loading
|
|
|
|
# before zhaquirks is imported
|
|
|
|
# pylint: disable=W0611, W0612
|
|
|
|
import zhaquirks # noqa
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
usb_path = config_entry.data.get(CONF_USB_PATH)
|
|
|
|
baudrate = config.get(CONF_BAUDRATE, DEFAULT_BAUDRATE)
|
|
|
|
radio_type = config_entry.data.get(CONF_RADIO_TYPE)
|
|
|
|
if radio_type == RadioType.ezsp.name:
|
2018-02-06 18:46:28 +00:00
|
|
|
import bellows.ezsp
|
|
|
|
from bellows.zigbee.application import ControllerApplication
|
|
|
|
radio = bellows.ezsp.EZSP()
|
2018-12-01 09:31:49 +00:00
|
|
|
radio_description = "EZSP"
|
2018-11-27 20:21:25 +00:00
|
|
|
elif radio_type == RadioType.xbee.name:
|
2018-02-06 18:46:28 +00:00
|
|
|
import zigpy_xbee.api
|
|
|
|
from zigpy_xbee.zigbee.application import ControllerApplication
|
|
|
|
radio = zigpy_xbee.api.XBee()
|
2018-12-01 09:31:49 +00:00
|
|
|
radio_description = "XBee"
|
2019-01-17 00:09:09 +00:00
|
|
|
elif radio_type == RadioType.deconz.name:
|
|
|
|
import zigpy_deconz.api
|
|
|
|
from zigpy_deconz.zigbee.application import ControllerApplication
|
|
|
|
radio = zigpy_deconz.api.Deconz()
|
|
|
|
radio_description = "Deconz"
|
2018-02-06 18:46:28 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
await radio.connect(usb_path, baudrate)
|
2018-11-27 20:21:25 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_RADIO] = radio
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
if CONF_DATABASE in config:
|
|
|
|
database = config[CONF_DATABASE]
|
|
|
|
else:
|
|
|
|
database = os.path.join(hass.config.config_dir, DEFAULT_DATABASE_NAME)
|
2018-12-10 15:59:50 +00:00
|
|
|
|
|
|
|
# patch zigpy listener to prevent flooding logs with warnings due to
|
|
|
|
# how zigpy implemented its listeners
|
|
|
|
from zigpy.appdb import ClusterPersistingListener
|
|
|
|
|
|
|
|
def zha_send_event(self, cluster, command, args):
|
|
|
|
pass
|
|
|
|
|
|
|
|
ClusterPersistingListener.zha_send_event = types.MethodType(
|
|
|
|
zha_send_event,
|
|
|
|
ClusterPersistingListener
|
|
|
|
)
|
|
|
|
|
2019-01-26 18:28:13 +00:00
|
|
|
zha_gateway = ZHAGateway(hass, config)
|
2019-02-06 18:33:21 +00:00
|
|
|
|
|
|
|
# Patch handle_message until zigpy can provide an event here
|
|
|
|
def handle_message(sender, is_reply, profile, cluster,
|
|
|
|
src_ep, dst_ep, tsn, command_id, args):
|
|
|
|
"""Handle message from a device."""
|
2019-02-13 13:52:29 +00:00
|
|
|
if not sender.initializing and sender.ieee in zha_gateway.devices and \
|
|
|
|
not zha_gateway.devices[sender.ieee].available:
|
2019-02-26 18:48:10 +00:00
|
|
|
zha_gateway.async_device_became_available(
|
|
|
|
sender, is_reply, profile, cluster, src_ep, dst_ep, tsn,
|
|
|
|
command_id, args
|
2019-02-13 13:52:29 +00:00
|
|
|
)
|
2019-02-06 18:33:21 +00:00
|
|
|
return sender.handle_message(
|
|
|
|
is_reply, profile, cluster, src_ep, dst_ep, tsn, command_id, args)
|
|
|
|
|
|
|
|
application_controller = ControllerApplication(radio, database)
|
|
|
|
application_controller.handle_message = handle_message
|
2019-01-26 18:28:13 +00:00
|
|
|
application_controller.add_listener(zha_gateway)
|
2018-12-25 08:20:09 +00:00
|
|
|
await application_controller.startup(auto_form=True)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2019-02-06 18:33:21 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(application_controller.ieee)
|
|
|
|
|
|
|
|
init_tasks = []
|
2018-12-25 08:20:09 +00:00
|
|
|
for device in application_controller.devices.values():
|
2019-02-06 18:33:21 +00:00
|
|
|
init_tasks.append(zha_gateway.async_device_initialized(device, False))
|
|
|
|
await asyncio.gather(*init_tasks)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-12-01 09:31:49 +00:00
|
|
|
device_registry = await \
|
|
|
|
hass.helpers.device_registry.async_get_registry()
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
2018-12-25 08:20:09 +00:00
|
|
|
connections={(CONNECTION_ZIGBEE, str(application_controller.ieee))},
|
|
|
|
identifiers={(DOMAIN, str(application_controller.ieee))},
|
2018-12-01 09:31:49 +00:00
|
|
|
name="Zigbee Coordinator",
|
|
|
|
manufacturer="ZHA",
|
|
|
|
model=radio_description,
|
|
|
|
)
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
for component in COMPONENTS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
|
|
|
config_entry, component)
|
|
|
|
)
|
|
|
|
|
2019-01-26 18:28:13 +00:00
|
|
|
api.async_load_api(hass, application_controller, zha_gateway)
|
2018-02-12 04:34:19 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
def zha_shutdown(event):
|
|
|
|
"""Close radio."""
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_RADIO].close()
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(ha_const.EVENT_HOMEASSISTANT_STOP, zha_shutdown)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload ZHA config entry."""
|
2019-01-11 19:34:29 +00:00
|
|
|
api.async_unload_api(hass)
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
dispatchers = hass.data[DATA_ZHA].get(DATA_ZHA_DISPATCHERS, [])
|
|
|
|
for unsub_dispatcher in dispatchers:
|
|
|
|
unsub_dispatcher()
|
|
|
|
|
|
|
|
for component in COMPONENTS:
|
|
|
|
await hass.config_entries.async_forward_entry_unload(
|
|
|
|
config_entry, component)
|
|
|
|
|
|
|
|
# clean up device entities
|
|
|
|
component = hass.data[DATA_ZHA][DATA_ZHA_CORE_COMPONENT]
|
|
|
|
entity_ids = [entity.entity_id for entity in component.entities]
|
|
|
|
for entity_id in entity_ids:
|
|
|
|
await component.async_remove_entity(entity_id)
|
|
|
|
|
2018-12-10 15:59:50 +00:00
|
|
|
# clean up events
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_CORE_EVENTS].clear()
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
_LOGGER.debug("Closing zha radio")
|
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_RADIO].close()
|
|
|
|
|
|
|
|
del hass.data[DATA_ZHA]
|
2017-04-25 05:24:57 +00:00
|
|
|
return True
|