From 5167658a1d569c8a488c7674385d50e1205e9d67 Mon Sep 17 00:00:00 2001 From: damarco Date: Tue, 9 Oct 2018 12:53:02 +0200 Subject: [PATCH] Add support for zha custom cluster mappings (#16714) * Add support for custom cluster mappings * Refactor sub_component mapping --- homeassistant/components/sensor/zha.py | 4 +++- homeassistant/components/zha/__init__.py | 12 +++++++++++- homeassistant/components/zha/const.py | 10 +++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index 0d5b40d1d98..9a9de0d6cf2 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -36,7 +36,9 @@ async def make_sensor(discovery_info): from zigpy.zcl.clusters.smartenergy import Metering from zigpy.zcl.clusters.homeautomation import ElectricalMeasurement in_clusters = discovery_info['in_clusters'] - if RelativeHumidity.cluster_id in in_clusters: + if 'sub_component' in discovery_info: + sensor = discovery_info['sub_component'](**discovery_info) + elif RelativeHumidity.cluster_id in in_clusters: sensor = RelativeHumiditySensor(**discovery_info) elif TemperatureMeasurement.cluster_id in in_clusters: sensor = TemperatureSensor(**discovery_info) diff --git a/homeassistant/components/zha/__init__.py b/homeassistant/components/zha/__init__.py index 2e74e079d5f..8cea746f89a 100644 --- a/homeassistant/components/zha/__init__.py +++ b/homeassistant/components/zha/__init__.py @@ -276,15 +276,23 @@ class ApplicationListener: device_classes, discovery_attr, is_new_join): """Try to set up an entity from a "bare" cluster.""" + import homeassistant.components.zha.const as zha_const if cluster.cluster_id in profile_clusters: return - component = None + component = sub_component = None for cluster_type, candidate_component in device_classes.items(): if isinstance(cluster, cluster_type): component = candidate_component break + 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 + if component is None: return @@ -301,6 +309,8 @@ class ApplicationListener: 'entity_suffix': '_{}'.format(cluster.cluster_id), } 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( diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 37c7f5592a0..0b3e926fadc 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -3,6 +3,7 @@ DEVICE_CLASS = {} SINGLE_INPUT_CLUSTER_DEVICE_CLASS = {} SINGLE_OUTPUT_CLUSTER_DEVICE_CLASS = {} +CUSTOM_CLUSTER_MAPPINGS = {} COMPONENT_CLUSTERS = {} @@ -12,8 +13,9 @@ def populate_data(): These cannot be module level, as importing bellows must be done in a in a function. """ - from zigpy import zcl + from zigpy import zcl, quirks from zigpy.profiles import PROFILES, zha, zll + from homeassistant.components.sensor import zha as sensor_zha DEVICE_CLASS[zha.PROFILE_ID] = { zha.DeviceType.ON_OFF_SWITCH: 'binary_sensor', @@ -58,6 +60,12 @@ def populate_data(): zcl.clusters.general.OnOff: 'binary_sensor', }) + # A map of device/cluster to component/sub-component + CUSTOM_CLUSTER_MAPPINGS.update({ + (quirks.smartthings.SmartthingsTemperatureHumiditySensor, 64581): + ('sensor', sensor_zha.RelativeHumiditySensor) + }) + # A map of hass components to all Zigbee clusters it could use for profile_id, classes in DEVICE_CLASS.items(): profile = PROFILES[profile_id]