python-kasa 0.7.0.dev1__py3-none-any.whl → 0.7.0.dev2__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 +16 -9
- kasa/aestransport.py +0 -3
- kasa/cli.py +12 -10
- kasa/device.py +26 -3
- kasa/deviceconfig.py +45 -20
- kasa/discover.py +106 -46
- kasa/feature.py +2 -1
- kasa/httpclient.py +24 -0
- kasa/iot/iotdevice.py +1 -1
- kasa/iot/iotlightstrip.py +3 -2
- kasa/iot/iotplug.py +1 -1
- kasa/iot/iotstrip.py +1 -1
- kasa/iot/modules/lightpreset.py +6 -1
- kasa/smart/modules/autooff.py +13 -8
- kasa/smartprotocol.py +3 -1
- kasa/tests/device_fixtures.py +10 -1
- kasa/tests/discovery_fixtures.py +82 -29
- kasa/tests/fakeprotocol_iot.py +30 -7
- kasa/tests/fixtures/HS110(EU)_1.0_1.2.5.json +1 -1
- kasa/tests/fixtures/HS110(US)_1.0_1.2.6.json +37 -0
- kasa/tests/fixtures/HS220(US)_1.0_1.5.7.json +1 -1
- kasa/tests/fixtures/HS220(US)_2.0_1.0.3.json +1 -1
- kasa/tests/fixtures/HS300(US)_1.0_1.0.21.json +89 -0
- kasa/tests/fixtures/KL120(US)_1.0_1.8.11.json +85 -0
- kasa/tests/fixtures/KL430(US)_1.0_1.0.10.json +51 -12
- kasa/tests/fixtures/KP303(UK)_1.0_1.0.3.json +1 -1
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +1 -1
- kasa/tests/fixtures/smart/P115(EU)_1.0_1.2.3.json +386 -0
- kasa/tests/fixtures/smart/S505D(US)_1.0_1.1.0.json +262 -0
- kasa/tests/smart/modules/test_autooff.py +103 -0
- kasa/tests/test_aestransport.py +78 -2
- kasa/tests/test_bulb.py +9 -4
- kasa/tests/test_device.py +28 -6
- kasa/tests/test_device_factory.py +8 -8
- kasa/tests/test_discovery.py +61 -4
- kasa/tests/test_readme_examples.py +37 -22
- kasa/tests/test_smartprotocol.py +3 -2
- {python_kasa-0.7.0.dev1.dist-info → python_kasa-0.7.0.dev2.dist-info}/METADATA +4 -4
- {python_kasa-0.7.0.dev1.dist-info → python_kasa-0.7.0.dev2.dist-info}/RECORD +42 -36
- {python_kasa-0.7.0.dev1.dist-info → python_kasa-0.7.0.dev2.dist-info}/LICENSE +0 -0
- {python_kasa-0.7.0.dev1.dist-info → python_kasa-0.7.0.dev2.dist-info}/WHEEL +0 -0
- {python_kasa-0.7.0.dev1.dist-info → python_kasa-0.7.0.dev2.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
{
|
|
2
|
+
"component_nego": {
|
|
3
|
+
"component_list": [
|
|
4
|
+
{
|
|
5
|
+
"id": "device",
|
|
6
|
+
"ver_code": 2
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"id": "firmware",
|
|
10
|
+
"ver_code": 2
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"id": "quick_setup",
|
|
14
|
+
"ver_code": 3
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"id": "inherit",
|
|
18
|
+
"ver_code": 1
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"id": "time",
|
|
22
|
+
"ver_code": 1
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"id": "wireless",
|
|
26
|
+
"ver_code": 1
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"id": "schedule",
|
|
30
|
+
"ver_code": 2
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"id": "countdown",
|
|
34
|
+
"ver_code": 2
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
"id": "antitheft",
|
|
38
|
+
"ver_code": 1
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"id": "account",
|
|
42
|
+
"ver_code": 1
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"id": "synchronize",
|
|
46
|
+
"ver_code": 1
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"id": "sunrise_sunset",
|
|
50
|
+
"ver_code": 1
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"id": "led",
|
|
54
|
+
"ver_code": 1
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"id": "cloud_connect",
|
|
58
|
+
"ver_code": 1
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"id": "iot_cloud",
|
|
62
|
+
"ver_code": 1
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"id": "device_local_time",
|
|
66
|
+
"ver_code": 1
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"id": "default_states",
|
|
70
|
+
"ver_code": 1
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"id": "brightness",
|
|
74
|
+
"ver_code": 1
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"id": "preset",
|
|
78
|
+
"ver_code": 1
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"id": "on_off_gradually",
|
|
82
|
+
"ver_code": 2
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
"id": "dimmer_calibration",
|
|
86
|
+
"ver_code": 1
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"id": "localSmart",
|
|
90
|
+
"ver_code": 1
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"id": "overheat_protection",
|
|
94
|
+
"ver_code": 1
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
"id": "matter",
|
|
98
|
+
"ver_code": 2
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
},
|
|
102
|
+
"discovery_result": {
|
|
103
|
+
"device_id": "00000000000000000000000000000000",
|
|
104
|
+
"device_model": "S505D(US)",
|
|
105
|
+
"device_type": "SMART.TAPOSWITCH",
|
|
106
|
+
"factory_default": false,
|
|
107
|
+
"ip": "127.0.0.123",
|
|
108
|
+
"is_support_iot_cloud": true,
|
|
109
|
+
"mac": "48-22-54-00-00-00",
|
|
110
|
+
"mgt_encrypt_schm": {
|
|
111
|
+
"encrypt_type": "KLAP",
|
|
112
|
+
"http_port": 80,
|
|
113
|
+
"is_support_https": false,
|
|
114
|
+
"lv": 2
|
|
115
|
+
},
|
|
116
|
+
"obd_src": "matter",
|
|
117
|
+
"owner": "00000000000000000000000000000000"
|
|
118
|
+
},
|
|
119
|
+
"get_antitheft_rules": {
|
|
120
|
+
"antitheft_rule_max_count": 1,
|
|
121
|
+
"enable": false,
|
|
122
|
+
"rule_list": []
|
|
123
|
+
},
|
|
124
|
+
"get_auto_update_info": {
|
|
125
|
+
"enable": true,
|
|
126
|
+
"random_range": 120,
|
|
127
|
+
"time": 180
|
|
128
|
+
},
|
|
129
|
+
"get_connect_cloud_state": {
|
|
130
|
+
"status": 1
|
|
131
|
+
},
|
|
132
|
+
"get_countdown_rules": {
|
|
133
|
+
"countdown_rule_max_count": 1,
|
|
134
|
+
"enable": false,
|
|
135
|
+
"rule_list": []
|
|
136
|
+
},
|
|
137
|
+
"get_device_info": {
|
|
138
|
+
"avatar": "switch_s500d",
|
|
139
|
+
"brightness": 100,
|
|
140
|
+
"default_states": {
|
|
141
|
+
"re_power_type": "always_off",
|
|
142
|
+
"re_power_type_capability": [
|
|
143
|
+
"last_states",
|
|
144
|
+
"always_on",
|
|
145
|
+
"always_off"
|
|
146
|
+
],
|
|
147
|
+
"type": "last_states"
|
|
148
|
+
},
|
|
149
|
+
"device_id": "0000000000000000000000000000000000000000",
|
|
150
|
+
"device_on": false,
|
|
151
|
+
"fw_id": "00000000000000000000000000000000",
|
|
152
|
+
"fw_ver": "1.1.0 Build 231024 Rel.201030",
|
|
153
|
+
"has_set_location_info": false,
|
|
154
|
+
"hw_id": "00000000000000000000000000000000",
|
|
155
|
+
"hw_ver": "1.0",
|
|
156
|
+
"ip": "127.0.0.123",
|
|
157
|
+
"lang": "en_US",
|
|
158
|
+
"latitude": 0,
|
|
159
|
+
"longitude": 0,
|
|
160
|
+
"mac": "48-22-54-00-00-00",
|
|
161
|
+
"model": "S505D",
|
|
162
|
+
"nickname": "I01BU0tFRF9OQU1FIw==",
|
|
163
|
+
"oem_id": "00000000000000000000000000000000",
|
|
164
|
+
"on_time": 0,
|
|
165
|
+
"overheat_status": "normal",
|
|
166
|
+
"region": "America/Chicago",
|
|
167
|
+
"rssi": -39,
|
|
168
|
+
"signal_level": 3,
|
|
169
|
+
"specs": "",
|
|
170
|
+
"ssid": "I01BU0tFRF9TU0lEIw==",
|
|
171
|
+
"time_diff": -360,
|
|
172
|
+
"type": "SMART.TAPOSWITCH"
|
|
173
|
+
},
|
|
174
|
+
"get_device_time": {
|
|
175
|
+
"region": "America/Chicago",
|
|
176
|
+
"time_diff": -360,
|
|
177
|
+
"timestamp": 952082825
|
|
178
|
+
},
|
|
179
|
+
"get_fw_download_state": {
|
|
180
|
+
"auto_upgrade": false,
|
|
181
|
+
"download_progress": 0,
|
|
182
|
+
"reboot_time": 5,
|
|
183
|
+
"status": 0,
|
|
184
|
+
"upgrade_time": 5
|
|
185
|
+
},
|
|
186
|
+
"get_inherit_info": null,
|
|
187
|
+
"get_led_info": {
|
|
188
|
+
"led_rule": "always",
|
|
189
|
+
"led_status": true,
|
|
190
|
+
"night_mode": {
|
|
191
|
+
"end_time": 420,
|
|
192
|
+
"night_mode_type": "sunrise_sunset",
|
|
193
|
+
"start_time": 1140,
|
|
194
|
+
"sunrise_offset": 0,
|
|
195
|
+
"sunset_offset": 0
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
"get_matter_setup_info": {
|
|
199
|
+
"setup_code": "00000000000",
|
|
200
|
+
"setup_payload": "00:-00000000000000.000"
|
|
201
|
+
},
|
|
202
|
+
"get_next_event": {},
|
|
203
|
+
"get_preset_rules": {
|
|
204
|
+
"brightness": [
|
|
205
|
+
100,
|
|
206
|
+
75,
|
|
207
|
+
50,
|
|
208
|
+
25,
|
|
209
|
+
1
|
|
210
|
+
]
|
|
211
|
+
},
|
|
212
|
+
"get_schedule_rules": {
|
|
213
|
+
"enable": false,
|
|
214
|
+
"rule_list": [],
|
|
215
|
+
"schedule_rule_max_count": 32,
|
|
216
|
+
"start_index": 0,
|
|
217
|
+
"sum": 0
|
|
218
|
+
},
|
|
219
|
+
"get_wireless_scan_info": {
|
|
220
|
+
"ap_list": [],
|
|
221
|
+
"start_index": 0,
|
|
222
|
+
"sum": 0,
|
|
223
|
+
"wep_supported": false
|
|
224
|
+
},
|
|
225
|
+
"qs_component_nego": {
|
|
226
|
+
"component_list": [
|
|
227
|
+
{
|
|
228
|
+
"id": "quick_setup",
|
|
229
|
+
"ver_code": 3
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
"id": "sunrise_sunset",
|
|
233
|
+
"ver_code": 1
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
"id": "ble_whole_setup",
|
|
237
|
+
"ver_code": 1
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
"id": "matter",
|
|
241
|
+
"ver_code": 2
|
|
242
|
+
},
|
|
243
|
+
{
|
|
244
|
+
"id": "iot_cloud",
|
|
245
|
+
"ver_code": 1
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
"id": "inherit",
|
|
249
|
+
"ver_code": 1
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"id": "firmware",
|
|
253
|
+
"ver_code": 2
|
|
254
|
+
}
|
|
255
|
+
],
|
|
256
|
+
"extra_info": {
|
|
257
|
+
"device_model": "S505D",
|
|
258
|
+
"device_type": "SMART.TAPOSWITCH",
|
|
259
|
+
"is_klap": true
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
from datetime import datetime
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from pytest_mock import MockerFixture
|
|
9
|
+
|
|
10
|
+
from kasa import Module
|
|
11
|
+
from kasa.smart import SmartDevice
|
|
12
|
+
from kasa.tests.device_fixtures import parametrize
|
|
13
|
+
|
|
14
|
+
autooff = parametrize(
|
|
15
|
+
"has autooff", component_filter="auto_off", protocol_filter={"SMART"}
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@autooff
|
|
20
|
+
@pytest.mark.parametrize(
|
|
21
|
+
"feature, prop_name, type",
|
|
22
|
+
[
|
|
23
|
+
("auto_off_enabled", "enabled", bool),
|
|
24
|
+
("auto_off_minutes", "delay", int),
|
|
25
|
+
("auto_off_at", "auto_off_at", Optional[datetime]),
|
|
26
|
+
],
|
|
27
|
+
)
|
|
28
|
+
@pytest.mark.skipif(
|
|
29
|
+
sys.version_info < (3, 10),
|
|
30
|
+
reason="Subscripted generics cannot be used with class and instance checks",
|
|
31
|
+
)
|
|
32
|
+
async def test_autooff_features(
|
|
33
|
+
dev: SmartDevice, feature: str, prop_name: str, type: type
|
|
34
|
+
):
|
|
35
|
+
"""Test that features are registered and work as expected."""
|
|
36
|
+
autooff = dev.modules.get(Module.AutoOff)
|
|
37
|
+
assert autooff is not None
|
|
38
|
+
|
|
39
|
+
prop = getattr(autooff, prop_name)
|
|
40
|
+
assert isinstance(prop, type)
|
|
41
|
+
|
|
42
|
+
feat = dev.features[feature]
|
|
43
|
+
assert feat.value == prop
|
|
44
|
+
assert isinstance(feat.value, type)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@autooff
|
|
48
|
+
async def test_settings(dev: SmartDevice, mocker: MockerFixture):
|
|
49
|
+
"""Test autooff settings."""
|
|
50
|
+
autooff = dev.modules.get(Module.AutoOff)
|
|
51
|
+
assert autooff
|
|
52
|
+
|
|
53
|
+
enabled = dev.features["auto_off_enabled"]
|
|
54
|
+
assert autooff.enabled == enabled.value
|
|
55
|
+
|
|
56
|
+
delay = dev.features["auto_off_minutes"]
|
|
57
|
+
assert autooff.delay == delay.value
|
|
58
|
+
|
|
59
|
+
call = mocker.spy(autooff, "call")
|
|
60
|
+
new_state = True
|
|
61
|
+
|
|
62
|
+
await autooff.set_enabled(new_state)
|
|
63
|
+
call.assert_called_with(
|
|
64
|
+
"set_auto_off_config", {"enable": new_state, "delay_min": delay.value}
|
|
65
|
+
)
|
|
66
|
+
call.reset_mock()
|
|
67
|
+
await dev.update()
|
|
68
|
+
|
|
69
|
+
new_delay = 123
|
|
70
|
+
|
|
71
|
+
await autooff.set_delay(new_delay)
|
|
72
|
+
|
|
73
|
+
call.assert_called_with(
|
|
74
|
+
"set_auto_off_config", {"enable": new_state, "delay_min": new_delay}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
await dev.update()
|
|
78
|
+
|
|
79
|
+
assert autooff.enabled == new_state
|
|
80
|
+
assert autooff.delay == new_delay
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@autooff
|
|
84
|
+
@pytest.mark.parametrize("is_timer_active", [True, False])
|
|
85
|
+
async def test_auto_off_at(
|
|
86
|
+
dev: SmartDevice, mocker: MockerFixture, is_timer_active: bool
|
|
87
|
+
):
|
|
88
|
+
"""Test auto-off at sensor."""
|
|
89
|
+
autooff = dev.modules.get(Module.AutoOff)
|
|
90
|
+
assert autooff
|
|
91
|
+
|
|
92
|
+
autooff_at = dev.features["auto_off_at"]
|
|
93
|
+
|
|
94
|
+
mocker.patch.object(
|
|
95
|
+
type(autooff),
|
|
96
|
+
"is_timer_active",
|
|
97
|
+
new_callable=mocker.PropertyMock,
|
|
98
|
+
return_value=is_timer_active,
|
|
99
|
+
)
|
|
100
|
+
if is_timer_active:
|
|
101
|
+
assert isinstance(autooff_at.value, datetime)
|
|
102
|
+
else:
|
|
103
|
+
assert autooff_at.value is None
|
kasa/tests/test_aestransport.py
CHANGED
|
@@ -24,6 +24,7 @@ from ..exceptions import (
|
|
|
24
24
|
AuthenticationError,
|
|
25
25
|
KasaException,
|
|
26
26
|
SmartErrorCode,
|
|
27
|
+
_ConnectionError,
|
|
27
28
|
)
|
|
28
29
|
from ..httpclient import HttpClient
|
|
29
30
|
|
|
@@ -137,7 +138,7 @@ async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
|
|
|
137
138
|
transport._state = TransportState.LOGIN_REQUIRED
|
|
138
139
|
transport._session_expire_at = time.time() + 86400
|
|
139
140
|
transport._encryption_session = mock_aes_device.encryption_session
|
|
140
|
-
mocker.patch.object(transport, "
|
|
141
|
+
mocker.patch.object(transport._http_client, "WAIT_BETWEEN_REQUESTS_ON_OSERROR", 0)
|
|
141
142
|
|
|
142
143
|
assert transport._token_url is None
|
|
143
144
|
|
|
@@ -285,6 +286,68 @@ async def test_port_override():
|
|
|
285
286
|
assert str(transport._app_url) == "http://127.0.0.1:12345/app"
|
|
286
287
|
|
|
287
288
|
|
|
289
|
+
@pytest.mark.parametrize(
|
|
290
|
+
"request_delay, should_error, should_succeed",
|
|
291
|
+
[(0, False, True), (0.125, True, True), (0.3, True, True), (0.7, True, False)],
|
|
292
|
+
ids=["No error", "Error then succeed", "Two errors then succeed", "No succeed"],
|
|
293
|
+
)
|
|
294
|
+
async def test_device_closes_connection(
|
|
295
|
+
mocker, request_delay, should_error, should_succeed
|
|
296
|
+
):
|
|
297
|
+
"""Test the delay logic in http client to deal with devices that close connections after each request.
|
|
298
|
+
|
|
299
|
+
Currently only the P100 on older firmware.
|
|
300
|
+
"""
|
|
301
|
+
host = "127.0.0.1"
|
|
302
|
+
|
|
303
|
+
# Speed up the test by dividing all times by a factor. Doesn't seem to work on windows
|
|
304
|
+
# but leaving here as a TODO to manipulate system time for testing.
|
|
305
|
+
speed_up_factor = 1
|
|
306
|
+
default_delay = HttpClient.WAIT_BETWEEN_REQUESTS_ON_OSERROR / speed_up_factor
|
|
307
|
+
request_delay = request_delay / speed_up_factor
|
|
308
|
+
mock_aes_device = MockAesDevice(
|
|
309
|
+
host, 200, 0, 0, sequential_request_delay=request_delay
|
|
310
|
+
)
|
|
311
|
+
mocker.patch.object(aiohttp.ClientSession, "post", side_effect=mock_aes_device.post)
|
|
312
|
+
|
|
313
|
+
config = DeviceConfig(host, credentials=Credentials("foo", "bar"))
|
|
314
|
+
transport = AesTransport(config=config)
|
|
315
|
+
transport._http_client.WAIT_BETWEEN_REQUESTS_ON_OSERROR = default_delay
|
|
316
|
+
transport._state = TransportState.LOGIN_REQUIRED
|
|
317
|
+
transport._session_expire_at = time.time() + 86400
|
|
318
|
+
transport._encryption_session = mock_aes_device.encryption_session
|
|
319
|
+
transport._token_url = transport._app_url.with_query(
|
|
320
|
+
f"token={mock_aes_device.token}"
|
|
321
|
+
)
|
|
322
|
+
request = {
|
|
323
|
+
"method": "get_device_info",
|
|
324
|
+
"params": None,
|
|
325
|
+
"request_time_milis": round(time.time() * 1000),
|
|
326
|
+
"requestID": 1,
|
|
327
|
+
"terminal_uuid": "foobar",
|
|
328
|
+
}
|
|
329
|
+
error_count = 0
|
|
330
|
+
success = False
|
|
331
|
+
|
|
332
|
+
# If the device errors without a delay then it should error immedately ( + 1)
|
|
333
|
+
# and then the number of times the default delay passes within the request delay window
|
|
334
|
+
expected_error_count = (
|
|
335
|
+
0 if not should_error else int(request_delay / default_delay) + 1
|
|
336
|
+
)
|
|
337
|
+
for _ in range(3):
|
|
338
|
+
try:
|
|
339
|
+
await transport.send(json_dumps(request))
|
|
340
|
+
except _ConnectionError:
|
|
341
|
+
error_count += 1
|
|
342
|
+
else:
|
|
343
|
+
success = True
|
|
344
|
+
|
|
345
|
+
assert bool(transport._http_client._wait_between_requests) == should_error
|
|
346
|
+
assert bool(error_count) == should_error
|
|
347
|
+
assert error_count == expected_error_count
|
|
348
|
+
assert success == should_succeed
|
|
349
|
+
|
|
350
|
+
|
|
288
351
|
class MockAesDevice:
|
|
289
352
|
class _mock_response:
|
|
290
353
|
def __init__(self, status, json: dict):
|
|
@@ -313,6 +376,7 @@ class MockAesDevice:
|
|
|
313
376
|
*,
|
|
314
377
|
do_not_encrypt_response=False,
|
|
315
378
|
send_response=None,
|
|
379
|
+
sequential_request_delay=0,
|
|
316
380
|
):
|
|
317
381
|
self.host = host
|
|
318
382
|
self.status_code = status_code
|
|
@@ -323,6 +387,9 @@ class MockAesDevice:
|
|
|
323
387
|
self.http_client = HttpClient(DeviceConfig(self.host))
|
|
324
388
|
self.inner_call_count = 0
|
|
325
389
|
self.token = "".join(random.choices(string.ascii_uppercase, k=32)) # noqa: S311
|
|
390
|
+
self.sequential_request_delay = sequential_request_delay
|
|
391
|
+
self.last_request_time = None
|
|
392
|
+
self.sequential_error_raised = False
|
|
326
393
|
|
|
327
394
|
@property
|
|
328
395
|
def inner_error_code(self):
|
|
@@ -332,10 +399,19 @@ class MockAesDevice:
|
|
|
332
399
|
return self._inner_error_code
|
|
333
400
|
|
|
334
401
|
async def post(self, url: URL, params=None, json=None, data=None, *_, **__):
|
|
402
|
+
if self.sequential_request_delay and self.last_request_time:
|
|
403
|
+
now = time.time()
|
|
404
|
+
print(now - self.last_request_time)
|
|
405
|
+
if (now - self.last_request_time) < self.sequential_request_delay:
|
|
406
|
+
self.sequential_error_raised = True
|
|
407
|
+
raise aiohttp.ClientOSError("Test connection closed")
|
|
335
408
|
if data:
|
|
336
409
|
async for item in data:
|
|
337
410
|
json = json_loads(item.decode())
|
|
338
|
-
|
|
411
|
+
res = await self._post(url, json)
|
|
412
|
+
if self.sequential_request_delay:
|
|
413
|
+
self.last_request_time = time.time()
|
|
414
|
+
return res
|
|
339
415
|
|
|
340
416
|
async def _post(self, url: URL, json: dict[str, Any]):
|
|
341
417
|
if json["method"] == "handshake":
|
kasa/tests/test_bulb.py
CHANGED
|
@@ -283,12 +283,17 @@ async def test_ignore_default_not_set_without_color_mode_change_turn_on(
|
|
|
283
283
|
@bulb_iot
|
|
284
284
|
async def test_list_presets(dev: IotBulb):
|
|
285
285
|
presets = dev.presets
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
286
|
+
# Light strip devices may list some light effects along with normal presets but these
|
|
287
|
+
# are handled by the LightEffect module so exclude preferred states with id
|
|
288
|
+
raw_presets = [
|
|
289
|
+
pstate for pstate in dev.sys_info["preferred_state"] if "id" not in pstate
|
|
290
|
+
]
|
|
291
|
+
assert len(presets) == len(raw_presets)
|
|
292
|
+
|
|
293
|
+
for preset, raw in zip(presets, raw_presets):
|
|
289
294
|
assert preset.index == raw["index"]
|
|
290
|
-
assert preset.hue == raw["hue"]
|
|
291
295
|
assert preset.brightness == raw["brightness"]
|
|
296
|
+
assert preset.hue == raw["hue"]
|
|
292
297
|
assert preset.saturation == raw["saturation"]
|
|
293
298
|
assert preset.color_temp == raw["color_temp"]
|
|
294
299
|
|
kasa/tests/test_device.py
CHANGED
|
@@ -116,13 +116,11 @@ def test_deprecated_devices(device_class, use_class):
|
|
|
116
116
|
getattr(module, use_class.__name__)
|
|
117
117
|
|
|
118
118
|
|
|
119
|
-
@pytest.mark.parametrize(
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def test_deprecated_exceptions(exceptions_class, use_class):
|
|
123
|
-
msg = f"{exceptions_class} is deprecated, use {use_class.__name__} instead"
|
|
119
|
+
@pytest.mark.parametrize("deprecated_class, use_class", kasa.deprecated_classes.items())
|
|
120
|
+
def test_deprecated_classes(deprecated_class, use_class):
|
|
121
|
+
msg = f"{deprecated_class} is deprecated, use {use_class.__name__} instead"
|
|
124
122
|
with pytest.deprecated_call(match=msg):
|
|
125
|
-
getattr(kasa,
|
|
123
|
+
getattr(kasa, deprecated_class)
|
|
126
124
|
getattr(kasa, use_class.__name__)
|
|
127
125
|
|
|
128
126
|
|
|
@@ -266,3 +264,27 @@ async def test_deprecated_light_preset_attributes(dev: Device):
|
|
|
266
264
|
IotLightPreset(index=0, hue=100, brightness=100, saturation=0, color_temp=0), # type: ignore[call-arg]
|
|
267
265
|
will_raise=exc,
|
|
268
266
|
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
async def test_device_type_aliases():
|
|
270
|
+
"""Test that the device type aliases in Device work."""
|
|
271
|
+
|
|
272
|
+
def _mock_connect(config, *args, **kwargs):
|
|
273
|
+
mock = Mock()
|
|
274
|
+
mock.config = config
|
|
275
|
+
return mock
|
|
276
|
+
|
|
277
|
+
with patch("kasa.device_factory.connect", side_effect=_mock_connect):
|
|
278
|
+
dev = await Device.connect(
|
|
279
|
+
config=Device.Config(
|
|
280
|
+
host="127.0.0.1",
|
|
281
|
+
credentials=Device.Credentials(username="user", password="foobar"), # noqa: S106
|
|
282
|
+
connection_type=Device.ConnectionParameters(
|
|
283
|
+
device_family=Device.Family.SmartKasaPlug,
|
|
284
|
+
encryption_type=Device.EncryptionType.Klap,
|
|
285
|
+
login_version=2,
|
|
286
|
+
),
|
|
287
|
+
)
|
|
288
|
+
)
|
|
289
|
+
assert isinstance(dev.config, DeviceConfig)
|
|
290
|
+
assert DeviceType.Dimmer == Device.Type.Dimmer
|
|
@@ -17,10 +17,10 @@ from kasa.device_factory import (
|
|
|
17
17
|
get_protocol,
|
|
18
18
|
)
|
|
19
19
|
from kasa.deviceconfig import (
|
|
20
|
-
ConnectionType,
|
|
21
20
|
DeviceConfig,
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
DeviceConnectionParameters,
|
|
22
|
+
DeviceEncryptionType,
|
|
23
|
+
DeviceFamily,
|
|
24
24
|
)
|
|
25
25
|
from kasa.discover import DiscoveryResult
|
|
26
26
|
from kasa.smart.smartdevice import SmartDevice
|
|
@@ -31,12 +31,12 @@ def _get_connection_type_device_class(discovery_info):
|
|
|
31
31
|
device_class = Discover._get_device_class(discovery_info)
|
|
32
32
|
dr = DiscoveryResult(**discovery_info["result"])
|
|
33
33
|
|
|
34
|
-
connection_type =
|
|
34
|
+
connection_type = DeviceConnectionParameters.from_values(
|
|
35
35
|
dr.device_type, dr.mgt_encrypt_schm.encrypt_type
|
|
36
36
|
)
|
|
37
37
|
else:
|
|
38
|
-
connection_type =
|
|
39
|
-
|
|
38
|
+
connection_type = DeviceConnectionParameters.from_values(
|
|
39
|
+
DeviceFamily.IotSmartPlugSwitch.value, DeviceEncryptionType.Xor.value
|
|
40
40
|
)
|
|
41
41
|
device_class = Discover._get_device_class(discovery_info)
|
|
42
42
|
|
|
@@ -137,7 +137,7 @@ async def test_connect_http_client(discovery_data, mocker):
|
|
|
137
137
|
host=host, credentials=Credentials("foor", "bar"), connection_type=ctype
|
|
138
138
|
)
|
|
139
139
|
dev = await connect(config=config)
|
|
140
|
-
if ctype.encryption_type !=
|
|
140
|
+
if ctype.encryption_type != DeviceEncryptionType.Xor:
|
|
141
141
|
assert dev.protocol._transport._http_client.client != http_client
|
|
142
142
|
await dev.disconnect()
|
|
143
143
|
|
|
@@ -148,7 +148,7 @@ async def test_connect_http_client(discovery_data, mocker):
|
|
|
148
148
|
http_client=http_client,
|
|
149
149
|
)
|
|
150
150
|
dev = await connect(config=config)
|
|
151
|
-
if ctype.encryption_type !=
|
|
151
|
+
if ctype.encryption_type != DeviceEncryptionType.Xor:
|
|
152
152
|
assert dev.protocol._transport._http_client.client == http_client
|
|
153
153
|
await dev.disconnect()
|
|
154
154
|
await http_client.close()
|