python-kasa 0.6.2.1__py3-none-any.whl → 0.7.0.dev0__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/__init__.py +88 -27
- kasa/aestransport.py +51 -34
- kasa/cli.py +326 -130
- kasa/device.py +406 -0
- kasa/device_factory.py +83 -44
- kasa/device_type.py +7 -3
- kasa/deviceconfig.py +16 -9
- kasa/discover.py +100 -81
- kasa/emeterstatus.py +7 -5
- kasa/exceptions.py +41 -26
- kasa/feature.py +201 -0
- kasa/httpclient.py +21 -14
- kasa/interfaces/__init__.py +16 -0
- kasa/interfaces/fan.py +20 -0
- kasa/interfaces/led.py +38 -0
- kasa/interfaces/light.py +133 -0
- kasa/interfaces/lighteffect.py +80 -0
- kasa/interfaces/lightpreset.py +76 -0
- kasa/iot/__init__.py +18 -0
- kasa/{effects.py → iot/effects.py} +4 -2
- kasa/{smartbulb.py → iot/iotbulb.py} +100 -147
- kasa/{smartdevice.py → iot/iotdevice.py} +157 -286
- kasa/{smartdimmer.py → iot/iotdimmer.py} +42 -33
- kasa/iot/iotlightstrip.py +71 -0
- kasa/{modules/module.py → iot/iotmodule.py} +22 -42
- kasa/iot/iotplug.py +94 -0
- kasa/{smartstrip.py → iot/iotstrip.py} +57 -75
- kasa/{modules → iot/modules}/__init__.py +10 -2
- kasa/{modules → iot/modules}/ambientlight.py +31 -3
- kasa/{modules → iot/modules}/antitheft.py +1 -0
- kasa/{modules → iot/modules}/cloud.py +25 -6
- kasa/{modules → iot/modules}/countdown.py +1 -0
- kasa/iot/modules/emeter.py +210 -0
- kasa/iot/modules/led.py +32 -0
- kasa/iot/modules/light.py +221 -0
- kasa/iot/modules/lighteffect.py +122 -0
- kasa/iot/modules/lightpreset.py +151 -0
- kasa/{modules → iot/modules}/motion.py +8 -8
- kasa/{modules → iot/modules}/rulemodule.py +11 -12
- kasa/{modules → iot/modules}/schedule.py +1 -0
- kasa/{modules → iot/modules}/time.py +5 -4
- kasa/{modules → iot/modules}/usage.py +10 -8
- kasa/iotprotocol.py +22 -20
- kasa/klaptransport.py +25 -15
- kasa/module.py +136 -0
- kasa/modulemapping.py +25 -0
- kasa/modulemapping.pyi +96 -0
- kasa/protocol.py +6 -4
- kasa/smart/__init__.py +6 -0
- kasa/smart/effects.py +429 -0
- kasa/smart/modules/__init__.py +57 -0
- kasa/smart/modules/alarm.py +142 -0
- kasa/smart/modules/autooff.py +96 -0
- kasa/smart/modules/batterysensor.py +54 -0
- kasa/smart/modules/brightness.py +66 -0
- kasa/smart/modules/childdevice.py +10 -0
- kasa/smart/modules/cloud.py +42 -0
- kasa/smart/modules/color.py +96 -0
- kasa/smart/modules/colortemperature.py +78 -0
- kasa/smart/modules/contactsensor.py +42 -0
- kasa/smart/modules/devicemodule.py +22 -0
- kasa/smart/modules/energy.py +101 -0
- kasa/smart/modules/fan.py +80 -0
- kasa/smart/modules/firmware.py +216 -0
- kasa/smart/modules/frostprotection.py +59 -0
- kasa/smart/modules/humiditysensor.py +55 -0
- kasa/smart/modules/led.py +49 -0
- kasa/smart/modules/light.py +139 -0
- kasa/smart/modules/lighteffect.py +115 -0
- kasa/smart/modules/lightpreset.py +142 -0
- kasa/smart/modules/lightstripeffect.py +135 -0
- kasa/smart/modules/lighttransition.py +183 -0
- kasa/smart/modules/reportmode.py +36 -0
- kasa/smart/modules/temperaturecontrol.py +177 -0
- kasa/smart/modules/temperaturesensor.py +77 -0
- kasa/smart/modules/time.py +57 -0
- kasa/smart/modules/waterleaksensor.py +66 -0
- kasa/smart/smartchilddevice.py +65 -0
- kasa/smart/smartdevice.py +653 -0
- kasa/smart/smartmodule.py +112 -0
- kasa/smartprotocol.py +107 -41
- kasa/tests/conftest.py +27 -562
- kasa/tests/device_fixtures.py +427 -0
- kasa/tests/discovery_fixtures.py +197 -0
- kasa/tests/fakeprotocol_iot.py +64 -6
- kasa/tests/fakeprotocol_smart.py +215 -22
- kasa/tests/fixtureinfo.py +154 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.2.3.json +221 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.10.json +547 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.5.json +391 -0
- kasa/tests/fixtures/smart/KH100(UK)_1.0_1.5.6.json +1557 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.4.json +899 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.5.json +479 -0
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +449 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.1.3.json +173 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.4.0.json +204 -0
- kasa/tests/fixtures/smart/P110(EU)_1.0_1.0.7.json +566 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.4.0.json +170 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/KE100(UK)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/T110(EU)_1.0_1.8.0.json +526 -0
- kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json +533 -0
- kasa/tests/fixtures/smart/child/T310(EU)_1.0_1.5.0.json +530 -0
- kasa/tests/fixtures/smart/child/T315(EU)_1.0_1.7.0.json +537 -0
- kasa/tests/smart/__init__.py +0 -0
- kasa/tests/smart/features/__init__.py +0 -0
- kasa/tests/smart/features/test_brightness.py +55 -0
- kasa/tests/smart/features/test_colortemp.py +30 -0
- kasa/tests/smart/modules/__init__.py +0 -0
- kasa/tests/smart/modules/test_contact.py +28 -0
- kasa/tests/smart/modules/test_fan.py +79 -0
- kasa/tests/smart/modules/test_firmware.py +108 -0
- kasa/tests/smart/modules/test_humidity.py +28 -0
- kasa/tests/smart/modules/test_light_effect.py +41 -0
- kasa/tests/smart/modules/test_temperature.py +47 -0
- kasa/tests/smart/modules/test_temperaturecontrol.py +137 -0
- kasa/tests/smart/modules/test_waterleak.py +42 -0
- kasa/tests/test_aestransport.py +115 -23
- kasa/tests/test_bulb.py +115 -85
- kasa/tests/test_childdevice.py +19 -7
- kasa/tests/test_cli.py +453 -99
- kasa/tests/test_common_modules.py +216 -0
- kasa/tests/test_device.py +268 -0
- kasa/tests/test_device_factory.py +61 -35
- kasa/tests/test_device_type.py +1 -1
- kasa/tests/test_deviceconfig.py +2 -2
- kasa/tests/test_dimmer.py +33 -24
- kasa/tests/test_discovery.py +112 -63
- kasa/tests/test_emeter.py +14 -10
- kasa/tests/test_feature.py +205 -0
- kasa/tests/test_httpclient.py +9 -9
- kasa/tests/test_iotdevice.py +286 -0
- kasa/tests/test_klapprotocol.py +94 -37
- kasa/tests/test_lightstrip.py +24 -19
- kasa/tests/test_plug.py +46 -8
- kasa/tests/test_protocol.py +8 -10
- kasa/tests/test_readme_examples.py +87 -26
- kasa/tests/test_smartdevice.py +203 -311
- kasa/tests/test_smartprotocol.py +130 -25
- kasa/tests/test_strip.py +15 -6
- kasa/tests/test_usage.py +1 -1
- kasa/xortransport.py +15 -12
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -143
- python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
- kasa/modules/emeter.py +0 -111
- kasa/smartlightstrip.py +0 -143
- kasa/smartplug.py +0 -91
- kasa/tapo/__init__.py +0 -7
- kasa/tapo/childdevice.py +0 -41
- kasa/tapo/tapobulb.py +0 -262
- kasa/tapo/tapodevice.py +0 -409
- kasa/tapo/tapoplug.py +0 -45
- kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -50
- kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -50
- kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -75
- kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -104
- python_kasa-0.6.2.1.dist-info/RECORD +0 -167
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +0 -0
- {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
kasa/device.py
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
"""Module for Device base class."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from datetime import datetime
|
|
9
|
+
from typing import TYPE_CHECKING, Any, Mapping, Sequence
|
|
10
|
+
from warnings import warn
|
|
11
|
+
|
|
12
|
+
from .credentials import Credentials
|
|
13
|
+
from .device_type import DeviceType
|
|
14
|
+
from .deviceconfig import DeviceConfig
|
|
15
|
+
from .emeterstatus import EmeterStatus
|
|
16
|
+
from .exceptions import KasaException
|
|
17
|
+
from .feature import Feature
|
|
18
|
+
from .iotprotocol import IotProtocol
|
|
19
|
+
from .module import Module
|
|
20
|
+
from .protocol import BaseProtocol
|
|
21
|
+
from .xortransport import XorTransport
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from .modulemapping import ModuleMapping, ModuleName
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass
|
|
28
|
+
class WifiNetwork:
|
|
29
|
+
"""Wifi network container."""
|
|
30
|
+
|
|
31
|
+
ssid: str
|
|
32
|
+
key_type: int
|
|
33
|
+
# These are available only on softaponboarding
|
|
34
|
+
cipher_type: int | None = None
|
|
35
|
+
bssid: str | None = None
|
|
36
|
+
channel: int | None = None
|
|
37
|
+
rssi: int | None = None
|
|
38
|
+
|
|
39
|
+
# For SMART devices
|
|
40
|
+
signal_level: int | None = None
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_LOGGER = logging.getLogger(__name__)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Device(ABC):
|
|
47
|
+
"""Common device interface.
|
|
48
|
+
|
|
49
|
+
Do not instantiate this class directly, instead get a device instance from
|
|
50
|
+
:func:`Device.connect()`, :func:`Discover.discover()`
|
|
51
|
+
or :func:`Discover.discover_single()`.
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
host: str,
|
|
57
|
+
*,
|
|
58
|
+
config: DeviceConfig | None = None,
|
|
59
|
+
protocol: BaseProtocol | None = None,
|
|
60
|
+
) -> None:
|
|
61
|
+
"""Create a new Device instance.
|
|
62
|
+
|
|
63
|
+
:param str host: host name or IP address of the device
|
|
64
|
+
:param DeviceConfig config: device configuration
|
|
65
|
+
:param BaseProtocol protocol: protocol for communicating with the device
|
|
66
|
+
"""
|
|
67
|
+
if config and protocol:
|
|
68
|
+
protocol._transport._config = config
|
|
69
|
+
self.protocol: BaseProtocol = protocol or IotProtocol(
|
|
70
|
+
transport=XorTransport(config=config or DeviceConfig(host=host)),
|
|
71
|
+
)
|
|
72
|
+
_LOGGER.debug("Initializing %s of type %s", self.host, type(self))
|
|
73
|
+
self._device_type = DeviceType.Unknown
|
|
74
|
+
# TODO: typing Any is just as using Optional[Dict] would require separate
|
|
75
|
+
# checks in accessors. the @updated_required decorator does not ensure
|
|
76
|
+
# mypy that these are not accessed incorrectly.
|
|
77
|
+
self._last_update: Any = None
|
|
78
|
+
self._discovery_info: dict[str, Any] | None = None
|
|
79
|
+
|
|
80
|
+
self._features: dict[str, Feature] = {}
|
|
81
|
+
self._parent: Device | None = None
|
|
82
|
+
self._children: Mapping[str, Device] = {}
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
async def connect(
|
|
86
|
+
*,
|
|
87
|
+
host: str | None = None,
|
|
88
|
+
config: DeviceConfig | None = None,
|
|
89
|
+
) -> Device:
|
|
90
|
+
"""Connect to a single device by the given hostname or device configuration.
|
|
91
|
+
|
|
92
|
+
This method avoids the UDP based discovery process and
|
|
93
|
+
will connect directly to the device.
|
|
94
|
+
|
|
95
|
+
It is generally preferred to avoid :func:`discover_single()` and
|
|
96
|
+
use this function instead as it should perform better when
|
|
97
|
+
the WiFi network is congested or the device is not responding
|
|
98
|
+
to discovery requests.
|
|
99
|
+
|
|
100
|
+
:param host: Hostname of device to query
|
|
101
|
+
:param config: Connection parameters to ensure the correct protocol
|
|
102
|
+
and connection options are used.
|
|
103
|
+
:rtype: SmartDevice
|
|
104
|
+
:return: Object for querying/controlling found device.
|
|
105
|
+
"""
|
|
106
|
+
from .device_factory import connect # pylint: disable=import-outside-toplevel
|
|
107
|
+
|
|
108
|
+
return await connect(host=host, config=config) # type: ignore[arg-type]
|
|
109
|
+
|
|
110
|
+
@abstractmethod
|
|
111
|
+
async def update(self, update_children: bool = True):
|
|
112
|
+
"""Update the device."""
|
|
113
|
+
|
|
114
|
+
async def disconnect(self):
|
|
115
|
+
"""Disconnect and close any underlying connection resources."""
|
|
116
|
+
await self.protocol.close()
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
@abstractmethod
|
|
120
|
+
def modules(self) -> ModuleMapping[Module]:
|
|
121
|
+
"""Return the device modules."""
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
@abstractmethod
|
|
125
|
+
def is_on(self) -> bool:
|
|
126
|
+
"""Return true if the device is on."""
|
|
127
|
+
|
|
128
|
+
@property
|
|
129
|
+
def is_off(self) -> bool:
|
|
130
|
+
"""Return True if device is off."""
|
|
131
|
+
return not self.is_on
|
|
132
|
+
|
|
133
|
+
@abstractmethod
|
|
134
|
+
async def turn_on(self, **kwargs) -> dict | None:
|
|
135
|
+
"""Turn on the device."""
|
|
136
|
+
|
|
137
|
+
@abstractmethod
|
|
138
|
+
async def turn_off(self, **kwargs) -> dict | None:
|
|
139
|
+
"""Turn off the device."""
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def host(self) -> str:
|
|
143
|
+
"""The device host."""
|
|
144
|
+
return self.protocol._transport._host
|
|
145
|
+
|
|
146
|
+
@host.setter
|
|
147
|
+
def host(self, value):
|
|
148
|
+
"""Set the device host.
|
|
149
|
+
|
|
150
|
+
Generally used by discovery to set the hostname after ip discovery.
|
|
151
|
+
"""
|
|
152
|
+
self.protocol._transport._host = value
|
|
153
|
+
self.protocol._transport._config.host = value
|
|
154
|
+
|
|
155
|
+
@property
|
|
156
|
+
def port(self) -> int:
|
|
157
|
+
"""The device port."""
|
|
158
|
+
return self.protocol._transport._port
|
|
159
|
+
|
|
160
|
+
@property
|
|
161
|
+
def credentials(self) -> Credentials | None:
|
|
162
|
+
"""The device credentials."""
|
|
163
|
+
return self.protocol._transport._credentials
|
|
164
|
+
|
|
165
|
+
@property
|
|
166
|
+
def credentials_hash(self) -> str | None:
|
|
167
|
+
"""The protocol specific hash of the credentials the device is using."""
|
|
168
|
+
return self.protocol._transport.credentials_hash
|
|
169
|
+
|
|
170
|
+
@property
|
|
171
|
+
def device_type(self) -> DeviceType:
|
|
172
|
+
"""Return the device type."""
|
|
173
|
+
return self._device_type
|
|
174
|
+
|
|
175
|
+
@abstractmethod
|
|
176
|
+
def update_from_discover_info(self, info):
|
|
177
|
+
"""Update state from info from the discover call."""
|
|
178
|
+
|
|
179
|
+
@property
|
|
180
|
+
def config(self) -> DeviceConfig:
|
|
181
|
+
"""Return the device configuration."""
|
|
182
|
+
return self.protocol.config
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
@abstractmethod
|
|
186
|
+
def model(self) -> str:
|
|
187
|
+
"""Returns the device model."""
|
|
188
|
+
|
|
189
|
+
@property
|
|
190
|
+
@abstractmethod
|
|
191
|
+
def alias(self) -> str | None:
|
|
192
|
+
"""Returns the device alias or nickname."""
|
|
193
|
+
|
|
194
|
+
async def _raw_query(self, request: str | dict) -> Any:
|
|
195
|
+
"""Send a raw query to the device."""
|
|
196
|
+
return await self.protocol.query(request=request)
|
|
197
|
+
|
|
198
|
+
@property
|
|
199
|
+
def children(self) -> Sequence[Device]:
|
|
200
|
+
"""Returns the child devices."""
|
|
201
|
+
return list(self._children.values())
|
|
202
|
+
|
|
203
|
+
def get_child_device(self, id_: str) -> Device:
|
|
204
|
+
"""Return child device by its ID."""
|
|
205
|
+
return self._children[id_]
|
|
206
|
+
|
|
207
|
+
@property
|
|
208
|
+
@abstractmethod
|
|
209
|
+
def sys_info(self) -> dict[str, Any]:
|
|
210
|
+
"""Returns the device info."""
|
|
211
|
+
|
|
212
|
+
def get_plug_by_name(self, name: str) -> Device:
|
|
213
|
+
"""Return child device for the given name."""
|
|
214
|
+
for p in self.children:
|
|
215
|
+
if p.alias == name:
|
|
216
|
+
return p
|
|
217
|
+
|
|
218
|
+
raise KasaException(f"Device has no child with {name}")
|
|
219
|
+
|
|
220
|
+
def get_plug_by_index(self, index: int) -> Device:
|
|
221
|
+
"""Return child device for the given index."""
|
|
222
|
+
if index + 1 > len(self.children) or index < 0:
|
|
223
|
+
raise KasaException(
|
|
224
|
+
f"Invalid index {index}, device has {len(self.children)} plugs"
|
|
225
|
+
)
|
|
226
|
+
return self.children[index]
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
@abstractmethod
|
|
230
|
+
def time(self) -> datetime:
|
|
231
|
+
"""Return the time."""
|
|
232
|
+
|
|
233
|
+
@property
|
|
234
|
+
@abstractmethod
|
|
235
|
+
def timezone(self) -> dict:
|
|
236
|
+
"""Return the timezone and time_difference."""
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
@abstractmethod
|
|
240
|
+
def hw_info(self) -> dict:
|
|
241
|
+
"""Return hardware info for the device."""
|
|
242
|
+
|
|
243
|
+
@property
|
|
244
|
+
@abstractmethod
|
|
245
|
+
def location(self) -> dict:
|
|
246
|
+
"""Return the device location."""
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
@abstractmethod
|
|
250
|
+
def rssi(self) -> int | None:
|
|
251
|
+
"""Return the rssi."""
|
|
252
|
+
|
|
253
|
+
@property
|
|
254
|
+
@abstractmethod
|
|
255
|
+
def mac(self) -> str:
|
|
256
|
+
"""Return the mac formatted with colons."""
|
|
257
|
+
|
|
258
|
+
@property
|
|
259
|
+
@abstractmethod
|
|
260
|
+
def device_id(self) -> str:
|
|
261
|
+
"""Return the device id."""
|
|
262
|
+
|
|
263
|
+
@property
|
|
264
|
+
@abstractmethod
|
|
265
|
+
def internal_state(self) -> Any:
|
|
266
|
+
"""Return all the internal state data."""
|
|
267
|
+
|
|
268
|
+
@property
|
|
269
|
+
def state_information(self) -> dict[str, Any]:
|
|
270
|
+
"""Return available features and their values."""
|
|
271
|
+
return {feat.name: feat.value for feat in self._features.values()}
|
|
272
|
+
|
|
273
|
+
@property
|
|
274
|
+
def features(self) -> dict[str, Feature]:
|
|
275
|
+
"""Return the list of supported features."""
|
|
276
|
+
return self._features
|
|
277
|
+
|
|
278
|
+
def _add_feature(self, feature: Feature):
|
|
279
|
+
"""Add a new feature to the device."""
|
|
280
|
+
if feature.id in self._features:
|
|
281
|
+
raise KasaException("Duplicate feature id %s" % feature.id)
|
|
282
|
+
assert feature.id is not None # TODO: hack for typing # noqa: S101
|
|
283
|
+
self._features[feature.id] = feature
|
|
284
|
+
|
|
285
|
+
@property
|
|
286
|
+
@abstractmethod
|
|
287
|
+
def has_emeter(self) -> bool:
|
|
288
|
+
"""Return if the device has emeter."""
|
|
289
|
+
|
|
290
|
+
@property
|
|
291
|
+
@abstractmethod
|
|
292
|
+
def on_since(self) -> datetime | None:
|
|
293
|
+
"""Return the time that the device was turned on or None if turned off."""
|
|
294
|
+
|
|
295
|
+
@abstractmethod
|
|
296
|
+
async def get_emeter_realtime(self) -> EmeterStatus:
|
|
297
|
+
"""Retrieve current energy readings."""
|
|
298
|
+
|
|
299
|
+
@property
|
|
300
|
+
@abstractmethod
|
|
301
|
+
def emeter_realtime(self) -> EmeterStatus:
|
|
302
|
+
"""Get the emeter status."""
|
|
303
|
+
|
|
304
|
+
@property
|
|
305
|
+
@abstractmethod
|
|
306
|
+
def emeter_this_month(self) -> float | None:
|
|
307
|
+
"""Get the emeter value for this month."""
|
|
308
|
+
|
|
309
|
+
@property
|
|
310
|
+
@abstractmethod
|
|
311
|
+
def emeter_today(self) -> float | None | Any:
|
|
312
|
+
"""Get the emeter value for today."""
|
|
313
|
+
# Return type of Any ensures consumers being shielded from the return
|
|
314
|
+
# type by @update_required are not affected.
|
|
315
|
+
|
|
316
|
+
@abstractmethod
|
|
317
|
+
async def wifi_scan(self) -> list[WifiNetwork]:
|
|
318
|
+
"""Scan for available wifi networks."""
|
|
319
|
+
|
|
320
|
+
@abstractmethod
|
|
321
|
+
async def wifi_join(self, ssid: str, password: str, keytype: str = "wpa2_psk"):
|
|
322
|
+
"""Join the given wifi network."""
|
|
323
|
+
|
|
324
|
+
@abstractmethod
|
|
325
|
+
async def set_alias(self, alias: str):
|
|
326
|
+
"""Set the device name (alias)."""
|
|
327
|
+
|
|
328
|
+
def __repr__(self):
|
|
329
|
+
if self._last_update is None:
|
|
330
|
+
return f"<{self.device_type} at {self.host} - update() needed>"
|
|
331
|
+
return f"<{self.device_type} at {self.host} - {self.alias} ({self.model})>"
|
|
332
|
+
|
|
333
|
+
_deprecated_device_type_attributes = {
|
|
334
|
+
# is_type
|
|
335
|
+
"is_bulb": (Module.Light, DeviceType.Bulb),
|
|
336
|
+
"is_dimmer": (Module.Light, DeviceType.Dimmer),
|
|
337
|
+
"is_light_strip": (Module.LightEffect, DeviceType.LightStrip),
|
|
338
|
+
"is_plug": (Module.Led, DeviceType.Plug),
|
|
339
|
+
"is_wallswitch": (Module.Led, DeviceType.WallSwitch),
|
|
340
|
+
"is_strip": (None, DeviceType.Strip),
|
|
341
|
+
"is_strip_socket": (None, DeviceType.StripSocket),
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
def _get_replacing_attr(self, module_name: ModuleName, *attrs):
|
|
345
|
+
if module_name not in self.modules:
|
|
346
|
+
return None
|
|
347
|
+
|
|
348
|
+
for attr in attrs:
|
|
349
|
+
if hasattr(self.modules[module_name], attr):
|
|
350
|
+
return getattr(self.modules[module_name], attr)
|
|
351
|
+
|
|
352
|
+
return None
|
|
353
|
+
|
|
354
|
+
_deprecated_other_attributes = {
|
|
355
|
+
# light attributes
|
|
356
|
+
"is_color": (Module.Light, ["is_color"]),
|
|
357
|
+
"is_dimmable": (Module.Light, ["is_dimmable"]),
|
|
358
|
+
"is_variable_color_temp": (Module.Light, ["is_variable_color_temp"]),
|
|
359
|
+
"brightness": (Module.Light, ["brightness"]),
|
|
360
|
+
"set_brightness": (Module.Light, ["set_brightness"]),
|
|
361
|
+
"hsv": (Module.Light, ["hsv"]),
|
|
362
|
+
"set_hsv": (Module.Light, ["set_hsv"]),
|
|
363
|
+
"color_temp": (Module.Light, ["color_temp"]),
|
|
364
|
+
"set_color_temp": (Module.Light, ["set_color_temp"]),
|
|
365
|
+
"valid_temperature_range": (Module.Light, ["valid_temperature_range"]),
|
|
366
|
+
"has_effects": (Module.Light, ["has_effects"]),
|
|
367
|
+
"_deprecated_set_light_state": (Module.Light, ["has_effects"]),
|
|
368
|
+
# led attributes
|
|
369
|
+
"led": (Module.Led, ["led"]),
|
|
370
|
+
"set_led": (Module.Led, ["set_led"]),
|
|
371
|
+
# light effect attributes
|
|
372
|
+
# The return values for effect is a str instead of dict so the lightstrip
|
|
373
|
+
# modules have a _deprecated method to return the value as before.
|
|
374
|
+
"effect": (Module.LightEffect, ["_deprecated_effect", "effect"]),
|
|
375
|
+
# The return values for effect_list includes the Off effect so the lightstrip
|
|
376
|
+
# modules have a _deprecated method to return the values as before.
|
|
377
|
+
"effect_list": (Module.LightEffect, ["_deprecated_effect_list", "effect_list"]),
|
|
378
|
+
"set_effect": (Module.LightEffect, ["set_effect"]),
|
|
379
|
+
"set_custom_effect": (Module.LightEffect, ["set_custom_effect"]),
|
|
380
|
+
# light preset attributes
|
|
381
|
+
"presets": (Module.LightPreset, ["_deprecated_presets", "preset_states_list"]),
|
|
382
|
+
"save_preset": (Module.LightPreset, ["_deprecated_save_preset"]),
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
def __getattr__(self, name):
|
|
386
|
+
# is_device_type
|
|
387
|
+
if dep_device_type_attr := self._deprecated_device_type_attributes.get(name):
|
|
388
|
+
module = dep_device_type_attr[0]
|
|
389
|
+
msg = f"{name} is deprecated"
|
|
390
|
+
if module:
|
|
391
|
+
msg += f", use: {module} in device.modules instead"
|
|
392
|
+
warn(msg, DeprecationWarning, stacklevel=1)
|
|
393
|
+
return self.device_type == dep_device_type_attr[1]
|
|
394
|
+
# Other deprecated attributes
|
|
395
|
+
if (dep_attr := self._deprecated_other_attributes.get(name)) and (
|
|
396
|
+
(replacing_attr := self._get_replacing_attr(dep_attr[0], *dep_attr[1]))
|
|
397
|
+
is not None
|
|
398
|
+
):
|
|
399
|
+
module_name = dep_attr[0]
|
|
400
|
+
msg = (
|
|
401
|
+
f"{name} is deprecated, use: "
|
|
402
|
+
+ f"Module.{module_name} in device.modules instead"
|
|
403
|
+
)
|
|
404
|
+
warn(msg, DeprecationWarning, stacklevel=1)
|
|
405
|
+
return replacing_attr
|
|
406
|
+
raise AttributeError(f"Device has no attribute {name!r}")
|
kasa/device_factory.py
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
1
|
"""Device creation via DeviceConfig."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
2
5
|
import logging
|
|
3
6
|
import time
|
|
4
|
-
from typing import Any
|
|
7
|
+
from typing import Any
|
|
5
8
|
|
|
6
9
|
from .aestransport import AesTransport
|
|
10
|
+
from .device import Device
|
|
11
|
+
from .device_type import DeviceType
|
|
7
12
|
from .deviceconfig import DeviceConfig
|
|
8
|
-
from .exceptions import
|
|
13
|
+
from .exceptions import KasaException, UnsupportedDeviceError
|
|
14
|
+
from .iot import (
|
|
15
|
+
IotBulb,
|
|
16
|
+
IotDevice,
|
|
17
|
+
IotDimmer,
|
|
18
|
+
IotLightStrip,
|
|
19
|
+
IotPlug,
|
|
20
|
+
IotStrip,
|
|
21
|
+
IotWallSwitch,
|
|
22
|
+
)
|
|
9
23
|
from .iotprotocol import IotProtocol
|
|
10
24
|
from .klaptransport import KlapTransport, KlapTransportV2
|
|
11
25
|
from .protocol import (
|
|
12
26
|
BaseProtocol,
|
|
13
27
|
BaseTransport,
|
|
14
28
|
)
|
|
15
|
-
from .
|
|
16
|
-
from .smartdevice import SmartDevice
|
|
17
|
-
from .smartdimmer import SmartDimmer
|
|
18
|
-
from .smartlightstrip import SmartLightStrip
|
|
19
|
-
from .smartplug import SmartPlug
|
|
29
|
+
from .smart import SmartDevice
|
|
20
30
|
from .smartprotocol import SmartProtocol
|
|
21
|
-
from .smartstrip import SmartStrip
|
|
22
|
-
from .tapo import TapoBulb, TapoPlug
|
|
23
31
|
from .xortransport import XorTransport
|
|
24
32
|
|
|
25
33
|
_LOGGER = logging.getLogger(__name__)
|
|
@@ -29,7 +37,7 @@ GET_SYSINFO_QUERY = {
|
|
|
29
37
|
}
|
|
30
38
|
|
|
31
39
|
|
|
32
|
-
async def connect(*, host:
|
|
40
|
+
async def connect(*, host: str | None = None, config: DeviceConfig) -> Device:
|
|
33
41
|
"""Connect to a single device by the given hostname or device configuration.
|
|
34
42
|
|
|
35
43
|
This method avoids the UDP based discovery process and
|
|
@@ -49,10 +57,24 @@ async def connect(*, host: Optional[str] = None, config: DeviceConfig) -> "Smart
|
|
|
49
57
|
:return: Object for querying/controlling found device.
|
|
50
58
|
"""
|
|
51
59
|
if host and config or (not host and not config):
|
|
52
|
-
raise
|
|
60
|
+
raise KasaException("One of host or config must be provded and not both")
|
|
53
61
|
if host:
|
|
54
62
|
config = DeviceConfig(host=host)
|
|
55
63
|
|
|
64
|
+
if (protocol := get_protocol(config=config)) is None:
|
|
65
|
+
raise UnsupportedDeviceError(
|
|
66
|
+
f"Unsupported device for {config.host}: "
|
|
67
|
+
+ f"{config.connection_type.device_family.value}"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
try:
|
|
71
|
+
return await _connect(config, protocol)
|
|
72
|
+
except:
|
|
73
|
+
await protocol.close()
|
|
74
|
+
raise
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
async def _connect(config: DeviceConfig, protocol: BaseProtocol) -> Device:
|
|
56
78
|
debug_enabled = _LOGGER.isEnabledFor(logging.DEBUG)
|
|
57
79
|
if debug_enabled:
|
|
58
80
|
start_time = time.perf_counter()
|
|
@@ -67,13 +89,8 @@ async def connect(*, host: Optional[str] = None, config: DeviceConfig) -> "Smart
|
|
|
67
89
|
)
|
|
68
90
|
start_time = time.perf_counter()
|
|
69
91
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
f"Unsupported device for {config.host}: "
|
|
73
|
-
+ f"{config.connection_type.device_family.value}"
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
device_class: Optional[Type[SmartDevice]]
|
|
92
|
+
device_class: type[Device] | None
|
|
93
|
+
device: Device | None = None
|
|
77
94
|
|
|
78
95
|
if isinstance(protocol, IotProtocol) and isinstance(
|
|
79
96
|
protocol._transport, XorTransport
|
|
@@ -94,63 +111,85 @@ async def connect(*, host: Optional[str] = None, config: DeviceConfig) -> "Smart
|
|
|
94
111
|
_perf_log(True, "update")
|
|
95
112
|
return device
|
|
96
113
|
else:
|
|
97
|
-
raise
|
|
114
|
+
raise UnsupportedDeviceError(
|
|
98
115
|
f"Unsupported device for {config.host}: "
|
|
99
116
|
+ f"{config.connection_type.device_family.value}"
|
|
100
117
|
)
|
|
101
118
|
|
|
102
119
|
|
|
103
|
-
def
|
|
120
|
+
def _get_device_type_from_sys_info(info: dict[str, Any]) -> DeviceType:
|
|
104
121
|
"""Find SmartDevice subclass for device described by passed data."""
|
|
105
122
|
if "system" not in info or "get_sysinfo" not in info["system"]:
|
|
106
|
-
raise
|
|
123
|
+
raise KasaException("No 'system' or 'get_sysinfo' in response")
|
|
107
124
|
|
|
108
|
-
sysinfo:
|
|
109
|
-
type_:
|
|
125
|
+
sysinfo: dict[str, Any] = info["system"]["get_sysinfo"]
|
|
126
|
+
type_: str | None = sysinfo.get("type", sysinfo.get("mic_type"))
|
|
110
127
|
if type_ is None:
|
|
111
|
-
raise
|
|
128
|
+
raise KasaException("Unable to find the device type field!")
|
|
112
129
|
|
|
113
130
|
if "dev_name" in sysinfo and "Dimmer" in sysinfo["dev_name"]:
|
|
114
|
-
return
|
|
131
|
+
return DeviceType.Dimmer
|
|
115
132
|
|
|
116
133
|
if "smartplug" in type_.lower():
|
|
117
134
|
if "children" in sysinfo:
|
|
118
|
-
return
|
|
119
|
-
|
|
120
|
-
|
|
135
|
+
return DeviceType.Strip
|
|
136
|
+
if (dev_name := sysinfo.get("dev_name")) and "light" in dev_name.lower():
|
|
137
|
+
return DeviceType.WallSwitch
|
|
138
|
+
return DeviceType.Plug
|
|
121
139
|
|
|
122
140
|
if "smartbulb" in type_.lower():
|
|
123
141
|
if "length" in sysinfo: # strips have length
|
|
124
|
-
return
|
|
142
|
+
return DeviceType.LightStrip
|
|
125
143
|
|
|
126
|
-
return
|
|
127
|
-
raise
|
|
144
|
+
return DeviceType.Bulb
|
|
145
|
+
raise UnsupportedDeviceError("Unknown device type: %s" % type_)
|
|
128
146
|
|
|
129
147
|
|
|
130
|
-
def
|
|
148
|
+
def get_device_class_from_sys_info(sysinfo: dict[str, Any]) -> type[IotDevice]:
|
|
149
|
+
"""Find SmartDevice subclass for device described by passed data."""
|
|
150
|
+
TYPE_TO_CLASS = {
|
|
151
|
+
DeviceType.Bulb: IotBulb,
|
|
152
|
+
DeviceType.Plug: IotPlug,
|
|
153
|
+
DeviceType.Dimmer: IotDimmer,
|
|
154
|
+
DeviceType.Strip: IotStrip,
|
|
155
|
+
DeviceType.WallSwitch: IotWallSwitch,
|
|
156
|
+
DeviceType.LightStrip: IotLightStrip,
|
|
157
|
+
}
|
|
158
|
+
return TYPE_TO_CLASS[_get_device_type_from_sys_info(sysinfo)]
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def get_device_class_from_family(device_type: str) -> type[Device] | None:
|
|
131
162
|
"""Return the device class from the type name."""
|
|
132
|
-
supported_device_types:
|
|
133
|
-
"SMART.TAPOPLUG":
|
|
134
|
-
"SMART.TAPOBULB":
|
|
135
|
-
"SMART.TAPOSWITCH":
|
|
136
|
-
"SMART.KASAPLUG":
|
|
137
|
-
"SMART.
|
|
138
|
-
"
|
|
139
|
-
"
|
|
163
|
+
supported_device_types: dict[str, type[Device]] = {
|
|
164
|
+
"SMART.TAPOPLUG": SmartDevice,
|
|
165
|
+
"SMART.TAPOBULB": SmartDevice,
|
|
166
|
+
"SMART.TAPOSWITCH": SmartDevice,
|
|
167
|
+
"SMART.KASAPLUG": SmartDevice,
|
|
168
|
+
"SMART.TAPOHUB": SmartDevice,
|
|
169
|
+
"SMART.KASAHUB": SmartDevice,
|
|
170
|
+
"SMART.KASASWITCH": SmartDevice,
|
|
171
|
+
"IOT.SMARTPLUGSWITCH": IotPlug,
|
|
172
|
+
"IOT.SMARTBULB": IotBulb,
|
|
140
173
|
}
|
|
141
|
-
|
|
174
|
+
if (
|
|
175
|
+
cls := supported_device_types.get(device_type)
|
|
176
|
+
) is None and device_type.startswith("SMART."):
|
|
177
|
+
_LOGGER.warning("Unknown SMART device with %s, using SmartDevice", device_type)
|
|
178
|
+
cls = SmartDevice
|
|
179
|
+
|
|
180
|
+
return cls
|
|
142
181
|
|
|
143
182
|
|
|
144
183
|
def get_protocol(
|
|
145
184
|
config: DeviceConfig,
|
|
146
|
-
) ->
|
|
185
|
+
) -> BaseProtocol | None:
|
|
147
186
|
"""Return the protocol from the connection name."""
|
|
148
187
|
protocol_name = config.connection_type.device_family.value.split(".")[0]
|
|
149
188
|
protocol_transport_key = (
|
|
150
189
|
protocol_name + "." + config.connection_type.encryption_type.value
|
|
151
190
|
)
|
|
152
|
-
supported_device_protocols:
|
|
153
|
-
str,
|
|
191
|
+
supported_device_protocols: dict[
|
|
192
|
+
str, tuple[type[BaseProtocol], type[BaseTransport]]
|
|
154
193
|
] = {
|
|
155
194
|
"IOT.XOR": (IotProtocol, XorTransport),
|
|
156
195
|
"IOT.KLAP": (IotProtocol, KlapTransport),
|
kasa/device_type.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"""TP-Link device types."""
|
|
2
2
|
|
|
3
|
+
from __future__ import annotations
|
|
3
4
|
|
|
4
5
|
from enum import Enum
|
|
5
6
|
|
|
@@ -11,15 +12,18 @@ class DeviceType(Enum):
|
|
|
11
12
|
Plug = "plug"
|
|
12
13
|
Bulb = "bulb"
|
|
13
14
|
Strip = "strip"
|
|
15
|
+
WallSwitch = "wallswitch"
|
|
14
16
|
StripSocket = "stripsocket"
|
|
15
17
|
Dimmer = "dimmer"
|
|
16
18
|
LightStrip = "lightstrip"
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
Sensor = "sensor"
|
|
20
|
+
Hub = "hub"
|
|
21
|
+
Fan = "fan"
|
|
22
|
+
Thermostat = "thermostat"
|
|
19
23
|
Unknown = "unknown"
|
|
20
24
|
|
|
21
25
|
@staticmethod
|
|
22
|
-
def from_value(name: str) ->
|
|
26
|
+
def from_value(name: str) -> DeviceType:
|
|
23
27
|
"""Return device type from string value."""
|
|
24
28
|
for device_type in DeviceType:
|
|
25
29
|
if device_type.value == name:
|