Cleanup ZHA metering and electrical measurement channels (#33992)

* clean up homeautomation and smartenergy channels

* fix get attributes on base channel
This commit is contained in:
David F. Mulcahey 2020-04-10 20:45:38 -04:00 committed by GitHub
parent 328cadbaa2
commit 5ea4a68aae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 35 deletions

View File

@ -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",

View File

@ -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
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 divisor is None:
divisor = 1
self._divisor = divisor
mult = await self.get_attribute_value(
"ac_power_multiplier", from_cache=from_cache
)
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]:

View File

@ -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