python-kasa 0.7.0.dev3__py3-none-any.whl → 0.7.0.dev5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kasa/cli.py +58 -22
- kasa/device.py +21 -31
- kasa/feature.py +5 -4
- kasa/interfaces/__init__.py +2 -0
- kasa/interfaces/energy.py +181 -0
- kasa/iot/iotbulb.py +1 -1
- kasa/iot/iotdevice.py +26 -101
- kasa/iot/iotstrip.py +112 -60
- kasa/iot/modules/emeter.py +30 -89
- kasa/iot/modules/led.py +5 -0
- kasa/module.py +2 -3
- kasa/smart/modules/autooff.py +8 -0
- kasa/smart/modules/energy.py +58 -60
- kasa/smart/modules/lightpreset.py +10 -0
- kasa/smart/modules/lightstripeffect.py +14 -4
- kasa/smart/modules/lighttransition.py +10 -0
- kasa/smart/modules/time.py +7 -1
- kasa/smart/smartchilddevice.py +12 -1
- kasa/smart/smartdevice.py +34 -74
- kasa/tests/device_fixtures.py +31 -0
- kasa/tests/fakeprotocol_smart.py +19 -1
- kasa/tests/fixtures/smart/L920-5(EU)_1.0_1.0.7.json +350 -0
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.15.json +966 -0
- kasa/tests/smart/features/test_brightness.py +3 -3
- kasa/tests/smart/modules/test_autooff.py +8 -8
- kasa/tests/smart/modules/test_fan.py +6 -7
- kasa/tests/test_childdevice.py +19 -1
- kasa/tests/test_cli.py +37 -5
- kasa/tests/test_common_modules.py +8 -7
- kasa/tests/test_device.py +14 -6
- kasa/tests/test_emeter.py +36 -8
- kasa/tests/test_iotdevice.py +9 -2
- kasa/tests/test_smartdevice.py +11 -7
- {python_kasa-0.7.0.dev3.dist-info → python_kasa-0.7.0.dev5.dist-info}/METADATA +1 -1
- {python_kasa-0.7.0.dev3.dist-info → python_kasa-0.7.0.dev5.dist-info}/RECORD +38 -35
- {python_kasa-0.7.0.dev3.dist-info → python_kasa-0.7.0.dev5.dist-info}/LICENSE +0 -0
- {python_kasa-0.7.0.dev3.dist-info → python_kasa-0.7.0.dev5.dist-info}/WHEEL +0 -0
- {python_kasa-0.7.0.dev3.dist-info → python_kasa-0.7.0.dev5.dist-info}/entry_points.txt +0 -0
kasa/iot/iotdevice.py
CHANGED
|
@@ -18,12 +18,11 @@ import collections.abc
|
|
|
18
18
|
import functools
|
|
19
19
|
import inspect
|
|
20
20
|
import logging
|
|
21
|
-
from datetime import datetime, timedelta
|
|
21
|
+
from datetime import datetime, timedelta, timezone
|
|
22
22
|
from typing import TYPE_CHECKING, Any, Mapping, Sequence, cast
|
|
23
23
|
|
|
24
24
|
from ..device import Device, WifiNetwork
|
|
25
25
|
from ..deviceconfig import DeviceConfig
|
|
26
|
-
from ..emeterstatus import EmeterStatus
|
|
27
26
|
from ..exceptions import KasaException
|
|
28
27
|
from ..feature import Feature
|
|
29
28
|
from ..module import Module
|
|
@@ -188,7 +187,7 @@ class IotDevice(Device):
|
|
|
188
187
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
189
188
|
|
|
190
189
|
self._sys_info: Any = None # TODO: this is here to avoid changing tests
|
|
191
|
-
self._supported_modules: dict[str, IotModule] | None = None
|
|
190
|
+
self._supported_modules: dict[str | ModuleName[Module], IotModule] | None = None
|
|
192
191
|
self._legacy_features: set[str] = set()
|
|
193
192
|
self._children: Mapping[str, IotDevice] = {}
|
|
194
193
|
self._modules: dict[str | ModuleName[Module], IotModule] = {}
|
|
@@ -199,15 +198,16 @@ class IotDevice(Device):
|
|
|
199
198
|
return list(self._children.values())
|
|
200
199
|
|
|
201
200
|
@property
|
|
201
|
+
@requires_update
|
|
202
202
|
def modules(self) -> ModuleMapping[IotModule]:
|
|
203
203
|
"""Return the device modules."""
|
|
204
204
|
if TYPE_CHECKING:
|
|
205
|
-
return cast(ModuleMapping[IotModule], self.
|
|
206
|
-
return self.
|
|
205
|
+
return cast(ModuleMapping[IotModule], self._supported_modules)
|
|
206
|
+
return self._supported_modules
|
|
207
207
|
|
|
208
208
|
def add_module(self, name: str | ModuleName[Module], module: IotModule):
|
|
209
209
|
"""Register a module."""
|
|
210
|
-
if name in self.
|
|
210
|
+
if name in self._modules:
|
|
211
211
|
_LOGGER.debug("Module %s already registered, ignoring..." % name)
|
|
212
212
|
return
|
|
213
213
|
|
|
@@ -272,14 +272,6 @@ class IotDevice(Device):
|
|
|
272
272
|
"""Return a set of features that the device supports."""
|
|
273
273
|
return self._features
|
|
274
274
|
|
|
275
|
-
@property # type: ignore
|
|
276
|
-
@requires_update
|
|
277
|
-
def supported_modules(self) -> list[str | ModuleName[Module]]:
|
|
278
|
-
"""Return a set of modules supported by the device."""
|
|
279
|
-
# TODO: this should rather be called `features`, but we don't want to break
|
|
280
|
-
# the API now. Maybe just deprecate it and point the users to use this?
|
|
281
|
-
return list(self._modules.keys())
|
|
282
|
-
|
|
283
275
|
@property # type: ignore
|
|
284
276
|
@requires_update
|
|
285
277
|
def has_emeter(self) -> bool:
|
|
@@ -321,6 +313,11 @@ class IotDevice(Device):
|
|
|
321
313
|
|
|
322
314
|
async def _initialize_modules(self):
|
|
323
315
|
"""Initialize modules not added in init."""
|
|
316
|
+
if self.has_emeter:
|
|
317
|
+
_LOGGER.debug(
|
|
318
|
+
"The device has emeter, querying its information along sysinfo"
|
|
319
|
+
)
|
|
320
|
+
self.add_module(Module.Energy, Emeter(self, self.emeter_type))
|
|
324
321
|
|
|
325
322
|
async def _initialize_features(self):
|
|
326
323
|
"""Initialize common features."""
|
|
@@ -345,7 +342,8 @@ class IotDevice(Device):
|
|
|
345
342
|
category=Feature.Category.Debug,
|
|
346
343
|
)
|
|
347
344
|
)
|
|
348
|
-
|
|
345
|
+
# iot strips calculate on_since from the children
|
|
346
|
+
if "on_time" in self._sys_info or self.device_type == Device.Type.Strip:
|
|
349
347
|
self._add_feature(
|
|
350
348
|
Feature(
|
|
351
349
|
device=self,
|
|
@@ -356,29 +354,13 @@ class IotDevice(Device):
|
|
|
356
354
|
)
|
|
357
355
|
)
|
|
358
356
|
|
|
359
|
-
for module in self.
|
|
357
|
+
for module in self._supported_modules.values():
|
|
360
358
|
module._initialize_features()
|
|
361
359
|
for module_feat in module._module_features.values():
|
|
362
360
|
self._add_feature(module_feat)
|
|
363
361
|
|
|
364
362
|
async def _modular_update(self, req: dict) -> None:
|
|
365
363
|
"""Execute an update query."""
|
|
366
|
-
if self.has_emeter:
|
|
367
|
-
_LOGGER.debug(
|
|
368
|
-
"The device has emeter, querying its information along sysinfo"
|
|
369
|
-
)
|
|
370
|
-
self.add_module(Module.IotEmeter, Emeter(self, self.emeter_type))
|
|
371
|
-
|
|
372
|
-
# TODO: perhaps modules should not have unsupported modules,
|
|
373
|
-
# making separate handling for this unnecessary
|
|
374
|
-
if self._supported_modules is None:
|
|
375
|
-
supported = {}
|
|
376
|
-
for module in self._modules.values():
|
|
377
|
-
if module.is_supported:
|
|
378
|
-
supported[module._module] = module
|
|
379
|
-
|
|
380
|
-
self._supported_modules = supported
|
|
381
|
-
|
|
382
364
|
request_list = []
|
|
383
365
|
est_response_size = 1024 if "system" in req else 0
|
|
384
366
|
for module in self._modules.values():
|
|
@@ -410,6 +392,15 @@ class IotDevice(Device):
|
|
|
410
392
|
update = {**update, **response}
|
|
411
393
|
self._last_update = update
|
|
412
394
|
|
|
395
|
+
# IOT modules are added as default but could be unsupported post first update
|
|
396
|
+
if self._supported_modules is None:
|
|
397
|
+
supported = {}
|
|
398
|
+
for module_name, module in self._modules.items():
|
|
399
|
+
if module.is_supported:
|
|
400
|
+
supported[module_name] = module
|
|
401
|
+
|
|
402
|
+
self._supported_modules = supported
|
|
403
|
+
|
|
413
404
|
def update_from_discover_info(self, info: dict[str, Any]) -> None:
|
|
414
405
|
"""Update state from info from the discover call."""
|
|
415
406
|
self._discovery_info = info
|
|
@@ -556,74 +547,6 @@ class IotDevice(Device):
|
|
|
556
547
|
"""
|
|
557
548
|
return await self._query_helper("system", "set_mac_addr", {"mac": mac})
|
|
558
549
|
|
|
559
|
-
@property
|
|
560
|
-
@requires_update
|
|
561
|
-
def emeter_realtime(self) -> EmeterStatus:
|
|
562
|
-
"""Return current energy readings."""
|
|
563
|
-
self._verify_emeter()
|
|
564
|
-
return EmeterStatus(self.modules[Module.IotEmeter].realtime)
|
|
565
|
-
|
|
566
|
-
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
567
|
-
"""Retrieve current energy readings."""
|
|
568
|
-
self._verify_emeter()
|
|
569
|
-
return EmeterStatus(await self.modules[Module.IotEmeter].get_realtime())
|
|
570
|
-
|
|
571
|
-
@property
|
|
572
|
-
@requires_update
|
|
573
|
-
def emeter_today(self) -> float | None:
|
|
574
|
-
"""Return today's energy consumption in kWh."""
|
|
575
|
-
self._verify_emeter()
|
|
576
|
-
return self.modules[Module.IotEmeter].emeter_today
|
|
577
|
-
|
|
578
|
-
@property
|
|
579
|
-
@requires_update
|
|
580
|
-
def emeter_this_month(self) -> float | None:
|
|
581
|
-
"""Return this month's energy consumption in kWh."""
|
|
582
|
-
self._verify_emeter()
|
|
583
|
-
return self.modules[Module.IotEmeter].emeter_this_month
|
|
584
|
-
|
|
585
|
-
async def get_emeter_daily(
|
|
586
|
-
self, year: int | None = None, month: int | None = None, kwh: bool = True
|
|
587
|
-
) -> dict:
|
|
588
|
-
"""Retrieve daily statistics for a given month.
|
|
589
|
-
|
|
590
|
-
:param year: year for which to retrieve statistics (default: this year)
|
|
591
|
-
:param month: month for which to retrieve statistics (default: this
|
|
592
|
-
month)
|
|
593
|
-
:param kwh: return usage in kWh (default: True)
|
|
594
|
-
:return: mapping of day of month to value
|
|
595
|
-
"""
|
|
596
|
-
self._verify_emeter()
|
|
597
|
-
return await self.modules[Module.IotEmeter].get_daystat(
|
|
598
|
-
year=year, month=month, kwh=kwh
|
|
599
|
-
)
|
|
600
|
-
|
|
601
|
-
@requires_update
|
|
602
|
-
async def get_emeter_monthly(
|
|
603
|
-
self, year: int | None = None, kwh: bool = True
|
|
604
|
-
) -> dict:
|
|
605
|
-
"""Retrieve monthly statistics for a given year.
|
|
606
|
-
|
|
607
|
-
:param year: year for which to retrieve statistics (default: this year)
|
|
608
|
-
:param kwh: return usage in kWh (default: True)
|
|
609
|
-
:return: dict: mapping of month to value
|
|
610
|
-
"""
|
|
611
|
-
self._verify_emeter()
|
|
612
|
-
return await self.modules[Module.IotEmeter].get_monthstat(year=year, kwh=kwh)
|
|
613
|
-
|
|
614
|
-
@requires_update
|
|
615
|
-
async def erase_emeter_stats(self) -> dict:
|
|
616
|
-
"""Erase energy meter statistics."""
|
|
617
|
-
self._verify_emeter()
|
|
618
|
-
return await self.modules[Module.IotEmeter].erase_stats()
|
|
619
|
-
|
|
620
|
-
@requires_update
|
|
621
|
-
async def current_consumption(self) -> float:
|
|
622
|
-
"""Get the current power consumption in Watt."""
|
|
623
|
-
self._verify_emeter()
|
|
624
|
-
response = self.emeter_realtime
|
|
625
|
-
return float(response["power"])
|
|
626
|
-
|
|
627
550
|
async def reboot(self, delay: int = 1) -> None:
|
|
628
551
|
"""Reboot the device.
|
|
629
552
|
|
|
@@ -665,7 +588,9 @@ class IotDevice(Device):
|
|
|
665
588
|
|
|
666
589
|
on_time = self._sys_info["on_time"]
|
|
667
590
|
|
|
668
|
-
return datetime.now().
|
|
591
|
+
return datetime.now(timezone.utc).astimezone().replace(
|
|
592
|
+
microsecond=0
|
|
593
|
+
) - timedelta(seconds=on_time)
|
|
669
594
|
|
|
670
595
|
@property # type: ignore
|
|
671
596
|
@requires_update
|
kasa/iot/iotstrip.py
CHANGED
|
@@ -4,21 +4,22 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
import logging
|
|
6
6
|
from collections import defaultdict
|
|
7
|
-
from datetime import datetime, timedelta
|
|
7
|
+
from datetime import datetime, timedelta, timezone
|
|
8
8
|
from typing import Any
|
|
9
9
|
|
|
10
10
|
from ..device_type import DeviceType
|
|
11
11
|
from ..deviceconfig import DeviceConfig
|
|
12
|
+
from ..emeterstatus import EmeterStatus
|
|
12
13
|
from ..exceptions import KasaException
|
|
13
14
|
from ..feature import Feature
|
|
15
|
+
from ..interfaces import Energy
|
|
14
16
|
from ..module import Module
|
|
15
17
|
from ..protocol import BaseProtocol
|
|
16
18
|
from .iotdevice import (
|
|
17
|
-
EmeterStatus,
|
|
18
19
|
IotDevice,
|
|
19
|
-
merge,
|
|
20
20
|
requires_update,
|
|
21
21
|
)
|
|
22
|
+
from .iotmodule import IotModule
|
|
22
23
|
from .iotplug import IotPlug
|
|
23
24
|
from .modules import Antitheft, Countdown, Schedule, Time, Usage
|
|
24
25
|
|
|
@@ -97,11 +98,20 @@ class IotStrip(IotDevice):
|
|
|
97
98
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
98
99
|
self.emeter_type = "emeter"
|
|
99
100
|
self._device_type = DeviceType.Strip
|
|
101
|
+
|
|
102
|
+
async def _initialize_modules(self):
|
|
103
|
+
"""Initialize modules."""
|
|
104
|
+
# Strip has different modules to plug so do not call super
|
|
100
105
|
self.add_module(Module.IotAntitheft, Antitheft(self, "anti_theft"))
|
|
101
106
|
self.add_module(Module.IotSchedule, Schedule(self, "schedule"))
|
|
102
107
|
self.add_module(Module.IotUsage, Usage(self, "schedule"))
|
|
103
108
|
self.add_module(Module.IotTime, Time(self, "time"))
|
|
104
109
|
self.add_module(Module.IotCountdown, Countdown(self, "countdown"))
|
|
110
|
+
if self.has_emeter:
|
|
111
|
+
_LOGGER.debug(
|
|
112
|
+
"The device has emeter, querying its information along sysinfo"
|
|
113
|
+
)
|
|
114
|
+
self.add_module(Module.Energy, StripEmeter(self, self.emeter_type))
|
|
105
115
|
|
|
106
116
|
@property # type: ignore
|
|
107
117
|
@requires_update
|
|
@@ -114,10 +124,12 @@ class IotStrip(IotDevice):
|
|
|
114
124
|
|
|
115
125
|
Needed for methods that are decorated with `requires_update`.
|
|
116
126
|
"""
|
|
127
|
+
# Super initializes modules and features
|
|
117
128
|
await super().update(update_children)
|
|
118
129
|
|
|
130
|
+
initialize_children = not self.children
|
|
119
131
|
# Initialize the child devices during the first update.
|
|
120
|
-
if
|
|
132
|
+
if initialize_children:
|
|
121
133
|
children = self.sys_info["children"]
|
|
122
134
|
_LOGGER.debug("Initializing %s child sockets", len(children))
|
|
123
135
|
self._children = {
|
|
@@ -127,12 +139,22 @@ class IotStrip(IotDevice):
|
|
|
127
139
|
for child in children
|
|
128
140
|
}
|
|
129
141
|
for child in self._children.values():
|
|
130
|
-
await child.
|
|
142
|
+
await child._initialize_modules()
|
|
131
143
|
|
|
132
|
-
if update_children
|
|
144
|
+
if update_children:
|
|
133
145
|
for plug in self.children:
|
|
134
146
|
await plug.update()
|
|
135
147
|
|
|
148
|
+
if not self.features:
|
|
149
|
+
await self._initialize_features()
|
|
150
|
+
|
|
151
|
+
async def _initialize_features(self):
|
|
152
|
+
"""Initialize common features."""
|
|
153
|
+
# Do not initialize features until children are created
|
|
154
|
+
if not self.children:
|
|
155
|
+
return
|
|
156
|
+
await super()._initialize_features()
|
|
157
|
+
|
|
136
158
|
async def turn_on(self, **kwargs):
|
|
137
159
|
"""Turn the strip on."""
|
|
138
160
|
await self._query_helper("system", "set_relay_state", {"state": 1})
|
|
@@ -148,23 +170,45 @@ class IotStrip(IotDevice):
|
|
|
148
170
|
if self.is_off:
|
|
149
171
|
return None
|
|
150
172
|
|
|
151
|
-
return
|
|
173
|
+
return min(plug.on_since for plug in self.children if plug.on_since is not None)
|
|
152
174
|
|
|
153
|
-
|
|
175
|
+
|
|
176
|
+
class StripEmeter(IotModule, Energy):
|
|
177
|
+
"""Energy module implementation to aggregate child modules."""
|
|
178
|
+
|
|
179
|
+
_supported = (
|
|
180
|
+
Energy.ModuleFeature.CONSUMPTION_TOTAL
|
|
181
|
+
| Energy.ModuleFeature.PERIODIC_STATS
|
|
182
|
+
| Energy.ModuleFeature.VOLTAGE_CURRENT
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def supports(self, module_feature: Energy.ModuleFeature) -> bool:
|
|
186
|
+
"""Return True if module supports the feature."""
|
|
187
|
+
return module_feature in self._supported
|
|
188
|
+
|
|
189
|
+
def query(self):
|
|
190
|
+
"""Return the base query."""
|
|
191
|
+
return {}
|
|
192
|
+
|
|
193
|
+
@property
|
|
194
|
+
def current_consumption(self) -> float | None:
|
|
154
195
|
"""Get the current power consumption in watts."""
|
|
155
|
-
return sum(
|
|
196
|
+
return sum(
|
|
197
|
+
v if (v := plug.modules[Module.Energy].current_consumption) else 0.0
|
|
198
|
+
for plug in self._device.children
|
|
199
|
+
)
|
|
156
200
|
|
|
157
|
-
|
|
158
|
-
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
201
|
+
async def get_status(self) -> EmeterStatus:
|
|
159
202
|
"""Retrieve current energy readings."""
|
|
160
|
-
emeter_rt = await self._async_get_emeter_sum("
|
|
203
|
+
emeter_rt = await self._async_get_emeter_sum("get_status", {})
|
|
161
204
|
# Voltage is averaged since each read will result
|
|
162
205
|
# in a slightly different voltage since they are not atomic
|
|
163
|
-
emeter_rt["voltage_mv"] = int(
|
|
206
|
+
emeter_rt["voltage_mv"] = int(
|
|
207
|
+
emeter_rt["voltage_mv"] / len(self._device.children)
|
|
208
|
+
)
|
|
164
209
|
return EmeterStatus(emeter_rt)
|
|
165
210
|
|
|
166
|
-
|
|
167
|
-
async def get_emeter_daily(
|
|
211
|
+
async def get_daily_stats(
|
|
168
212
|
self, year: int | None = None, month: int | None = None, kwh: bool = True
|
|
169
213
|
) -> dict:
|
|
170
214
|
"""Retrieve daily statistics for a given month.
|
|
@@ -176,11 +220,10 @@ class IotStrip(IotDevice):
|
|
|
176
220
|
:return: mapping of day of month to value
|
|
177
221
|
"""
|
|
178
222
|
return await self._async_get_emeter_sum(
|
|
179
|
-
"
|
|
223
|
+
"get_daily_stats", {"year": year, "month": month, "kwh": kwh}
|
|
180
224
|
)
|
|
181
225
|
|
|
182
|
-
|
|
183
|
-
async def get_emeter_monthly(
|
|
226
|
+
async def get_monthly_stats(
|
|
184
227
|
self, year: int | None = None, kwh: bool = True
|
|
185
228
|
) -> dict:
|
|
186
229
|
"""Retrieve monthly statistics for a given year.
|
|
@@ -189,44 +232,68 @@ class IotStrip(IotDevice):
|
|
|
189
232
|
:param kwh: return usage in kWh (default: True)
|
|
190
233
|
"""
|
|
191
234
|
return await self._async_get_emeter_sum(
|
|
192
|
-
"
|
|
235
|
+
"get_monthly_stats", {"year": year, "kwh": kwh}
|
|
193
236
|
)
|
|
194
237
|
|
|
195
238
|
async def _async_get_emeter_sum(self, func: str, kwargs: dict[str, Any]) -> dict:
|
|
196
|
-
"""
|
|
197
|
-
self._verify_emeter()
|
|
239
|
+
"""Retrieve emeter stats for a time period from children."""
|
|
198
240
|
return merge_sums(
|
|
199
|
-
[
|
|
241
|
+
[
|
|
242
|
+
await getattr(plug.modules[Module.Energy], func)(**kwargs)
|
|
243
|
+
for plug in self._device.children
|
|
244
|
+
]
|
|
200
245
|
)
|
|
201
246
|
|
|
202
|
-
|
|
203
|
-
async def erase_emeter_stats(self):
|
|
247
|
+
async def erase_stats(self):
|
|
204
248
|
"""Erase energy meter statistics for all plugs."""
|
|
205
|
-
for plug in self.children:
|
|
206
|
-
await plug.
|
|
249
|
+
for plug in self._device.children:
|
|
250
|
+
await plug.modules[Module.Energy].erase_stats()
|
|
207
251
|
|
|
208
252
|
@property # type: ignore
|
|
209
|
-
|
|
210
|
-
def emeter_this_month(self) -> float | None:
|
|
253
|
+
def consumption_this_month(self) -> float | None:
|
|
211
254
|
"""Return this month's energy consumption in kWh."""
|
|
212
|
-
return sum(
|
|
255
|
+
return sum(
|
|
256
|
+
v if (v := plug.modules[Module.Energy].consumption_this_month) else 0.0
|
|
257
|
+
for plug in self._device.children
|
|
258
|
+
)
|
|
213
259
|
|
|
214
260
|
@property # type: ignore
|
|
215
|
-
|
|
216
|
-
def emeter_today(self) -> float | None:
|
|
261
|
+
def consumption_today(self) -> float | None:
|
|
217
262
|
"""Return this month's energy consumption in kWh."""
|
|
218
|
-
return sum(
|
|
263
|
+
return sum(
|
|
264
|
+
v if (v := plug.modules[Module.Energy].consumption_today) else 0.0
|
|
265
|
+
for plug in self._device.children
|
|
266
|
+
)
|
|
219
267
|
|
|
220
268
|
@property # type: ignore
|
|
221
|
-
|
|
222
|
-
|
|
269
|
+
def consumption_total(self) -> float | None:
|
|
270
|
+
"""Return total energy consumption since reboot in kWh."""
|
|
271
|
+
return sum(
|
|
272
|
+
v if (v := plug.modules[Module.Energy].consumption_total) else 0.0
|
|
273
|
+
for plug in self._device.children
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
@property # type: ignore
|
|
277
|
+
def status(self) -> EmeterStatus:
|
|
223
278
|
"""Return current energy readings."""
|
|
224
|
-
emeter = merge_sums(
|
|
279
|
+
emeter = merge_sums(
|
|
280
|
+
[plug.modules[Module.Energy].status for plug in self._device.children]
|
|
281
|
+
)
|
|
225
282
|
# Voltage is averaged since each read will result
|
|
226
283
|
# in a slightly different voltage since they are not atomic
|
|
227
|
-
emeter["voltage_mv"] = int(emeter["voltage_mv"] / len(self.children))
|
|
284
|
+
emeter["voltage_mv"] = int(emeter["voltage_mv"] / len(self._device.children))
|
|
228
285
|
return EmeterStatus(emeter)
|
|
229
286
|
|
|
287
|
+
@property
|
|
288
|
+
def current(self) -> float | None:
|
|
289
|
+
"""Return the current in A."""
|
|
290
|
+
return self.status.current
|
|
291
|
+
|
|
292
|
+
@property
|
|
293
|
+
def voltage(self) -> float | None:
|
|
294
|
+
"""Get the current voltage in V."""
|
|
295
|
+
return self.status.voltage
|
|
296
|
+
|
|
230
297
|
|
|
231
298
|
class IotStripPlug(IotPlug):
|
|
232
299
|
"""Representation of a single socket in a power strip.
|
|
@@ -275,9 +342,10 @@ class IotStripPlug(IotPlug):
|
|
|
275
342
|
icon="mdi:clock",
|
|
276
343
|
)
|
|
277
344
|
)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
345
|
+
for module in self._supported_modules.values():
|
|
346
|
+
module._initialize_features()
|
|
347
|
+
for module_feat in module._module_features.values():
|
|
348
|
+
self._add_feature(module_feat)
|
|
281
349
|
|
|
282
350
|
async def update(self, update_children: bool = True):
|
|
283
351
|
"""Query the device to update the data.
|
|
@@ -285,26 +353,8 @@ class IotStripPlug(IotPlug):
|
|
|
285
353
|
Needed for properties that are decorated with `requires_update`.
|
|
286
354
|
"""
|
|
287
355
|
await self._modular_update({})
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
"""Create a request for requesting all emeter statistics at once."""
|
|
291
|
-
if year is None:
|
|
292
|
-
year = datetime.now().year
|
|
293
|
-
if month is None:
|
|
294
|
-
month = datetime.now().month
|
|
295
|
-
|
|
296
|
-
req: dict[str, Any] = {}
|
|
297
|
-
|
|
298
|
-
merge(req, self._create_request("emeter", "get_realtime"))
|
|
299
|
-
merge(req, self._create_request("emeter", "get_monthstat", {"year": year}))
|
|
300
|
-
merge(
|
|
301
|
-
req,
|
|
302
|
-
self._create_request(
|
|
303
|
-
"emeter", "get_daystat", {"month": month, "year": year}
|
|
304
|
-
),
|
|
305
|
-
)
|
|
306
|
-
|
|
307
|
-
return req
|
|
356
|
+
if not self._features:
|
|
357
|
+
await self._initialize_features()
|
|
308
358
|
|
|
309
359
|
def _create_request(
|
|
310
360
|
self, target: str, cmd: str, arg: dict | None = None, child_ids=None
|
|
@@ -372,7 +422,9 @@ class IotStripPlug(IotPlug):
|
|
|
372
422
|
info = self._get_child_info()
|
|
373
423
|
on_time = info["on_time"]
|
|
374
424
|
|
|
375
|
-
return datetime.now().
|
|
425
|
+
return datetime.now(timezone.utc).astimezone().replace(
|
|
426
|
+
microsecond=0
|
|
427
|
+
) - timedelta(seconds=on_time)
|
|
376
428
|
|
|
377
429
|
@property # type: ignore
|
|
378
430
|
@requires_update
|
kasa/iot/modules/emeter.py
CHANGED
|
@@ -4,130 +4,71 @@ from __future__ import annotations
|
|
|
4
4
|
|
|
5
5
|
from datetime import datetime
|
|
6
6
|
|
|
7
|
-
from ... import Device
|
|
8
7
|
from ...emeterstatus import EmeterStatus
|
|
9
|
-
from ...
|
|
8
|
+
from ...interfaces.energy import Energy as EnergyInterface
|
|
10
9
|
from .usage import Usage
|
|
11
10
|
|
|
12
11
|
|
|
13
|
-
class Emeter(Usage):
|
|
12
|
+
class Emeter(Usage, EnergyInterface):
|
|
14
13
|
"""Emeter module."""
|
|
15
14
|
|
|
16
|
-
def
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
unit="W",
|
|
25
|
-
id="current_power_w", # for homeassistant backwards compat
|
|
26
|
-
precision_hint=1,
|
|
27
|
-
category=Feature.Category.Primary,
|
|
15
|
+
def _post_update_hook(self) -> None:
|
|
16
|
+
self._supported = EnergyInterface.ModuleFeature.PERIODIC_STATS
|
|
17
|
+
if (
|
|
18
|
+
"voltage_mv" in self.data["get_realtime"]
|
|
19
|
+
or "voltage" in self.data["get_realtime"]
|
|
20
|
+
):
|
|
21
|
+
self._supported = (
|
|
22
|
+
self._supported | EnergyInterface.ModuleFeature.VOLTAGE_CURRENT
|
|
28
23
|
)
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
container=self,
|
|
36
|
-
unit="kWh",
|
|
37
|
-
id="today_energy_kwh", # for homeassistant backwards compat
|
|
38
|
-
precision_hint=3,
|
|
39
|
-
category=Feature.Category.Info,
|
|
24
|
+
if (
|
|
25
|
+
"total_wh" in self.data["get_realtime"]
|
|
26
|
+
or "total" in self.data["get_realtime"]
|
|
27
|
+
):
|
|
28
|
+
self._supported = (
|
|
29
|
+
self._supported | EnergyInterface.ModuleFeature.CONSUMPTION_TOTAL
|
|
40
30
|
)
|
|
41
|
-
)
|
|
42
|
-
self._add_feature(
|
|
43
|
-
Feature(
|
|
44
|
-
device,
|
|
45
|
-
id="consumption_this_month",
|
|
46
|
-
name="This month's consumption",
|
|
47
|
-
attribute_getter="emeter_this_month",
|
|
48
|
-
container=self,
|
|
49
|
-
unit="kWh",
|
|
50
|
-
precision_hint=3,
|
|
51
|
-
category=Feature.Category.Info,
|
|
52
|
-
)
|
|
53
|
-
)
|
|
54
|
-
self._add_feature(
|
|
55
|
-
Feature(
|
|
56
|
-
device,
|
|
57
|
-
name="Total consumption since reboot",
|
|
58
|
-
attribute_getter="emeter_total",
|
|
59
|
-
container=self,
|
|
60
|
-
unit="kWh",
|
|
61
|
-
id="total_energy_kwh", # for homeassistant backwards compat
|
|
62
|
-
precision_hint=3,
|
|
63
|
-
category=Feature.Category.Info,
|
|
64
|
-
)
|
|
65
|
-
)
|
|
66
|
-
self._add_feature(
|
|
67
|
-
Feature(
|
|
68
|
-
device,
|
|
69
|
-
name="Voltage",
|
|
70
|
-
attribute_getter="voltage",
|
|
71
|
-
container=self,
|
|
72
|
-
unit="V",
|
|
73
|
-
id="voltage", # for homeassistant backwards compat
|
|
74
|
-
precision_hint=1,
|
|
75
|
-
category=Feature.Category.Primary,
|
|
76
|
-
)
|
|
77
|
-
)
|
|
78
|
-
self._add_feature(
|
|
79
|
-
Feature(
|
|
80
|
-
device,
|
|
81
|
-
name="Current",
|
|
82
|
-
attribute_getter="current",
|
|
83
|
-
container=self,
|
|
84
|
-
unit="A",
|
|
85
|
-
id="current_a", # for homeassistant backwards compat
|
|
86
|
-
precision_hint=2,
|
|
87
|
-
category=Feature.Category.Primary,
|
|
88
|
-
)
|
|
89
|
-
)
|
|
90
31
|
|
|
91
32
|
@property # type: ignore
|
|
92
|
-
def
|
|
33
|
+
def status(self) -> EmeterStatus:
|
|
93
34
|
"""Return current energy readings."""
|
|
94
35
|
return EmeterStatus(self.data["get_realtime"])
|
|
95
36
|
|
|
96
37
|
@property
|
|
97
|
-
def
|
|
38
|
+
def consumption_today(self) -> float | None:
|
|
98
39
|
"""Return today's energy consumption in kWh."""
|
|
99
40
|
raw_data = self.daily_data
|
|
100
41
|
today = datetime.now().day
|
|
101
42
|
data = self._convert_stat_data(raw_data, entry_key="day", key=today)
|
|
102
|
-
return data.get(today)
|
|
43
|
+
return data.get(today, 0.0)
|
|
103
44
|
|
|
104
45
|
@property
|
|
105
|
-
def
|
|
46
|
+
def consumption_this_month(self) -> float | None:
|
|
106
47
|
"""Return this month's energy consumption in kWh."""
|
|
107
48
|
raw_data = self.monthly_data
|
|
108
49
|
current_month = datetime.now().month
|
|
109
50
|
data = self._convert_stat_data(raw_data, entry_key="month", key=current_month)
|
|
110
|
-
return data.get(current_month)
|
|
51
|
+
return data.get(current_month, 0.0)
|
|
111
52
|
|
|
112
53
|
@property
|
|
113
54
|
def current_consumption(self) -> float | None:
|
|
114
55
|
"""Get the current power consumption in Watt."""
|
|
115
|
-
return self.
|
|
56
|
+
return self.status.power
|
|
116
57
|
|
|
117
58
|
@property
|
|
118
|
-
def
|
|
59
|
+
def consumption_total(self) -> float | None:
|
|
119
60
|
"""Return total consumption since last reboot in kWh."""
|
|
120
|
-
return self.
|
|
61
|
+
return self.status.total
|
|
121
62
|
|
|
122
63
|
@property
|
|
123
64
|
def current(self) -> float | None:
|
|
124
65
|
"""Return the current in A."""
|
|
125
|
-
return self.
|
|
66
|
+
return self.status.current
|
|
126
67
|
|
|
127
68
|
@property
|
|
128
69
|
def voltage(self) -> float | None:
|
|
129
70
|
"""Get the current voltage in V."""
|
|
130
|
-
return self.
|
|
71
|
+
return self.status.voltage
|
|
131
72
|
|
|
132
73
|
async def erase_stats(self):
|
|
133
74
|
"""Erase all stats.
|
|
@@ -136,11 +77,11 @@ class Emeter(Usage):
|
|
|
136
77
|
"""
|
|
137
78
|
return await self.call("erase_emeter_stat")
|
|
138
79
|
|
|
139
|
-
async def
|
|
80
|
+
async def get_status(self) -> EmeterStatus:
|
|
140
81
|
"""Return real-time statistics."""
|
|
141
|
-
return await self.call("get_realtime")
|
|
82
|
+
return EmeterStatus(await self.call("get_realtime"))
|
|
142
83
|
|
|
143
|
-
async def
|
|
84
|
+
async def get_daily_stats(self, *, year=None, month=None, kwh=True) -> dict:
|
|
144
85
|
"""Return daily stats for the given year & month.
|
|
145
86
|
|
|
146
87
|
The return value is a dictionary of {day: energy, ...}.
|
|
@@ -149,7 +90,7 @@ class Emeter(Usage):
|
|
|
149
90
|
data = self._convert_stat_data(data["day_list"], entry_key="day", kwh=kwh)
|
|
150
91
|
return data
|
|
151
92
|
|
|
152
|
-
async def
|
|
93
|
+
async def get_monthly_stats(self, *, year=None, kwh=True) -> dict:
|
|
153
94
|
"""Return monthly stats for the given year.
|
|
154
95
|
|
|
155
96
|
The return value is a dictionary of {month: energy, ...}.
|