From 33a84838b40b2cb41b1b37401a19bbc1069f5ab4 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 22 Jun 2022 13:41:28 +0200 Subject: [PATCH] Fix type hints in zha smartenergy channel (#73775) * Fix type hints in zha smartenergy channel * Adjust unit_of_measurement --- .../zha/core/channels/smartenergy.py | 22 +++++++++++-------- mypy.ini | 3 --- script/hassfest/mypy_config.py | 1 - 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/zha/core/channels/smartenergy.py b/homeassistant/components/zha/core/channels/smartenergy.py index 731ec0030115..66d3e3d68100 100644 --- a/homeassistant/components/zha/core/channels/smartenergy.py +++ b/homeassistant/components/zha/core/channels/smartenergy.py @@ -122,8 +122,8 @@ class Metering(ZigbeeChannel): def __init__(self, cluster: zigpy.zcl.Cluster, ch_pool: ChannelPool) -> None: """Initialize Metering.""" super().__init__(cluster, ch_pool) - self._format_spec = None - self._summa_format = None + self._format_spec: str | None = None + self._summa_format: str | None = None @property def divisor(self) -> int: @@ -131,7 +131,7 @@ class Metering(ZigbeeChannel): return self.cluster.get("divisor") or 1 @property - def device_type(self) -> int | None: + def device_type(self) -> str | int | None: """Return metering device type.""" dev_type = self.cluster.get("metering_device_type") if dev_type is None: @@ -154,7 +154,7 @@ class Metering(ZigbeeChannel): return self.DeviceStatusDefault(status) @property - def unit_of_measurement(self) -> str: + def unit_of_measurement(self) -> int: """Return unit of measurement.""" return self.cluster.get("unit_of_measure") @@ -210,18 +210,22 @@ class Metering(ZigbeeChannel): return f"{{:0{width}.{r_digits}f}}" - def _formatter_function(self, selector: FormatSelector, value: int) -> int | float: + def _formatter_function( + self, selector: FormatSelector, value: int + ) -> int | float | str: """Return formatted value for display.""" - value = value * self.multiplier / self.divisor + value_float = value * self.multiplier / self.divisor if self.unit_of_measurement == 0: # Zigbee spec power unit is kW, but we show the value in W - value_watt = value * 1000 + value_watt = value_float * 1000 if value_watt < 100: return round(value_watt, 1) return round(value_watt) if selector == self.FormatSelector.SUMMATION: - return self._summa_format.format(value).lstrip() - return self._format_spec.format(value).lstrip() + assert self._summa_format + return self._summa_format.format(value_float).lstrip() + assert self._format_spec + return self._format_spec.format(value_float).lstrip() demand_formatter = partialmethod(_formatter_function, FormatSelector.DEMAND) summa_formatter = partialmethod(_formatter_function, FormatSelector.SUMMATION) diff --git a/mypy.ini b/mypy.ini index 26314c5bcada..192418724746 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2997,9 +2997,6 @@ ignore_errors = true [mypy-homeassistant.components.zha.core.channels.security] ignore_errors = true -[mypy-homeassistant.components.zha.core.channels.smartenergy] -ignore_errors = true - [mypy-homeassistant.components.zha.core.device] ignore_errors = true diff --git a/script/hassfest/mypy_config.py b/script/hassfest/mypy_config.py index 6a4ff9d8cdf5..f319ccb52358 100644 --- a/script/hassfest/mypy_config.py +++ b/script/hassfest/mypy_config.py @@ -148,7 +148,6 @@ IGNORED_MODULES: Final[list[str]] = [ "homeassistant.components.zha.core.channels.homeautomation", "homeassistant.components.zha.core.channels.hvac", "homeassistant.components.zha.core.channels.security", - "homeassistant.components.zha.core.channels.smartenergy", "homeassistant.components.zha.core.device", "homeassistant.components.zha.core.discovery", "homeassistant.components.zha.core.gateway",