From acdf9c7ce2c0bf746fb9a8b04bfeffa42ec1ed20 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Tue, 8 Jan 2019 11:20:50 -0500 Subject: [PATCH] Relay events for onoff and levelcontrol output clusters in ZHA (#19863) * auto relay events for onoff and levelcontrol output clusters * fix docstring * correct copy/paste failure - review comment * add space - review comment --- homeassistant/components/zha/__init__.py | 18 ++++++++++---- homeassistant/components/zha/event.py | 30 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 3fe8980c451..5dc8f628581 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -21,7 +21,7 @@ from homeassistant.helpers.entity_component import EntityComponent # Loading the config flow file will register the flow from . import config_flow # noqa # pylint: disable=unused-import from . import const as zha_const -from .event import ZhaEvent +from .event import ZhaEvent, ZhaRelayEvent from .const import ( COMPONENTS, CONF_BAUDRATE, CONF_DATABASE, CONF_DEVICE_CONFIG, CONF_RADIO_TYPE, CONF_USB_PATH, DATA_ZHA, DATA_ZHA_BRIDGE_ID, @@ -393,10 +393,18 @@ class ApplicationListener: 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 - )) + from zigpy.zcl.clusters.general import OnOff, LevelControl + if discovery_attr == 'out_clusters' and \ + (cluster.cluster_id == OnOff.cluster_id or + cluster.cluster_id == LevelControl.cluster_id): + self._events[cluster.endpoint.device.ieee].append( + ZhaRelayEvent(self._hass, cluster) + ) + else: + self._events[cluster.endpoint.device.ieee].append(ZhaEvent( + self._hass, + cluster + )) if cluster.cluster_id in profile_clusters: return diff --git a/homeassistant/components/zha/event.py b/homeassistant/components/zha/event.py index 20175dd097f..7828a695a7b 100644 --- a/homeassistant/components/zha/event.py +++ b/homeassistant/components/zha/event.py @@ -67,3 +67,33 @@ class ZhaEvent(): }, EventOrigin.remote ) + + +class ZhaRelayEvent(ZhaEvent): + """Event relay that can be attached to zigbee clusters.""" + + @callback + def attribute_updated(self, attribute, value): + """Handle an attribute updated on this cluster.""" + self.zha_send_event( + self._cluster, + 'attribute_updated', + { + 'attribute_id': attribute, + 'attribute_name': self._cluster.attributes.get( + attribute, + ['Unknown'])[0], + 'value': value + } + ) + + @callback + def cluster_command(self, tsn, command_id, args): + """Handle a cluster command received on this cluster.""" + if self._cluster.server_commands is not None and\ + self._cluster.server_commands.get(command_id) is not None: + self.zha_send_event( + self._cluster, + self._cluster.server_commands.get(command_id)[0], + args + )