From 63fcf9d4253ca2be430179c46c5c25e518334543 Mon Sep 17 00:00:00 2001 From: Russell Cloran Date: Sun, 18 Feb 2018 15:03:18 -0800 Subject: [PATCH] zha: Add support for humidity sensors (#12496) * zha: Add support for humidity sensors * Fix lint issue --- homeassistant/components/sensor/zha.py | 30 ++++++++++++++++++++++---- homeassistant/components/zha/const.py | 1 + 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index a1820f7d7dd..36cdca2e638 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -31,19 +31,22 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None): @asyncio.coroutine def make_sensor(discovery_info): """Create ZHA sensors factory.""" - from zigpy.zcl.clusters.measurement import TemperatureMeasurement + from zigpy.zcl.clusters.measurement import ( + RelativeHumidity, TemperatureMeasurement + ) in_clusters = discovery_info['in_clusters'] - if TemperatureMeasurement.cluster_id in in_clusters: + if RelativeHumidity.cluster_id in in_clusters: + sensor = RelativeHumiditySensor(**discovery_info) + elif TemperatureMeasurement.cluster_id in in_clusters: sensor = TemperatureSensor(**discovery_info) else: sensor = Sensor(**discovery_info) - attr = sensor.value_attribute if discovery_info['new_join']: cluster = list(in_clusters.values())[0] yield from cluster.bind() yield from cluster.configure_reporting( - attr, 300, 600, sensor.min_reportable_change, + sensor.value_attribute, 300, 600, sensor.min_reportable_change, ) return sensor @@ -89,3 +92,22 @@ class TemperatureSensor(Sensor): celsius = round(float(self._state) / 100, 1) return convert_temperature( celsius, TEMP_CELSIUS, self.unit_of_measurement) + + +class RelativeHumiditySensor(Sensor): + """ZHA relative humidity sensor.""" + + min_reportable_change = 50 # 0.5% + + @property + def unit_of_measurement(self): + """Return the unit of measurement of this entity.""" + return '%' + + @property + def state(self): + """Return the state of the entity.""" + if self._state == 'unknown': + return 'unknown' + + return round(float(self._state) / 100, 1) diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 05716da58ce..deaa1257396 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -33,6 +33,7 @@ def populate_data(): SINGLE_CLUSTER_DEVICE_CLASS.update({ zcl.clusters.general.OnOff: 'switch', + zcl.clusters.measurement.RelativeHumidity: 'sensor', zcl.clusters.measurement.TemperatureMeasurement: 'sensor', zcl.clusters.security.IasZone: 'binary_sensor', })