diff --git a/homeassistant/components/binary_sensor/zha.py b/homeassistant/components/binary_sensor/zha.py index 224d694e0f5b63..4df840df15626a 100644 --- a/homeassistant/components/binary_sensor/zha.py +++ b/homeassistant/components/binary_sensor/zha.py @@ -23,6 +23,8 @@ 0x002d: 'vibration', } +DEVICE_CLASS_OPENING = 'opening' + async def async_setup_platform(hass, config, async_add_devices, discovery_info=None): @@ -37,8 +39,8 @@ async def async_setup_platform(hass, config, async_add_devices, await _async_setup_iaszone(hass, config, async_add_devices, discovery_info) elif OnOff.cluster_id in discovery_info['out_clusters']: - await _async_setup_remote(hass, config, async_add_devices, - discovery_info) + await _async_setup_onoff(hass, config, async_add_devices, + discovery_info, DEVICE_CLASS_OPENING) async def _async_setup_iaszone(hass, config, async_add_devices, @@ -58,47 +60,57 @@ async def _async_setup_iaszone(hass, config, async_add_devices, # If we fail to read from the device, use a non-specific class pass - sensor = BinarySensor(device_class, **discovery_info) - async_add_devices([sensor], update_before_add=True) - + sensor = IasZoneSensor(device_class, **discovery_info) + async_add_devices([sensor]) -async def _async_setup_remote(hass, config, async_add_devices, discovery_info): - async def safe(coro): - """Run coro, catching ZigBee delivery errors, and ignoring them.""" - import zigpy.exceptions - try: - await coro - except zigpy.exceptions.DeliveryError as exc: - _LOGGER.warning("Ignoring error during setup: %s", exc) +async def _async_setup_onoff(hass, config, async_add_devices, + discovery_info, device_class): + sensor = BinarySensor(device_class, **discovery_info) if discovery_info['new_join']: - from zigpy.zcl.clusters.general import OnOff, LevelControl - out_clusters = discovery_info['out_clusters'] - if OnOff.cluster_id in out_clusters: - cluster = out_clusters[OnOff.cluster_id] - await safe(cluster.bind()) - await safe(cluster.configure_reporting(0, 0, 600, 1)) - if LevelControl.cluster_id in out_clusters: - cluster = out_clusters[LevelControl.cluster_id] - await safe(cluster.bind()) - await safe(cluster.configure_reporting(0, 1, 600, 1)) + from zigpy.exceptions import ZigbeeException + from zigpy.zcl.clusters.general import OnOff + in_clusters = discovery_info['in_clusters'] + endpoint = discovery_info['endpoint'] + cluster = in_clusters[OnOff.cluster_id] + attr, min_report, max_report, report_change = [0, 0, 600, 1] + try: + await cluster.bind() + except ZigbeeException as ex: + _LOGGER.debug("Failed to bind {}-{}-{}: {}". + format(endpoint.device.ieee, endpoint.endpoint_id, + cluster.cluster_id, ex)) + try: + await cluster.configure_reporting(attr, min_report, + max_report, report_change) + except ZigbeeException as ex: + _LOGGER.debug( + "Failed to configure reporting for attr {} on {}-{}-{}: {}" + .format(attr, endpoint.device.ieee, endpoint.endpoint_id, + cluster.cluster_id, ex)) - sensor = Switch(**discovery_info) - async_add_devices([sensor], update_before_add=True) + async_add_devices([sensor]) class BinarySensor(zha.Entity, BinarySensorDevice): - """The ZHA Binary Sensor.""" + """ZHA Binary Sensor.""" _domain = DOMAIN + _device_class = None + value_attribute = 0 def __init__(self, device_class, **kwargs): """Initialize the ZHA binary sensor.""" super().__init__(**kwargs) self._device_class = device_class - from zigpy.zcl.clusters.security import IasZone - self._ias_zone_cluster = self._in_clusters[IasZone.cluster_id] + + def attribute_updated(self, attribute, value): + """Handle attribute update from device.""" + _LOGGER.debug("Attribute updated: %s %s %s", self, attribute, value) + if attribute == self.value_attribute: + self._state = bool(value) + self.async_schedule_update_ha_state() @property def should_poll(self) -> bool: @@ -107,152 +119,33 @@ def should_poll(self) -> bool: @property def is_on(self) -> bool: - """Return True if entity is on.""" + """Return if the switch is on based on the statemachine.""" if self._state is None: return False - return bool(self._state) + return self._state @property - def device_class(self): - """Return the class of this device, from component DEVICE_CLASSES.""" + def device_class(self) -> str: + """Return device class from component DEVICE_CLASSES.""" return self._device_class + +class IasZoneSensor(BinarySensor, BinarySensorDevice): + """The ZHA Binary Sensor.""" + + def __init__(self, device_class, **kwargs): + """Initialize the ZHA binary sensor.""" + super().__init__(device_class, **kwargs) + from zigpy.zcl.clusters.security import IasZone + self._ias_zone_cluster = self._in_clusters[IasZone.cluster_id] + def cluster_command(self, tsn, command_id, args): """Handle commands received to this cluster.""" if command_id == 0: - self._state = args[0] & 3 + self._state = bool(args[0] & 3) _LOGGER.debug("Updated alarm state: %s", self._state) self.async_schedule_update_ha_state() elif command_id == 1: _LOGGER.debug("Enroll requested") res = self._ias_zone_cluster.enroll_response(0, 0) self.hass.async_add_job(res) - - async def async_update(self): - """Retrieve latest state.""" - from bellows.types.basic import uint16_t - - result = await zha.safe_read(self._endpoint.ias_zone, - ['zone_status'], - allow_cache=False) - state = result.get('zone_status', self._state) - if isinstance(state, (int, uint16_t)): - self._state = result.get('zone_status', self._state) & 3 - - -class Switch(zha.Entity, BinarySensorDevice): - """ZHA switch/remote controller/button.""" - - _domain = DOMAIN - - class OnOffListener: - """Listener for the OnOff ZigBee cluster.""" - - def __init__(self, entity): - """Initialize OnOffListener.""" - self._entity = entity - - def cluster_command(self, tsn, command_id, args): - """Handle commands received to this cluster.""" - if command_id in (0x0000, 0x0040): - self._entity.set_state(False) - elif command_id in (0x0001, 0x0041, 0x0042): - self._entity.set_state(True) - elif command_id == 0x0002: - self._entity.set_state(not self._entity.is_on) - - def attribute_updated(self, attrid, value): - """Handle attribute updates on this cluster.""" - if attrid == 0: - self._entity.set_state(value) - - def zdo_command(self, *args, **kwargs): - """Handle ZDO commands on this cluster.""" - pass - - class LevelListener: - """Listener for the LevelControl ZigBee cluster.""" - - def __init__(self, entity): - """Initialize LevelListener.""" - self._entity = entity - - def cluster_command(self, tsn, command_id, args): - """Handle commands received to this cluster.""" - if command_id in (0x0000, 0x0004): # move_to_level, -with_on_off - self._entity.set_level(args[0]) - elif command_id in (0x0001, 0x0005): # move, -with_on_off - # We should dim slowly -- for now, just step once - rate = args[1] - if args[0] == 0xff: - rate = 10 # Should read default move rate - self._entity.move_level(-rate if args[0] else rate) - elif command_id in (0x0002, 0x0006): # step, -with_on_off - # Step (technically may change on/off) - self._entity.move_level(-args[1] if args[0] else args[1]) - - def attribute_update(self, attrid, value): - """Handle attribute updates on this cluster.""" - if attrid == 0: - self._entity.set_level(value) - - def zdo_command(self, *args, **kwargs): - """Handle ZDO commands on this cluster.""" - pass - - def __init__(self, **kwargs): - """Initialize Switch.""" - super().__init__(**kwargs) - self._state = False - self._level = 0 - from zigpy.zcl.clusters import general - self._out_listeners = { - general.OnOff.cluster_id: self.OnOffListener(self), - general.LevelControl.cluster_id: self.LevelListener(self), - } - - @property - def should_poll(self) -> bool: - """Let zha handle polling.""" - return False - - @property - def is_on(self) -> bool: - """Return true if the binary sensor is on.""" - return self._state - - @property - def device_state_attributes(self): - """Return the device state attributes.""" - self._device_state_attributes.update({ - 'level': self._state and self._level or 0 - }) - return self._device_state_attributes - - def move_level(self, change): - """Increment the level, setting state if appropriate.""" - if not self._state and change > 0: - self._level = 0 - self._level = min(255, max(0, self._level + change)) - self._state = bool(self._level) - self.async_schedule_update_ha_state() - - def set_level(self, level): - """Set the level, setting state if appropriate.""" - self._level = level - self._state = bool(self._level) - self.async_schedule_update_ha_state() - - def set_state(self, state): - """Set the state.""" - self._state = state - if self._level == 0: - self._level = 255 - self.async_schedule_update_ha_state() - - async def async_update(self): - """Retrieve latest state.""" - from zigpy.zcl.clusters.general import OnOff - result = await zha.safe_read( - self._endpoint.out_clusters[OnOff.cluster_id], ['on_off']) - self._state = result.get('on_off', self._state) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 030e342847d719..50a8f7661871f4 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -12,6 +12,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant import const as ha_const +from homeassistant.core import EventOrigin, callback from homeassistant.helpers import discovery, entity from homeassistant.util import slugify @@ -22,6 +23,14 @@ ] DOMAIN = 'zha' +SWITCH = 'switch' +DEVICE = 'device' +LEVEL = 'level' +DATA_ZHA_EVENT = 'zha_events' +OFF_EVENT_KEY = 'zha.off' +ON_EVENT_KEY = 'zha.on' +TOGGLE_EVENT_KEY = 'zha.toggle' +LEVEL_CHANGE_EVENT_KEY = 'zha.level_change' class RadioType(enum.Enum): @@ -129,6 +138,8 @@ async def remove(service): hass.services.async_register(DOMAIN, SERVICE_REMOVE, remove, schema=SERVICE_SCHEMAS[SERVICE_REMOVE]) + hass.data[DATA_ZHA_EVENT] = [] + return True @@ -225,6 +236,27 @@ async def async_device_initialized(self, device, join): {'discovery_key': device_key}, self._config, ) + else: + if endpoint.device_type in zha_const.REMOTE_DEVICE_TYPES.get( + endpoint.profile_id, {}): + profile_clusters = profile.CLUSTERS[endpoint.device_type] + 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] + discovery_info = { + 'application_listener': self, + 'endpoint': endpoint, + 'in_clusters': {c.cluster_id: c for c in in_clusters}, + 'out_clusters': {c.cluster_id: c for c in + out_clusters}, + 'new_join': join, + 'unique_id': device_key, + } + discovery_info.update(discovered_info) + await self._async_setup_remote(discovery_info) for cluster in endpoint.in_clusters.values(): await self._attempt_single_cluster_device( @@ -293,6 +325,35 @@ async def _attempt_single_cluster_device(self, endpoint, cluster, self._config, ) + async def _async_setup_remote(self, discovery_info): + + async def safe(coro): + """Run coro, catching ZigBee delivery errors, and ignoring them.""" + import zigpy.exceptions + try: + await coro + except zigpy.exceptions.DeliveryError as exc: + _LOGGER.warning("Ignoring error during setup: %s", exc) + + from zigpy.zcl.clusters.general import OnOff, LevelControl + out_clusters = discovery_info['out_clusters'] + if OnOff.cluster_id in out_clusters: + cluster = out_clusters[OnOff.cluster_id] + if discovery_info['new_join']: + await safe(cluster.bind()) + await safe(cluster.configure_reporting(0, 0, 600, 1)) + self._hass.data[DATA_ZHA_EVENT].append( + ZHASwitchEvent(self._hass, cluster, discovery_info) + ) + if LevelControl.cluster_id in out_clusters: + cluster = out_clusters[LevelControl.cluster_id] + if discovery_info['new_join']: + await safe(cluster.bind()) + await safe(cluster.configure_reporting(0, 1, 600, 1)) + self._hass.data[DATA_ZHA_EVENT].append( + ZHALevelEvent(self._hass, cluster, discovery_info) + ) + class Entity(entity.Entity): """A base class for ZHA entities.""" @@ -369,6 +430,122 @@ def zdo_command(self, tsn, command_id, args): pass +class ZHAEvent(object): + """When you want signals instead of entities. + Stateless sensors such as remotes are expected to generate an event + instead of a sensor entity in hass. + """ + + def __init__(self, hass, cluster, domain, discovery_info): + """Register callback that will be used for signals.""" + self._hass = hass + self._cluster = cluster + self._cluster.add_listener(self) + ieee = discovery_info['endpoint'].device.ieee + ieeetail = ''.join(['%02x' % (o, ) for o in ieee[-4:]]) + if discovery_info['manufacturer'] and discovery_info['model'] is not \ + None: + self._id = "{}.{}_{}_{}_{}{}".format( + domain, + slugify(discovery_info['manufacturer']), + slugify(discovery_info['model']), + ieeetail, + discovery_info['endpoint'].endpoint_id, + discovery_info.get('entity_suffix', '') + ) + else: + self._id = "{}.zha_{}_{}{}".format( + domain, + ieeetail, + discovery_info['endpoint'].endpoint_id, + discovery_info.get('entity_suffix', '') + ) + + +class ZHASwitchEvent(ZHAEvent): + """Switch / remote event for zha""" + + def __init__(self, hass, cluster, discovery_info): + """Initialize Switch.""" + super().__init__(hass, cluster, SWITCH, discovery_info) + + @callback + def cluster_command(self, tsn, command_id, args): + """Handle commands received to this cluster.""" + if command_id in (0x0000, 0x0040): + self._hass.bus.fire( + OFF_EVENT_KEY, + {DEVICE: self._id}, + EventOrigin.remote + ) + elif command_id in (0x0001, 0x0041, 0x0042): + self._hass.bus.fire( + ON_EVENT_KEY, + {DEVICE: self._id}, + EventOrigin.remote + ) + elif command_id == 0x0002: + self._hass.bus.fire( + TOGGLE_EVENT_KEY, + {DEVICE: self._id}, + EventOrigin.remote + ) + + +class ZHALevelEvent(ZHAEvent): + """Switch / remote event for zha""" + + def __init__(self, hass, cluster, discovery_info): + """Initialize Switch.""" + super().__init__(hass, cluster, SWITCH, discovery_info) + self._level = 0 + + @callback + def cluster_command(self, tsn, command_id, args): + """Handle commands received to this cluster.""" + if command_id in (0x0000, 0x0004): # move_to_level, -with_on_off + self.set_level(args[0]) + elif command_id in (0x0001, 0x0005): # move, -with_on_off + # We should dim slowly -- for now, just step once + rate = args[1] + if args[0] == 0xff: + rate = 10 # Should read default move rate + self.move_level(-rate if args[0] else rate) + elif command_id == 0x0002: # step + # Step (technically shouldn't change on/off) + self.move_level(-args[1] if args[0] else args[1]) + + def attribute_update(self, attrid, value): + """Handle attribute updates on this cluster.""" + if attrid == 0: + self._level = value + + def move_level(self, change): + """Increment the level.""" + self.set_level(min(255, max(0, self._level + change))) + + def set_level(self, level): + """Set the level.""" + if level == 0 and self._level > 0: + self._hass.bus.fire( + OFF_EVENT_KEY, + {DEVICE: self._id}, + EventOrigin.remote + ) + elif level > 0 and self._level == 0: + self._hass.bus.fire( + ON_EVENT_KEY, + {DEVICE: self._id}, + EventOrigin.remote + ) + self._level = level + self._hass.bus.fire( + LEVEL_CHANGE_EVENT_KEY, + {DEVICE: self._id, LEVEL: self._level}, + EventOrigin.remote + ) + + async def _discover_endpoint_info(endpoint): """Find some basic information about an endpoint.""" extra_info = { diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 37c7f5592a02e8..614b6b46495c34 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -4,6 +4,7 @@ SINGLE_INPUT_CLUSTER_DEVICE_CLASS = {} SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS = {} COMPONENT_CLUSTERS = {} +REMOTE_DEVICE_TYPES = {} def populate_data(): @@ -16,18 +17,20 @@ def populate_data(): from zigpy.profiles import PROFILES, zha, zll DEVICE_CLASS[zha.PROFILE_ID] = { - zha.DeviceType.ON_OFF_SWITCH: 'binary_sensor', - zha.DeviceType.LEVEL_CONTROL_SWITCH: 'binary_sensor', - zha.DeviceType.REMOTE_CONTROL: 'binary_sensor', + zha.DeviceType.ON_OFF_OUTPUT: 'binary_sensor', zha.DeviceType.SMART_PLUG: 'switch', - zha.DeviceType.ON_OFF_LIGHT: 'light', zha.DeviceType.DIMMABLE_LIGHT: 'light', zha.DeviceType.COLOR_DIMMABLE_LIGHT: 'light', - zha.DeviceType.ON_OFF_LIGHT_SWITCH: 'binary_sensor', - zha.DeviceType.DIMMER_SWITCH: 'binary_sensor', - zha.DeviceType.COLOR_DIMMER_SWITCH: 'binary_sensor', } + REMOTE_DEVICE_TYPES[zha.PROFILE_ID] = [ + zha.DeviceType.ON_OFF_SWITCH, + zha.DeviceType.LEVEL_CONTROL_SWITCH, + zha.DeviceType.REMOTE_CONTROL, + zha.DeviceType.ON_OFF_LIGHT_SWITCH, + zha.DeviceType.DIMMER_SWITCH, + zha.DeviceType.COLOR_DIMMER_SWITCH, + ] DEVICE_CLASS[zll.PROFILE_ID] = { zll.DeviceType.ON_OFF_LIGHT: 'light', zll.DeviceType.ON_OFF_PLUGIN_UNIT: 'switch', @@ -36,13 +39,14 @@ def populate_data(): zll.DeviceType.COLOR_LIGHT: 'light', zll.DeviceType.EXTENDED_COLOR_LIGHT: 'light', zll.DeviceType.COLOR_TEMPERATURE_LIGHT: 'light', - zll.DeviceType.COLOR_CONTROLLER: 'binary_sensor', - zll.DeviceType.COLOR_SCENE_CONTROLLER: 'binary_sensor', - zll.DeviceType.CONTROLLER: 'binary_sensor', - zll.DeviceType.SCENE_CONTROLLER: 'binary_sensor', - zll.DeviceType.ON_OFF_SENSOR: 'binary_sensor', } - + REMOTE_DEVICE_TYPES[zll.PROFILE_ID] = [ + zll.DeviceType.COLOR_CONTROLLER, + zll.DeviceType.COLOR_SCENE_CONTROLLER, + zll.DeviceType.CONTROLLER, + zll.DeviceType.SCENE_CONTROLLER, + zll.DeviceType.ON_OFF_SENSOR, + ] SINGLE_INPUT_CLUSTER_DEVICE_CLASS.update({ zcl.clusters.general.OnOff: 'switch', zcl.clusters.measurement.RelativeHumidity: 'sensor',