From 5ea4a68aae382d3486e76a474e24d0db7f59119a Mon Sep 17 00:00:00 2001 From: "David F. Mulcahey" Date: Fri, 10 Apr 2020 20:45:38 -0400 Subject: [PATCH] Cleanup ZHA metering and electrical measurement channels (#33992) * clean up homeautomation and smartenergy channels * fix get attributes on base channel --- .../components/zha/core/channels/base.py | 6 +++- .../zha/core/channels/homeautomation.py | 30 +++++++----------- .../zha/core/channels/smartenergy.py | 31 ++++++++++--------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/homeassistant/components/zha/core/channels/base.py b/homeassistant/components/zha/core/channels/base.py index 8478c2b9c49..995d109941d 100644 --- a/homeassistant/components/zha/core/channels/base.py +++ b/homeassistant/components/zha/core/channels/base.py @@ -263,7 +263,11 @@ class ZigbeeChannel(LogMixin): only_cache=from_cache, manufacturer=manufacturer, ) - results = {attribute: result.get(attribute) for attribute in attributes} + results = { + attribute: result.get(attribute) + for attribute in attributes + if result.get(attribute) is not None + } except (asyncio.TimeoutError, zigpy.exceptions.DeliveryError) as ex: self.debug( "failed to get attributes '%s' on '%s' cluster: %s", diff --git a/homeassistant/components/zha/core/channels/homeautomation.py b/homeassistant/components/zha/core/channels/homeautomation.py index c867fdc621d..b295a567b3d 100644 --- a/homeassistant/components/zha/core/channels/homeautomation.py +++ b/homeassistant/components/zha/core/channels/homeautomation.py @@ -81,27 +81,19 @@ class ElectricalMeasurementChannel(ZigbeeChannel): async def fetch_config(self, from_cache): """Fetch config from device and updates format specifier.""" - divisor = await self.get_attribute_value( - "ac_power_divisor", from_cache=from_cache + results = await self.get_attributes( + [ + "ac_power_divisor", + "power_divisor", + "ac_power_multiplier", + "power_multiplier", + ], + from_cache=from_cache, ) - if divisor is None: - divisor = await self.get_attribute_value( - "power_divisor", from_cache=from_cache - ) - if divisor is None: - divisor = 1 - self._divisor = divisor - - mult = await self.get_attribute_value( - "ac_power_multiplier", from_cache=from_cache + self._divisor = results.get("ac_power_divisor", results.get("power_divisor", 1)) + self._multiplier = results.get( + "ac_power_multiplier", results.get("power_multiplier", 1) ) - if mult is None: - mult = await self.get_attribute_value( - "power_multiplier", from_cache=from_cache - ) - if mult is None: - mult = 1 - self._multiplier = mult @property def divisor(self) -> Optional[int]: diff --git a/homeassistant/components/zha/core/channels/smartenergy.py b/homeassistant/components/zha/core/channels/smartenergy.py index 4226aad3f0a..58a394e7c80 100644 --- a/homeassistant/components/zha/core/channels/smartenergy.py +++ b/homeassistant/components/zha/core/channels/smartenergy.py @@ -98,6 +98,8 @@ class Metering(ZigbeeChannel): @callback def attribute_updated(self, attrid, value): """Handle attribute update from Metering cluster.""" + if None in (self._multiplier, self._divisor, self._format_spec): + return super().attribute_updated(attrid, value * self._multiplier / self._divisor) @property @@ -107,25 +109,24 @@ class Metering(ZigbeeChannel): async def fetch_config(self, from_cache): """Fetch config from device and updates format specifier.""" - self._divisor = await self.get_attribute_value("divisor", from_cache=from_cache) - self._multiplier = await self.get_attribute_value( - "multiplier", from_cache=from_cache - ) - self._unit_enum = await self.get_attribute_value( - "unit_of_measure", from_cache=from_cache - ) - fmting = await self.get_attribute_value( - "demand_formatting", from_cache=from_cache + results = await self.get_attributes( + ["divisor", "multiplier", "unit_of_measure", "demand_formatting"], + from_cache=from_cache, ) - if self._divisor is None or self._divisor == 0: + self._divisor = results.get("divisor", 1) + if self._divisor == 0: self._divisor = 1 - if self._multiplier is None or self._multiplier == 0: + + self._multiplier = results.get("multiplier", 1) + if self._multiplier == 0: self._multiplier = 1 - if self._unit_enum is None: - self._unit_enum = 0x7F # unknown - if fmting is None: - fmting = 0xF9 # 1 digit to the right, 15 digits to the left + + self._unit_enum = results.get("unit_of_measure", 0x7F) # default to unknown + + fmting = results.get( + "demand_formatting", 0xF9 + ) # 1 digit to the right, 15 digits to the left r_digits = fmting & 0x07 # digits to the right of decimal point l_digits = (fmting >> 3) & 0x0F # digits to the left of decimal point