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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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():

View File

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