Add config entry for ZHA (#18352)
* Add support for zha config entries * Add support for zha config entries * Fix node_config retrieval * Dynamically load discovered entities * Restore device config support * Refactor loading of entities * Remove device registry support * Send discovery_info directly * Clean up discovery_info in hass.data * Update tests * Clean up rebase * Simplify config flow * Address comments * Fix config path and zigpy check timeout * Remove device entities when unloading config entry
This commit is contained in:
1 parent
43676fcaf4
commit
052d305243
14 files changed
+567
-135
No files matched your search
@@ -5,51 +5,47 @@ For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/zha/
|
||||
"""
|
||||
import collections
|
||||
import enum
|
||||
import logging
|
||||
import os
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant import const as ha_const
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.components.zha.entities import ZhaDeviceEntity
|
||||
from homeassistant import config_entries, const as ha_const
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from . import const as zha_const
|
||||
|
||||
# Loading the config flow file will register the flow
|
||||
from . import config_flow # noqa # pylint: disable=unused-import
|
||||
from .const import (
|
||||
DOMAIN, COMPONENTS, CONF_BAUDRATE, CONF_DATABASE, CONF_RADIO_TYPE,
|
||||
CONF_USB_PATH, CONF_DEVICE_CONFIG, ZHA_DISCOVERY_NEW, DATA_ZHA,
|
||||
DATA_ZHA_CONFIG, DATA_ZHA_BRIDGE_ID, DATA_ZHA_RADIO, DATA_ZHA_DISPATCHERS,
|
||||
DATA_ZHA_CORE_COMPONENT, DEFAULT_RADIO_TYPE, DEFAULT_DATABASE_NAME,
|
||||
DEFAULT_BAUDRATE, RadioType
|
||||
)
|
||||
|
||||
REQUIREMENTS = [
|
||||
'bellows==0.7.0',
|
||||
'zigpy==0.2.0',
|
||||
'zigpy-xbee==0.1.1',
|
||||
]
|
||||
|
||||
DOMAIN = 'zha'
|
||||
|
||||
|
||||
class RadioType(enum.Enum):
|
||||
"""Possible options for radio type in config."""
|
||||
|
||||
ezsp = 'ezsp'
|
||||
xbee = 'xbee'
|
||||
|
||||
|
||||
CONF_BAUDRATE = 'baudrate'
|
||||
CONF_DATABASE = 'database_path'
|
||||
CONF_DEVICE_CONFIG = 'device_config'
|
||||
CONF_RADIO_TYPE = 'radio_type'
|
||||
CONF_USB_PATH = 'usb_path'
|
||||
DATA_DEVICE_CONFIG = 'zha_device_config'
|
||||
|
||||
DEVICE_CONFIG_SCHEMA_ENTRY = vol.Schema({
|
||||
vol.Optional(ha_const.CONF_TYPE): cv.string,
|
||||
})
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_RADIO_TYPE, default='ezsp'): cv.enum(RadioType),
|
||||
vol.Optional(
|
||||
CONF_RADIO_TYPE,
|
||||
default=DEFAULT_RADIO_TYPE
|
||||
): cv.enum(RadioType),
|
||||
CONF_USB_PATH: cv.string,
|
||||
vol.Optional(CONF_BAUDRATE, default=57600): cv.positive_int,
|
||||
CONF_DATABASE: cv.string,
|
||||
vol.Optional(CONF_BAUDRATE, default=DEFAULT_BAUDRATE): cv.positive_int,
|
||||
vol.Optional(CONF_DATABASE): cv.string,
|
||||
vol.Optional(CONF_DEVICE_CONFIG, default={}):
|
||||
vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
|
||||
})
|
||||
@@ -73,8 +69,6 @@ SERVICE_SCHEMAS = {
|
||||
|
||||
# Zigbee definitions
|
||||
CENTICELSIUS = 'C-100'
|
||||
# Key in hass.data dict containing discovery info
|
||||
DISCOVERY_KEY = 'zha_discovery_info'
|
||||
|
||||
# Internal definitions
|
||||
APPLICATION_CONTROLLER = None
|
||||
@@ -82,27 +76,58 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""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],
|
||||
CONF_RADIO_TYPE: conf.get(CONF_RADIO_TYPE).value
|
||||
}
|
||||
))
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
"""Set up ZHA.
|
||||
|
||||
Will automatically load components to support devices found on the network.
|
||||
"""
|
||||
global APPLICATION_CONTROLLER
|
||||
|
||||
usb_path = config[DOMAIN].get(CONF_USB_PATH)
|
||||
baudrate = config[DOMAIN].get(CONF_BAUDRATE)
|
||||
radio_type = config[DOMAIN].get(CONF_RADIO_TYPE)
|
||||
if radio_type == RadioType.ezsp:
|
||||
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, {})
|
||||
|
||||
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:
|
||||
import bellows.ezsp
|
||||
from bellows.zigbee.application import ControllerApplication
|
||||
radio = bellows.ezsp.EZSP()
|
||||
elif radio_type == RadioType.xbee:
|
||||
elif radio_type == RadioType.xbee.name:
|
||||
import zigpy_xbee.api
|
||||
from zigpy_xbee.zigbee.application import ControllerApplication
|
||||
radio = zigpy_xbee.api.XBee()
|
||||
|
||||
await radio.connect(usb_path, baudrate)
|
||||
hass.data[DATA_ZHA][DATA_ZHA_RADIO] = radio
|
||||
|
||||
database = config[DOMAIN].get(CONF_DATABASE)
|
||||
if CONF_DATABASE in config:
|
||||
database = config[CONF_DATABASE]
|
||||
else:
|
||||
database = os.path.join(hass.config.config_dir, DEFAULT_DATABASE_NAME)
|
||||
APPLICATION_CONTROLLER = ControllerApplication(radio, database)
|
||||
listener = ApplicationListener(hass, config)
|
||||
APPLICATION_CONTROLLER.add_listener(listener)
|
||||
@@ -112,6 +137,14 @@ async def async_setup(hass, config):
|
||||
hass.async_create_task(
|
||||
listener.async_device_initialized(device, False))
|
||||
|
||||
hass.data[DATA_ZHA][DATA_ZHA_BRIDGE_ID] = str(APPLICATION_CONTROLLER.ieee)
|
||||
|
||||
for component in COMPONENTS:
|
||||
hass.async_create_task(
|
||||
hass.config_entries.async_forward_entry_setup(
|
||||
config_entry, component)
|
||||
)
|
||||
|
||||
async def permit(service):
|
||||
"""Allow devices to join this network."""
|
||||
duration = service.data.get(ATTR_DURATION)
|
||||
@@ -132,6 +165,37 @@ async def async_setup(hass, config):
|
||||
hass.services.async_register(DOMAIN, SERVICE_REMOVE, remove,
|
||||
schema=SERVICE_SCHEMAS[SERVICE_REMOVE])
|
||||
|
||||
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."""
|
||||
hass.services.async_remove(DOMAIN, SERVICE_PERMIT)
|
||||
hass.services.async_remove(DOMAIN, SERVICE_REMOVE)
|
||||
|
||||
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)
|
||||
|
||||
_LOGGER.debug("Closing zha radio")
|
||||
hass.data[DATA_ZHA][DATA_ZHA_RADIO].close()
|
||||
|
||||
del hass.data[DATA_ZHA]
|
||||
return True
|
||||
|
||||
|
||||
@@ -144,9 +208,14 @@ class ApplicationListener:
|
||||
self._config = config
|
||||
self._component = EntityComponent(_LOGGER, DOMAIN, hass)
|
||||
self._device_registry = collections.defaultdict(list)
|
||||
hass.data[DISCOVERY_KEY] = hass.data.get(DISCOVERY_KEY, {})
|
||||
zha_const.populate_data()
|
||||
|
||||
for component in COMPONENTS:
|
||||
hass.data[DATA_ZHA][component] = (
|
||||
hass.data[DATA_ZHA].get(component, {})
|
||||
)
|
||||
hass.data[DATA_ZHA][DATA_ZHA_CORE_COMPONENT] = self._component
|
||||
|
||||
def device_joined(self, device):
|
||||
"""Handle device joined.
|
||||
|
||||
@@ -193,8 +262,11 @@ class ApplicationListener:
|
||||
component = None
|
||||
profile_clusters = ([], [])
|
||||
device_key = "{}-{}".format(device.ieee, endpoint_id)
|
||||
node_config = self._config[DOMAIN][CONF_DEVICE_CONFIG].get(
|
||||
device_key, {})
|
||||
node_config = {}
|
||||
if CONF_DEVICE_CONFIG in self._config:
|
||||
node_config = self._config[CONF_DEVICE_CONFIG].get(
|
||||
device_key, {}
|
||||
)
|
||||
|
||||
if endpoint.profile_id in zigpy.profiles.PROFILES:
|
||||
profile = zigpy.profiles.PROFILES[endpoint.profile_id]
|
||||
@@ -226,15 +298,17 @@ class ApplicationListener:
|
||||
'new_join': join,
|
||||
'unique_id': device_key,
|
||||
}
|
||||
self._hass.data[DISCOVERY_KEY][device_key] = discovery_info
|
||||
|
||||
await discovery.async_load_platform(
|
||||
self._hass,
|
||||
component,
|
||||
DOMAIN,
|
||||
{'discovery_key': device_key},
|
||||
self._config,
|
||||
)
|
||||
if join:
|
||||
async_dispatcher_send(
|
||||
self._hass,
|
||||
ZHA_DISCOVERY_NEW.format(component),
|
||||
discovery_info
|
||||
)
|
||||
else:
|
||||
self._hass.data[DATA_ZHA][component][device_key] = (
|
||||
discovery_info
|
||||
)
|
||||
|
||||
for cluster in endpoint.in_clusters.values():
|
||||
await self._attempt_single_cluster_device(
|
||||
@@ -309,12 +383,12 @@ class ApplicationListener:
|
||||
discovery_info[discovery_attr] = {cluster.cluster_id: cluster}
|
||||
if sub_component:
|
||||
discovery_info.update({'sub_component': sub_component})
|
||||
self._hass.data[DISCOVERY_KEY][cluster_key] = discovery_info
|
||||
|
||||
await discovery.async_load_platform(
|
||||
self._hass,
|
||||
component,
|
||||
DOMAIN,
|
||||
{'discovery_key': cluster_key},
|
||||
self._config,
|
||||
)
|
||||
if is_new_join:
|
||||
async_dispatcher_send(
|
||||
self._hass,
|
||||
ZHA_DISCOVERY_NEW.format(component),
|
||||
discovery_info
|
||||
)
|
||||
else:
|
||||
self._hass.data[DATA_ZHA][component][cluster_key] = discovery_info
|
||||
Reference in new issue
Block a user