From 1c250913b68467a26e1e04f387a057f82d99b9e8 Mon Sep 17 00:00:00 2001 From: Ryan Nitcher Date: Fri, 6 Dec 2024 20:48:16 -0700 Subject: [PATCH] Rename `current_consumption` to `power` - Note: This is a breaking change. - Fix: Rename `current_consumption` to `power` in energy modules, to deconflict and clarify. - Fix: Report `0` instead of `None` for current when current is zero. - Fix: Report `0` instead of `None` for voltage when voltage is zero (not that this was possible to see). --- kasa/device.py | 4 ++-- kasa/interfaces/energy.py | 13 +++++++------ kasa/iot/iotstrip.py | 4 ++-- kasa/iot/modules/emeter.py | 4 ++-- kasa/smart/modules/energy.py | 8 ++++---- tests/iot/modules/test_emeter.py | 4 ++-- tests/smart/test_smartdevice.py | 4 ++-- tests/test_strip.py | 2 +- 8 files changed, 22 insertions(+), 21 deletions(-) diff --git a/kasa/device.py b/kasa/device.py index 76d7a7c5..6988ae0f 100644 --- a/kasa/device.py +++ b/kasa/device.py @@ -85,7 +85,7 @@ state rssi on_since reboot -current_consumption +power consumption_today consumption_this_month consumption_total @@ -559,7 +559,7 @@ class Device(ABC): "emeter_realtime": (Module.Energy, ["status"]), "emeter_today": (Module.Energy, ["consumption_today"]), "emeter_this_month": (Module.Energy, ["consumption_this_month"]), - "current_consumption": (Module.Energy, ["current_consumption"]), + "current_consumption": (Module.Energy, ["power"]), "get_emeter_daily": (Module.Energy, ["get_daily_stats"]), "get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]), # Other attributes diff --git a/kasa/interfaces/energy.py b/kasa/interfaces/energy.py index c57a3ed8..0bf37814 100644 --- a/kasa/interfaces/energy.py +++ b/kasa/interfaces/energy.py @@ -38,11 +38,11 @@ class Energy(Module, ABC): self._add_feature( Feature( device, - name="Current consumption", - attribute_getter="current_consumption", + name="Power", + attribute_getter="power", container=self, unit_getter=lambda: "W", - id="current_consumption", + id="power", precision_hint=1, category=Feature.Category.Primary, type=Feature.Type.Sensor, @@ -64,11 +64,11 @@ class Energy(Module, ABC): self._add_feature( Feature( device, - id="consumption_this_month", name="This month's consumption", attribute_getter="consumption_this_month", container=self, unit_getter=lambda: "kWh", + id="consumption_this_month", precision_hint=3, category=Feature.Category.Info, type=Feature.Type.Sensor, @@ -123,8 +123,8 @@ class Energy(Module, ABC): @property @abstractmethod - def current_consumption(self) -> float | None: - """Get the current power consumption in Watt.""" + def power(self) -> float | None: + """Get the current power draw in Watts.""" @property @abstractmethod @@ -182,6 +182,7 @@ class Energy(Module, ABC): "erase_emeter_stats": "erase_stats", "get_daystat": "get_daily_stats", "get_monthstat": "get_monthly_stats", + "current_consumption": "power", } if not TYPE_CHECKING: diff --git a/kasa/iot/iotstrip.py b/kasa/iot/iotstrip.py index a4b2ab99..717176c1 100755 --- a/kasa/iot/iotstrip.py +++ b/kasa/iot/iotstrip.py @@ -195,10 +195,10 @@ class StripEmeter(IotModule, Energy): return {} @property - def current_consumption(self) -> float | None: + def power(self) -> float | None: """Get the current power consumption in watts.""" return sum( - v if (v := plug.modules[Module.Energy].current_consumption) else 0.0 + v if (v := plug.modules[Module.Energy].power) else 0.0 for plug in self._device.children ) diff --git a/kasa/iot/modules/emeter.py b/kasa/iot/modules/emeter.py index 012bda04..4bc195c2 100644 --- a/kasa/iot/modules/emeter.py +++ b/kasa/iot/modules/emeter.py @@ -51,8 +51,8 @@ class Emeter(Usage, EnergyInterface): return data.get(current_month, 0.0) @property - def current_consumption(self) -> float | None: - """Get the current power consumption in Watt.""" + def power(self) -> float | None: + """Get the current power draw in Watts.""" return self.status.power @property diff --git a/kasa/smart/modules/energy.py b/kasa/smart/modules/energy.py index 6b5bdb57..edf1f33c 100644 --- a/kasa/smart/modules/energy.py +++ b/kasa/smart/modules/energy.py @@ -34,8 +34,8 @@ class Energy(SmartModule, EnergyInterface): @property @raise_if_update_error - def current_consumption(self) -> float | None: - """Current power in watts.""" + def power(self) -> float | None: + """Current power draw in Watts.""" if (power := self.energy.get("current_power")) is not None or ( power := self.data.get("get_emeter_data", {}).get("power_mw") ) is not None: @@ -105,14 +105,14 @@ class Energy(SmartModule, EnergyInterface): def current(self) -> float | None: """Return the current in A.""" ma = self.data.get("get_emeter_data", {}).get("current_ma") - return ma / 1000 if ma else None + return ma / 1000 if ma is not None else None @property @raise_if_update_error def voltage(self) -> float | None: """Get the current voltage in V.""" mv = self.data.get("get_emeter_data", {}).get("voltage_mv") - return mv / 1000 if mv else None + return mv / 1000 if mv is not None else None async def _deprecated_get_realtime(self) -> EmeterStatus: """Retrieve current energy readings.""" diff --git a/tests/iot/modules/test_emeter.py b/tests/iot/modules/test_emeter.py index 54fd02b2..0187b2df 100644 --- a/tests/iot/modules/test_emeter.py +++ b/tests/iot/modules/test_emeter.py @@ -133,9 +133,9 @@ async def test_erase_emeter_stats(dev): @has_emeter_iot -async def test_current_consumption(dev): +async def test_power(dev): emeter = dev.modules[Module.Energy] - x = emeter.current_consumption + x = emeter.power assert isinstance(x, float) assert x >= 0.0 diff --git a/tests/smart/test_smartdevice.py b/tests/smart/test_smartdevice.py index 81707a11..48a5ccc9 100644 --- a/tests/smart/test_smartdevice.py +++ b/tests/smart/test_smartdevice.py @@ -352,7 +352,7 @@ async def test_update_module_query_errors( if mod.name == "Energy": emod = cast(Energy, mod) with pytest.raises(KasaException, match="Module update error"): - assert emod.current_consumption is not None + assert emod.power is not None else: assert mod.disabled is False assert mod._error_count == 0 @@ -360,7 +360,7 @@ async def test_update_module_query_errors( # Test one of the raise_if_update_error doesn't raise if mod.name == "Energy": emod = cast(Energy, mod) - assert emod.current_consumption is not None + assert emod.power is not None async def test_get_modules(): diff --git a/tests/test_strip.py b/tests/test_strip.py index 73d10bb7..33d19f3c 100644 --- a/tests/test_strip.py +++ b/tests/test_strip.py @@ -159,4 +159,4 @@ async def test_children_energy(dev: Device): energy = plug.modules[Module.Energy] assert "voltage" in energy._module_features assert "current" in energy._module_features - assert "current_consumption" in energy._module_features + assert "power" in energy._module_features