python-kasa 0.7.0.dev4__py3-none-any.whl → 0.7.0.1__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/aestransport.py +6 -2
- kasa/cli.py +117 -66
- kasa/device.py +126 -33
- kasa/discover.py +2 -1
- kasa/feature.py +90 -14
- kasa/httpclient.py +1 -1
- kasa/interfaces/__init__.py +2 -0
- kasa/interfaces/energy.py +187 -0
- kasa/interfaces/light.py +61 -1
- kasa/interfaces/lighteffect.py +41 -1
- kasa/interfaces/lightpreset.py +70 -2
- kasa/iot/iotbulb.py +1 -1
- kasa/iot/iotdevice.py +32 -102
- kasa/iot/iotstrip.py +125 -62
- kasa/iot/modules/emeter.py +30 -89
- kasa/iot/modules/led.py +5 -0
- kasa/iot/modules/lightpreset.py +2 -1
- kasa/module.py +41 -4
- kasa/smart/modules/alarm.py +1 -0
- kasa/smart/modules/autooff.py +3 -1
- kasa/smart/modules/childdevice.py +39 -1
- kasa/smart/modules/energy.py +58 -60
- kasa/smart/modules/firmware.py +4 -1
- kasa/smart/modules/frostprotection.py +0 -22
- kasa/smart/modules/humiditysensor.py +1 -0
- kasa/smart/modules/led.py +1 -1
- kasa/smart/modules/lightpreset.py +2 -1
- kasa/smart/modules/lightstripeffect.py +14 -4
- kasa/smart/modules/lighttransition.py +8 -0
- kasa/smart/modules/reportmode.py +2 -0
- kasa/smart/modules/temperaturecontrol.py +3 -2
- kasa/smart/modules/temperaturesensor.py +3 -2
- kasa/smart/modules/time.py +10 -3
- kasa/smart/modules/waterleaksensor.py +2 -0
- kasa/smart/smartdevice.py +29 -51
- kasa/tests/device_fixtures.py +1 -1
- kasa/tests/discovery_fixtures.py +3 -1
- kasa/tests/fakeprotocol_smart.py +2 -2
- kasa/tests/fixtures/smart/L920-5(EU)_1.0_1.0.7.json +350 -0
- kasa/tests/smart/modules/test_firmware.py +12 -1
- kasa/tests/test_cli.py +55 -8
- kasa/tests/test_device.py +16 -7
- kasa/tests/test_emeter.py +44 -16
- kasa/tests/test_feature.py +6 -2
- kasa/tests/test_httpclient.py +2 -2
- kasa/tests/test_iotdevice.py +14 -6
- kasa/tests/test_readme_examples.py +59 -0
- kasa/tests/test_smartdevice.py +4 -3
- kasa/tests/test_strip.py +15 -2
- kasa/xortransport.py +3 -3
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/METADATA +102 -129
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/RECORD +55 -53
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/LICENSE +0 -0
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/WHEEL +0 -0
- {python_kasa-0.7.0.dev4.dist-info → python_kasa-0.7.0.1.dist-info}/entry_points.txt +0 -0
kasa/iot/iotdevice.py
CHANGED
|
@@ -18,12 +18,12 @@ import collections.abc
|
|
|
18
18
|
import functools
|
|
19
19
|
import inspect
|
|
20
20
|
import logging
|
|
21
|
-
from
|
|
22
|
-
from
|
|
21
|
+
from collections.abc import Mapping, Sequence
|
|
22
|
+
from datetime import datetime, timedelta, timezone
|
|
23
|
+
from typing import TYPE_CHECKING, Any, cast
|
|
23
24
|
|
|
24
25
|
from ..device import Device, WifiNetwork
|
|
25
26
|
from ..deviceconfig import DeviceConfig
|
|
26
|
-
from ..emeterstatus import EmeterStatus
|
|
27
27
|
from ..exceptions import KasaException
|
|
28
28
|
from ..feature import Feature
|
|
29
29
|
from ..module import Module
|
|
@@ -188,7 +188,7 @@ class IotDevice(Device):
|
|
|
188
188
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
189
189
|
|
|
190
190
|
self._sys_info: Any = None # TODO: this is here to avoid changing tests
|
|
191
|
-
self._supported_modules: dict[str, IotModule] | None = None
|
|
191
|
+
self._supported_modules: dict[str | ModuleName[Module], IotModule] | None = None
|
|
192
192
|
self._legacy_features: set[str] = set()
|
|
193
193
|
self._children: Mapping[str, IotDevice] = {}
|
|
194
194
|
self._modules: dict[str | ModuleName[Module], IotModule] = {}
|
|
@@ -199,15 +199,16 @@ class IotDevice(Device):
|
|
|
199
199
|
return list(self._children.values())
|
|
200
200
|
|
|
201
201
|
@property
|
|
202
|
+
@requires_update
|
|
202
203
|
def modules(self) -> ModuleMapping[IotModule]:
|
|
203
204
|
"""Return the device modules."""
|
|
204
205
|
if TYPE_CHECKING:
|
|
205
|
-
return cast(ModuleMapping[IotModule], self.
|
|
206
|
-
return self.
|
|
206
|
+
return cast(ModuleMapping[IotModule], self._supported_modules)
|
|
207
|
+
return self._supported_modules
|
|
207
208
|
|
|
208
209
|
def add_module(self, name: str | ModuleName[Module], module: IotModule):
|
|
209
210
|
"""Register a module."""
|
|
210
|
-
if name in self.
|
|
211
|
+
if name in self._modules:
|
|
211
212
|
_LOGGER.debug("Module %s already registered, ignoring..." % name)
|
|
212
213
|
return
|
|
213
214
|
|
|
@@ -272,14 +273,6 @@ class IotDevice(Device):
|
|
|
272
273
|
"""Return a set of features that the device supports."""
|
|
273
274
|
return self._features
|
|
274
275
|
|
|
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
276
|
@property # type: ignore
|
|
284
277
|
@requires_update
|
|
285
278
|
def has_emeter(self) -> bool:
|
|
@@ -321,6 +314,11 @@ class IotDevice(Device):
|
|
|
321
314
|
|
|
322
315
|
async def _initialize_modules(self):
|
|
323
316
|
"""Initialize modules not added in init."""
|
|
317
|
+
if self.has_emeter:
|
|
318
|
+
_LOGGER.debug(
|
|
319
|
+
"The device has emeter, querying its information along sysinfo"
|
|
320
|
+
)
|
|
321
|
+
self.add_module(Module.Energy, Emeter(self, self.emeter_type))
|
|
324
322
|
|
|
325
323
|
async def _initialize_features(self):
|
|
326
324
|
"""Initialize common features."""
|
|
@@ -342,10 +340,13 @@ class IotDevice(Device):
|
|
|
342
340
|
name="RSSI",
|
|
343
341
|
attribute_getter="rssi",
|
|
344
342
|
icon="mdi:signal",
|
|
343
|
+
unit="dBm",
|
|
345
344
|
category=Feature.Category.Debug,
|
|
345
|
+
type=Feature.Type.Sensor,
|
|
346
346
|
)
|
|
347
347
|
)
|
|
348
|
-
|
|
348
|
+
# iot strips calculate on_since from the children
|
|
349
|
+
if "on_time" in self._sys_info or self.device_type == Device.Type.Strip:
|
|
349
350
|
self._add_feature(
|
|
350
351
|
Feature(
|
|
351
352
|
device=self,
|
|
@@ -353,32 +354,18 @@ class IotDevice(Device):
|
|
|
353
354
|
name="On since",
|
|
354
355
|
attribute_getter="on_since",
|
|
355
356
|
icon="mdi:clock",
|
|
357
|
+
category=Feature.Category.Info,
|
|
358
|
+
type=Feature.Type.Sensor,
|
|
356
359
|
)
|
|
357
360
|
)
|
|
358
361
|
|
|
359
|
-
for module in self.
|
|
362
|
+
for module in self._supported_modules.values():
|
|
360
363
|
module._initialize_features()
|
|
361
364
|
for module_feat in module._module_features.values():
|
|
362
365
|
self._add_feature(module_feat)
|
|
363
366
|
|
|
364
367
|
async def _modular_update(self, req: dict) -> None:
|
|
365
368
|
"""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
369
|
request_list = []
|
|
383
370
|
est_response_size = 1024 if "system" in req else 0
|
|
384
371
|
for module in self._modules.values():
|
|
@@ -410,6 +397,15 @@ class IotDevice(Device):
|
|
|
410
397
|
update = {**update, **response}
|
|
411
398
|
self._last_update = update
|
|
412
399
|
|
|
400
|
+
# IOT modules are added as default but could be unsupported post first update
|
|
401
|
+
if self._supported_modules is None:
|
|
402
|
+
supported = {}
|
|
403
|
+
for module_name, module in self._modules.items():
|
|
404
|
+
if module.is_supported:
|
|
405
|
+
supported[module_name] = module
|
|
406
|
+
|
|
407
|
+
self._supported_modules = supported
|
|
408
|
+
|
|
413
409
|
def update_from_discover_info(self, info: dict[str, Any]) -> None:
|
|
414
410
|
"""Update state from info from the discover call."""
|
|
415
411
|
self._discovery_info = info
|
|
@@ -556,74 +552,6 @@ class IotDevice(Device):
|
|
|
556
552
|
"""
|
|
557
553
|
return await self._query_helper("system", "set_mac_addr", {"mac": mac})
|
|
558
554
|
|
|
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
555
|
async def reboot(self, delay: int = 1) -> None:
|
|
628
556
|
"""Reboot the device.
|
|
629
557
|
|
|
@@ -665,7 +593,9 @@ class IotDevice(Device):
|
|
|
665
593
|
|
|
666
594
|
on_time = self._sys_info["on_time"]
|
|
667
595
|
|
|
668
|
-
return datetime.now().
|
|
596
|
+
return datetime.now(timezone.utc).astimezone().replace(
|
|
597
|
+
microsecond=0
|
|
598
|
+
) - timedelta(seconds=on_time)
|
|
669
599
|
|
|
670
600
|
@property # type: ignore
|
|
671
601
|
@requires_update
|
kasa/iot/iotstrip.py
CHANGED
|
@@ -4,23 +4,24 @@ 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
|
-
from .modules import Antitheft, Countdown, Schedule, Time, Usage
|
|
24
|
+
from .modules import Antitheft, Cloud, Countdown, Emeter, Led, Schedule, Time, Usage
|
|
24
25
|
|
|
25
26
|
_LOGGER = logging.getLogger(__name__)
|
|
26
27
|
|
|
@@ -97,11 +98,22 @@ 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
|
+
self.add_module(Module.Led, Led(self, "system"))
|
|
111
|
+
self.add_module(Module.IotCloud, Cloud(self, "cnCloud"))
|
|
112
|
+
if self.has_emeter:
|
|
113
|
+
_LOGGER.debug(
|
|
114
|
+
"The device has emeter, querying its information along sysinfo"
|
|
115
|
+
)
|
|
116
|
+
self.add_module(Module.Energy, StripEmeter(self, self.emeter_type))
|
|
105
117
|
|
|
106
118
|
@property # type: ignore
|
|
107
119
|
@requires_update
|
|
@@ -114,10 +126,12 @@ class IotStrip(IotDevice):
|
|
|
114
126
|
|
|
115
127
|
Needed for methods that are decorated with `requires_update`.
|
|
116
128
|
"""
|
|
129
|
+
# Super initializes modules and features
|
|
117
130
|
await super().update(update_children)
|
|
118
131
|
|
|
132
|
+
initialize_children = not self.children
|
|
119
133
|
# Initialize the child devices during the first update.
|
|
120
|
-
if
|
|
134
|
+
if initialize_children:
|
|
121
135
|
children = self.sys_info["children"]
|
|
122
136
|
_LOGGER.debug("Initializing %s child sockets", len(children))
|
|
123
137
|
self._children = {
|
|
@@ -127,12 +141,22 @@ class IotStrip(IotDevice):
|
|
|
127
141
|
for child in children
|
|
128
142
|
}
|
|
129
143
|
for child in self._children.values():
|
|
130
|
-
await child.
|
|
144
|
+
await child._initialize_modules()
|
|
131
145
|
|
|
132
|
-
if update_children
|
|
146
|
+
if update_children:
|
|
133
147
|
for plug in self.children:
|
|
134
148
|
await plug.update()
|
|
135
149
|
|
|
150
|
+
if not self.features:
|
|
151
|
+
await self._initialize_features()
|
|
152
|
+
|
|
153
|
+
async def _initialize_features(self):
|
|
154
|
+
"""Initialize common features."""
|
|
155
|
+
# Do not initialize features until children are created
|
|
156
|
+
if not self.children:
|
|
157
|
+
return
|
|
158
|
+
await super()._initialize_features()
|
|
159
|
+
|
|
136
160
|
async def turn_on(self, **kwargs):
|
|
137
161
|
"""Turn the strip on."""
|
|
138
162
|
await self._query_helper("system", "set_relay_state", {"state": 1})
|
|
@@ -148,23 +172,45 @@ class IotStrip(IotDevice):
|
|
|
148
172
|
if self.is_off:
|
|
149
173
|
return None
|
|
150
174
|
|
|
151
|
-
return
|
|
175
|
+
return min(plug.on_since for plug in self.children if plug.on_since is not None)
|
|
176
|
+
|
|
152
177
|
|
|
153
|
-
|
|
178
|
+
class StripEmeter(IotModule, Energy):
|
|
179
|
+
"""Energy module implementation to aggregate child modules."""
|
|
180
|
+
|
|
181
|
+
_supported = (
|
|
182
|
+
Energy.ModuleFeature.CONSUMPTION_TOTAL
|
|
183
|
+
| Energy.ModuleFeature.PERIODIC_STATS
|
|
184
|
+
| Energy.ModuleFeature.VOLTAGE_CURRENT
|
|
185
|
+
)
|
|
186
|
+
|
|
187
|
+
def supports(self, module_feature: Energy.ModuleFeature) -> bool:
|
|
188
|
+
"""Return True if module supports the feature."""
|
|
189
|
+
return module_feature in self._supported
|
|
190
|
+
|
|
191
|
+
def query(self):
|
|
192
|
+
"""Return the base query."""
|
|
193
|
+
return {}
|
|
194
|
+
|
|
195
|
+
@property
|
|
196
|
+
def current_consumption(self) -> float | None:
|
|
154
197
|
"""Get the current power consumption in watts."""
|
|
155
|
-
return sum(
|
|
198
|
+
return sum(
|
|
199
|
+
v if (v := plug.modules[Module.Energy].current_consumption) else 0.0
|
|
200
|
+
for plug in self._device.children
|
|
201
|
+
)
|
|
156
202
|
|
|
157
|
-
|
|
158
|
-
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
203
|
+
async def get_status(self) -> EmeterStatus:
|
|
159
204
|
"""Retrieve current energy readings."""
|
|
160
|
-
emeter_rt = await self._async_get_emeter_sum("
|
|
205
|
+
emeter_rt = await self._async_get_emeter_sum("get_status", {})
|
|
161
206
|
# Voltage is averaged since each read will result
|
|
162
207
|
# in a slightly different voltage since they are not atomic
|
|
163
|
-
emeter_rt["voltage_mv"] = int(
|
|
208
|
+
emeter_rt["voltage_mv"] = int(
|
|
209
|
+
emeter_rt["voltage_mv"] / len(self._device.children)
|
|
210
|
+
)
|
|
164
211
|
return EmeterStatus(emeter_rt)
|
|
165
212
|
|
|
166
|
-
|
|
167
|
-
async def get_emeter_daily(
|
|
213
|
+
async def get_daily_stats(
|
|
168
214
|
self, year: int | None = None, month: int | None = None, kwh: bool = True
|
|
169
215
|
) -> dict:
|
|
170
216
|
"""Retrieve daily statistics for a given month.
|
|
@@ -176,11 +222,10 @@ class IotStrip(IotDevice):
|
|
|
176
222
|
:return: mapping of day of month to value
|
|
177
223
|
"""
|
|
178
224
|
return await self._async_get_emeter_sum(
|
|
179
|
-
"
|
|
225
|
+
"get_daily_stats", {"year": year, "month": month, "kwh": kwh}
|
|
180
226
|
)
|
|
181
227
|
|
|
182
|
-
|
|
183
|
-
async def get_emeter_monthly(
|
|
228
|
+
async def get_monthly_stats(
|
|
184
229
|
self, year: int | None = None, kwh: bool = True
|
|
185
230
|
) -> dict:
|
|
186
231
|
"""Retrieve monthly statistics for a given year.
|
|
@@ -189,44 +234,68 @@ class IotStrip(IotDevice):
|
|
|
189
234
|
:param kwh: return usage in kWh (default: True)
|
|
190
235
|
"""
|
|
191
236
|
return await self._async_get_emeter_sum(
|
|
192
|
-
"
|
|
237
|
+
"get_monthly_stats", {"year": year, "kwh": kwh}
|
|
193
238
|
)
|
|
194
239
|
|
|
195
240
|
async def _async_get_emeter_sum(self, func: str, kwargs: dict[str, Any]) -> dict:
|
|
196
|
-
"""
|
|
197
|
-
self._verify_emeter()
|
|
241
|
+
"""Retrieve emeter stats for a time period from children."""
|
|
198
242
|
return merge_sums(
|
|
199
|
-
[
|
|
243
|
+
[
|
|
244
|
+
await getattr(plug.modules[Module.Energy], func)(**kwargs)
|
|
245
|
+
for plug in self._device.children
|
|
246
|
+
]
|
|
200
247
|
)
|
|
201
248
|
|
|
202
|
-
|
|
203
|
-
async def erase_emeter_stats(self):
|
|
249
|
+
async def erase_stats(self):
|
|
204
250
|
"""Erase energy meter statistics for all plugs."""
|
|
205
|
-
for plug in self.children:
|
|
206
|
-
await plug.
|
|
251
|
+
for plug in self._device.children:
|
|
252
|
+
await plug.modules[Module.Energy].erase_stats()
|
|
207
253
|
|
|
208
254
|
@property # type: ignore
|
|
209
|
-
|
|
210
|
-
def emeter_this_month(self) -> float | None:
|
|
255
|
+
def consumption_this_month(self) -> float | None:
|
|
211
256
|
"""Return this month's energy consumption in kWh."""
|
|
212
|
-
return sum(
|
|
257
|
+
return sum(
|
|
258
|
+
v if (v := plug.modules[Module.Energy].consumption_this_month) else 0.0
|
|
259
|
+
for plug in self._device.children
|
|
260
|
+
)
|
|
213
261
|
|
|
214
262
|
@property # type: ignore
|
|
215
|
-
|
|
216
|
-
def emeter_today(self) -> float | None:
|
|
263
|
+
def consumption_today(self) -> float | None:
|
|
217
264
|
"""Return this month's energy consumption in kWh."""
|
|
218
|
-
return sum(
|
|
265
|
+
return sum(
|
|
266
|
+
v if (v := plug.modules[Module.Energy].consumption_today) else 0.0
|
|
267
|
+
for plug in self._device.children
|
|
268
|
+
)
|
|
219
269
|
|
|
220
270
|
@property # type: ignore
|
|
221
|
-
|
|
222
|
-
|
|
271
|
+
def consumption_total(self) -> float | None:
|
|
272
|
+
"""Return total energy consumption since reboot in kWh."""
|
|
273
|
+
return sum(
|
|
274
|
+
v if (v := plug.modules[Module.Energy].consumption_total) else 0.0
|
|
275
|
+
for plug in self._device.children
|
|
276
|
+
)
|
|
277
|
+
|
|
278
|
+
@property # type: ignore
|
|
279
|
+
def status(self) -> EmeterStatus:
|
|
223
280
|
"""Return current energy readings."""
|
|
224
|
-
emeter = merge_sums(
|
|
281
|
+
emeter = merge_sums(
|
|
282
|
+
[plug.modules[Module.Energy].status for plug in self._device.children]
|
|
283
|
+
)
|
|
225
284
|
# Voltage is averaged since each read will result
|
|
226
285
|
# in a slightly different voltage since they are not atomic
|
|
227
|
-
emeter["voltage_mv"] = int(emeter["voltage_mv"] / len(self.children))
|
|
286
|
+
emeter["voltage_mv"] = int(emeter["voltage_mv"] / len(self._device.children))
|
|
228
287
|
return EmeterStatus(emeter)
|
|
229
288
|
|
|
289
|
+
@property
|
|
290
|
+
def current(self) -> float | None:
|
|
291
|
+
"""Return the current in A."""
|
|
292
|
+
return self.status.current
|
|
293
|
+
|
|
294
|
+
@property
|
|
295
|
+
def voltage(self) -> float | None:
|
|
296
|
+
"""Get the current voltage in V."""
|
|
297
|
+
return self.status.voltage
|
|
298
|
+
|
|
230
299
|
|
|
231
300
|
class IotStripPlug(IotPlug):
|
|
232
301
|
"""Representation of a single socket in a power strip.
|
|
@@ -250,8 +319,12 @@ class IotStripPlug(IotPlug):
|
|
|
250
319
|
|
|
251
320
|
async def _initialize_modules(self):
|
|
252
321
|
"""Initialize modules not added in init."""
|
|
253
|
-
|
|
254
|
-
|
|
322
|
+
if self.has_emeter:
|
|
323
|
+
self.add_module(Module.Energy, Emeter(self, self.emeter_type))
|
|
324
|
+
self.add_module(Module.IotUsage, Usage(self, "schedule"))
|
|
325
|
+
self.add_module(Module.IotAntitheft, Antitheft(self, "anti_theft"))
|
|
326
|
+
self.add_module(Module.IotSchedule, Schedule(self, "schedule"))
|
|
327
|
+
self.add_module(Module.IotCountdown, Countdown(self, "countdown"))
|
|
255
328
|
|
|
256
329
|
async def _initialize_features(self):
|
|
257
330
|
"""Initialize common features."""
|
|
@@ -273,11 +346,14 @@ class IotStripPlug(IotPlug):
|
|
|
273
346
|
name="On since",
|
|
274
347
|
attribute_getter="on_since",
|
|
275
348
|
icon="mdi:clock",
|
|
349
|
+
category=Feature.Category.Info,
|
|
350
|
+
type=Feature.Type.Sensor,
|
|
276
351
|
)
|
|
277
352
|
)
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
353
|
+
for module in self._supported_modules.values():
|
|
354
|
+
module._initialize_features()
|
|
355
|
+
for module_feat in module._module_features.values():
|
|
356
|
+
self._add_feature(module_feat)
|
|
281
357
|
|
|
282
358
|
async def update(self, update_children: bool = True):
|
|
283
359
|
"""Query the device to update the data.
|
|
@@ -285,26 +361,11 @@ class IotStripPlug(IotPlug):
|
|
|
285
361
|
Needed for properties that are decorated with `requires_update`.
|
|
286
362
|
"""
|
|
287
363
|
await self._modular_update({})
|
|
364
|
+
for module in self._modules.values():
|
|
365
|
+
module._post_update_hook()
|
|
288
366
|
|
|
289
|
-
|
|
290
|
-
|
|
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
|
|
367
|
+
if not self._features:
|
|
368
|
+
await self._initialize_features()
|
|
308
369
|
|
|
309
370
|
def _create_request(
|
|
310
371
|
self, target: str, cmd: str, arg: dict | None = None, child_ids=None
|
|
@@ -372,7 +433,9 @@ class IotStripPlug(IotPlug):
|
|
|
372
433
|
info = self._get_child_info()
|
|
373
434
|
on_time = info["on_time"]
|
|
374
435
|
|
|
375
|
-
return datetime.now().
|
|
436
|
+
return datetime.now(timezone.utc).astimezone().replace(
|
|
437
|
+
microsecond=0
|
|
438
|
+
) - timedelta(seconds=on_time)
|
|
376
439
|
|
|
377
440
|
@property # type: ignore
|
|
378
441
|
@requires_update
|