8000 Expose IOT emeter info as features (#844) · python-kasa/python-kasa@6dddcd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6dddcd9

Browse files
rytilahtisdb9696
andcommitted
Expose IOT emeter info as features (#844)
Exposes IOT emeter information using features, bases on #843 to allow defining the units. --------- Co-authored-by: Steven B <51370195+sdb9696@users.noreply.github.com>
1 parent e676053 commit 6dddcd9

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

kasa/iot/iotstrip.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
requires_update,
1919
)
2020
from .iotplug import IotPlug
21-
from .modules import Antitheft, Countdown, Emeter, Schedule, Time, Usage
21+
from .modules import Antitheft, Countdown, Schedule, Time, Usage
2222

2323
_LOGGER = logging.getLogger(__name__)
2424

@@ -100,7 +100,6 @@ def __init__(
100100
self.add_module("usage", Usage(self, "schedule"))
101101
self.add_module("time", Time(self, "time"))
102102
self.add_module("countdown", Countdown(self, "countdown"))
103-
self.add_module("emeter", Emeter(self, "emeter"))
104103

105104
@property # type: ignore
106105
@requires_update
@@ -217,13 +216,13 @@ async def erase_emeter_stats(self):
217216
@requires_update
218217
def emeter_this_month(self) -> float | None:
219218
"""Return this month's energy consumption in kWh."""
220-
return sum(plug.emeter_this_month for plug in self.children)
219+
return sum(v if (v := plug.emeter_this_month) else 0 for plug in self.children)
221220

222221
@property # type: ignore
223222
@requires_update
224223
def emeter_today(self) -> float | None:
225224
"""Return this month's energy consumption in kWh."""
226-
return sum(plug.emeter_today for plug in self.children)
225+
return sum(v if (v := plug.emeter_today) else 0 for plug in self.children)
227226

228227
@property # type: ignore
229228
@requires_update

kasa/iot/modules/emeter.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,77 @@
44

55
from datetime import datetime
66

7+
from ... import Device
78
from ...emeterstatus import EmeterStatus
9+
from ...feature import Feature
810
from .usage import Usage
911

1012

1113
class Emeter(Usage):
1214
"""Emeter module."""
1315

16+
def __init__(self, device: Device, module: str):
17+
super().__init__(device, module)
18+
self._add_feature(
19+
Feature(
20+
device,
21+
name="Current consumption",
22+
attribute_getter="current_consumption",
23+
container=self,
24+
unit="W",
25+
id="current_power_w", # for homeassistant backwards compat
26+
)
27+
)
28+
self._add_feature(
29+
Feature(
30+
device,
31+
name="Today's consumption",
32+
attribute_getter="emeter_today",
33+
container=self,
34+
unit="kWh",
35+
id="today_energy_kwh", # for homeassistant backwards compat
36+
)
37+
)
38+
self._add_feature(
39+
Feature(
40+
device,
41+
name="This month's consumption",
42+
attribute_getter="emeter_this_month",
43+
container=self,
44+
unit="kWh",
45+
)
46+
)
47+
self._add_feature(
48+
Feature(
49+
device,
50+
name="Total consumption since reboot",
51+
attribute_getter="emeter_total",
52+
container=self,
53+
unit="kWh",
54+
id="total_energy_kwh", # for homeassistant backwards compat
55+
)
56+
)
57+
self._add_feature(
58+
Feature(
59+
device,
60+
name="Voltage",
61+
attribute_getter="voltage",
62+
container=self,
63+
unit="V",
64+
id="voltage", # for homeassistant backwards compat
65+
)
66+
)
67+
self._add_feature(
68+
Feature(
69+
device,
70+
name="Current",
71+
attribute_getter="current",
72+
container=self,
73+
unit="A",
74+
id="current_a", # for homeassistant backwards compat
75+
)
76+
)
77+
1478
@property # type: ignore
1579
def realtime(self) -> EmeterStatus:
1680
"""Return current energy readings."""
@@ -32,6 +96,26 @@ def emeter_this_month(self) -> float | None:
3296
data = self._convert_stat_data(raw_data, entry_key="month", key=current_month)
3397
return data.get(current_month)
3498

99+
@property
100+
def current_consumption(self) -> float | None:
101+
"""Get the current power consumption in Watt."""
102+
return self.realtime.power
103+
104+
@property
105+
def emeter_total(self) -> float | None:
106+
"""Return total consumption since last reboot in kWh."""
107+
return self.realtime.total
108+
109+
@property
110+
def current(self) -> float | None:
111+
"""Return the current in A."""
112+
return self.realtime.current
113+
114+
@property
115+
def voltage(self) -> float | None:
116+
"""Get the current voltage in V."""
117+
return self.realtime.voltage
118+
35119
async def erase_stats(self):
36120
"""Erase all stats.
37121

0 commit comments

Comments
 (0)
0