python-kasa 0.6.2__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 +12 -7
- 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 -551
- kasa/tests/device_fixtures.py +427 -0
- kasa/tests/discovery_fixtures.py +197 -0
- kasa/tests/{newfakes.py → fakeprotocol_iot.py} +61 -291
- kasa/tests/fakeprotocol_smart.py +318 -0
- 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/P300(EU)_1.0_1.0.13.json +13 -23
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.7.json +6 -6
- kasa/tests/fixtures/smart/TP15(US)_1.0_1.0.3.json +295 -0
- kasa/tests/fixtures/smart/TP25(US)_1.0_1.0.2.json +423 -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 -25
- kasa/tests/test_bulb.py +169 -86
- kasa/tests/test_childdevice.py +84 -0
- kasa/tests/test_cli.py +452 -100
- kasa/tests/test_common_modules.py +216 -0
- kasa/tests/test_device.py +268 -0
- kasa/tests/test_device_factory.py +61 -41
- kasa/tests/test_device_type.py +1 -1
- kasa/tests/test_deviceconfig.py +2 -5
- kasa/tests/test_dimmer.py +33 -24
- kasa/tests/test_discovery.py +112 -69
- kasa/tests/test_emeter.py +39 -12
- 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 -41
- kasa/tests/test_lightstrip.py +24 -19
- kasa/tests/test_plug.py +51 -9
- kasa/tests/test_protocol.py +8 -12
- kasa/tests/test_readme_examples.py +86 -27
- kasa/tests/test_smartdevice.py +211 -247
- kasa/tests/test_smartprotocol.py +129 -26
- kasa/tests/test_strip.py +15 -6
- kasa/tests/test_usage.py +1 -3
- kasa/xortransport.py +15 -12
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -141
- python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +1 -1
- 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 -44
- kasa/tapo/tapobulb.py +0 -262
- kasa/tapo/tapodevice.py +0 -383
- 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.dist-info/RECORD +0 -163
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
kasa/tests/test_smartdevice.py
CHANGED
|
@@ -1,265 +1,229 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
from
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
from
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
1
|
+
"""Tests for SMART devices."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from typing import Any
|
|
7
|
+
from unittest.mock import patch
|
|
8
|
+
|
|
9
|
+
import pytest
|
|
10
|
+
from pytest_mock import MockerFixture
|
|
11
|
+
|
|
12
|
+
from kasa import KasaException, Module
|
|
13
|
+
from kasa.exceptions import SmartErrorCode
|
|
14
|
+
from kasa.smart import SmartDevice
|
|
15
|
+
|
|
16
|
+
from .conftest import (
|
|
17
|
+
device_smart,
|
|
18
|
+
get_device_for_fixture_protocol,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@device_smart
|
|
23
|
+
async def test_try_get_response(dev: SmartDevice, caplog):
|
|
24
|
+
mock_response: dict = {
|
|
25
|
+
"get_device_info": SmartErrorCode.PARAMS_ERROR,
|
|
26
|
+
}
|
|
27
|
+
caplog.set_level(logging.DEBUG)
|
|
28
|
+
dev._try_get_response(mock_response, "get_device_info", {})
|
|
29
|
+
msg = "Error PARAMS_ERROR(-1008) getting request get_device_info for device 127.0.0.123"
|
|
30
|
+
assert msg in caplog.text
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@device_smart
|
|
34
|
+
async def test_update_no_device_info(dev: SmartDevice, mocker: MockerFixture):
|
|
35
|
+
mock_response: dict = {
|
|
36
|
+
"get_device_usage": {},
|
|
37
|
+
"get_device_time": {},
|
|
38
|
+
}
|
|
39
|
+
msg = f"get_device_info not found in {mock_response} for device 127.0.0.123"
|
|
40
|
+
with mocker.patch.object(
|
|
41
|
+
dev.protocol, "query", return_value=mock_response
|
|
42
|
+
), pytest.raises(KasaException, match=msg):
|
|
43
|
+
await dev.update()
|
|
23
44
|
|
|
24
|
-
@device_iot
|
|
25
|
-
async def test_state_info(dev):
|
|
26
|
-
assert isinstance(dev.state_information, dict)
|
|
27
45
|
|
|
46
|
+
@device_smart
|
|
47
|
+
async def test_initial_update(dev: SmartDevice, mocker: MockerFixture):
|
|
48
|
+
"""Test the initial update cycle."""
|
|
49
|
+
# As the fixture data is already initialized, we reset the state for testing
|
|
50
|
+
dev._components_raw = None
|
|
51
|
+
dev._components = {}
|
|
52
|
+
dev._modules = {}
|
|
53
|
+
dev._features = {}
|
|
54
|
+
dev._children = {}
|
|
28
55
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
with patch.object(
|
|
33
|
-
FakeTransportProtocol, "query", side_effect=SmartDeviceException
|
|
34
|
-
), pytest.raises(SmartDeviceException):
|
|
35
|
-
await dev.update()
|
|
56
|
+
negotiate = mocker.spy(dev, "_negotiate")
|
|
57
|
+
initialize_modules = mocker.spy(dev, "_initialize_modules")
|
|
58
|
+
initialize_features = mocker.spy(dev, "_initialize_features")
|
|
36
59
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
async def test_initial_update_emeter(dev, mocker):
|
|
40
|
-
"""Test that the initial update performs second query if emeter is available."""
|
|
41
|
-
dev._last_update = None
|
|
42
|
-
dev._features = set()
|
|
43
|
-
spy = mocker.spy(dev.protocol, "query")
|
|
60
|
+
# Perform two updates and verify that initialization is only done once
|
|
61
|
+
await dev.update()
|
|
44
62
|
await dev.update()
|
|
45
|
-
# Devices with small buffers may require 3 queries
|
|
46
|
-
expected_queries = 2 if dev.max_device_response_size > 4096 else 3
|
|
47
|
-
assert spy.call_count == expected_queries + len(dev.children)
|
|
48
63
|
|
|
64
|
+
negotiate.assert_called_once()
|
|
65
|
+
assert dev._components_raw is not None
|
|
66
|
+
initialize_modules.assert_called_once()
|
|
67
|
+
assert dev.modules
|
|
68
|
+
initialize_features.assert_called_once()
|
|
69
|
+
assert dev.features
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@device_smart
|
|
73
|
+
async def test_negotiate(dev: SmartDevice, mocker: MockerFixture):
|
|
74
|
+
"""Test that the initial negotiation performs expected steps."""
|
|
75
|
+
# As the fixture data is already initialized, we reset the state for testing
|
|
76
|
+
dev._components_raw = None
|
|
77
|
+
dev._children = {}
|
|
78
|
+
|
|
79
|
+
query = mocker.spy(dev.protocol, "query")
|
|
80
|
+
initialize_children = mocker.spy(dev, "_initialize_children")
|
|
81
|
+
await dev._negotiate()
|
|
82
|
+
|
|
83
|
+
# Check that we got the initial negotiation call
|
|
84
|
+
query.assert_any_call(
|
|
85
|
+
{
|
|
86
|
+
"component_nego": None,
|
|
87
|
+
"get_device_info": None,
|
|
88
|
+
"get_connect_cloud_state": None,
|
|
89
|
+
}
|
|
90
|
+
)
|
|
91
|
+
assert dev._components_raw
|
|
92
|
+
|
|
93
|
+
# Check the children are created, if device supports them
|
|
94
|
+
if "child_device" in dev._components:
|
|
95
|
+
initialize_children.assert_called_once()
|
|
96
|
+
query.assert_any_call(
|
|
97
|
+
{
|
|
98
|
+
"get_child_device_component_list": None,
|
|
99
|
+
"get_child_device_list": None,
|
|
100
|
+
}
|
|
101
|
+
)
|
|
102
|
+
assert len(dev._children) == dev.internal_state["get_child_device_list"]["sum"]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
@device_smart
|
|
106
|
+
async def test_update_module_queries(dev: SmartDevice, mocker: MockerFixture):
|
|
107
|
+
"""Test that the regular update uses queries from all supported modules."""
|
|
108
|
+
# We need to have some modules initialized by now
|
|
109
|
+
assert dev._modules
|
|
110
|
+
|
|
111
|
+
device_queries: dict[SmartDevice, dict[str, Any]] = {}
|
|
112
|
+
for mod in dev._modules.values():
|
|
113
|
+
device_queries.setdefault(mod._device, {}).update(mod.query())
|
|
114
|
+
|
|
115
|
+
spies = {}
|
|
116
|
+
for device in device_queries:
|
|
117
|
+
spies[device] = mocker.spy(device.protocol, "query")
|
|
49
118
|
|
|
50
|
-
@no_emeter_iot
|
|
51
|
-
async def test_initial_update_no_emeter(dev, mocker):
|
|
52
|
-
"""Test that the initial update performs second query if emeter is available."""
|
|
53
|
-
dev._last_update = None
|
|
54
|
-
dev._features = set()
|
|
55
|
-
spy = mocker.spy(dev.protocol, "query")
|
|
56
119
|
await dev.update()
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
120
|
+
for device in device_queries:
|
|
121
|
+
if device_queries[device]:
|
|
122
|
+
spies[device].assert_called_with(device_queries[device])
|
|
123
|
+
else:
|
|
124
|
+
spies[device].assert_not_called()
|
|
60
125
|
|
|
61
126
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
127
|
+
async def test_get_modules():
|
|
128
|
+
"""Test getting modules for child and parent modules."""
|
|
129
|
+
dummy_device = await get_device_for_fixture_protocol(
|
|
130
|
+
"KS240(US)_1.0_1.0.5.json", "SMART"
|
|
131
|
+
)
|
|
132
|
+
from kasa.smart.modules import Cloud
|
|
133
|
+
|
|
134
|
+
# Modules on device
|
|
135
|
+
module = dummy_device.modules.get("Cloud")
|
|
136
|
+
assert module
|
|
137
|
+
assert module._device == dummy_device
|
|
138
|
+
assert isinstance(module, Cloud)
|
|
139
|
+
|
|
140
|
+
module = dummy_device.modules.get(Module.Cloud)
|
|
141
|
+
assert module
|
|
142
|
+
assert module._device == dummy_device
|
|
143
|
+
assert isinstance(module, Cloud)
|
|
144
|
+
|
|
145
|
+
# Modules on child
|
|
146
|
+
module = dummy_device.modules.get("Fan")
|
|
147
|
+
assert module
|
|
148
|
+
assert module._device != dummy_device
|
|
149
|
+
assert module._device._parent == dummy_device
|
|
150
|
+
|
|
151
|
+
module = dummy_device.modules.get(Module.Fan)
|
|
152
|
+
assert module
|
|
153
|
+
assert module._device != dummy_device
|
|
154
|
+
assert module._device._parent == dummy_device
|
|
155
|
+
|
|
156
|
+
# Invalid modules
|
|
157
|
+
module = dummy_device.modules.get("DummyModule")
|
|
158
|
+
assert module is None
|
|
159
|
+
|
|
160
|
+
module = dummy_device.modules.get(Module.IotAmbientLight)
|
|
161
|
+
assert module is None
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@device_smart
|
|
165
|
+
async def test_smartdevice_cloud_connection(dev: SmartDevice, mocker: MockerFixture):
|
|
166
|
+
"""Test is_cloud_connected property."""
|
|
167
|
+
assert isinstance(dev, SmartDevice)
|
|
168
|
+
assert "cloud_connect" in dev._components
|
|
169
|
+
|
|
170
|
+
is_connected = (
|
|
171
|
+
(cc := dev._last_update.get("get_connect_cloud_state"))
|
|
172
|
+
and not isinstance(cc, SmartErrorCode)
|
|
173
|
+
and cc["status"] == 0
|
|
174
|
+
)
|
|
67
175
|
|
|
176
|
+
assert dev.is_cloud_connected == is_connected
|
|
177
|
+
last_update = dev._last_update
|
|
68
178
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
async def test_state(dev, turn_on):
|
|
72
|
-
await handle_turn_on(dev, turn_on)
|
|
73
|
-
orig_state = dev.is_on
|
|
74
|
-
if orig_state:
|
|
75
|
-
await dev.turn_off()
|
|
179
|
+
last_update["get_connect_cloud_state"] = {"status": 0}
|
|
180
|
+
with patch.object(dev.protocol, "query", return_value=last_update):
|
|
76
181
|
await dev.update()
|
|
77
|
-
assert
|
|
78
|
-
assert dev.is_off
|
|
182
|
+
assert dev.is_cloud_connected is True
|
|
79
183
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
assert dev.is_on
|
|
83
|
-
assert not dev.is_off
|
|
84
|
-
else:
|
|
85
|
-
await dev.turn_on()
|
|
184
|
+
last_update["get_connect_cloud_state"] = {"status": 1}
|
|
185
|
+
with patch.object(dev.protocol, "query", return_value=last_update):
|
|
86
186
|
await dev.update()
|
|
87
|
-
assert dev.
|
|
88
|
-
assert not dev.is_off
|
|
187
|
+
assert dev.is_cloud_connected is False
|
|
89
188
|
|
|
90
|
-
|
|
189
|
+
last_update["get_connect_cloud_state"] = SmartErrorCode.UNKNOWN_METHOD_ERROR
|
|
190
|
+
with patch.object(dev.protocol, "query", return_value=last_update):
|
|
91
191
|
await dev.update()
|
|
92
|
-
assert
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
@device_iot
|
|
125
|
-
async def test_time(dev):
|
|
126
|
-
assert isinstance(await dev.get_time(), datetime)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
@device_iot
|
|
130
|
-
async def test_timezone(dev):
|
|
131
|
-
TZ_SCHEMA(await dev.get_timezone())
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
@device_iot
|
|
135
|
-
async def test_hw_info(dev):
|
|
136
|
-
PLUG_SCHEMA(dev.hw_info)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
@device_iot
|
|
140
|
-
async def test_location(dev):
|
|
141
|
-
PLUG_SCHEMA(dev.location)
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
@device_iot
|
|
145
|
-
async def test_rssi(dev):
|
|
146
|
-
PLUG_SCHEMA({"rssi": dev.rssi}) # wrapping for vol
|
|
192
|
+
assert dev.is_cloud_connected is False
|
|
193
|
+
|
|
194
|
+
# Test for no cloud_connect component during device initialisation
|
|
195
|
+
component_list = [
|
|
196
|
+
val
|
|
197
|
+
for val in dev._components_raw["component_list"]
|
|
198
|
+
if val["id"] not in {"cloud_connect"}
|
|
199
|
+
]
|
|
200
|
+
initial_response = {
|
|
201
|
+
"component_nego": {"component_list": component_list},
|
|
202
|
+
"get_connect_cloud_state": last_update["get_connect_cloud_state"],
|
|
203
|
+
"get_device_info": last_update["get_device_info"],
|
|
204
|
+
}
|
|
205
|
+
# Child component list is not stored on the device
|
|
206
|
+
if "get_child_device_list" in last_update:
|
|
207
|
+
child_component_list = await dev.protocol.query(
|
|
208
|
+
"get_child_device_component_list"
|
|
209
|
+
)
|
|
210
|
+
last_update["get_child_device_component_list"] = child_component_list[
|
|
211
|
+
"get_child_device_component_list"
|
|
212
|
+
]
|
|
213
|
+
new_dev = SmartDevice("127.0.0.1", protocol=dev.protocol)
|
|
214
|
+
|
|
215
|
+
first_call = True
|
|
216
|
+
|
|
217
|
+
def side_effect_func(*_, **__):
|
|
218
|
+
nonlocal first_call
|
|
219
|
+
resp = initial_response if first_call else last_update
|
|
220
|
+
first_call = False
|
|
221
|
+
return resp
|
|
147
222
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
async def test_representation(dev):
|
|
156
|
-
import re
|
|
157
|
-
|
|
158
|
-
pattern = re.compile("<.* model .* at .* (.*), is_on: .* - dev specific: .*>")
|
|
159
|
-
assert pattern.match(str(dev))
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
@device_iot
|
|
163
|
-
async def test_childrens(dev):
|
|
164
|
-
"""Make sure that children property is exposed by every device."""
|
|
165
|
-
if dev.is_strip:
|
|
166
|
-
assert len(dev.children) > 0
|
|
167
|
-
else:
|
|
168
|
-
assert len(dev.children) == 0
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
@device_iot
|
|
172
|
-
async def test_children(dev):
|
|
173
|
-
"""Make sure that children property is exposed by every device."""
|
|
174
|
-
if dev.is_strip:
|
|
175
|
-
assert len(dev.children) > 0
|
|
176
|
-
assert dev.has_children is True
|
|
177
|
-
else:
|
|
178
|
-
assert len(dev.children) == 0
|
|
179
|
-
assert dev.has_children is False
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
@device_iot
|
|
183
|
-
async def test_internal_state(dev):
|
|
184
|
-
"""Make sure the internal state returns the last update results."""
|
|
185
|
-
assert dev.internal_state == dev._last_update
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
@device_iot
|
|
189
|
-
async def test_features(dev):
|
|
190
|
-
"""Make sure features is always accessible."""
|
|
191
|
-
sysinfo = dev._last_update["system"]["get_sysinfo"]
|
|
192
|
-
if "feature" in sysinfo:
|
|
193
|
-
assert dev.features == set(sysinfo["feature"].split(":"))
|
|
194
|
-
else:
|
|
195
|
-
assert dev.features == set()
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
@device_iot
|
|
199
|
-
async def test_max_device_response_size(dev):
|
|
200
|
-
"""Make sure every device return has a set max response size."""
|
|
201
|
-
assert dev.max_device_response_size > 0
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
@device_iot
|
|
205
|
-
async def test_estimated_response_sizes(dev):
|
|
206
|
-
"""Make sure every module has an estimated response size set."""
|
|
207
|
-
for mod in dev.modules.values():
|
|
208
|
-
assert mod.estimated_query_response_size > 0
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
@pytest.mark.parametrize("device_class", smart_device_classes)
|
|
212
|
-
def test_device_class_ctors(device_class):
|
|
213
|
-
"""Make sure constructor api not broken for new and existing SmartDevices."""
|
|
214
|
-
host = "127.0.0.2"
|
|
215
|
-
port = 1234
|
|
216
|
-
credentials = Credentials("foo", "bar")
|
|
217
|
-
config = DeviceConfig(host, port_override=port, credentials=credentials)
|
|
218
|
-
dev = device_class(host, config=config)
|
|
219
|
-
assert dev.host == host
|
|
220
|
-
assert dev.port == port
|
|
221
|
-
assert dev.credentials == credentials
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
@device_iot
|
|
225
|
-
async def test_modules_preserved(dev: SmartDevice):
|
|
226
|
-
"""Make modules that are not being updated are preserved between updates."""
|
|
227
|
-
dev._last_update["some_module_not_being_updated"] = "should_be_kept"
|
|
228
|
-
await dev.update()
|
|
229
|
-
assert dev._last_update["some_module_not_being_updated"] == "should_be_kept"
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
async def test_create_smart_device_with_timeout():
|
|
233
|
-
"""Make sure timeout is passed to the protocol."""
|
|
234
|
-
host = "127.0.0.1"
|
|
235
|
-
dev = SmartDevice(host, config=DeviceConfig(host, timeout=100))
|
|
236
|
-
assert dev.protocol._transport._timeout == 100
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
async def test_create_thin_wrapper():
|
|
240
|
-
"""Make sure thin wrapper is created with the correct device type."""
|
|
241
|
-
mock = Mock()
|
|
242
|
-
config = DeviceConfig(
|
|
243
|
-
host="test_host",
|
|
244
|
-
port_override=1234,
|
|
245
|
-
timeout=100,
|
|
246
|
-
credentials=Credentials("username", "password"),
|
|
247
|
-
)
|
|
248
|
-
with patch("kasa.device_factory.connect", return_value=mock) as connect:
|
|
249
|
-
dev = await SmartDevice.connect(config=config)
|
|
250
|
-
assert dev is mock
|
|
251
|
-
|
|
252
|
-
connect.assert_called_once_with(
|
|
253
|
-
host=None,
|
|
254
|
-
config=config,
|
|
255
|
-
)
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
@device_iot
|
|
259
|
-
async def test_modules_not_supported(dev: SmartDevice):
|
|
260
|
-
"""Test that unsupported modules do not break the device."""
|
|
261
|
-
for module in dev.modules.values():
|
|
262
|
-
assert module.is_supported is not None
|
|
263
|
-
await dev.update()
|
|
264
|
-
for module in dev.modules.values():
|
|
265
|
-
assert module.is_supported is not None
|
|
223
|
+
with patch.object(
|
|
224
|
+
new_dev.protocol,
|
|
225
|
+
"query",
|
|
226
|
+
side_effect=side_effect_func,
|
|
227
|
+
):
|
|
228
|
+
await new_dev.update()
|
|
229
|
+
assert new_dev.is_cloud_connected is False
|
kasa/tests/test_smartprotocol.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
from typing import Dict
|
|
1
|
+
import logging
|
|
3
2
|
|
|
4
3
|
import pytest
|
|
5
4
|
|
|
@@ -7,12 +6,11 @@ from ..credentials import Credentials
|
|
|
7
6
|
from ..deviceconfig import DeviceConfig
|
|
8
7
|
from ..exceptions import (
|
|
9
8
|
SMART_RETRYABLE_ERRORS,
|
|
10
|
-
|
|
11
|
-
SmartDeviceException,
|
|
9
|
+
KasaException,
|
|
12
10
|
SmartErrorCode,
|
|
13
11
|
)
|
|
14
|
-
from ..protocol import BaseTransport
|
|
15
12
|
from ..smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
|
13
|
+
from .fakeprotocol_smart import FakeSmartTransport
|
|
16
14
|
|
|
17
15
|
DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
|
|
18
16
|
DUMMY_MULTIPLE_QUERY = {
|
|
@@ -30,13 +28,10 @@ async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
|
|
30
28
|
dummy_protocol._transport, "send", return_value=mock_response
|
|
31
29
|
)
|
|
32
30
|
|
|
33
|
-
with pytest.raises(
|
|
31
|
+
with pytest.raises(KasaException):
|
|
34
32
|
await dummy_protocol.query(DUMMY_QUERY, retry_count=2)
|
|
35
33
|
|
|
36
|
-
if error_code in
|
|
37
|
-
expected_calls = 3
|
|
38
|
-
else:
|
|
39
|
-
expected_calls = 1
|
|
34
|
+
expected_calls = 3 if error_code in SMART_RETRYABLE_ERRORS else 1
|
|
40
35
|
assert send_mock.call_count == expected_calls
|
|
41
36
|
|
|
42
37
|
|
|
@@ -44,6 +39,11 @@ async def test_smart_device_errors(dummy_protocol, mocker, error_code):
|
|
|
44
39
|
async def test_smart_device_errors_in_multiple_request(
|
|
45
40
|
dummy_protocol, mocker, error_code
|
|
46
41
|
):
|
|
42
|
+
mock_request = {
|
|
43
|
+
"foobar1": {"foo": "bar", "bar": "foo"},
|
|
44
|
+
"foobar2": {"foo": "bar", "bar": "foo"},
|
|
45
|
+
"foobar3": {"foo": "bar", "bar": "foo"},
|
|
46
|
+
}
|
|
47
47
|
mock_response = {
|
|
48
48
|
"result": {
|
|
49
49
|
"responses": [
|
|
@@ -62,13 +62,11 @@ async def test_smart_device_errors_in_multiple_request(
|
|
|
62
62
|
send_mock = mocker.patch.object(
|
|
63
63
|
dummy_protocol._transport, "send", return_value=mock_response
|
|
64
64
|
)
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
expected_calls = 1
|
|
71
|
-
assert send_mock.call_count == expected_calls
|
|
65
|
+
|
|
66
|
+
resp_dict = await dummy_protocol.query(mock_request, retry_count=2)
|
|
67
|
+
assert resp_dict["foobar2"] == error_code
|
|
68
|
+
assert send_mock.call_count == 1
|
|
69
|
+
assert len(resp_dict) == len(mock_request)
|
|
72
70
|
|
|
73
71
|
|
|
74
72
|
@pytest.mark.parametrize("request_size", [1, 3, 5, 10])
|
|
@@ -129,11 +127,10 @@ async def test_childdevicewrapper_error(dummy_protocol, mocker):
|
|
|
129
127
|
mock_response = {"error_code": 0, "result": {"responseData": {"error_code": -1001}}}
|
|
130
128
|
|
|
131
129
|
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
132
|
-
with pytest.raises(
|
|
130
|
+
with pytest.raises(KasaException):
|
|
133
131
|
await wrapped_protocol.query(DUMMY_QUERY)
|
|
134
132
|
|
|
135
133
|
|
|
136
|
-
@pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
|
|
137
134
|
async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker):
|
|
138
135
|
"""Test that unwrapping multiplerequest works correctly."""
|
|
139
136
|
mock_response = {
|
|
@@ -157,13 +154,12 @@ async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, moc
|
|
|
157
154
|
}
|
|
158
155
|
},
|
|
159
156
|
}
|
|
160
|
-
|
|
161
|
-
mocker.patch.object(
|
|
162
|
-
resp = await
|
|
157
|
+
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
158
|
+
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
159
|
+
resp = await wrapped_protocol.query(DUMMY_QUERY)
|
|
163
160
|
assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}}
|
|
164
161
|
|
|
165
162
|
|
|
166
|
-
@pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
|
|
167
163
|
async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
|
|
168
164
|
"""Test that errors inside multipleRequest response of responseData raise an exception."""
|
|
169
165
|
mock_response = {
|
|
@@ -183,7 +179,114 @@ async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
|
|
|
183
179
|
}
|
|
184
180
|
},
|
|
185
181
|
}
|
|
182
|
+
wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
|
|
183
|
+
mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
|
|
184
|
+
with pytest.raises(KasaException):
|
|
185
|
+
await wrapped_protocol.query(DUMMY_QUERY)
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
|
189
|
+
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
|
190
|
+
async def test_smart_protocol_lists_single_request(mocker, list_sum, batch_size):
|
|
191
|
+
child_device_list = [{"foo": i} for i in range(list_sum)]
|
|
192
|
+
response = {
|
|
193
|
+
"get_child_device_list": {
|
|
194
|
+
"child_device_list": child_device_list,
|
|
195
|
+
"start_index": 0,
|
|
196
|
+
"sum": list_sum,
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
request = {"get_child_device_list": None}
|
|
200
|
+
|
|
201
|
+
ft = FakeSmartTransport(
|
|
202
|
+
response,
|
|
203
|
+
"foobar",
|
|
204
|
+
list_return_size=batch_size,
|
|
205
|
+
component_nego_not_included=True,
|
|
206
|
+
)
|
|
207
|
+
protocol = SmartProtocol(transport=ft)
|
|
208
|
+
query_spy = mocker.spy(protocol, "_execute_query")
|
|
209
|
+
resp = await protocol.query(request)
|
|
210
|
+
expected_count = int(list_sum / batch_size) + (1 if list_sum % batch_size else 0)
|
|
211
|
+
assert query_spy.call_count == expected_count
|
|
212
|
+
assert resp == response
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
@pytest.mark.parametrize("list_sum", [5, 10, 30])
|
|
216
|
+
@pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
|
|
217
|
+
async def test_smart_protocol_lists_multiple_request(mocker, list_sum, batch_size):
|
|
218
|
+
child_list = [{"foo": i} for i in range(list_sum)]
|
|
219
|
+
response = {
|
|
220
|
+
"get_child_device_list": {
|
|
221
|
+
"child_device_list": child_list,
|
|
222
|
+
"start_index": 0,
|
|
223
|
+
"sum": list_sum,
|
|
224
|
+
},
|
|
225
|
+
"get_child_device_component_list": {
|
|
226
|
+
"child_component_list": child_list,
|
|
227
|
+
"start_index": 0,
|
|
228
|
+
"sum": list_sum,
|
|
229
|
+
},
|
|
230
|
+
}
|
|
231
|
+
request = {"get_child_device_list": None, "get_child_device_component_list": None}
|
|
232
|
+
|
|
233
|
+
ft = FakeSmartTransport(
|
|
234
|
+
response,
|
|
235
|
+
"foobar",
|
|
236
|
+
list_return_size=batch_size,
|
|
237
|
+
component_nego_not_included=True,
|
|
238
|
+
)
|
|
239
|
+
protocol = SmartProtocol(transport=ft)
|
|
240
|
+
query_spy = mocker.spy(protocol, "_execute_query")
|
|
241
|
+
resp = await protocol.query(request)
|
|
242
|
+
expected_count = 1 + 2 * (
|
|
243
|
+
int(list_sum / batch_size) + (0 if list_sum % batch_size else -1)
|
|
244
|
+
)
|
|
245
|
+
assert query_spy.call_count == expected_count
|
|
246
|
+
assert resp == response
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
async def test_incomplete_list(mocker, caplog):
|
|
250
|
+
"""Test for handling incomplete lists returned from queries."""
|
|
251
|
+
info = {
|
|
252
|
+
"get_preset_rules": {
|
|
253
|
+
"start_index": 0,
|
|
254
|
+
"states": [
|
|
255
|
+
{
|
|
256
|
+
"brightness": 50,
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
"brightness": 100,
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
"sum": 7,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
caplog.set_level(logging.ERROR)
|
|
266
|
+
transport = FakeSmartTransport(
|
|
267
|
+
info,
|
|
268
|
+
"dummy-name",
|
|
269
|
+
component_nego_not_included=True,
|
|
270
|
+
warn_fixture_missing_methods=False,
|
|
271
|
+
)
|
|
272
|
+
protocol = SmartProtocol(transport=transport)
|
|
273
|
+
resp = await protocol.query({"get_preset_rules": None})
|
|
274
|
+
assert resp
|
|
275
|
+
assert resp["get_preset_rules"]["sum"] == 2 # FakeTransport fixes sum
|
|
276
|
+
assert caplog.text == ""
|
|
186
277
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
278
|
+
# Test behaviour without FakeTranport fix
|
|
279
|
+
transport = FakeSmartTransport(
|
|
280
|
+
info,
|
|
281
|
+
"dummy-name",
|
|
282
|
+
component_nego_not_included=True,
|
|
283
|
+
warn_fixture_missing_methods=False,
|
|
284
|
+
fix_incomplete_fixture_lists=False,
|
|
285
|
+
)
|
|
286
|
+
protocol = SmartProtocol(transport=transport)
|
|
287
|
+
resp = await protocol.query({"get_preset_rules": None})
|
|
288
|
+
assert resp["get_preset_rules"]["sum"] == 7
|
|
289
|
+
assert (
|
|
290
|
+
"Device 127.0.0.123 returned empty results list for method get_preset_rules"
|
|
291
|
+
in caplog.text
|
|
292
|
+
)
|