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).
This commit is contained in:
Ryan Nitcher 2024-12-06 20:48:16 -07:00
parent cb89342be1
commit 1c250913b6
8 changed files with 22 additions and 21 deletions

View File

@ -85,7 +85,7 @@ state
rssi rssi
on_since on_since
reboot reboot
current_consumption power
consumption_today consumption_today
consumption_this_month consumption_this_month
consumption_total consumption_total
@ -559,7 +559,7 @@ class Device(ABC):
"emeter_realtime": (Module.Energy, ["status"]), "emeter_realtime": (Module.Energy, ["status"]),
"emeter_today": (Module.Energy, ["consumption_today"]), "emeter_today": (Module.Energy, ["consumption_today"]),
"emeter_this_month": (Module.Energy, ["consumption_this_month"]), "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_daily": (Module.Energy, ["get_daily_stats"]),
"get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]), "get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]),
# Other attributes # Other attributes

View File

@ -38,11 +38,11 @@ class Energy(Module, ABC):
self._add_feature( self._add_feature(
Feature( Feature(
device, device,
name="Current consumption", name="Power",
attribute_getter="current_consumption", attribute_getter="power",
container=self, container=self,
unit_getter=lambda: "W", unit_getter=lambda: "W",
id="current_consumption", id="power",
precision_hint=1, precision_hint=1,
category=Feature.Category.Primary, category=Feature.Category.Primary,
type=Feature.Type.Sensor, type=Feature.Type.Sensor,
@ -64,11 +64,11 @@ class Energy(Module, ABC):
self._add_feature( self._add_feature(
Feature( Feature(
device, device,
id="consumption_this_month",
name="This month's consumption", name="This month's consumption",
attribute_getter="consumption_this_month", attribute_getter="consumption_this_month",
container=self, container=self,
unit_getter=lambda: "kWh", unit_getter=lambda: "kWh",
id="consumption_this_month",
precision_hint=3, precision_hint=3,
category=Feature.Category.Info, category=Feature.Category.Info,
type=Feature.Type.Sensor, type=Feature.Type.Sensor,
@ -123,8 +123,8 @@ class Energy(Module, ABC):
@property @property
@abstractmethod @abstractmethod
def current_consumption(self) -> float | None: def power(self) -> float | None:
"""Get the current power consumption in Watt.""" """Get the current power draw in Watts."""
@property @property
@abstractmethod @abstractmethod
@ -182,6 +182,7 @@ class Energy(Module, ABC):
"erase_emeter_stats": "erase_stats", "erase_emeter_stats": "erase_stats",
"get_daystat": "get_daily_stats", "get_daystat": "get_daily_stats",
"get_monthstat": "get_monthly_stats", "get_monthstat": "get_monthly_stats",
"current_consumption": "power",
} }
if not TYPE_CHECKING: if not TYPE_CHECKING:

View File

@ -195,10 +195,10 @@ class StripEmeter(IotModule, Energy):
return {} return {}
@property @property
def current_consumption(self) -> float | None: def power(self) -> float | None:
"""Get the current power consumption in watts.""" """Get the current power consumption in watts."""
return sum( 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 for plug in self._device.children
) )

View File

@ -51,8 +51,8 @@ class Emeter(Usage, EnergyInterface):
return data.get(current_month, 0.0) return data.get(current_month, 0.0)
@property @property
def current_consumption(self) -> float | None: def power(self) -> float | None:
"""Get the current power consumption in Watt.""" """Get the current power draw in Watts."""
return self.status.power return self.status.power
@property @property

View File

@ -34,8 +34,8 @@ class Energy(SmartModule, EnergyInterface):
@property @property
@raise_if_update_error @raise_if_update_error
def current_consumption(self) -> float | None: def power(self) -> float | None:
"""Current power in watts.""" """Current power draw in Watts."""
if (power := self.energy.get("current_power")) is not None or ( if (power := self.energy.get("current_power")) is not None or (
power := self.data.get("get_emeter_data", {}).get("power_mw") power := self.data.get("get_emeter_data", {}).get("power_mw")
) is not None: ) is not None:
@ -105,14 +105,14 @@ class Energy(SmartModule, EnergyInterface):
def current(self) -> float | None: def current(self) -> float | None:
"""Return the current in A.""" """Return the current in A."""
ma = self.data.get("get_emeter_data", {}).get("current_ma") 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 @property
@raise_if_update_error @raise_if_update_error
def voltage(self) -> float | None: def voltage(self) -> float | None:
"""Get the current voltage in V.""" """Get the current voltage in V."""
mv = self.data.get("get_emeter_data", {}).get("voltage_mv") 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: async def _deprecated_get_realtime(self) -> EmeterStatus:
"""Retrieve current energy readings.""" """Retrieve current energy readings."""

View File

@ -133,9 +133,9 @@ async def test_erase_emeter_stats(dev):
@has_emeter_iot @has_emeter_iot
async def test_current_consumption(dev): async def test_power(dev):
emeter = dev.modules[Module.Energy] emeter = dev.modules[Module.Energy]
x = emeter.current_consumption x = emeter.power
assert isinstance(x, float) assert isinstance(x, float)
assert x >= 0.0 assert x >= 0.0

View File

@ -352,7 +352,7 @@ async def test_update_module_query_errors(
if mod.name == "Energy": if mod.name == "Energy":
emod = cast(Energy, mod) emod = cast(Energy, mod)
with pytest.raises(KasaException, match="Module update error"): with pytest.raises(KasaException, match="Module update error"):
assert emod.current_consumption is not None assert emod.power is not None
else: else:
assert mod.disabled is False assert mod.disabled is False
assert mod._error_count == 0 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 # Test one of the raise_if_update_error doesn't raise
if mod.name == "Energy": if mod.name == "Energy":
emod = cast(Energy, mod) emod = cast(Energy, mod)
assert emod.current_consumption is not None assert emod.power is not None
async def test_get_modules(): async def test_get_modules():

View File

@ -159,4 +159,4 @@ async def test_children_energy(dev: Device):
energy = plug.modules[Module.Energy] energy = plug.modules[Module.Energy]
assert "voltage" in energy._module_features assert "voltage" in energy._module_features
assert "current" in energy._module_features assert "current" in energy._module_features
assert "current_consumption" in energy._module_features assert "power" in energy._module_features