From eae9726beca0b0da84fe06ab94785d4c6a3f4c14 Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Sat, 26 May 2018 16:50:05 -0400 Subject: [PATCH] Add electrical measurement sensor to ZHA (#14561) * Add electrical measurement sensor * correct state update * hound fix * zha: Add metering sensor (#14562) * Add IlluminanceMeasurementSensor to ZHA (#14563) * add IlluminanceMeasurementSensor * address review comment * Fix whitespace error during merge * Add electrical measurement sensor * correct state update * hound / flake8 --- homeassistant/components/sensor/zha.py | 38 ++++++++++++++++++++++++++ homeassistant/components/zha/const.py | 1 + 2 files changed, 39 insertions(+) diff --git a/homeassistant/components/sensor/zha.py b/homeassistant/components/sensor/zha.py index 72368bdb3ba..984d6efed66 100644 --- a/homeassistant/components/sensor/zha.py +++ b/homeassistant/components/sensor/zha.py @@ -36,6 +36,7 @@ def make_sensor(discovery_info): IlluminanceMeasurement ) 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: sensor = RelativeHumiditySensor(**discovery_info) @@ -47,6 +48,9 @@ def make_sensor(discovery_info): sensor = IlluminanceMeasurementSensor(**discovery_info) elif Metering.cluster_id in in_clusters: sensor = MeteringSensor(**discovery_info) + elif ElectricalMeasurement.cluster_id in in_clusters: + sensor = ElectricalMeasurementSensor(**discovery_info) + return sensor else: sensor = Sensor(**discovery_info) @@ -182,3 +186,37 @@ class MeteringSensor(Sensor): return None return round(float(self._state)) + + +class ElectricalMeasurementSensor(Sensor): + """ZHA Electrical Measurement sensor.""" + + value_attribute = 1291 + + @property + def unit_of_measurement(self): + """Return the unit of measurement of this entity.""" + return 'W' + + @property + def state(self): + """Return the state of the entity.""" + if self._state is None: + return None + + return round(float(self._state) / 10, 1) + + @property + def should_poll(self) -> bool: + """Poll state from device.""" + return True + + async def async_update(self): + """Retrieve latest state.""" + _LOGGER.debug("%s async_update", self.entity_id) + + result = await zha.safe_read( + self._endpoint.electrical_measurement, + ['active_power'], + allow_cache=False) + self._state = result.get('active_power', self._state) diff --git a/homeassistant/components/zha/const.py b/homeassistant/components/zha/const.py index 087b19c6693..37c7f5592a0 100644 --- a/homeassistant/components/zha/const.py +++ b/homeassistant/components/zha/const.py @@ -50,6 +50,7 @@ def populate_data(): zcl.clusters.measurement.PressureMeasurement: 'sensor', zcl.clusters.measurement.IlluminanceMeasurement: 'sensor', zcl.clusters.smartenergy.Metering: 'sensor', + zcl.clusters.homeautomation.ElectricalMeasurement: 'sensor', zcl.clusters.security.IasZone: 'binary_sensor', zcl.clusters.hvac.Fan: 'fan', })