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
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
import re
|
|
3
|
+
|
|
4
|
+
import aiohttp
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
from ..deviceconfig import DeviceConfig
|
|
8
|
+
from ..exceptions import (
|
|
9
|
+
ConnectionException,
|
|
10
|
+
SmartDeviceException,
|
|
11
|
+
TimeoutException,
|
|
12
|
+
)
|
|
13
|
+
from ..httpclient import HttpClient
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@pytest.mark.parametrize(
|
|
17
|
+
"error, error_raises, error_message",
|
|
18
|
+
[
|
|
19
|
+
(
|
|
20
|
+
aiohttp.ServerDisconnectedError(),
|
|
21
|
+
ConnectionException,
|
|
22
|
+
"Device connection error: ",
|
|
23
|
+
),
|
|
24
|
+
(
|
|
25
|
+
aiohttp.ClientOSError(),
|
|
26
|
+
ConnectionException,
|
|
27
|
+
"Device connection error: ",
|
|
28
|
+
),
|
|
29
|
+
(
|
|
30
|
+
aiohttp.ServerTimeoutError(),
|
|
31
|
+
TimeoutException,
|
|
32
|
+
"Unable to query the device, timed out: ",
|
|
33
|
+
),
|
|
34
|
+
(
|
|
35
|
+
asyncio.TimeoutError(),
|
|
36
|
+
TimeoutException,
|
|
37
|
+
"Unable to query the device, timed out: ",
|
|
38
|
+
),
|
|
39
|
+
(Exception(), SmartDeviceException, "Unable to query the device: "),
|
|
40
|
+
(
|
|
41
|
+
aiohttp.ServerFingerprintMismatch("exp", "got", "host", 1),
|
|
42
|
+
SmartDeviceException,
|
|
43
|
+
"Unable to query the device: ",
|
|
44
|
+
),
|
|
45
|
+
],
|
|
46
|
+
ids=(
|
|
47
|
+
"ServerDisconnectedError",
|
|
48
|
+
"ClientOSError",
|
|
49
|
+
"ServerTimeoutError",
|
|
50
|
+
"TimeoutError",
|
|
51
|
+
"Exception",
|
|
52
|
+
"ServerFingerprintMismatch",
|
|
53
|
+
),
|
|
54
|
+
)
|
|
55
|
+
@pytest.mark.parametrize("mock_read", (False, True), ids=("post", "read"))
|
|
56
|
+
async def test_httpclient_errors(mocker, error, error_raises, error_message, mock_read):
|
|
57
|
+
class _mock_response:
|
|
58
|
+
def __init__(self, status, error):
|
|
59
|
+
self.status = status
|
|
60
|
+
self.error = error
|
|
61
|
+
self.call_count = 0
|
|
62
|
+
|
|
63
|
+
async def __aenter__(self):
|
|
64
|
+
return self
|
|
65
|
+
|
|
66
|
+
async def __aexit__(self, exc_t, exc_v, exc_tb):
|
|
67
|
+
pass
|
|
68
|
+
|
|
69
|
+
async def read(self):
|
|
70
|
+
self.call_count += 1
|
|
71
|
+
raise self.error
|
|
72
|
+
|
|
73
|
+
mock_response = _mock_response(200, error)
|
|
74
|
+
|
|
75
|
+
async def _post(url, *_, **__):
|
|
76
|
+
nonlocal mock_response
|
|
77
|
+
return mock_response
|
|
78
|
+
|
|
79
|
+
host = "127.0.0.1"
|
|
80
|
+
|
|
81
|
+
side_effect = _post if mock_read else error
|
|
82
|
+
|
|
83
|
+
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=side_effect)
|
|
84
|
+
client = HttpClient(DeviceConfig(host))
|
|
85
|
+
# Exceptions with parameters print with double quotes, without use single quotes
|
|
86
|
+
full_msg = (
|
|
87
|
+
"\("
|
|
88
|
+
+ "['\"]"
|
|
89
|
+
+ re.escape(f"{error_message}{host}: {error}")
|
|
90
|
+
+ "['\"]"
|
|
91
|
+
+ re.escape(f", {repr(error)})")
|
|
92
|
+
)
|
|
93
|
+
with pytest.raises(error_raises, match=error_message) as exc_info:
|
|
94
|
+
await client.post("http://foobar")
|
|
95
|
+
|
|
96
|
+
assert re.match(full_msg, str(exc_info.value))
|
|
97
|
+
if mock_read:
|
|
98
|
+
assert mock_response.call_count == 1
|
|
99
|
+
else:
|
|
100
|
+
assert conn.call_count == 1
|
kasa/tests/test_klapprotocol.py
CHANGED
|
@@ -6,14 +6,22 @@ import struct
|
|
|
6
6
|
import sys
|
|
7
7
|
import time
|
|
8
8
|
from contextlib import nullcontext as does_not_raise
|
|
9
|
+
from unittest.mock import PropertyMock
|
|
9
10
|
|
|
10
|
-
import
|
|
11
|
+
import aiohttp
|
|
11
12
|
import pytest
|
|
12
13
|
|
|
13
14
|
from ..aestransport import AesTransport
|
|
14
15
|
from ..credentials import Credentials
|
|
15
16
|
from ..deviceconfig import DeviceConfig
|
|
16
|
-
from ..exceptions import
|
|
17
|
+
from ..exceptions import (
|
|
18
|
+
AuthenticationException,
|
|
19
|
+
ConnectionException,
|
|
20
|
+
RetryableException,
|
|
21
|
+
SmartDeviceException,
|
|
22
|
+
TimeoutException,
|
|
23
|
+
)
|
|
24
|
+
from ..httpclient import HttpClient
|
|
17
25
|
from ..iotprotocol import IotProtocol
|
|
18
26
|
from ..klaptransport import (
|
|
19
27
|
KlapEncryptionSession,
|
|
@@ -21,34 +29,75 @@ from ..klaptransport import (
|
|
|
21
29
|
KlapTransportV2,
|
|
22
30
|
_sha256,
|
|
23
31
|
)
|
|
32
|
+
from ..protocol import DEFAULT_CREDENTIALS, get_default_credentials
|
|
24
33
|
from ..smartprotocol import SmartProtocol
|
|
25
34
|
|
|
26
35
|
DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
|
|
27
36
|
|
|
28
37
|
|
|
29
38
|
class _mock_response:
|
|
30
|
-
def __init__(self,
|
|
31
|
-
self.
|
|
39
|
+
def __init__(self, status, content: bytes):
|
|
40
|
+
self.status = status
|
|
32
41
|
self.content = content
|
|
33
42
|
|
|
43
|
+
async def __aenter__(self):
|
|
44
|
+
return self
|
|
45
|
+
|
|
46
|
+
async def __aexit__(self, exc_t, exc_v, exc_tb):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
async def read(self):
|
|
50
|
+
return self.content
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.mark.parametrize(
|
|
54
|
+
"error, retry_expectation",
|
|
55
|
+
[
|
|
56
|
+
(Exception("dummy exception"), False),
|
|
57
|
+
(aiohttp.ServerTimeoutError("dummy exception"), True),
|
|
58
|
+
(aiohttp.ServerDisconnectedError("dummy exception"), True),
|
|
59
|
+
(aiohttp.ClientOSError("dummy exception"), True),
|
|
60
|
+
],
|
|
61
|
+
ids=("Exception", "ServerTimeoutError", "ServerDisconnectedError", "ClientOSError"),
|
|
62
|
+
)
|
|
63
|
+
@pytest.mark.parametrize("transport_class", [AesTransport, KlapTransport])
|
|
64
|
+
@pytest.mark.parametrize("protocol_class", [IotProtocol, SmartProtocol])
|
|
65
|
+
@pytest.mark.parametrize("retry_count", [1, 3, 5])
|
|
66
|
+
async def test_protocol_retries_via_client_session(
|
|
67
|
+
mocker, retry_count, protocol_class, transport_class, error, retry_expectation
|
|
68
|
+
):
|
|
69
|
+
host = "127.0.0.1"
|
|
70
|
+
conn = mocker.patch.object(aiohttp.ClientSession, "post", side_effect=error)
|
|
71
|
+
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
|
|
72
|
+
|
|
73
|
+
config = DeviceConfig(host)
|
|
74
|
+
with pytest.raises(SmartDeviceException):
|
|
75
|
+
await protocol_class(transport=transport_class(config=config)).query(
|
|
76
|
+
DUMMY_QUERY, retry_count=retry_count
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
expected_count = retry_count + 1 if retry_expectation else 1
|
|
80
|
+
assert conn.call_count == expected_count
|
|
81
|
+
|
|
34
82
|
|
|
35
83
|
@pytest.mark.parametrize(
|
|
36
84
|
"error, retry_expectation",
|
|
37
85
|
[
|
|
38
|
-
(Exception("dummy exception"), True),
|
|
39
86
|
(SmartDeviceException("dummy exception"), False),
|
|
40
|
-
(
|
|
87
|
+
(RetryableException("dummy exception"), True),
|
|
88
|
+
(TimeoutException("dummy exception"), True),
|
|
41
89
|
],
|
|
42
|
-
ids=("
|
|
90
|
+
ids=("SmartDeviceException", "RetryableException", "TimeoutException"),
|
|
43
91
|
)
|
|
44
92
|
@pytest.mark.parametrize("transport_class", [AesTransport, KlapTransport])
|
|
45
93
|
@pytest.mark.parametrize("protocol_class", [IotProtocol, SmartProtocol])
|
|
46
94
|
@pytest.mark.parametrize("retry_count", [1, 3, 5])
|
|
47
|
-
async def
|
|
95
|
+
async def test_protocol_retries_via_httpclient(
|
|
48
96
|
mocker, retry_count, protocol_class, transport_class, error, retry_expectation
|
|
49
97
|
):
|
|
50
98
|
host = "127.0.0.1"
|
|
51
|
-
conn = mocker.patch.object(
|
|
99
|
+
conn = mocker.patch.object(HttpClient, "post", side_effect=error)
|
|
100
|
+
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
|
|
52
101
|
|
|
53
102
|
config = DeviceConfig(host)
|
|
54
103
|
with pytest.raises(SmartDeviceException):
|
|
@@ -67,10 +116,11 @@ async def test_protocol_no_retry_on_connection_error(
|
|
|
67
116
|
):
|
|
68
117
|
host = "127.0.0.1"
|
|
69
118
|
conn = mocker.patch.object(
|
|
70
|
-
|
|
119
|
+
aiohttp.ClientSession,
|
|
71
120
|
"post",
|
|
72
121
|
side_effect=AuthenticationException("foo"),
|
|
73
122
|
)
|
|
123
|
+
mocker.patch.object(protocol_class, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0)
|
|
74
124
|
config = DeviceConfig(host)
|
|
75
125
|
with pytest.raises(SmartDeviceException):
|
|
76
126
|
await protocol_class(transport=transport_class(config=config)).query(
|
|
@@ -87,9 +137,9 @@ async def test_protocol_retry_recoverable_error(
|
|
|
87
137
|
):
|
|
88
138
|
host = "127.0.0.1"
|
|
89
139
|
conn = mocker.patch.object(
|
|
90
|
-
|
|
140
|
+
aiohttp.ClientSession,
|
|
91
141
|
"post",
|
|
92
|
-
side_effect=
|
|
142
|
+
side_effect=aiohttp.ClientOSError("foo"),
|
|
93
143
|
)
|
|
94
144
|
config = DeviceConfig(host)
|
|
95
145
|
with pytest.raises(SmartDeviceException):
|
|
@@ -112,7 +162,7 @@ async def test_protocol_reconnect(mocker, retry_count, protocol_class, transport
|
|
|
112
162
|
nonlocal remaining
|
|
113
163
|
remaining -= 1
|
|
114
164
|
if remaining:
|
|
115
|
-
raise
|
|
165
|
+
raise ConnectionException("Simulated connection failure")
|
|
116
166
|
|
|
117
167
|
return mock_response
|
|
118
168
|
|
|
@@ -155,7 +205,7 @@ async def test_protocol_logging(mocker, caplog, log_level):
|
|
|
155
205
|
protocol._transport._handshake_done = True
|
|
156
206
|
protocol._transport._session_expire_at = time.time() + 86400
|
|
157
207
|
protocol._transport._encryption_session = encryption_session
|
|
158
|
-
mocker.patch.object(
|
|
208
|
+
mocker.patch.object(HttpClient, "post", side_effect=_return_encrypted)
|
|
159
209
|
|
|
160
210
|
response = await protocol.query({})
|
|
161
211
|
assert response == {"great": "success"}
|
|
@@ -197,10 +247,7 @@ def test_encrypt_unicode():
|
|
|
197
247
|
(Credentials("foo", "bar"), does_not_raise()),
|
|
198
248
|
(Credentials(), does_not_raise()),
|
|
199
249
|
(
|
|
200
|
-
|
|
201
|
-
KlapTransport.KASA_SETUP_EMAIL,
|
|
202
|
-
KlapTransport.KASA_SETUP_PASSWORD,
|
|
203
|
-
),
|
|
250
|
+
get_default_credentials(DEFAULT_CREDENTIALS["KASA"]),
|
|
204
251
|
does_not_raise(),
|
|
205
252
|
),
|
|
206
253
|
(
|
|
@@ -235,7 +282,7 @@ async def test_handshake1(
|
|
|
235
282
|
device_auth_hash = transport_class.generate_auth_hash(device_credentials)
|
|
236
283
|
|
|
237
284
|
mocker.patch.object(
|
|
238
|
-
|
|
285
|
+
aiohttp.ClientSession, "post", side_effect=_return_handshake1_response
|
|
239
286
|
)
|
|
240
287
|
|
|
241
288
|
config = DeviceConfig("127.0.0.1", credentials=client_credentials)
|
|
@@ -294,12 +341,12 @@ async def test_handshake(
|
|
|
294
341
|
device_auth_hash = transport_class.generate_auth_hash(client_credentials)
|
|
295
342
|
|
|
296
343
|
mocker.patch.object(
|
|
297
|
-
|
|
344
|
+
aiohttp.ClientSession, "post", side_effect=_return_handshake_response
|
|
298
345
|
)
|
|
299
346
|
|
|
300
347
|
config = DeviceConfig("127.0.0.1", credentials=client_credentials)
|
|
301
348
|
protocol = IotProtocol(transport=transport_class(config=config))
|
|
302
|
-
protocol._transport.http_client =
|
|
349
|
+
protocol._transport.http_client = aiohttp.ClientSession()
|
|
303
350
|
|
|
304
351
|
response_status = 200
|
|
305
352
|
await protocol._transport.perform_handshake()
|
|
@@ -342,7 +389,7 @@ async def test_query(mocker):
|
|
|
342
389
|
client_credentials = Credentials("foo", "bar")
|
|
343
390
|
device_auth_hash = KlapTransport.generate_auth_hash(client_credentials)
|
|
344
391
|
|
|
345
|
-
mocker.patch.object(
|
|
392
|
+
mocker.patch.object(aiohttp.ClientSession, "post", side_effect=_return_response)
|
|
346
393
|
|
|
347
394
|
config = DeviceConfig("127.0.0.1", credentials=client_credentials)
|
|
348
395
|
protocol = IotProtocol(transport=KlapTransport(config=config))
|
|
@@ -387,7 +434,7 @@ async def test_authentication_failures(mocker, response_status, expectation):
|
|
|
387
434
|
client_credentials = Credentials("foo", "bar")
|
|
388
435
|
device_auth_hash = KlapTransport.generate_auth_hash(client_credentials)
|
|
389
436
|
|
|
390
|
-
mocker.patch.object(
|
|
437
|
+
mocker.patch.object(aiohttp.ClientSession, "post", side_effect=_return_response)
|
|
391
438
|
|
|
392
439
|
config = DeviceConfig("127.0.0.1", credentials=client_credentials)
|
|
393
440
|
protocol = IotProtocol(transport=KlapTransport(config=config))
|
kasa/tests/test_protocol.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import asyncio
|
|
1
2
|
import errno
|
|
2
3
|
import importlib
|
|
3
4
|
import inspect
|
|
@@ -15,8 +16,8 @@ from ..deviceconfig import DeviceConfig
|
|
|
15
16
|
from ..exceptions import SmartDeviceException
|
|
16
17
|
from ..klaptransport import KlapTransport, KlapTransportV2
|
|
17
18
|
from ..protocol import (
|
|
19
|
+
BaseProtocol,
|
|
18
20
|
BaseTransport,
|
|
19
|
-
TPLinkProtocol,
|
|
20
21
|
TPLinkSmartHomeProtocol,
|
|
21
22
|
_XorTransport,
|
|
22
23
|
)
|
|
@@ -122,6 +123,79 @@ async def test_protocol_reconnect(mocker, retry_count):
|
|
|
122
123
|
assert response == {"great": "success"}
|
|
123
124
|
|
|
124
125
|
|
|
126
|
+
async def test_protocol_handles_cancellation_during_write(mocker):
|
|
127
|
+
attempts = 0
|
|
128
|
+
encrypted = TPLinkSmartHomeProtocol.encrypt('{"great":"success"}')[
|
|
129
|
+
TPLinkSmartHomeProtocol.BLOCK_SIZE :
|
|
130
|
+
]
|
|
131
|
+
|
|
132
|
+
def _cancel_first_attempt(*_):
|
|
133
|
+
nonlocal attempts
|
|
134
|
+
attempts += 1
|
|
135
|
+
if attempts == 1:
|
|
136
|
+
raise asyncio.CancelledError("Simulated task cancel")
|
|
137
|
+
|
|
138
|
+
async def _mock_read(byte_count):
|
|
139
|
+
nonlocal encrypted
|
|
140
|
+
if byte_count == TPLinkSmartHomeProtocol.BLOCK_SIZE:
|
|
141
|
+
return struct.pack(">I", len(encrypted))
|
|
142
|
+
if byte_count == len(encrypted):
|
|
143
|
+
return encrypted
|
|
144
|
+
|
|
145
|
+
raise ValueError(f"No mock for {byte_count}")
|
|
146
|
+
|
|
147
|
+
def aio_mock_writer(_, __):
|
|
148
|
+
reader = mocker.patch("asyncio.StreamReader")
|
|
149
|
+
writer = mocker.patch("asyncio.StreamWriter")
|
|
150
|
+
mocker.patch.object(writer, "write", _cancel_first_attempt)
|
|
151
|
+
mocker.patch.object(reader, "readexactly", _mock_read)
|
|
152
|
+
return reader, writer
|
|
153
|
+
|
|
154
|
+
config = DeviceConfig("127.0.0.1")
|
|
155
|
+
protocol = TPLinkSmartHomeProtocol(transport=_XorTransport(config=config))
|
|
156
|
+
mocker.patch("asyncio.open_connection", side_effect=aio_mock_writer)
|
|
157
|
+
with pytest.raises(asyncio.CancelledError):
|
|
158
|
+
await protocol.query({})
|
|
159
|
+
assert protocol.writer is None
|
|
160
|
+
response = await protocol.query({})
|
|
161
|
+
assert response == {"great": "success"}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
async def test_protocol_handles_cancellation_during_connection(mocker):
|
|
165
|
+
attempts = 0
|
|
166
|
+
encrypted = TPLinkSmartHomeProtocol.encrypt('{"great":"success"}')[
|
|
167
|
+
TPLinkSmartHomeProtocol.BLOCK_SIZE :
|
|
168
|
+
]
|
|
169
|
+
|
|
170
|
+
async def _mock_read(byte_count):
|
|
171
|
+
nonlocal encrypted
|
|
172
|
+
if byte_count == TPLinkSmartHomeProtocol.BLOCK_SIZE:
|
|
173
|
+
return struct.pack(">I", len(encrypted))
|
|
174
|
+
if byte_count == len(encrypted):
|
|
175
|
+
return encrypted
|
|
176
|
+
|
|
177
|
+
raise ValueError(f"No mock for {byte_count}")
|
|
178
|
+
|
|
179
|
+
def aio_mock_writer(_, __):
|
|
180
|
+
nonlocal attempts
|
|
181
|
+
attempts += 1
|
|
182
|
+
if attempts == 1:
|
|
183
|
+
raise asyncio.CancelledError("Simulated task cancel")
|
|
184
|
+
reader = mocker.patch("asyncio.StreamReader")
|
|
185
|
+
writer = mocker.patch("asyncio.StreamWriter")
|
|
186
|
+
mocker.patch.object(reader, "readexactly", _mock_read)
|
|
187
|
+
return reader, writer
|
|
188
|
+
|
|
189
|
+
config = DeviceConfig("127.0.0.1")
|
|
190
|
+
protocol = TPLinkSmartHomeProtocol(transport=_XorTransport(config=config))
|
|
191
|
+
mocker.patch("asyncio.open_connection", side_effect=aio_mock_writer)
|
|
192
|
+
with pytest.raises(asyncio.CancelledError):
|
|
193
|
+
await protocol.query({})
|
|
194
|
+
assert protocol.writer is None
|
|
195
|
+
response = await protocol.query({})
|
|
196
|
+
assert response == {"great": "success"}
|
|
197
|
+
|
|
198
|
+
|
|
125
199
|
@pytest.mark.parametrize("log_level", [logging.WARNING, logging.DEBUG])
|
|
126
200
|
async def test_protocol_logging(mocker, caplog, log_level):
|
|
127
201
|
caplog.set_level(log_level)
|
|
@@ -271,7 +345,7 @@ def _get_subclasses(of_class):
|
|
|
271
345
|
|
|
272
346
|
|
|
273
347
|
@pytest.mark.parametrize(
|
|
274
|
-
"class_name_obj", _get_subclasses(
|
|
348
|
+
"class_name_obj", _get_subclasses(BaseProtocol), ids=lambda t: t[0]
|
|
275
349
|
)
|
|
276
350
|
def test_protocol_init_signature(class_name_obj):
|
|
277
351
|
params = list(inspect.signature(class_name_obj[1].__init__).parameters.values())
|
kasa/tests/test_smartprotocol.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: python-kasa
|
|
3
|
-
Version: 0.6.
|
|
3
|
+
Version: 0.6.1
|
|
4
4
|
Summary: Python API for TP-Link Kasa Smarthome devices
|
|
5
5
|
Home-page: https://github.com/python-kasa/python-kasa
|
|
6
6
|
License: GPL-3.0-or-later
|
|
@@ -15,17 +15,17 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Provides-Extra: docs
|
|
17
17
|
Provides-Extra: speedups
|
|
18
|
+
Requires-Dist: aiohttp (>=3)
|
|
18
19
|
Requires-Dist: anyio
|
|
19
20
|
Requires-Dist: async-timeout (>=3.0.0)
|
|
20
21
|
Requires-Dist: asyncclick (>=8)
|
|
21
22
|
Requires-Dist: cryptography (>=1.9)
|
|
22
23
|
Requires-Dist: docutils (>=0.17) ; extra == "docs"
|
|
23
|
-
Requires-Dist: httpx (>=0.25.0)
|
|
24
24
|
Requires-Dist: kasa-crypt (>=0.2.0) ; extra == "speedups"
|
|
25
25
|
Requires-Dist: myst-parser ; extra == "docs"
|
|
26
26
|
Requires-Dist: orjson (>=3.9.1) ; extra == "speedups"
|
|
27
27
|
Requires-Dist: pydantic (>=1)
|
|
28
|
-
Requires-Dist: sphinx (>=
|
|
28
|
+
Requires-Dist: sphinx (>=5,<6) ; extra == "docs"
|
|
29
29
|
Requires-Dist: sphinx_rtd_theme (>=0,<1) ; extra == "docs"
|
|
30
30
|
Requires-Dist: sphinxcontrib-programoutput (>=0,<1) ; extra == "docs"
|
|
31
31
|
Project-URL: Bug Tracker, https://github.com/python-kasa/python-kasa/issues
|
|
@@ -250,7 +250,7 @@ Note, that this works currently only on kasa-branded devices which use port 9999
|
|
|
250
250
|
In principle, most kasa-branded devices that are locally controllable using the official Kasa mobile app work with this library.
|
|
251
251
|
|
|
252
252
|
The following lists the devices that have been manually verified to work.
|
|
253
|
-
**If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools
|
|
253
|
+
**If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
|
|
254
254
|
|
|
255
255
|
### Plugs
|
|
256
256
|
|
|
@@ -263,10 +263,10 @@ The following lists the devices that have been manually verified to work.
|
|
|
263
263
|
* KP105
|
|
264
264
|
* KP115
|
|
265
265
|
* KP125
|
|
266
|
-
* KP125M [See note below](#
|
|
266
|
+
* KP125M [See note below](#newer-kasa-branded-devices)
|
|
267
267
|
* KP401
|
|
268
268
|
* EP10
|
|
269
|
-
* EP25 [See note below](#
|
|
269
|
+
* EP25 [See note below](#newer-kasa-branded-devices)
|
|
270
270
|
|
|
271
271
|
### Power Strips
|
|
272
272
|
|
|
@@ -308,18 +308,37 @@ The following lists the devices that have been manually verified to work.
|
|
|
308
308
|
* KL420L5
|
|
309
309
|
* KL430
|
|
310
310
|
|
|
311
|
-
### Tapo
|
|
311
|
+
### Tapo branded devices
|
|
312
312
|
|
|
313
313
|
The library has recently added a limited supported for devices that carry Tapo branding.
|
|
314
314
|
|
|
315
315
|
At the moment, the following devices have been confirmed to work:
|
|
316
316
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
* Tapo
|
|
320
|
-
* Tapo
|
|
321
|
-
*
|
|
322
|
-
|
|
317
|
+
#### Plugs
|
|
318
|
+
|
|
319
|
+
* Tapo P110
|
|
320
|
+
* Tapo P125M
|
|
321
|
+
* Tapo P135 (dimming not yet supported)
|
|
322
|
+
|
|
323
|
+
#### Bulbs
|
|
324
|
+
|
|
325
|
+
* Tapo L510B
|
|
326
|
+
* Tapo L510E
|
|
327
|
+
* Tapo L530E
|
|
328
|
+
|
|
329
|
+
#### Light strips
|
|
330
|
+
|
|
331
|
+
* Tapo L900-5
|
|
332
|
+
* Tapo L900-10
|
|
333
|
+
* Tapo L920-5
|
|
334
|
+
* Tapo L930-5
|
|
335
|
+
|
|
336
|
+
#### Wall switches
|
|
337
|
+
|
|
338
|
+
* Tapo S500D
|
|
339
|
+
* Tapo S505
|
|
340
|
+
|
|
341
|
+
### Newer Kasa branded devices
|
|
323
342
|
|
|
324
343
|
Some newer hardware versions of Kasa branded devices are now using the same protocol as
|
|
325
344
|
Tapo branded devices. Support for these devices is currently limited as per TAPO branded
|
|
@@ -327,8 +346,11 @@ devices:
|
|
|
327
346
|
|
|
328
347
|
* Kasa EP25 (plug) hw_version 2.6
|
|
329
348
|
* Kasa KP125M (plug)
|
|
349
|
+
* Kasa KS205 (Wifi/Matter Wall Switch)
|
|
350
|
+
* Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
|
|
351
|
+
|
|
330
352
|
|
|
331
|
-
**If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `devtools
|
|
353
|
+
**If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
|
|
332
354
|
|
|
333
355
|
## Resources
|
|
334
356
|
|
|
@@ -355,7 +377,7 @@ use it directly you should expect it could break in future releases until this s
|
|
|
355
377
|
Other TAPO libraries are:
|
|
356
378
|
|
|
357
379
|
* [PyTapo - Python library for communication with Tapo Cameras](https://github.com/JurajNyiri/pytapo)
|
|
358
|
-
* [Tapo P100 (Tapo
|
|
380
|
+
* [Tapo P100 (Tapo plugs, Tapo bulbs)](https://github.com/fishbigger/TapoP100)
|
|
359
381
|
* [Home Assistant integration](https://github.com/fishbigger/HomeAssistant-Tapo-P100-Control)
|
|
360
382
|
* [plugp100, another tapo library](https://github.com/petretiandrea/plugp100)
|
|
361
383
|
* [Home Assistant integration](https://github.com/petretiandrea/home-assistant-tapo-p100)
|