10000 Merge remote-tracking branch 'upstream/master' into feat/smartdevice_led · python-kasa/python-kasa@28cbc10 · GitHub
[go: up one dir, main page]

Skip to content

Commit 28cbc10

Browse files
committed
Merge remote-tracking branch 'upstream/master' into feat/smartdevice_led
2 parents 3418338 + 1171999 commit 28cbc10

14 files changed

+134
-65
lines changed

devtools/dump_devinfo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def scrub(res):
5252
"longitude_i",
5353
"latitude",
5454
"longitude",
55+
"la", # lat on ks240
56+
"lo", # lon on ks240
5557
"owner",
5658
"device_id",
5759
"ip",

kasa/cli.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,10 @@ async def state(ctx, dev: Device):
590590

591591
echo("\n\t[bold]== Modules ==[/bold]")
592592
for module in dev.modules.values():
593-
if module.is_supported:
594-
echo(f"\t[green]+ {module}[/green]")
593+
echo(f"\t[green]+ {module}[/green]")
595594
# TODO: fix this hack, probably with descriptors?
596595
if pretty_print := getattr(module, "__cli_output__", None):
597596
echo(f"\t\t{pretty_print()}")
598-
else:
599-
echo(f"\t[red]- {module}[/red]")
600597

601598
if verbose:
602599
echo("\n\t[bold]== Verbose information ==[/bold]")

kasa/iot/iotmodule.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def merge(d, u):
2121

2222
class IotModule(Module):
2323
"""Base class implemention for all IOT modules."""
24+
def call(self, method, params=None):
25+
"""Call the given method with the given parameters."""
26+
return self._device._query_helper(self._module, method, params)
27+
def query_for_command(self, query, params=None):
28+
"""Create a request object for the given parameters."""
29+
return self._device._create_request(self._module, query, params)
2430

2531
@property
2632
def estimated_query_response_size(self):

kasa/module.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def query(self):
4242
def data(self):
4343
"""Return the module specific raw data from the last update."""
4444

45-
@property
46-
@abstractmethod
47-
def is_supported(self) -> bool:
48-
"""Return whether the module is supported by the device."""
4945

5046
def __repr__(self) -> str:
5147
return (

kasa/smart/modules/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from .timemodule import TimeModule
2-
from .energymodule import EnergyModule
1+
"""Modules for SMART devices."""
2+
from .childdevicemodule import ChildDeviceModule
33
from .devicemodule import DeviceModule
4-
from .ledmodule import LedModule
4+
from .energymodule import EnergyModule
5+
from .timemodule import TimeModule
56

6-
__all__ = ["TimeModule", "EnergyModule", "DeviceModule", "LedModule"]
7+
__all__ = ["TimeModule", "EnergyModule", "DeviceModule", "ChildDeviceModule", "LedModule"]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"""Implementation for child devices."""
2+
from ..smartmodule import SmartModule
3+
4+
5+
class ChildDeviceModule(SmartModule):
6+
"""Implementation for child devices."""
7+
8+
REQUIRED_COMPONENT = "child_device"
9+
QUERY_GETTER_NAME = "get_child_device_list"

kasa/smart/modules/devicemodule.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
"""Implementation of device module."""
12
from typing import Dict
3+
24
from ..smartmodule import SmartModule
35

46

57
class DeviceModule(SmartModule):
8+
"""Implementation of device module."""
9+
610
REQUIRED_COMPONENT = "device"
711

812
def query(self) -> Dict:
13+
"""Query to execute during the update cycle."""
914
query = {
1015
"get_device_info": None,
1116
}
1217
# Device usage is not available on older firmware versions
1318
if self._device._components[self.REQUIRED_COMPONENT] >= 2:
1419
query["get_device_usage"] = None
1520

16-
return query
21+
return query

kasa/smart/modules/energymodule.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1-
from typing import Dict, Optional, TYPE_CHECKING
1+
"""Implementation of energy monitoring module."""
2+
from typing import TYPE_CHECKING, Dict, Optional
23

3-
from ..smartmodule import SmartModule
4-
from ...feature import Feature
54
from ...emeterstatus import EmeterStatus
6-
5+
from ...feature import Feature
6+
from ..smartmodule import SmartModule
77

88
if TYPE_CHECKING:
99
from ..smartdevice import SmartDevice
1010

1111

1212
class EnergyModule(SmartModule):
13+
"""Implementation of energy monitoring module."""
14+
1315
REQUIRED_COMPONENT = "energy_monitoring"
1416

1517
def __init__(self, device: "SmartDevice", module: str):
1618
super().__init__(device, module)
17-
self._add_feature(Feature(device, name="Current consumption", attribute_getter="current_power", container=self)) # W or mW?
18-
self._add_feature(Feature(device, name="Today's consumption", attribute_getter="emeter_today", container=self)) # Wh or kWh?
19-
self._add_feature(Feature(device, name="This month's consumption", attribute_getter="emeter_this_month", container=self)) # Wh or kWH?
19+
self._add_feature(
20+
Feature(
21+
device,
22+
name="Current consumption",
23+
attribute_getter="current_power",
24+
container=self,
25+
)
26+
) # W or mW?
27+
self._add_feature(
28+
Feature(
29+
device,
30+
name="Today's consumption",
31+
attribute_getter="emeter_today",
32+
container=self,
33+
)
34+
) # Wh or kWh?
35+
self._add_feature(
36+
Feature(
37+
device,
38+
name="This month's consumption",
39+
attribute_getter="emeter_this_month",
40+
container=self,
41+
)
42+
) # Wh or kWH?
2043

2144
def query(self) -> Dict:
45+
"""Query to execute during the update cycle."""
2246
return {
2347
"get_energy_usage": None,
2448
# The current_power in get_energy_usage is more precise (mw vs. w),
@@ -59,14 +83,6 @@ def emeter_today(self) -> Optional[float]:
5983
"""Get the emeter value for today."""
6084
return self._convert_energy_data(self.energy.get("today_energy"), 1 / 1000)
6185

62-
async def get_emeter_realtime(self) -> EmeterStatus:
63-
"""Retrieve current energy readings."""
64-
# TODO: maybe we should just have a generic `update()` or similar,
65-
# to execute the query() and return the raw results?
66-
resp = await self.call("get_energy_usage")
67-
self._energy = resp["get_energy_usage"]
68-
return self.emeter_realtime
69-
7086
def _convert_energy_data(self, data, scale) -> Optional[float]:
7187
"""Return adjusted emeter information."""
7288
return data if not data else data * scale

kasa/smart/modules/timemodule.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
"""Implementation of device time module."""
1+
"""Implementation of time module."""
22
from datetime import datetime, timedelta, timezone
33
from time import mktime
44
from typing import TYPE_CHECKING, cast
55

6+
from ...feature import Feature
67
from ..smartmodule import SmartModule
78

89
if TYPE_CHECKING:
@@ -12,9 +13,21 @@
1213
class TimeModule(SmartModule):
1314
"""Implementation of device_local_time."""
1415

15-
REQUIRED_COMPONENT = "device_local_time"
16+
REQUIRED_COMPONENT = "time"
1617
QUERY_GETTER_NAME = "get_device_time"
1718

19+
def __init__(self, device: "SmartDevice", module: str):
20+
super().__init__(device, module)
21+
22+
self._add_feature(
23+
Feature(
24+
device=device,
25+
name="Time",
26+
attribute_getter="time",
27+
container=self,
28+
)
29+
)
30+
1831
@property
1932
def time(self) -> datetime:
2033
"""Return device's current datetime."""
@@ -37,6 +50,3 @@ async def set_time(self, dt: datetime):
3750
"set_device_time",
3851
{"timestamp": unixtime, "time_diff": dt.utcoffset(), "region": dt.tzname()},
3952
)
40-
41-
def __cli_output__(self):
42-
return f"Time: {self.time}"

kasa/smart/smartchilddevice.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ def __init__(
2424
self._parent = parent
2525
self._id = child_id
2626
self.protocol = _ChildProtocolWrapper(child_id, parent.protocol)
27-
# TODO: remove the assignment after modularization is done,
28-
# currently required to allow accessing time-related properties
29-
self._time = parent._time
3027
self._device_type = DeviceType.StripSocket
3128

3229
async def update(self, update_children: bool = True):

0 commit comments

Comments
 (0)
0