python-kasa 0.6.0.dev2__py3-none-any.whl → 0.6.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/__init__.py +2 -2
- kasa/aestransport.py +154 -101
- kasa/cli.py +42 -23
- kasa/device_factory.py +4 -3
- kasa/deviceconfig.py +19 -10
- kasa/discover.py +28 -11
- kasa/exceptions.py +15 -1
- kasa/httpclient.py +113 -0
- kasa/iotprotocol.py +30 -26
- kasa/json.py +5 -1
- kasa/klaptransport.py +60 -75
- kasa/modules/emeter.py +1 -1
- kasa/protocol.py +61 -16
- kasa/smartbulb.py +2 -2
- kasa/smartdevice.py +11 -7
- kasa/smartdimmer.py +2 -2
- kasa/smartlightstrip.py +2 -2
- kasa/smartplug.py +2 -2
- kasa/smartprotocol.py +16 -38
- kasa/smartstrip.py +2 -2
- kasa/tapo/tapodevice.py +19 -4
- kasa/tapo/tapoplug.py +2 -2
- kasa/tests/conftest.py +24 -8
- kasa/tests/fixtures/HS100(UK)_1.0_1.2.6.json +28 -0
- kasa/tests/fixtures/HS100(UK)_4.1_1.1.0.json +3 -3
- kasa/tests/fixtures/HS110(EU)_1.0_1.2.5.json +11 -23
- kasa/tests/fixtures/HS110(EU)_4.0_1.0.4.json +40 -0
- kasa/tests/fixtures/HS300(US)_2.0_1.0.12.json +90 -0
- kasa/tests/fixtures/KL125(US)_4.0_1.0.5.json +93 -0
- kasa/tests/fixtures/KL130(EU)_1.0_1.8.8.json +15 -15
- kasa/tests/fixtures/KL430(US)_2.0_1.0.11.json +59 -0
- kasa/tests/fixtures/KP105(UK)_1.0_1.0.5.json +36 -0
- kasa/tests/fixtures/KP105(UK)_1.0_1.0.7.json +4 -4
- kasa/tests/fixtures/KP115(EU)_1.0_1.0.16.json +9 -9
- kasa/tests/fixtures/KP115(US)_1.0_1.0.21.json +42 -0
- kasa/tests/fixtures/KP303(UK)_1.0_1.0.3.json +9 -25
- kasa/tests/fixtures/smart/EP25(US)_2.6_1.0.2.json +414 -0
- kasa/tests/fixtures/smart/L510B(EU)_3.0_1.0.5.json +263 -0
- kasa/tests/fixtures/smart/L510E(US)_3.0_1.0.5.json +295 -0
- kasa/tests/fixtures/smart/L510E(US)_3.0_1.1.2.json +267 -0
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.0.6.json +19 -19
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.0.json +12 -12
- kasa/tests/fixtures/smart/L530E(US)_2.0_1.1.0.json +439 -0
- kasa/tests/fixtures/smart/L900-10(EU)_1.0_1.0.17.json +10 -10
- kasa/tests/fixtures/smart/L900-10(US)_1.0_1.0.11.json +428 -0
- kasa/tests/fixtures/smart/L900-5(EU)_1.0_1.0.17.json +98 -20
- kasa/tests/fixtures/smart/L900-5(EU)_1.0_1.1.0.json +390 -0
- kasa/tests/fixtures/smart/L920-5(US)_1.0_1.1.0.json +397 -0
- kasa/tests/fixtures/smart/L920-5(US)_1.0_1.1.3.json +415 -0
- kasa/tests/fixtures/smart/L930-5(US)_1.0_1.1.2.json +429 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.3.7.json +197 -0
- kasa/tests/fixtures/smart/P110(EU)_1.0_1.2.3.json +28 -19
- kasa/tests/fixtures/smart/P110(UK)_1.0_1.3.0.json +31 -31
- kasa/tests/fixtures/smart/P125M(US)_1.0_1.1.0.json +11 -11
- kasa/tests/fixtures/smart/P135(US)_1.0_1.0.5.json +317 -0
- kasa/tests/fixtures/smart/S500D(US)_1.0_1.0.5.json +317 -0
- kasa/tests/fixtures/smart/S505(US)_1.0_1.0.2.json +309 -0
- kasa/tests/newfakes.py +3 -0
- kasa/tests/test_aestransport.py +110 -22
- kasa/tests/test_cli.py +49 -2
- kasa/tests/test_device_factory.py +6 -4
- kasa/tests/test_deviceconfig.py +24 -9
- kasa/tests/test_discovery.py +126 -9
- kasa/tests/test_httpclient.py +100 -0
- kasa/tests/test_klapprotocol.py +70 -23
- kasa/tests/test_protocol.py +76 -2
- kasa/tests/test_smartprotocol.py +0 -1
- {python_kasa-0.6.0.dev2.dist-info → python_kasa-0.6.1.dist-info}/METADATA +37 -15
- {python_kasa-0.6.0.dev2.dist-info → python_kasa-0.6.1.dist-info}/RECORD +72 -49
- {python_kasa-0.6.0.dev2.dist-info → python_kasa-0.6.1.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.0.dev2.dist-info → python_kasa-0.6.1.dist-info}/WHEEL +0 -0
- {python_kasa-0.6.0.dev2.dist-info → python_kasa-0.6.1.dist-info}/entry_points.txt +0 -0
kasa/protocol.py
CHANGED
|
@@ -10,6 +10,7 @@ which are licensed under the Apache License, Version 2.0
|
|
|
10
10
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
11
11
|
"""
|
|
12
12
|
import asyncio
|
|
13
|
+
import base64
|
|
13
14
|
import contextlib
|
|
14
15
|
import errno
|
|
15
16
|
import logging
|
|
@@ -17,13 +18,14 @@ import socket
|
|
|
17
18
|
import struct
|
|
18
19
|
from abc import ABC, abstractmethod
|
|
19
20
|
from pprint import pformat as pf
|
|
20
|
-
from typing import Dict, Generator, Optional, Union
|
|
21
|
+
from typing import Dict, Generator, Optional, Tuple, Union
|
|
21
22
|
|
|
22
23
|
# When support for cpython older than 3.11 is dropped
|
|
23
24
|
# async_timeout can be replaced with asyncio.timeout
|
|
24
25
|
from async_timeout import timeout as asyncio_timeout
|
|
25
26
|
from cryptography.hazmat.primitives import hashes
|
|
26
27
|
|
|
28
|
+
from .credentials import Credentials
|
|
27
29
|
from .deviceconfig import DeviceConfig
|
|
28
30
|
from .exceptions import SmartDeviceException
|
|
29
31
|
from .json import dumps as json_dumps
|
|
@@ -78,8 +80,12 @@ class BaseTransport(ABC):
|
|
|
78
80
|
async def close(self) -> None:
|
|
79
81
|
"""Close the transport. Abstract method to be overriden."""
|
|
80
82
|
|
|
83
|
+
@abstractmethod
|
|
84
|
+
async def reset(self) -> None:
|
|
85
|
+
"""Reset internal state."""
|
|
86
|
+
|
|
81
87
|
|
|
82
|
-
class
|
|
88
|
+
class BaseProtocol(ABC):
|
|
83
89
|
"""Base class for all TP-Link Smart Home communication."""
|
|
84
90
|
|
|
85
91
|
def __init__(
|
|
@@ -137,10 +143,13 @@ class _XorTransport(BaseTransport):
|
|
|
137
143
|
return {}
|
|
138
144
|
|
|
139
145
|
async def close(self) -> None:
|
|
140
|
-
"""Close the transport.
|
|
146
|
+
"""Close the transport."""
|
|
141
147
|
|
|
148
|
+
async def reset(self) -> None:
|
|
149
|
+
"""Reset internal state.."""
|
|
142
150
|
|
|
143
|
-
|
|
151
|
+
|
|
152
|
+
class TPLinkSmartHomeProtocol(BaseProtocol):
|
|
144
153
|
"""Implementation of the TP-Link Smart Home protocol."""
|
|
145
154
|
|
|
146
155
|
INITIALIZATION_VECTOR = 171
|
|
@@ -168,7 +177,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
168
177
|
|
|
169
178
|
:param str host: host name or ip address of the device
|
|
170
179
|
:param request: command to send to the device (can be either dict or
|
|
171
|
-
|
|
180
|
+
json string)
|
|
172
181
|
:param retry_count: how many retries to do in case of failure
|
|
173
182
|
:return: response dict
|
|
174
183
|
"""
|
|
@@ -200,7 +209,6 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
200
209
|
assert self.writer is not None # noqa: S101
|
|
201
210
|
assert self.reader is not None # noqa: S101
|
|
202
211
|
debug_log = _LOGGER.isEnabledFor(logging.DEBUG)
|
|
203
|
-
|
|
204
212
|
if debug_log:
|
|
205
213
|
_LOGGER.debug("%s >> %s", self._host, request)
|
|
206
214
|
self.writer.write(TPLinkSmartHomeProtocol.encrypt(request))
|
|
@@ -220,15 +228,21 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
220
228
|
async def close(self) -> None:
|
|
221
229
|
"""Close the connection."""
|
|
222
230
|
writer = self.writer
|
|
223
|
-
self.
|
|
231
|
+
self.close_without_wait()
|
|
224
232
|
if writer:
|
|
225
|
-
writer.close()
|
|
226
233
|
with contextlib.suppress(Exception):
|
|
227
234
|
await writer.wait_closed()
|
|
228
235
|
|
|
229
|
-
def
|
|
230
|
-
"""
|
|
236
|
+
def close_without_wait(self) -> None:
|
|
237
|
+
"""Close the connection without waiting for the connection to close."""
|
|
238
|
+
writer = self.writer
|
|
231
239
|
self.reader = self.writer = None
|
|
240
|
+
if writer:
|
|
241
|
+
writer.close()
|
|
242
|
+
|
|
243
|
+
async def reset(self) -> None:
|
|
244
|
+
"""Reset the transport."""
|
|
245
|
+
await self.close()
|
|
232
246
|
|
|
233
247
|
async def _query(self, request: str, retry_count: int, timeout: int) -> Dict:
|
|
234
248
|
"""Try to query a device."""
|
|
@@ -245,12 +259,12 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
245
259
|
try:
|
|
246
260
|
await self._connect(timeout)
|
|
247
261
|
except ConnectionRefusedError as ex:
|
|
248
|
-
await self.
|
|
262
|
+
await self.reset()
|
|
249
263
|
raise SmartDeviceException(
|
|
250
264
|
f"Unable to connect to the device: {self._host}:{self._port}: {ex}"
|
|
251
265
|
) from ex
|
|
252
266
|
except OSError as ex:
|
|
253
|
-
await self.
|
|
267
|
+
await self.reset()
|
|
254
268
|
if ex.errno in _NO_RETRY_ERRORS or retry >= retry_count:
|
|
255
269
|
raise SmartDeviceException(
|
|
256
270
|
f"Unable to connect to the device:"
|
|
@@ -258,7 +272,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
258
272
|
) from ex
|
|
259
273
|
continue
|
|
260
274
|
except Exception as ex:
|
|
261
|
-
await self.
|
|
275
|
+
await self.reset()
|
|
262
276
|
if retry >= retry_count:
|
|
263
277
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
264
278
|
raise SmartDeviceException(
|
|
@@ -266,6 +280,16 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
266
280
|
f" {self._host}:{self._port}: {ex}"
|
|
267
281
|
) from ex
|
|
268
282
|
continue
|
|
283
|
+
except BaseException as ex:
|
|
284
|
+
# Likely something cancelled the task so we need to close the connection
|
|
285
|
+
# as we are not in an indeterminate state
|
|
286
|
+
self.close_without_wait()
|
|
287
|
+
_LOGGER.debug(
|
|
288
|
+
"%s: BaseException during connect, closing connection: %s",
|
|
289
|
+
self._host,
|
|
290
|
+
ex,
|
|
291
|
+
)
|
|
292
|
+
raise
|
|
269
293
|
|
|
270
294
|
try:
|
|
271
295
|
assert self.reader is not None # noqa: S101
|
|
@@ -273,7 +297,7 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
273
297
|
async with asyncio_timeout(timeout):
|
|
274
298
|
return await self._execute_query(request)
|
|
275
299
|
except Exception as ex:
|
|
276
|
-
await self.
|
|
300
|
+
await self.reset()
|
|
277
301
|
if retry >= retry_count:
|
|
278
302
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
279
303
|
raise SmartDeviceException(
|
|
@@ -283,9 +307,19 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
283
307
|
_LOGGER.debug(
|
|
284
308
|
"Unable to query the device %s, retrying: %s", self._host, ex
|
|
285
309
|
)
|
|
310
|
+
except BaseException as ex:
|
|
311
|
+
# Likely something cancelled the task so we need to close the connection
|
|
312
|
+
# as we are not in an indeterminate state
|
|
313
|
+
self.close_without_wait()
|
|
314
|
+
_LOGGER.debug(
|
|
315
|
+
"%s: BaseException during query, closing connection: %s",
|
|
316
|
+
self._host,
|
|
317
|
+
ex,
|
|
318
|
+
)
|
|
319
|
+
raise
|
|
286
320
|
|
|
287
321
|
# make mypy happy, this should never be reached..
|
|
288
|
-
await self.
|
|
322
|
+
await self.reset()
|
|
289
323
|
raise SmartDeviceException("Query reached somehow to unreachable")
|
|
290
324
|
|
|
291
325
|
def __del__(self) -> None:
|
|
@@ -295,7 +329,6 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
295
329
|
# or in another thread so we need to make sure the call to
|
|
296
330
|
# close is called safely with call_soon_threadsafe
|
|
297
331
|
self.loop.call_soon_threadsafe(self.writer.close)
|
|
298
|
-
self._reset()
|
|
299
332
|
|
|
300
333
|
@staticmethod
|
|
301
334
|
def _xor_payload(unencrypted: bytes) -> Generator[int, None, None]:
|
|
@@ -336,6 +369,18 @@ class TPLinkSmartHomeProtocol(TPLinkProtocol):
|
|
|
336
369
|
).decode()
|
|
337
370
|
|
|
338
371
|
|
|
372
|
+
def get_default_credentials(tuple: Tuple[str, str]) -> Credentials:
|
|
373
|
+
"""Return decoded default credentials."""
|
|
374
|
+
un = base64.b64decode(tuple[0].encode()).decode()
|
|
375
|
+
pw = base64.b64decode(tuple[1].encode()).decode()
|
|
376
|
+
return Credentials(un, pw)
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
DEFAULT_CREDENTIALS = {
|
|
380
|
+
"KASA": ("a2FzYUB0cC1saW5rLm5ldA==", "a2FzYVNldHVw"),
|
|
381
|
+
"TAPO": ("dGVzdEB0cC1saW5rLm5ldA==", "dGVzdA=="),
|
|
382
|
+
}
|
|
383
|
+
|
|
339
384
|
# Try to load the kasa_crypt module and if it is available
|
|
340
385
|
try:
|
|
341
386
|
from kasa_crypt import decrypt, encrypt
|
kasa/smartbulb.py
CHANGED
|
@@ -11,7 +11,7 @@ except ImportError:
|
|
|
11
11
|
|
|
12
12
|
from .deviceconfig import DeviceConfig
|
|
13
13
|
from .modules import Antitheft, Cloud, Countdown, Emeter, Schedule, Time, Usage
|
|
14
|
-
from .protocol import
|
|
14
|
+
from .protocol import BaseProtocol
|
|
15
15
|
from .smartdevice import DeviceType, SmartDevice, SmartDeviceException, requires_update
|
|
16
16
|
|
|
17
17
|
|
|
@@ -222,7 +222,7 @@ class SmartBulb(SmartDevice):
|
|
|
222
222
|
host: str,
|
|
223
223
|
*,
|
|
224
224
|
config: Optional[DeviceConfig] = None,
|
|
225
|
-
protocol: Optional[
|
|
225
|
+
protocol: Optional[BaseProtocol] = None,
|
|
226
226
|
) -> None:
|
|
227
227
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
228
228
|
self._device_type = DeviceType.Bulb
|
kasa/smartdevice.py
CHANGED
|
@@ -25,7 +25,7 @@ from .deviceconfig import DeviceConfig
|
|
|
25
25
|
from .emeterstatus import EmeterStatus
|
|
26
26
|
from .exceptions import SmartDeviceException
|
|
27
27
|
from .modules import Emeter, Module
|
|
28
|
-
from .protocol import
|
|
28
|
+
from .protocol import BaseProtocol, TPLinkSmartHomeProtocol, _XorTransport
|
|
29
29
|
|
|
30
30
|
_LOGGER = logging.getLogger(__name__)
|
|
31
31
|
|
|
@@ -125,7 +125,7 @@ class SmartDevice:
|
|
|
125
125
|
>>> dev.rssi
|
|
126
126
|
-71
|
|
127
127
|
>>> dev.mac
|
|
128
|
-
50:C7:BF:
|
|
128
|
+
50:C7:BF:00:00:00
|
|
129
129
|
|
|
130
130
|
Some information can also be changed programmatically:
|
|
131
131
|
|
|
@@ -155,9 +155,9 @@ class SmartDevice:
|
|
|
155
155
|
'hw_ver': '1.0',
|
|
156
156
|
'mac': '01:23:45:67:89:ab',
|
|
157
157
|
'type': 'IOT.SMARTPLUGSWITCH',
|
|
158
|
-
'hwId': '
|
|
158
|
+
'hwId': '00000000000000000000000000000000',
|
|
159
159
|
'fwId': '00000000000000000000000000000000',
|
|
160
|
-
'oemId': '
|
|
160
|
+
'oemId': '00000000000000000000000000000000',
|
|
161
161
|
'dev_name': 'Wi-Fi Smart Plug With Energy Monitoring'}
|
|
162
162
|
>>> dev.sys_info
|
|
163
163
|
|
|
@@ -175,7 +175,7 @@ class SmartDevice:
|
|
|
175
175
|
>>> dev.has_emeter
|
|
176
176
|
True
|
|
177
177
|
>>> dev.emeter_realtime
|
|
178
|
-
<EmeterStatus power=0.
|
|
178
|
+
<EmeterStatus power=0.928511 voltage=231.067823 current=0.014937 total=55.139>
|
|
179
179
|
>>> dev.emeter_today
|
|
180
180
|
>>> dev.emeter_this_month
|
|
181
181
|
|
|
@@ -196,7 +196,7 @@ class SmartDevice:
|
|
|
196
196
|
host: str,
|
|
197
197
|
*,
|
|
198
198
|
config: Optional[DeviceConfig] = None,
|
|
199
|
-
protocol: Optional[
|
|
199
|
+
protocol: Optional[BaseProtocol] = None,
|
|
200
200
|
) -> None:
|
|
201
201
|
"""Create a new SmartDevice instance.
|
|
202
202
|
|
|
@@ -204,7 +204,7 @@ class SmartDevice:
|
|
|
204
204
|
"""
|
|
205
205
|
if config and protocol:
|
|
206
206
|
protocol._transport._config = config
|
|
207
|
-
self.protocol:
|
|
207
|
+
self.protocol: BaseProtocol = protocol or TPLinkSmartHomeProtocol(
|
|
208
208
|
transport=_XorTransport(config=config or DeviceConfig(host=host)),
|
|
209
209
|
)
|
|
210
210
|
_LOGGER.debug("Initializing %s of type %s", self.host, type(self))
|
|
@@ -806,6 +806,10 @@ class SmartDevice:
|
|
|
806
806
|
"""Return the device configuration."""
|
|
807
807
|
return self.protocol.config
|
|
808
808
|
|
|
809
|
+
async def disconnect(self):
|
|
810
|
+
"""Disconnect and close any underlying connection resources."""
|
|
811
|
+
await self.protocol.close()
|
|
812
|
+
|
|
809
813
|
@staticmethod
|
|
810
814
|
async def connect(
|
|
811
815
|
*,
|
kasa/smartdimmer.py
CHANGED
|
@@ -4,7 +4,7 @@ from typing import Any, Dict, Optional
|
|
|
4
4
|
|
|
5
5
|
from kasa.deviceconfig import DeviceConfig
|
|
6
6
|
from kasa.modules import AmbientLight, Motion
|
|
7
|
-
from kasa.protocol import
|
|
7
|
+
from kasa.protocol import BaseProtocol
|
|
8
8
|
from kasa.smartdevice import DeviceType, SmartDeviceException, requires_update
|
|
9
9
|
from kasa.smartplug import SmartPlug
|
|
10
10
|
|
|
@@ -70,7 +70,7 @@ class SmartDimmer(SmartPlug):
|
|
|
70
70
|
host: str,
|
|
71
71
|
*,
|
|
72
72
|
config: Optional[DeviceConfig] = None,
|
|
73
|
-
protocol: Optional[
|
|
73
|
+
protocol: Optional[BaseProtocol] = None,
|
|
74
74
|
) -> None:
|
|
75
75
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
76
76
|
self._device_type = DeviceType.Dimmer
|
kasa/smartlightstrip.py
CHANGED
|
@@ -3,7 +3,7 @@ from typing import Any, Dict, List, Optional
|
|
|
3
3
|
|
|
4
4
|
from .deviceconfig import DeviceConfig
|
|
5
5
|
from .effects import EFFECT_MAPPING_V1, EFFECT_NAMES_V1
|
|
6
|
-
from .protocol import
|
|
6
|
+
from .protocol import BaseProtocol
|
|
7
7
|
from .smartbulb import SmartBulb
|
|
8
8
|
from .smartdevice import DeviceType, SmartDeviceException, requires_update
|
|
9
9
|
|
|
@@ -48,7 +48,7 @@ class SmartLightStrip(SmartBulb):
|
|
|
48
48
|
host: str,
|
|
49
49
|
*,
|
|
50
50
|
config: Optional[DeviceConfig] = None,
|
|
51
|
-
protocol: Optional[
|
|
51
|
+
protocol: Optional[BaseProtocol] = None,
|
|
52
52
|
) -> None:
|
|
53
53
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
54
54
|
self._device_type = DeviceType.LightStrip
|
kasa/smartplug.py
CHANGED
|
@@ -4,7 +4,7 @@ from typing import Any, Dict, Optional
|
|
|
4
4
|
|
|
5
5
|
from kasa.deviceconfig import DeviceConfig
|
|
6
6
|
from kasa.modules import Antitheft, Cloud, Schedule, Time, Usage
|
|
7
|
-
from kasa.protocol import
|
|
7
|
+
from kasa.protocol import BaseProtocol
|
|
8
8
|
from kasa.smartdevice import DeviceType, SmartDevice, requires_update
|
|
9
9
|
|
|
10
10
|
_LOGGER = logging.getLogger(__name__)
|
|
@@ -45,7 +45,7 @@ class SmartPlug(SmartDevice):
|
|
|
45
45
|
host: str,
|
|
46
46
|
*,
|
|
47
47
|
config: Optional[DeviceConfig] = None,
|
|
48
|
-
protocol: Optional[
|
|
48
|
+
protocol: Optional[BaseProtocol] = None,
|
|
49
49
|
) -> None:
|
|
50
50
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
51
51
|
self._device_type = DeviceType.Plug
|
kasa/smartprotocol.py
CHANGED
|
@@ -12,29 +12,27 @@ import uuid
|
|
|
12
12
|
from pprint import pformat as pf
|
|
13
13
|
from typing import Dict, Union
|
|
14
14
|
|
|
15
|
-
import httpx
|
|
16
|
-
|
|
17
15
|
from .exceptions import (
|
|
18
16
|
SMART_AUTHENTICATION_ERRORS,
|
|
19
17
|
SMART_RETRYABLE_ERRORS,
|
|
20
18
|
SMART_TIMEOUT_ERRORS,
|
|
21
19
|
AuthenticationException,
|
|
20
|
+
ConnectionException,
|
|
22
21
|
RetryableException,
|
|
23
22
|
SmartDeviceException,
|
|
24
23
|
SmartErrorCode,
|
|
25
24
|
TimeoutException,
|
|
26
25
|
)
|
|
27
26
|
from .json import dumps as json_dumps
|
|
28
|
-
from .protocol import
|
|
27
|
+
from .protocol import BaseProtocol, BaseTransport, md5
|
|
29
28
|
|
|
30
29
|
_LOGGER = logging.getLogger(__name__)
|
|
31
|
-
logging.getLogger("httpx").propagate = False
|
|
32
30
|
|
|
33
31
|
|
|
34
|
-
class SmartProtocol(
|
|
32
|
+
class SmartProtocol(BaseProtocol):
|
|
35
33
|
"""Class for the new TPLink SMART protocol."""
|
|
36
34
|
|
|
37
|
-
|
|
35
|
+
BACKOFF_SECONDS_AFTER_TIMEOUT = 1
|
|
38
36
|
|
|
39
37
|
def __init__(
|
|
40
38
|
self,
|
|
@@ -67,58 +65,38 @@ class SmartProtocol(TPLinkProtocol):
|
|
|
67
65
|
for retry in range(retry_count + 1):
|
|
68
66
|
try:
|
|
69
67
|
return await self._execute_query(request, retry)
|
|
70
|
-
except
|
|
68
|
+
except ConnectionException as sdex:
|
|
71
69
|
if retry >= retry_count:
|
|
72
|
-
await self.close()
|
|
73
70
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
74
|
-
raise
|
|
75
|
-
f"Unable to connect to the device: {self._host}: {sdex}"
|
|
76
|
-
) from sdex
|
|
77
|
-
continue
|
|
78
|
-
except TimeoutError as tex:
|
|
79
|
-
if retry >= retry_count:
|
|
80
|
-
await self.close()
|
|
81
|
-
raise SmartDeviceException(
|
|
82
|
-
"Unable to connect to the device, "
|
|
83
|
-
+ f"timed out: {self._host}: {tex}"
|
|
84
|
-
) from tex
|
|
85
|
-
await asyncio.sleep(self.SLEEP_SECONDS_AFTER_TIMEOUT)
|
|
71
|
+
raise sdex
|
|
86
72
|
continue
|
|
87
73
|
except AuthenticationException as auex:
|
|
88
|
-
await self.
|
|
74
|
+
await self._transport.reset()
|
|
89
75
|
_LOGGER.debug(
|
|
90
76
|
"Unable to authenticate with %s, not retrying", self._host
|
|
91
77
|
)
|
|
92
78
|
raise auex
|
|
93
79
|
except RetryableException as ex:
|
|
80
|
+
await self._transport.reset()
|
|
94
81
|
if retry >= retry_count:
|
|
95
|
-
await self.close()
|
|
96
82
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
97
83
|
raise ex
|
|
98
84
|
continue
|
|
99
85
|
except TimeoutException as ex:
|
|
86
|
+
await self._transport.reset()
|
|
100
87
|
if retry >= retry_count:
|
|
101
|
-
await self.close()
|
|
102
88
|
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
103
89
|
raise ex
|
|
104
|
-
await asyncio.sleep(self.
|
|
90
|
+
await asyncio.sleep(self.BACKOFF_SECONDS_AFTER_TIMEOUT)
|
|
105
91
|
continue
|
|
106
92
|
except SmartDeviceException as ex:
|
|
107
|
-
|
|
108
|
-
await self.close()
|
|
109
|
-
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
110
|
-
raise ex
|
|
111
|
-
except Exception as ex:
|
|
112
|
-
if retry >= retry_count:
|
|
113
|
-
await self.close()
|
|
114
|
-
_LOGGER.debug("Giving up on %s after %s retries", self._host, retry)
|
|
115
|
-
raise SmartDeviceException(
|
|
116
|
-
f"Unable to connect to the device: {self._host}: {ex}"
|
|
117
|
-
) from ex
|
|
93
|
+
await self._transport.reset()
|
|
118
94
|
_LOGGER.debug(
|
|
119
|
-
"Unable to query the device %s, retrying: %s",
|
|
95
|
+
"Unable to query the device: %s, not retrying: %s",
|
|
96
|
+
self._host,
|
|
97
|
+
ex,
|
|
120
98
|
)
|
|
121
|
-
|
|
99
|
+
raise ex
|
|
122
100
|
|
|
123
101
|
# make mypy happy, this should never be reached..
|
|
124
102
|
raise SmartDeviceException("Query reached somehow to unreachable")
|
|
@@ -188,7 +166,7 @@ class SmartProtocol(TPLinkProtocol):
|
|
|
188
166
|
raise SmartDeviceException(msg, error_code=error_code)
|
|
189
167
|
|
|
190
168
|
async def close(self) -> None:
|
|
191
|
-
"""Close the
|
|
169
|
+
"""Close the underlying transport."""
|
|
192
170
|
await self._transport.close()
|
|
193
171
|
|
|
194
172
|
|
kasa/smartstrip.py
CHANGED
|
@@ -16,7 +16,7 @@ from kasa.smartplug import SmartPlug
|
|
|
16
16
|
|
|
17
17
|
from .deviceconfig import DeviceConfig
|
|
18
18
|
from .modules import Antitheft, Countdown, Emeter, Schedule, Time, Usage
|
|
19
|
-
from .protocol import
|
|
19
|
+
from .protocol import BaseProtocol
|
|
20
20
|
|
|
21
21
|
_LOGGER = logging.getLogger(__name__)
|
|
22
22
|
|
|
@@ -87,7 +87,7 @@ class SmartStrip(SmartDevice):
|
|
|
87
87
|
host: str,
|
|
88
88
|
*,
|
|
89
89
|
config: Optional[DeviceConfig] = None,
|
|
90
|
-
protocol: Optional[
|
|
90
|
+
protocol: Optional[BaseProtocol] = None,
|
|
91
91
|
) -> None:
|
|
92
92
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
93
93
|
self.emeter_type = "emeter"
|
kasa/tapo/tapodevice.py
CHANGED
|
@@ -9,7 +9,7 @@ from ..deviceconfig import DeviceConfig
|
|
|
9
9
|
from ..emeterstatus import EmeterStatus
|
|
10
10
|
from ..exceptions import AuthenticationException, SmartDeviceException
|
|
11
11
|
from ..modules import Emeter
|
|
12
|
-
from ..protocol import
|
|
12
|
+
from ..protocol import BaseProtocol
|
|
13
13
|
from ..smartdevice import SmartDevice, WifiNetwork
|
|
14
14
|
from ..smartprotocol import SmartProtocol
|
|
15
15
|
|
|
@@ -24,7 +24,7 @@ class TapoDevice(SmartDevice):
|
|
|
24
24
|
host: str,
|
|
25
25
|
*,
|
|
26
26
|
config: Optional[DeviceConfig] = None,
|
|
27
|
-
protocol: Optional[
|
|
27
|
+
protocol: Optional[BaseProtocol] = None,
|
|
28
28
|
) -> None:
|
|
29
29
|
_protocol = protocol or SmartProtocol(
|
|
30
30
|
transport=AesTransport(config=config or DeviceConfig(host=host)),
|
|
@@ -147,8 +147,8 @@ class TapoDevice(SmartDevice):
|
|
|
147
147
|
def location(self) -> Dict:
|
|
148
148
|
"""Return the device location."""
|
|
149
149
|
loc = {
|
|
150
|
-
"latitude": cast(float, self._info.get("latitude")) / 10_000,
|
|
151
|
-
"longitude": cast(float, self._info.get("longitude")) / 10_000,
|
|
150
|
+
"latitude": cast(float, self._info.get("latitude", 0)) / 10_000,
|
|
151
|
+
"longitude": cast(float, self._info.get("longitude", 0)) / 10_000,
|
|
152
152
|
}
|
|
153
153
|
return loc
|
|
154
154
|
|
|
@@ -339,3 +339,18 @@ class TapoDevice(SmartDevice):
|
|
|
339
339
|
"time": t,
|
|
340
340
|
}
|
|
341
341
|
return await self.protocol.query({"set_qs_info": payload})
|
|
342
|
+
|
|
343
|
+
async def reboot(self, delay: int = 1) -> None:
|
|
344
|
+
"""Reboot the device.
|
|
345
|
+
|
|
346
|
+
Note that giving a delay of zero causes this to block,
|
|
347
|
+
as the device reboots immediately without responding to the call.
|
|
348
|
+
"""
|
|
349
|
+
await self.protocol.query({"device_reboot": {"delay": delay}})
|
|
350
|
+
|
|
351
|
+
async def factory_reset(self) -> None:
|
|
352
|
+
"""Reset device back to factory settings.
|
|
353
|
+
|
|
354
|
+
Note, this does not downgrade the firmware.
|
|
355
|
+
"""
|
|
356
|
+
await self.protocol.query("device_reset")
|
kasa/tapo/tapoplug.py
CHANGED
|
@@ -4,7 +4,7 @@ from datetime import datetime, timedelta
|
|
|
4
4
|
from typing import Any, Dict, Optional, cast
|
|
5
5
|
|
|
6
6
|
from ..deviceconfig import DeviceConfig
|
|
7
|
-
from ..protocol import
|
|
7
|
+
from ..protocol import BaseProtocol
|
|
8
8
|
from ..smartdevice import DeviceType
|
|
9
9
|
from .tapodevice import TapoDevice
|
|
10
10
|
|
|
@@ -19,7 +19,7 @@ class TapoPlug(TapoDevice):
|
|
|
19
19
|
host: str,
|
|
20
20
|
*,
|
|
21
21
|
config: Optional[DeviceConfig] = None,
|
|
22
|
-
protocol: Optional[
|
|
22
|
+
protocol: Optional[BaseProtocol] = None,
|
|
23
23
|
) -> None:
|
|
24
24
|
super().__init__(host=host, config=config, protocol=protocol)
|
|
25
25
|
self._device_type = DeviceType.Plug
|
kasa/tests/conftest.py
CHANGED
|
@@ -15,6 +15,7 @@ from kasa import (
|
|
|
15
15
|
Credentials,
|
|
16
16
|
Discover,
|
|
17
17
|
SmartBulb,
|
|
18
|
+
SmartDevice,
|
|
18
19
|
SmartDimmer,
|
|
19
20
|
SmartLightStrip,
|
|
20
21
|
SmartPlug,
|
|
@@ -43,10 +44,10 @@ SUPPORTED_SMART_DEVICES = [
|
|
|
43
44
|
SUPPORTED_DEVICES = SUPPORTED_IOT_DEVICES + SUPPORTED_SMART_DEVICES
|
|
44
45
|
|
|
45
46
|
# Tapo bulbs
|
|
46
|
-
BULBS_SMART_VARIABLE_TEMP = {"L530E"}
|
|
47
|
-
BULBS_SMART_LIGHT_STRIP = {"L900-5", "L900-10"}
|
|
47
|
+
BULBS_SMART_VARIABLE_TEMP = {"L530E", "L930-5"}
|
|
48
|
+
BULBS_SMART_LIGHT_STRIP = {"L900-5", "L900-10", "L920-5", "L930-5"}
|
|
48
49
|
BULBS_SMART_COLOR = {"L530E", *BULBS_SMART_LIGHT_STRIP}
|
|
49
|
-
BULBS_SMART_DIMMABLE = {"KS225"}
|
|
50
|
+
BULBS_SMART_DIMMABLE = {"KS225", "L510B", "L510E"}
|
|
50
51
|
BULBS_SMART = (
|
|
51
52
|
BULBS_SMART_VARIABLE_TEMP.union(BULBS_SMART_COLOR)
|
|
52
53
|
.union(BULBS_SMART_DIMMABLE)
|
|
@@ -98,17 +99,19 @@ PLUGS_IOT = {
|
|
|
98
99
|
"KP401",
|
|
99
100
|
"KS200M",
|
|
100
101
|
}
|
|
101
|
-
|
|
102
|
+
# P135 supports dimming, but its not currently support
|
|
103
|
+
# by the library
|
|
104
|
+
PLUGS_SMART = {"P100", "P110", "KP125M", "EP25", "KS205", "P125M", "P135", "S505"}
|
|
102
105
|
PLUGS = {
|
|
103
106
|
*PLUGS_IOT,
|
|
104
107
|
*PLUGS_SMART,
|
|
105
108
|
}
|
|
106
109
|
STRIPS_IOT = {"HS107", "HS300", "KP303", "KP200", "KP400", "EP40"}
|
|
107
|
-
STRIPS_SMART
|
|
110
|
+
STRIPS_SMART: Set[str] = set()
|
|
108
111
|
STRIPS = {*STRIPS_IOT, *STRIPS_SMART}
|
|
109
112
|
|
|
110
113
|
DIMMERS_IOT = {"ES20M", "HS220", "KS220M", "KS230", "KP405"}
|
|
111
|
-
DIMMERS_SMART = {}
|
|
114
|
+
DIMMERS_SMART = {"S500D"}
|
|
112
115
|
DIMMERS = {
|
|
113
116
|
*DIMMERS_IOT,
|
|
114
117
|
*DIMMERS_SMART,
|
|
@@ -237,6 +240,9 @@ bulb_iot = parametrize("bulb devices iot", BULBS_IOT, protocol_filter={"IOT"})
|
|
|
237
240
|
|
|
238
241
|
plug_smart = parametrize("plug devices smart", PLUGS_SMART, protocol_filter={"SMART"})
|
|
239
242
|
bulb_smart = parametrize("bulb devices smart", BULBS_SMART, protocol_filter={"SMART"})
|
|
243
|
+
dimmers_smart = parametrize(
|
|
244
|
+
"dimmer devices smart", DIMMERS_SMART, protocol_filter={"SMART"}
|
|
245
|
+
)
|
|
240
246
|
device_smart = parametrize(
|
|
241
247
|
"devices smart", ALL_DEVICES_SMART, protocol_filter={"SMART"}
|
|
242
248
|
)
|
|
@@ -297,6 +303,7 @@ def check_categories():
|
|
|
297
303
|
+ lightstrip.args[1]
|
|
298
304
|
+ plug_smart.args[1]
|
|
299
305
|
+ bulb_smart.args[1]
|
|
306
|
+
+ dimmers_smart.args[1]
|
|
300
307
|
)
|
|
301
308
|
diff = set(SUPPORTED_DEVICES) - set(categorized_fixtures)
|
|
302
309
|
if diff:
|
|
@@ -328,6 +335,9 @@ def device_for_file(model, protocol):
|
|
|
328
335
|
for d in BULBS_SMART:
|
|
329
336
|
if d in model:
|
|
330
337
|
return TapoBulb
|
|
338
|
+
for d in DIMMERS_SMART:
|
|
339
|
+
if d in model:
|
|
340
|
+
return TapoBulb
|
|
331
341
|
else:
|
|
332
342
|
for d in STRIPS_IOT:
|
|
333
343
|
if d in model:
|
|
@@ -414,9 +424,15 @@ async def dev(request):
|
|
|
414
424
|
IP_MODEL_CACHE[ip] = model = d.model
|
|
415
425
|
if model not in file:
|
|
416
426
|
pytest.skip(f"skipping file {file}")
|
|
417
|
-
|
|
427
|
+
dev: SmartDevice = (
|
|
428
|
+
d if d else await _discover_update_and_close(ip, username, password)
|
|
429
|
+
)
|
|
430
|
+
else:
|
|
431
|
+
dev: SmartDevice = await get_device_for_file(file, protocol)
|
|
432
|
+
|
|
433
|
+
yield dev
|
|
418
434
|
|
|
419
|
-
|
|
435
|
+
await dev.disconnect()
|
|
420
436
|
|
|
421
437
|
|
|
422
438
|
@pytest.fixture
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"system": {
|
|
3
|
+
"get_sysinfo": {
|
|
4
|
+
"active_mode": "schedule",
|
|
5
|
+
"alias": "#MASKED_NAME#",
|
|
6
|
+
"dev_name": "Wi-Fi Smart Plug",
|
|
7
|
+
"deviceId": "0000000000000000000000000000000000000000",
|
|
8
|
+
"err_code": 0,
|
|
9
|
+
"feature": "TIM",
|
|
10
|
+
"fwId": "00000000000000000000000000000000",
|
|
11
|
+
"hwId": "00000000000000000000000000000000",
|
|
12
|
+
"hw_ver": "1.0",
|
|
13
|
+
"icon_hash": "",
|
|
14
|
+
"latitude": 0,
|
|
15
|
+
"led_off": 0,
|
|
16
|
+
"longitude": 0,
|
|
17
|
+
"mac": "70:4F:57:00:00:00",
|
|
18
|
+
"model": "HS100(UK)",
|
|
19
|
+
"oemId": "00000000000000000000000000000000",
|
|
20
|
+
"on_time": 0,
|
|
21
|
+
"relay_state": 0,
|
|
22
|
+
"rssi": -59,
|
|
23
|
+
"sw_ver": "1.2.6 Build 200727 Rel.120236",
|
|
24
|
+
"type": "IOT.SMARTPLUGSWITCH",
|
|
25
|
+
"updating": 0
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"factory_default": true,
|
|
7
7
|
"hw_ver": "4.1",
|
|
8
8
|
"ip": "127.0.0.123",
|
|
9
|
-
"mac": "
|
|
9
|
+
"mac": "CC-32-E5-00-00-00",
|
|
10
10
|
"mgt_encrypt_schm": {
|
|
11
11
|
"encrypt_type": "KLAP",
|
|
12
12
|
"http_port": 80,
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"latitude_i": 0,
|
|
29
29
|
"led_off": 0,
|
|
30
30
|
"longitude_i": 0,
|
|
31
|
-
"mac": "
|
|
31
|
+
"mac": "CC:32:E5:00:00:00",
|
|
32
32
|
"mic_type": "IOT.SMARTPLUGSWITCH",
|
|
33
33
|
"model": "HS100(UK)",
|
|
34
34
|
"next_action": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"oemId": "00000000000000000000000000000000",
|
|
39
39
|
"on_time": 0,
|
|
40
40
|
"relay_state": 0,
|
|
41
|
-
"rssi": -
|
|
41
|
+
"rssi": -43,
|
|
42
42
|
"status": "new",
|
|
43
43
|
"sw_ver": "1.1.0 Build 201016 Rel.175121",
|
|
44
44
|
"updating": 0
|