FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Rename `current_consumption` to `power` by ryenitcher · Pull Request #1349 · python-kasa/python-kasa · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (7) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 2 additions & 2 deletions kasa/device.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
rssi
on_since
reboot
current_consumption
power
consumption_today
consumption_this_month
consumption_total
Expand Down Expand Up @@ -604,7 +604,7 @@ def _get_deprecated_callable_attribute(self, name: str) -> Any | None:
"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
Expand Down
13 changes: 7 additions & 6 deletions kasa/interfaces/energy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def _initialize_features(self) -> None:
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,
Expand All @@ -64,11 +64,11 @@ def _initialize_features(self) -> None:
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,
Expand Down Expand Up @@ -123,8 +123,8 @@ def status(self) -> EmeterStatus:

@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
Expand Down Expand Up @@ -182,6 +182,7 @@ async def get_monthly_stats(
"erase_emeter_stats": "erase_stats",
"get_daystat": "get_daily_stats",
"get_monthstat": "get_monthly_stats",
"current_consumption": "power",
}

if not TYPE_CHECKING:
Expand Down
4 changes: 2 additions & 2 deletions kasa/iot/iotstrip.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ def query(self) -> dict:
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
)

Expand Down
4 changes: 2 additions & 2 deletions kasa/iot/modules/emeter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def consumption_this_month(self) -> float | None:
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
Expand Down
27 changes: 20 additions & 7 deletions kasa/smart/modules/energy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,20 @@ def optional_response_keys(self) -> list[str]:
return []

@property
def current_consumption(self) -> float | None:
"""Current power in watts."""
return self._current_consumption
@raise_if_update_error
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:
return power / 1_000
# Fallback if get_energy_usage does not provide current_power,
# which can happen on some newer devices (e.g. P304M).
elif (
power := self.data.get("get_current_power", {}).get("current_power")
) is not None:
return power
return None

@property
def energy(self) -> dict:
Expand Down Expand Up @@ -126,15 +137,17 @@ def consumption_total(self) -> float | None:
@raise_if_update_error
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
if (ma := self.data.get("get_emeter_data", {}).get("current_ma")) is not None:
return ma / 1_000
return 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
if (mv := self.data.get("get_emeter_data", {}).get("voltage_mv")) is not None:
return mv / 1_000
return None

async def _deprecated_get_realtime(self) -> EmeterStatus:
"""Retrieve current energy readings."""
Expand Down
4 changes: 2 additions & 2 deletions tests/iot/modules/test_emeter.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_strip.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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

Back | FazBrowse Home | New Git URL