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/
|
|
|
|
"""
|
2018-02-12 04:34:19 +00:00
|
|
|
import collections
|
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
|
|
|
from homeassistant.components.zha.entities import ZhaDeviceEntity
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-12-01 09:31:49 +00:00
|
|
|
from homeassistant.helpers.device_registry import CONNECTION_ZIGBEE
|
2018-12-04 10:38:57 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
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
|
2018-12-04 10:38:57 +00:00
|
|
|
from . import const as zha_const
|
2018-12-10 15:59:50 +00:00
|
|
|
from .event import ZhaEvent
|
2018-11-27 20:21:25 +00:00
|
|
|
from .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,
|
2018-12-10 15:59:50 +00:00
|
|
|
DEFAULT_RADIO_TYPE, DOMAIN, ZHA_DISCOVERY_NEW, RadioType,
|
2018-12-12 16:06:22 +00:00
|
|
|
EVENTABLE_CLUSTERS, DATA_ZHA_CORE_EVENTS, ENABLE_QUIRKS)
|
2018-11-27 20:21:25 +00:00
|
|
|
|
2018-02-06 00:05:19 +00:00
|
|
|
REQUIREMENTS = [
|
2018-09-04 05:46:27 +00:00
|
|
|
'bellows==0.7.0',
|
|
|
|
'zigpy==0.2.0',
|
2018-05-21 04:57:09 +00:00
|
|
|
'zigpy-xbee==0.1.1',
|
2018-12-12 16:06:22 +00:00
|
|
|
'zha-quirks==0.0.5'
|
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)
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
ATTR_DURATION = 'duration'
|
2018-02-12 04:34:19 +00:00
|
|
|
ATTR_IEEE = 'ieee_address'
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
SERVICE_PERMIT = 'permit'
|
2018-02-12 04:34:19 +00:00
|
|
|
SERVICE_REMOVE = 'remove'
|
2017-04-25 05:24:57 +00:00
|
|
|
SERVICE_SCHEMAS = {
|
|
|
|
SERVICE_PERMIT: vol.Schema({
|
|
|
|
vol.Optional(ATTR_DURATION, default=60):
|
|
|
|
vol.All(vol.Coerce(int), vol.Range(1, 254)),
|
|
|
|
}),
|
2018-02-12 04:34:19 +00:00
|
|
|
SERVICE_REMOVE: vol.Schema({
|
|
|
|
vol.Required(ATTR_IEEE): cv.string,
|
|
|
|
}),
|
2017-04-25 05:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-24 12:12:30 +00:00
|
|
|
# Zigbee definitions
|
2017-04-25 05:24:57 +00:00
|
|
|
CENTICELSIUS = 'C-100'
|
|
|
|
|
|
|
|
# Internal definitions
|
|
|
|
APPLICATION_CONTROLLER = None
|
|
|
|
_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],
|
2018-12-12 16:06:22 +00:00
|
|
|
CONF_RADIO_TYPE: conf.get(CONF_RADIO_TYPE).value,
|
|
|
|
ENABLE_QUIRKS: conf[ENABLE_QUIRKS]
|
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.
|
|
|
|
"""
|
2018-12-12 16:06:22 +00:00
|
|
|
if config_entry.data.get(ENABLE_QUIRKS):
|
|
|
|
# needs to be done here so that the ZHA module is finished loading
|
|
|
|
# before zhaquirks is imported
|
|
|
|
# pylint: disable=W0611, W0612
|
|
|
|
import zhaquirks # noqa
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
global APPLICATION_CONTROLLER
|
|
|
|
|
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, {})
|
|
|
|
|
|
|
|
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"
|
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
|
|
|
|
)
|
|
|
|
|
2018-02-06 18:46:28 +00:00
|
|
|
APPLICATION_CONTROLLER = ControllerApplication(radio, database)
|
2017-04-25 05:24:57 +00:00
|
|
|
listener = ApplicationListener(hass, config)
|
|
|
|
APPLICATION_CONTROLLER.add_listener(listener)
|
2018-03-12 20:57:13 +00:00
|
|
|
await APPLICATION_CONTROLLER.startup(auto_form=True)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
for device in APPLICATION_CONTROLLER.devices.values():
|
2018-10-02 09:03:09 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
listener.async_device_initialized(device, False))
|
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,
|
|
|
|
connections={(CONNECTION_ZIGBEE, str(APPLICATION_CONTROLLER.ieee))},
|
|
|
|
identifiers={(DOMAIN, str(APPLICATION_CONTROLLER.ieee))},
|
|
|
|
name="Zigbee Coordinator",
|
|
|
|
manufacturer="ZHA",
|
|
|
|
model=radio_description,
|
|
|
|
)
|
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def permit(service):
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Allow devices to join this network."""
|
|
|
|
duration = service.data.get(ATTR_DURATION)
|
|
|
|
_LOGGER.info("Permitting joins for %ss", duration)
|
2018-03-12 20:57:13 +00:00
|
|
|
await APPLICATION_CONTROLLER.permit(duration)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_PERMIT, permit,
|
2018-01-07 22:54:16 +00:00
|
|
|
schema=SERVICE_SCHEMAS[SERVICE_PERMIT])
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def remove(service):
|
2018-02-12 04:34:19 +00:00
|
|
|
"""Remove a node from the network."""
|
|
|
|
from bellows.types import EmberEUI64, uint8_t
|
|
|
|
ieee = service.data.get(ATTR_IEEE)
|
|
|
|
ieee = EmberEUI64([uint8_t(p, base=16) for p in ieee.split(':')])
|
|
|
|
_LOGGER.info("Removing node %s", ieee)
|
2018-03-12 20:57:13 +00:00
|
|
|
await APPLICATION_CONTROLLER.remove(ieee)
|
2018-02-12 04:34:19 +00:00
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_REMOVE, remove,
|
|
|
|
schema=SERVICE_SCHEMAS[SERVICE_REMOVE])
|
|
|
|
|
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."""
|
|
|
|
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)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationListener:
|
2017-04-30 05:04:49 +00:00
|
|
|
"""All handlers for events that happen on the ZigBee application."""
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
def __init__(self, hass, config):
|
|
|
|
"""Initialize the listener."""
|
|
|
|
self._hass = hass
|
|
|
|
self._config = config
|
2018-09-17 08:42:21 +00:00
|
|
|
self._component = EntityComponent(_LOGGER, DOMAIN, hass)
|
2018-02-12 04:34:19 +00:00
|
|
|
self._device_registry = collections.defaultdict(list)
|
2018-12-10 15:59:50 +00:00
|
|
|
self._events = {}
|
2018-11-22 18:00:46 +00:00
|
|
|
zha_const.populate_data()
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
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
|
2018-12-10 15:59:50 +00:00
|
|
|
hass.data[DATA_ZHA][DATA_ZHA_CORE_EVENTS] = self._events
|
2018-11-27 20:21:25 +00:00
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
def device_joined(self, device):
|
|
|
|
"""Handle device joined.
|
|
|
|
|
|
|
|
At this point, no information about the device is known other than its
|
|
|
|
address
|
|
|
|
"""
|
|
|
|
# Wait for device_initialized, instead
|
|
|
|
pass
|
|
|
|
|
2018-05-21 04:57:09 +00:00
|
|
|
def raw_device_initialized(self, device):
|
|
|
|
"""Handle a device initialization without quirks loaded."""
|
|
|
|
# Wait for device_initialized, instead
|
|
|
|
pass
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
def device_initialized(self, device):
|
|
|
|
"""Handle device joined and basic information discovered."""
|
2018-10-02 09:03:09 +00:00
|
|
|
self._hass.async_create_task(
|
|
|
|
self.async_device_initialized(device, True))
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2017-07-11 04:16:44 +00:00
|
|
|
def device_left(self, device):
|
|
|
|
"""Handle device leaving the network."""
|
|
|
|
pass
|
|
|
|
|
2017-10-16 04:41:16 +00:00
|
|
|
def device_removed(self, device):
|
|
|
|
"""Handle device being removed from the network."""
|
2018-02-12 04:34:19 +00:00
|
|
|
for device_entity in self._device_registry[device.ieee]:
|
2018-10-02 09:03:09 +00:00
|
|
|
self._hass.async_create_task(device_entity.async_remove())
|
2018-12-10 15:59:50 +00:00
|
|
|
if device.ieee in self._events:
|
|
|
|
self._events.pop(device.ieee)
|
2017-10-16 04:41:16 +00:00
|
|
|
|
2018-03-12 20:57:13 +00:00
|
|
|
async def async_device_initialized(self, device, join):
|
2017-04-25 05:24:57 +00:00
|
|
|
"""Handle device joined and basic information discovered (async)."""
|
2018-02-06 00:05:19 +00:00
|
|
|
import zigpy.profiles
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-09-17 08:42:21 +00:00
|
|
|
device_manufacturer = device_model = None
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
for endpoint_id, endpoint in device.endpoints.items():
|
|
|
|
if endpoint_id == 0: # ZDO
|
|
|
|
continue
|
|
|
|
|
2018-09-17 08:42:21 +00:00
|
|
|
if endpoint.manufacturer is not None:
|
|
|
|
device_manufacturer = endpoint.manufacturer
|
|
|
|
if endpoint.model is not None:
|
|
|
|
device_model = endpoint.model
|
|
|
|
|
2017-04-25 05:24:57 +00:00
|
|
|
component = None
|
2017-07-11 04:16:44 +00:00
|
|
|
profile_clusters = ([], [])
|
2018-02-18 23:02:34 +00:00
|
|
|
device_key = "{}-{}".format(device.ieee, endpoint_id)
|
2018-11-27 20:21:25 +00:00
|
|
|
node_config = {}
|
|
|
|
if CONF_DEVICE_CONFIG in self._config:
|
|
|
|
node_config = self._config[CONF_DEVICE_CONFIG].get(
|
|
|
|
device_key, {}
|
|
|
|
)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-02-06 00:05:19 +00:00
|
|
|
if endpoint.profile_id in zigpy.profiles.PROFILES:
|
|
|
|
profile = zigpy.profiles.PROFILES[endpoint.profile_id]
|
2017-04-25 05:24:57 +00:00
|
|
|
if zha_const.DEVICE_CLASS.get(endpoint.profile_id,
|
|
|
|
{}).get(endpoint.device_type,
|
|
|
|
None):
|
2017-07-11 04:16:44 +00:00
|
|
|
profile_clusters = profile.CLUSTERS[endpoint.device_type]
|
2017-04-25 05:24:57 +00:00
|
|
|
profile_info = zha_const.DEVICE_CLASS[endpoint.profile_id]
|
|
|
|
component = profile_info[endpoint.device_type]
|
|
|
|
|
|
|
|
if ha_const.CONF_TYPE in node_config:
|
|
|
|
component = node_config[ha_const.CONF_TYPE]
|
2017-07-11 04:16:44 +00:00
|
|
|
profile_clusters = zha_const.COMPONENT_CLUSTERS[component]
|
2017-04-25 05:24:57 +00:00
|
|
|
|
|
|
|
if component:
|
2017-07-11 04:16:44 +00:00
|
|
|
in_clusters = [endpoint.in_clusters[c]
|
|
|
|
for c in profile_clusters[0]
|
|
|
|
if c in endpoint.in_clusters]
|
|
|
|
out_clusters = [endpoint.out_clusters[c]
|
|
|
|
for c in profile_clusters[1]
|
|
|
|
if c in endpoint.out_clusters]
|
2017-04-25 05:24:57 +00:00
|
|
|
discovery_info = {
|
2018-02-12 04:34:19 +00:00
|
|
|
'application_listener': self,
|
2017-04-25 05:24:57 +00:00
|
|
|
'endpoint': endpoint,
|
2017-07-11 04:16:44 +00:00
|
|
|
'in_clusters': {c.cluster_id: c for c in in_clusters},
|
|
|
|
'out_clusters': {c.cluster_id: c for c in out_clusters},
|
2018-09-12 09:39:23 +00:00
|
|
|
'manufacturer': endpoint.manufacturer,
|
|
|
|
'model': endpoint.model,
|
2017-04-25 05:24:57 +00:00
|
|
|
'new_join': join,
|
2018-02-18 23:02:34 +00:00
|
|
|
'unique_id': device_key,
|
2017-04-25 05:24:57 +00:00
|
|
|
}
|
2018-11-27 20:21:25 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
for cluster in endpoint.in_clusters.values():
|
|
|
|
await self._attempt_single_cluster_device(
|
|
|
|
endpoint,
|
|
|
|
cluster,
|
|
|
|
profile_clusters[0],
|
|
|
|
device_key,
|
|
|
|
zha_const.SINGLE_INPUT_CLUSTER_DEVICE_CLASS,
|
|
|
|
'in_clusters',
|
|
|
|
join,
|
|
|
|
)
|
2017-04-25 05:24:57 +00:00
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
for cluster in endpoint.out_clusters.values():
|
|
|
|
await self._attempt_single_cluster_device(
|
|
|
|
endpoint,
|
|
|
|
cluster,
|
|
|
|
profile_clusters[1],
|
|
|
|
device_key,
|
|
|
|
zha_const.SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS,
|
|
|
|
'out_clusters',
|
|
|
|
join,
|
2017-04-25 05:24:57 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 08:42:21 +00:00
|
|
|
endpoint_entity = ZhaDeviceEntity(
|
|
|
|
device,
|
|
|
|
device_manufacturer,
|
|
|
|
device_model,
|
|
|
|
self,
|
|
|
|
)
|
|
|
|
await self._component.async_add_entities([endpoint_entity])
|
|
|
|
|
2018-02-12 04:34:19 +00:00
|
|
|
def register_entity(self, ieee, entity_obj):
|
|
|
|
"""Record the creation of a hass entity associated with ieee."""
|
|
|
|
self._device_registry[ieee].append(entity_obj)
|
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
async def _attempt_single_cluster_device(self, endpoint, cluster,
|
|
|
|
profile_clusters, device_key,
|
|
|
|
device_classes, discovery_attr,
|
2018-09-12 09:39:23 +00:00
|
|
|
is_new_join):
|
2018-04-30 06:31:27 +00:00
|
|
|
"""Try to set up an entity from a "bare" cluster."""
|
2018-12-10 15:59:50 +00:00
|
|
|
if cluster.cluster_id in EVENTABLE_CLUSTERS:
|
|
|
|
if cluster.endpoint.device.ieee not in self._events:
|
|
|
|
self._events.update({cluster.endpoint.device.ieee: []})
|
|
|
|
self._events[cluster.endpoint.device.ieee].append(ZhaEvent(
|
|
|
|
self._hass,
|
|
|
|
cluster
|
|
|
|
))
|
|
|
|
|
2018-04-30 06:31:27 +00:00
|
|
|
if cluster.cluster_id in profile_clusters:
|
|
|
|
return
|
2018-05-14 14:50:09 +00:00
|
|
|
|
2018-10-09 10:53:02 +00:00
|
|
|
component = sub_component = None
|
2018-05-14 14:50:09 +00:00
|
|
|
for cluster_type, candidate_component in device_classes.items():
|
|
|
|
if isinstance(cluster, cluster_type):
|
|
|
|
component = candidate_component
|
|
|
|
break
|
|
|
|
|
2018-10-09 10:53:02 +00:00
|
|
|
for signature, comp in zha_const.CUSTOM_CLUSTER_MAPPINGS.items():
|
|
|
|
if (isinstance(endpoint.device, signature[0]) and
|
|
|
|
cluster.cluster_id == signature[1]):
|
|
|
|
component = comp[0]
|
|
|
|
sub_component = comp[1]
|
|
|
|
break
|
|
|
|
|
2018-05-14 14:50:09 +00:00
|
|
|
if component is None:
|
2018-04-30 06:31:27 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
cluster_key = "{}-{}".format(device_key, cluster.cluster_id)
|
|
|
|
discovery_info = {
|
|
|
|
'application_listener': self,
|
|
|
|
'endpoint': endpoint,
|
|
|
|
'in_clusters': {},
|
|
|
|
'out_clusters': {},
|
2018-09-12 09:39:23 +00:00
|
|
|
'manufacturer': endpoint.manufacturer,
|
|
|
|
'model': endpoint.model,
|
2018-04-30 06:31:27 +00:00
|
|
|
'new_join': is_new_join,
|
|
|
|
'unique_id': cluster_key,
|
|
|
|
'entity_suffix': '_{}'.format(cluster.cluster_id),
|
|
|
|
}
|
|
|
|
discovery_info[discovery_attr] = {cluster.cluster_id: cluster}
|
2018-10-09 10:53:02 +00:00
|
|
|
if sub_component:
|
|
|
|
discovery_info.update({'sub_component': sub_component})
|
2018-04-30 06:31:27 +00:00
|
|
|
|
2018-11-27 20:21:25 +00:00
|
|
|
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
|