8000 Rename `current_consumption` to `power` · python-kasa/python-kasa@0963d6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 0963d6f

Browse files
committed
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).
1 parent 9b7bf36 commit 0963d6f

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

kasa/device.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
rssi
8686
on_since
8787
reboot
88-
current_consumption
88+
power
8989
consumption_today
9090
consumption_this_month
9191
consumption_total
@@ -604,7 +604,7 @@ def _get_deprecated_callable_attribute(self, name: str) -> Any | None:
604604
"emeter_realtime": (Module.Energy, ["status"]),
605605
"emeter_today": (Module.Energy, ["consumption_today"]),
606606
"emeter_this_month": (Module.Energy, ["consumption_this_month"]),
607-
"current_consumption": (Module.Energy, ["current_consumption"]),
607+
"current_consumption": (Module.Energy, ["power"]),
608608
"get_emeter_daily": (Module.Energy, ["get_daily_stats"]),
609609
"get_emeter_monthly": (Module.Energy, ["get_monthly_stats"]),
610610
# Other attributes

kasa/interfaces/energy.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ def _initialize_features(self) -> None:
3838
self._add_feature(
3939
Feature(
4040
device,
41-
name="Current consumption",
42-
attribute_getter="current_consumption",
41+
name="Power",
42+
attribute_getter="power",
4343
container=self,
4444
unit_getter=lambda: "W",
45-
id="current_consumption",
45+
id="power",
4646
precision_hint=1,
4747
category=Feature.Category.Primary,
4848
type=Feature.Type.Sensor,
@@ -64,11 +64,11 @@ def _initialize_features(self) -> None:
6464
self._add_feature(
6565
Feature(
6666
device,
67-
id="consumption_this_month",
6867
name="This month's consumption",
6968
attribute_getter="consumption_this_month",
7069
container=self,
7170
unit_getter=lambda: "kWh",
71+
id="consumption_this_month",
7272
precision_hint=3,
7373
category=Feature.Category.Info,
7474
type=Feature.Type.Sensor,
@@ -123,8 +123,8 @@ def status(self) -> EmeterStatus:
123123

124124
@property
125125
@abstractmethod
126-
def current_consumption(self) -> float | None:
127-
"""Get the current power consumption in Watt."""
126+
def power(self) -> float | None:
127+
"""Get the current power draw in Watts."""
128128

129129
@property
130130
@abstractmethod
@@ -182,6 +182,7 @@ async def get_monthly_stats(
182182
"erase_emeter_stats": "erase_stats",
183183
"get_daystat": "get_daily_stats",
184184
"get_monthstat": "get_monthly_stats",
185+
"current_consumption": "power",
185186
}
186187

187188
if not TYPE_CHECKING:

kasa/iot/iotstrip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ def query(self) -> dict:
201201
return {}
202202

203203
@property
204-
def current_consumption(self) -> float | None:
204+
def power(self) -> float | None:
205205
"""Get the current power consumption in watts."""
206206
return sum(
207-
v if (v := plug.modules[Module.Energy].current_consumption) else 0.0
207+
v if (v := plug.modules[Module.Energy].power) else 0.0
208208
for plug in self._device.children
209209
)
210210

kasa/iot/modules/emeter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ def consumption_this_month(self) -> float | None:
5151
return data.get(current_month, 0.0)
5252

5353
@property
54-
def current_consumption(self) -> float | None:
55-
"""Get the current power consumption in Watt."""
54+
def power(self) -> float | None:
55+
"""Get the current power draw in Watts."""
5656
return self.status.power
5757

5858
@property

kasa/smart/modules/energy.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,20 @@ def optional_response_keys(self) -> list[str]:
6767
return []
6868

6969
@property
70-
def current_consumption(self) -> float | None:
71-
"""Current power in watts."""
72-
return self._current_consumption
70+
@raise_if_update_error
71+
def power(self) -> float | None:
72+
"""Current power draw in Watts."""
73+
if (power := self.energy.get("current_power")) is not None or (
74+
power := self.data.get("get_emeter_data", {}).get("power_mw")
75+
) is not None:
76+
return power / 1_000
77+
# Fallback if get_energy_usage does not provide current_power,
78+
# which can happen on some newer devices (e.g. P304M).
79+
elif (
80+
power := self.data.get("get_current_power", {}).get("current_power")
81+
) is not None:
82+
return power
83+
return None
7384

7485
@property
7586
def energy(self) -> dict:
@@ -127,14 +138,14 @@ def consumption_total(self) -> float | None:
127138
def current(self) -> float | None:
128139
"""Return the current in A."""
129140
ma = self.data.get("get_emeter_data", {}).get("current_ma")
130-
return ma / 1000 if ma else None
141+
return ma / 1000 if ma is not None else None
131142

132143
@property
133144
@raise_if_update_error
134145
def voltage(self) -> float | None:
135146
"""Get the current voltage in V."""
136147
mv = self.data.get("get_emeter_data", {}).get("voltage_mv")
137-
return mv / 1000 if mv else None
148+
return mv / 1000 if mv is not None else None
138149

139150
async def _deprecated_get_realtime(self) -> EmeterStatus:
140151
"""Retrieve current energy readings."""

tests/iot/modules/test_emeter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ async def test_erase_emeter_stats(dev):
133133

134134

135135
@has_emeter_iot
136-
async def test_current_consumption(dev):
136+
async def test_power(dev):
137137
emeter = dev.modules[Module.Energy]
138-
x = emeter.current_consumption
138+
x = emeter.power
139139
assert isinstance(x, float)
140140
assert x >= 0.0
141141

tests/test_strip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,4 @@ async def test_children_energy(dev: Device):
159159
energy = plug.modules[Module.Energy]
160160
assert "voltage" in energy._module_features
161161
assert "current" in energy._module_features
162-
assert "current_consumption" in energy._module_features
162+
assert "power" in energy._module_features

0 commit comments

Comments
 (0)
0