python-kasa 0.6.2.1__py3-none-any.whl → 0.7.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. kasa/__init__.py +88 -27
  2. kasa/aestransport.py +51 -34
  3. kasa/cli.py +326 -130
  4. kasa/device.py +406 -0
  5. kasa/device_factory.py +83 -44
  6. kasa/device_type.py +7 -3
  7. kasa/deviceconfig.py +16 -9
  8. kasa/discover.py +100 -81
  9. kasa/emeterstatus.py +7 -5
  10. kasa/exceptions.py +41 -26
  11. kasa/feature.py +201 -0
  12. kasa/httpclient.py +21 -14
  13. kasa/interfaces/__init__.py +16 -0
  14. kasa/interfaces/fan.py +20 -0
  15. kasa/interfaces/led.py +38 -0
  16. kasa/interfaces/light.py +133 -0
  17. kasa/interfaces/lighteffect.py +80 -0
  18. kasa/interfaces/lightpreset.py +76 -0
  19. kasa/iot/__init__.py +18 -0
  20. kasa/{effects.py → iot/effects.py} +4 -2
  21. kasa/{smartbulb.py → iot/iotbulb.py} +100 -147
  22. kasa/{smartdevice.py → iot/iotdevice.py} +157 -286
  23. kasa/{smartdimmer.py → iot/iotdimmer.py} +42 -33
  24. kasa/iot/iotlightstrip.py +71 -0
  25. kasa/{modules/module.py → iot/iotmodule.py} +22 -42
  26. kasa/iot/iotplug.py +94 -0
  27. kasa/{smartstrip.py → iot/iotstrip.py} +57 -75
  28. kasa/{modules → iot/modules}/__init__.py +10 -2
  29. kasa/{modules → iot/modules}/ambientlight.py +31 -3
  30. kasa/{modules → iot/modules}/antitheft.py +1 -0
  31. kasa/{modules → iot/modules}/cloud.py +25 -6
  32. kasa/{modules → iot/modules}/countdown.py +1 -0
  33. kasa/iot/modules/emeter.py +210 -0
  34. kasa/iot/modules/led.py +32 -0
  35. kasa/iot/modules/light.py +221 -0
  36. kasa/iot/modules/lighteffect.py +122 -0
  37. kasa/iot/modules/lightpreset.py +151 -0
  38. kasa/{modules → iot/modules}/motion.py +8 -8
  39. kasa/{modules → iot/modules}/rulemodule.py +11 -12
  40. kasa/{modules → iot/modules}/schedule.py +1 -0
  41. kasa/{modules → iot/modules}/time.py +5 -4
  42. kasa/{modules → iot/modules}/usage.py +10 -8
  43. kasa/iotprotocol.py +22 -20
  44. kasa/klaptransport.py +25 -15
  45. kasa/module.py +136 -0
  46. kasa/modulemapping.py +25 -0
  47. kasa/modulemapping.pyi +96 -0
  48. kasa/protocol.py +6 -4
  49. kasa/smart/__init__.py +6 -0
  50. kasa/smart/effects.py +429 -0
  51. kasa/smart/modules/__init__.py +57 -0
  52. kasa/smart/modules/alarm.py +142 -0
  53. kasa/smart/modules/autooff.py +96 -0
  54. kasa/smart/modules/batterysensor.py +54 -0
  55. kasa/smart/modules/brightness.py +66 -0
  56. kasa/smart/modules/childdevice.py +10 -0
  57. kasa/smart/modules/cloud.py +42 -0
  58. kasa/smart/modules/color.py +96 -0
  59. kasa/smart/modules/colortemperature.py +78 -0
  60. kasa/smart/modules/contactsensor.py +42 -0
  61. kasa/smart/modules/devicemodule.py +22 -0
  62. kasa/smart/modules/energy.py +101 -0
  63. kasa/smart/modules/fan.py +80 -0
  64. kasa/smart/modules/firmware.py +216 -0
  65. kasa/smart/modules/frostprotection.py +59 -0
  66. kasa/smart/modules/humiditysensor.py +55 -0
  67. kasa/smart/modules/led.py +49 -0
  68. kasa/smart/modules/light.py +139 -0
  69. kasa/smart/modules/lighteffect.py +115 -0
  70. kasa/smart/modules/lightpreset.py +142 -0
  71. kasa/smart/modules/lightstripeffect.py +135 -0
  72. kasa/smart/modules/lighttransition.py +183 -0
  73. kasa/smart/modules/reportmode.py +36 -0
  74. kasa/smart/modules/temperaturecontrol.py +177 -0
  75. kasa/smart/modules/temperaturesensor.py +77 -0
  76. kasa/smart/modules/time.py +57 -0
  77. kasa/smart/modules/waterleaksensor.py +66 -0
  78. kasa/smart/smartchilddevice.py +65 -0
  79. kasa/smart/smartdevice.py +653 -0
  80. kasa/smart/smartmodule.py +112 -0
  81. kasa/smartprotocol.py +107 -41
  82. kasa/tests/conftest.py +27 -562
  83. kasa/tests/device_fixtures.py +427 -0
  84. kasa/tests/discovery_fixtures.py +197 -0
  85. kasa/tests/fakeprotocol_iot.py +64 -6
  86. kasa/tests/fakeprotocol_smart.py +215 -22
  87. kasa/tests/fixtureinfo.py +154 -0
  88. kasa/tests/fixtures/smart/H100(EU)_1.0_1.2.3.json +221 -0
  89. kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.10.json +547 -0
  90. kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.5.json +391 -0
  91. kasa/tests/fixtures/smart/KH100(UK)_1.0_1.5.6.json +1557 -0
  92. kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.4.json +899 -0
  93. kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.5.json +479 -0
  94. kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +449 -0
  95. kasa/tests/fixtures/smart/P100_1.0.0_1.1.3.json +173 -0
  96. kasa/tests/fixtures/smart/P100_1.0.0_1.4.0.json +204 -0
  97. kasa/tests/fixtures/smart/P110(EU)_1.0_1.0.7.json +566 -0
  98. kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.4.0.json +170 -0
  99. kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.8.0.json +171 -0
  100. kasa/tests/fixtures/smart/child/KE100(UK)_1.0_2.8.0.json +171 -0
  101. kasa/tests/fixtures/smart/child/T110(EU)_1.0_1.8.0.json +526 -0
  102. kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json +533 -0
  103. kasa/tests/fixtures/smart/child/T310(EU)_1.0_1.5.0.json +530 -0
  104. kasa/tests/fixtures/smart/child/T315(EU)_1.0_1.7.0.json +537 -0
  105. kasa/tests/smart/__init__.py +0 -0
  106. kasa/tests/smart/features/__init__.py +0 -0
  107. kasa/tests/smart/features/test_brightness.py +55 -0
  108. kasa/tests/smart/features/test_colortemp.py +30 -0
  109. kasa/tests/smart/modules/__init__.py +0 -0
  110. kasa/tests/smart/modules/test_contact.py +28 -0
  111. kasa/tests/smart/modules/test_fan.py +79 -0
  112. kasa/tests/smart/modules/test_firmware.py +108 -0
  113. kasa/tests/smart/modules/test_humidity.py +28 -0
  114. kasa/tests/smart/modules/test_light_effect.py +41 -0
  115. kasa/tests/smart/modules/test_temperature.py +47 -0
  116. kasa/tests/smart/modules/test_temperaturecontrol.py +137 -0
  117. kasa/tests/smart/modules/test_waterleak.py +42 -0
  118. kasa/tests/test_aestransport.py +115 -23
  119. kasa/tests/test_bulb.py +115 -85
  120. kasa/tests/test_childdevice.py +19 -7
  121. kasa/tests/test_cli.py +453 -99
  122. kasa/tests/test_common_modules.py +216 -0
  123. kasa/tests/test_device.py +268 -0
  124. kasa/tests/test_device_factory.py +61 -35
  125. kasa/tests/test_device_type.py +1 -1
  126. kasa/tests/test_deviceconfig.py +2 -2
  127. kasa/tests/test_dimmer.py +33 -24
  128. kasa/tests/test_discovery.py +112 -63
  129. kasa/tests/test_emeter.py +14 -10
  130. kasa/tests/test_feature.py +205 -0
  131. kasa/tests/test_httpclient.py +9 -9
  132. kasa/tests/test_iotdevice.py +286 -0
  133. kasa/tests/test_klapprotocol.py +94 -37
  134. kasa/tests/test_lightstrip.py +24 -19
  135. kasa/tests/test_plug.py +46 -8
  136. kasa/tests/test_protocol.py +8 -10
  137. kasa/tests/test_readme_examples.py +87 -26
  138. kasa/tests/test_smartdevice.py +203 -311
  139. kasa/tests/test_smartprotocol.py +130 -25
  140. kasa/tests/test_strip.py +15 -6
  141. kasa/tests/test_usage.py +1 -1
  142. kasa/xortransport.py +15 -12
  143. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -143
  144. python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
  145. kasa/modules/emeter.py +0 -111
  146. kasa/smartlightstrip.py +0 -143
  147. kasa/smartplug.py +0 -91
  148. kasa/tapo/__init__.py +0 -7
  149. kasa/tapo/childdevice.py +0 -41
  150. kasa/tapo/tapobulb.py +0 -262
  151. kasa/tapo/tapodevice.py +0 -409
  152. kasa/tapo/tapoplug.py +0 -45
  153. kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -47
  154. kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -50
  155. kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -50
  156. kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -47
  157. kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -75
  158. kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -103
  159. kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -103
  160. kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -104
  161. python_kasa-0.6.2.1.dist-info/RECORD +0 -167
  162. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
  163. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +0 -0
  164. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
kasa/tests/conftest.py CHANGED
@@ -1,339 +1,18 @@
1
- import asyncio
2
- import glob
3
- import json
4
- import os
5
- from dataclasses import dataclass
6
- from json import dumps as json_dumps
7
- from os.path import basename
8
- from pathlib import Path
9
- from typing import Dict, Optional
10
- from unittest.mock import MagicMock
1
+ from __future__ import annotations
11
2
 
12
- import pytest # type: ignore # see https://github.com/pytest-dev/pytest/issues/3342
3
+ import warnings
4
+ from unittest.mock import MagicMock, patch
5
+
6
+ import pytest
13
7
 
14
8
  from kasa import (
15
- Credentials,
16
9
  DeviceConfig,
17
- Discover,
18
- SmartBulb,
19
- SmartDevice,
20
- SmartDimmer,
21
- SmartLightStrip,
22
- SmartPlug,
23
10
  SmartProtocol,
24
- SmartStrip,
25
11
  )
26
12
  from kasa.protocol import BaseTransport
27
- from kasa.tapo import TapoBulb, TapoPlug
28
- from kasa.xortransport import XorEncryption
29
-
30
- from .fakeprotocol_iot import FakeIotProtocol
31
- from .fakeprotocol_smart import FakeSmartProtocol
32
-
33
- SUPPORTED_IOT_DEVICES = [
34
- (device, "IOT")
35
- for device in glob.glob(
36
- os.path.dirname(os.path.abspath(__file__)) + "/fixtures/*.json"
37
- )
38
- ]
39
-
40
- SUPPORTED_SMART_DEVICES = [
41
- (device, "SMART")
42
- for device in glob.glob(
43
- os.path.dirname(os.path.abspath(__file__)) + "/fixtures/smart/*.json"
44
- )
45
- ]
46
-
47
-
48
- SUPPORTED_DEVICES = SUPPORTED_IOT_DEVICES + SUPPORTED_SMART_DEVICES
49
-
50
- # Tapo bulbs
51
- BULBS_SMART_VARIABLE_TEMP = {"L530E", "L930-5"}
52
- BULBS_SMART_LIGHT_STRIP = {"L900-5", "L900-10", "L920-5", "L930-5"}
53
- BULBS_SMART_COLOR = {"L530E", *BULBS_SMART_LIGHT_STRIP}
54
- BULBS_SMART_DIMMABLE = {"KS225", "L510B", "L510E"}
55
- BULBS_SMART = (
56
- BULBS_SMART_VARIABLE_TEMP.union(BULBS_SMART_COLOR)
57
- .union(BULBS_SMART_DIMMABLE)
58
- .union(BULBS_SMART_LIGHT_STRIP)
59
- )
60
-
61
- # Kasa (IOT-prefixed) bulbs
62
- BULBS_IOT_LIGHT_STRIP = {"KL400L5", "KL430", "KL420L5"}
63
- BULBS_IOT_VARIABLE_TEMP = {
64
- "LB120",
65
- "LB130",
66
- "KL120",
67
- "KL125",
68
- "KL130",
69
- "KL135",
70
- "KL430",
71
- }
72
- BULBS_IOT_COLOR = {"LB130", "KL125", "KL130", "KL135", *BULBS_IOT_LIGHT_STRIP}
73
- BULBS_IOT_DIMMABLE = {"KL50", "KL60", "LB100", "LB110", "KL110"}
74
- BULBS_IOT = (
75
- BULBS_IOT_VARIABLE_TEMP.union(BULBS_IOT_COLOR)
76
- .union(BULBS_IOT_DIMMABLE)
77
- .union(BULBS_IOT_LIGHT_STRIP)
78
- )
79
-
80
- BULBS_VARIABLE_TEMP = {*BULBS_SMART_VARIABLE_TEMP, *BULBS_IOT_VARIABLE_TEMP}
81
- BULBS_COLOR = {*BULBS_SMART_COLOR, *BULBS_IOT_COLOR}
82
-
83
-
84
- LIGHT_STRIPS = {*BULBS_SMART_LIGHT_STRIP, *BULBS_IOT_LIGHT_STRIP}
85
- BULBS = {
86
- *BULBS_IOT,
87
- *BULBS_SMART,
88
- }
89
-
90
-
91
- PLUGS_IOT = {
92
- "HS100",
93
- "HS103",
94
- "HS105",
95
- "HS110",
96
- "HS200",
97
- "HS210",
98
- "EP10",
99
- "KP100",
100
- "KP105",
101
- "KP115",
102
- "KP125",
103
- "KP401",
104
- "KS200M",
105
- }
106
- # P135 supports dimming, but its not currently support
107
- # by the library
108
- PLUGS_SMART = {
109
- "P100",
110
- "P110",
111
- "KP125M",
112
- "EP25",
113
- "KS205",
114
- "P125M",
115
- "P135",
116
- "S505",
117
- "TP15",
118
- }
119
- PLUGS = {
120
- *PLUGS_IOT,
121
- *PLUGS_SMART,
122
- }
123
- STRIPS_IOT = {"HS107", "HS300", "KP303", "KP200", "KP400", "EP40"}
124
- STRIPS_SMART = {"P300", "TP25"}
125
- STRIPS = {*STRIPS_IOT, *STRIPS_SMART}
126
-
127
- DIMMERS_IOT = {"ES20M", "HS220", "KS220M", "KS230", "KP405"}
128
- DIMMERS_SMART = {"S500D"}
129
- DIMMERS = {
130
- *DIMMERS_IOT,
131
- *DIMMERS_SMART,
132
- }
133
-
134
- WITH_EMETER_IOT = {"HS110", "HS300", "KP115", "KP125", *BULBS_IOT}
135
- WITH_EMETER_SMART = {"P110", "KP125M", "EP25"}
136
- WITH_EMETER = {*WITH_EMETER_IOT, *WITH_EMETER_SMART}
137
-
138
- DIMMABLE = {*BULBS, *DIMMERS}
139
-
140
- ALL_DEVICES_IOT = BULBS_IOT.union(PLUGS_IOT).union(STRIPS_IOT).union(DIMMERS_IOT)
141
- ALL_DEVICES_SMART = (
142
- BULBS_SMART.union(PLUGS_SMART).union(STRIPS_SMART).union(DIMMERS_SMART)
143
- )
144
- ALL_DEVICES = ALL_DEVICES_IOT.union(ALL_DEVICES_SMART)
145
-
146
- IP_MODEL_CACHE: Dict[str, str] = {}
147
-
148
-
149
- def _make_unsupported(device_family, encrypt_type):
150
- return {
151
- "result": {
152
- "device_id": "xx",
153
- "owner": "xx",
154
- "device_type": device_family,
155
- "device_model": "P110(EU)",
156
- "ip": "127.0.0.1",
157
- "mac": "48-22xxx",
158
- "is_support_iot_cloud": True,
159
- "obd_src": "tplink",
160
- "factory_default": False,
161
- "mgt_encrypt_schm": {
162
- "is_support_https": False,
163
- "encrypt_type": encrypt_type,
164
- "http_port": 80,
165
- "lv": 2,
166
- },
167
- },
168
- "error_code": 0,
169
- }
170
-
171
-
172
- UNSUPPORTED_DEVICES = {
173
- "unknown_device_family": _make_unsupported("SMART.TAPOXMASTREE", "AES"),
174
- "wrong_encryption_iot": _make_unsupported("IOT.SMARTPLUGSWITCH", "AES"),
175
- "wrong_encryption_smart": _make_unsupported("SMART.TAPOBULB", "IOT"),
176
- "unknown_encryption": _make_unsupported("IOT.SMARTPLUGSWITCH", "FOO"),
177
- }
178
-
179
-
180
- def idgenerator(paramtuple):
181
- try:
182
- return basename(paramtuple[0]) + (
183
- "" if paramtuple[1] == "IOT" else "-" + paramtuple[1]
184
- )
185
- except: # TODO: HACK as idgenerator is now used by default # noqa: E722
186
- return None
187
-
188
-
189
- def filter_model(desc, model_filter, protocol_filter=None):
190
- if protocol_filter is None:
191
- protocol_filter = {"IOT", "SMART"}
192
- filtered = list()
193
- for file, protocol in SUPPORTED_DEVICES:
194
- if protocol in protocol_filter:
195
- file_model_region = basename(file).split("_")[0]
196
- file_model = file_model_region.split("(")[0]
197
- for model in model_filter:
198
- if model == file_model:
199
- filtered.append((file, protocol))
200
-
201
- filtered_basenames = [basename(f) + "-" + p for f, p in filtered]
202
- print(f"# {desc}")
203
- for file in filtered_basenames:
204
- print(f"\t{file}")
205
- return filtered
206
-
207
-
208
- def parametrize(desc, devices, protocol_filter=None, ids=None):
209
- if ids is None:
210
- ids = idgenerator
211
- return pytest.mark.parametrize(
212
- "dev", filter_model(desc, devices, protocol_filter), indirect=True, ids=ids
213
- )
214
-
215
-
216
- has_emeter = parametrize("has emeter", WITH_EMETER, protocol_filter={"SMART", "IOT"})
217
- no_emeter = parametrize(
218
- "no emeter", ALL_DEVICES - WITH_EMETER, protocol_filter={"SMART", "IOT"}
219
- )
220
- has_emeter_iot = parametrize("has emeter iot", WITH_EMETER_IOT, protocol_filter={"IOT"})
221
- no_emeter_iot = parametrize(
222
- "no emeter iot", ALL_DEVICES_IOT - WITH_EMETER_IOT, protocol_filter={"IOT"}
223
- )
224
-
225
- bulb = parametrize("bulbs", BULBS, protocol_filter={"SMART", "IOT"})
226
- plug = parametrize("plugs", PLUGS, protocol_filter={"IOT"})
227
- strip = parametrize("strips", STRIPS, protocol_filter={"SMART", "IOT"})
228
- dimmer = parametrize("dimmers", DIMMERS, protocol_filter={"IOT"})
229
- lightstrip = parametrize("lightstrips", LIGHT_STRIPS, protocol_filter={"IOT"})
230
-
231
- # bulb types
232
- dimmable = parametrize("dimmable", DIMMABLE, protocol_filter={"IOT"})
233
- non_dimmable = parametrize("non-dimmable", BULBS - DIMMABLE, protocol_filter={"IOT"})
234
- variable_temp = parametrize(
235
- "variable color temp", BULBS_VARIABLE_TEMP, protocol_filter={"SMART", "IOT"}
236
- )
237
- non_variable_temp = parametrize(
238
- "non-variable color temp",
239
- BULBS - BULBS_VARIABLE_TEMP,
240
- protocol_filter={"SMART", "IOT"},
241
- )
242
- color_bulb = parametrize("color bulbs", BULBS_COLOR, protocol_filter={"SMART", "IOT"})
243
- non_color_bulb = parametrize(
244
- "non-color bulbs", BULBS - BULBS_COLOR, protocol_filter={"SMART", "IOT"}
245
- )
246
-
247
- color_bulb_iot = parametrize(
248
- "color bulbs iot", BULBS_IOT_COLOR, protocol_filter={"IOT"}
249
- )
250
- variable_temp_iot = parametrize(
251
- "variable color temp iot", BULBS_IOT_VARIABLE_TEMP, protocol_filter={"IOT"}
252
- )
253
- bulb_iot = parametrize("bulb devices iot", BULBS_IOT, protocol_filter={"IOT"})
254
-
255
- strip_iot = parametrize("strip devices iot", STRIPS_IOT, protocol_filter={"IOT"})
256
- strip_smart = parametrize(
257
- "strip devices smart", STRIPS_SMART, protocol_filter={"SMART"}
258
- )
259
-
260
- plug_smart = parametrize("plug devices smart", PLUGS_SMART, protocol_filter={"SMART"})
261
- bulb_smart = parametrize("bulb devices smart", BULBS_SMART, protocol_filter={"SMART"})
262
- dimmers_smart = parametrize(
263
- "dimmer devices smart", DIMMERS_SMART, protocol_filter={"SMART"}
264
- )
265
- device_smart = parametrize(
266
- "devices smart", ALL_DEVICES_SMART, protocol_filter={"SMART"}
267
- )
268
- device_iot = parametrize("devices iot", ALL_DEVICES_IOT, protocol_filter={"IOT"})
269
-
270
-
271
- def get_fixture_data():
272
- """Return raw discovery file contents as JSON. Used for discovery tests."""
273
- fixture_data = {}
274
- for file, protocol in SUPPORTED_DEVICES:
275
- p = Path(file)
276
- if not p.is_absolute():
277
- folder = Path(__file__).parent / "fixtures"
278
- if protocol == "SMART":
279
- folder = folder / "smart"
280
- p = folder / file
281
-
282
- with open(p) as f:
283
- fixture_data[basename(p)] = json.load(f)
284
- return fixture_data
285
-
286
-
287
- FIXTURE_DATA = get_fixture_data()
288
-
289
-
290
- def filter_fixtures(desc, root_filter):
291
- filtered = {}
292
- for key, val in FIXTURE_DATA.items():
293
- if root_filter in val:
294
- filtered[key] = val
295
-
296
- print(f"# {desc}")
297
- for key in filtered:
298
- print(f"\t{key}")
299
- return filtered
300
-
301
-
302
- def parametrize_discovery(desc, root_key):
303
- filtered_fixtures = filter_fixtures(desc, root_key)
304
- return pytest.mark.parametrize(
305
- "all_fixture_data",
306
- filtered_fixtures.values(),
307
- indirect=True,
308
- ids=filtered_fixtures.keys(),
309
- )
310
-
311
-
312
- new_discovery = parametrize_discovery("new discovery", "discovery_result")
313
13
 
314
-
315
- def check_categories():
316
- """Check that every fixture file is categorized."""
317
- categorized_fixtures = set(
318
- dimmer.args[1]
319
- + strip.args[1]
320
- + plug.args[1]
321
- + bulb.args[1]
322
- + lightstrip.args[1]
323
- + plug_smart.args[1]
324
- + bulb_smart.args[1]
325
- + dimmers_smart.args[1]
326
- )
327
- diff = set(SUPPORTED_DEVICES) - set(categorized_fixtures)
328
- if diff:
329
- for file, protocol in diff:
330
- print(
331
- f"No category for file {file} protocol {protocol}, add to the corresponding set (BULBS, PLUGS, ..)"
332
- )
333
- raise Exception(f"Missing category for {diff}")
334
-
335
-
336
- check_categories()
14
+ from .device_fixtures import * # noqa: F403
15
+ from .discovery_fixtures import * # noqa: F403
337
16
 
338
17
  # Parametrize tests to run with device both on and off
339
18
  turn_on = pytest.mark.parametrize("turn_on", [True, False])
@@ -346,239 +25,7 @@ async def handle_turn_on(dev, turn_on):
346
25
  await dev.turn_off()
347
26
 
348
27
 
349
- def device_for_file(model, protocol):
350
- if protocol == "SMART":
351
- for d in PLUGS_SMART:
352
- if d in model:
353
- return TapoPlug
354
- for d in BULBS_SMART:
355
- if d in model:
356
- return TapoBulb
357
- for d in DIMMERS_SMART:
358
- if d in model:
359
- return TapoBulb
360
- for d in STRIPS_SMART:
361
- if d in model:
362
- return TapoPlug
363
- else:
364
- for d in STRIPS_IOT:
365
- if d in model:
366
- return SmartStrip
367
-
368
- for d in PLUGS_IOT:
369
- if d in model:
370
- return SmartPlug
371
-
372
- # Light strips are recognized also as bulbs, so this has to go first
373
- for d in BULBS_IOT_LIGHT_STRIP:
374
- if d in model:
375
- return SmartLightStrip
376
-
377
- for d in BULBS_IOT:
378
- if d in model:
379
- return SmartBulb
380
-
381
- for d in DIMMERS_IOT:
382
- if d in model:
383
- return SmartDimmer
384
-
385
- raise Exception("Unable to find type for %s", model)
386
-
387
-
388
- async def _update_and_close(d):
389
- await d.update()
390
- await d.protocol.close()
391
- return d
392
-
393
-
394
- async def _discover_update_and_close(ip, username, password):
395
- if username and password:
396
- credentials = Credentials(username=username, password=password)
397
- else:
398
- credentials = None
399
- d = await Discover.discover_single(ip, timeout=10, credentials=credentials)
400
- return await _update_and_close(d)
401
-
402
-
403
- async def get_device_for_file(file, protocol):
404
- # if the wanted file is not an absolute path, prepend the fixtures directory
405
- p = Path(file)
406
- if not p.is_absolute():
407
- folder = Path(__file__).parent / "fixtures"
408
- if protocol == "SMART":
409
- folder = folder / "smart"
410
- p = folder / file
411
-
412
- def load_file():
413
- with open(p) as f:
414
- return json.load(f)
415
-
416
- loop = asyncio.get_running_loop()
417
- sysinfo = await loop.run_in_executor(None, load_file)
418
-
419
- model = basename(file)
420
- d = device_for_file(model, protocol)(host="127.0.0.123")
421
- if protocol == "SMART":
422
- d.protocol = FakeSmartProtocol(sysinfo)
423
- else:
424
- d.protocol = FakeIotProtocol(sysinfo)
425
- await _update_and_close(d)
426
- return d
427
-
428
-
429
- @pytest.fixture(params=SUPPORTED_DEVICES, ids=idgenerator)
430
- async def dev(request):
431
- """Device fixture.
432
-
433
- Provides a device (given --ip) or parametrized fixture for the supported devices.
434
- The initial update is called automatically before returning the device.
435
- """
436
- file, protocol = request.param
437
-
438
- ip = request.config.getoption("--ip")
439
- username = request.config.getoption("--username")
440
- password = request.config.getoption("--password")
441
- if ip:
442
- model = IP_MODEL_CACHE.get(ip)
443
- d = None
444
- if not model:
445
- d = await _discover_update_and_close(ip, username, password)
446
- IP_MODEL_CACHE[ip] = model = d.model
447
- if model not in file:
448
- pytest.skip(f"skipping file {file}")
449
- dev: SmartDevice = (
450
- d if d else await _discover_update_and_close(ip, username, password)
451
- )
452
- else:
453
- dev: SmartDevice = await get_device_for_file(file, protocol)
454
-
455
- yield dev
456
-
457
- await dev.disconnect()
458
-
459
-
460
28
  @pytest.fixture
461
- def discovery_mock(all_fixture_data, mocker):
462
- @dataclass
463
- class _DiscoveryMock:
464
- ip: str
465
- default_port: int
466
- discovery_port: int
467
- discovery_data: dict
468
- query_data: dict
469
- device_type: str
470
- encrypt_type: str
471
- login_version: Optional[int] = None
472
- port_override: Optional[int] = None
473
-
474
- if "discovery_result" in all_fixture_data:
475
- discovery_data = {"result": all_fixture_data["discovery_result"]}
476
- device_type = all_fixture_data["discovery_result"]["device_type"]
477
- encrypt_type = all_fixture_data["discovery_result"]["mgt_encrypt_schm"][
478
- "encrypt_type"
479
- ]
480
- login_version = all_fixture_data["discovery_result"]["mgt_encrypt_schm"].get(
481
- "lv"
482
- )
483
- datagram = (
484
- b"\x02\x00\x00\x01\x01[\x00\x00\x00\x00\x00\x00W\xcev\xf8"
485
- + json_dumps(discovery_data).encode()
486
- )
487
- dm = _DiscoveryMock(
488
- "127.0.0.123",
489
- 80,
490
- 20002,
491
- discovery_data,
492
- all_fixture_data,
493
- device_type,
494
- encrypt_type,
495
- login_version,
496
- )
497
- else:
498
- sys_info = all_fixture_data["system"]["get_sysinfo"]
499
- discovery_data = {"system": {"get_sysinfo": sys_info}}
500
- device_type = sys_info.get("mic_type") or sys_info.get("type")
501
- encrypt_type = "XOR"
502
- login_version = None
503
- datagram = XorEncryption.encrypt(json_dumps(discovery_data))[4:]
504
- dm = _DiscoveryMock(
505
- "127.0.0.123",
506
- 9999,
507
- 9999,
508
- discovery_data,
509
- all_fixture_data,
510
- device_type,
511
- encrypt_type,
512
- login_version,
513
- )
514
-
515
- def mock_discover(self):
516
- port = (
517
- dm.port_override
518
- if dm.port_override and dm.discovery_port != 20002
519
- else dm.discovery_port
520
- )
521
- self.datagram_received(
522
- datagram,
523
- (dm.ip, port),
524
- )
525
-
526
- mocker.patch("kasa.discover._DiscoverProtocol.do_discover", mock_discover)
527
- mocker.patch(
528
- "socket.getaddrinfo",
529
- side_effect=lambda *_, **__: [(None, None, None, None, (dm.ip, 0))],
530
- )
531
-
532
- if "component_nego" in dm.query_data:
533
- proto = FakeSmartProtocol(dm.query_data)
534
- else:
535
- proto = FakeIotProtocol(dm.query_data)
536
-
537
- async def _query(request, retry_count: int = 3):
538
- return await proto.query(request)
539
-
540
- mocker.patch("kasa.IotProtocol.query", side_effect=_query)
541
- mocker.patch("kasa.SmartProtocol.query", side_effect=_query)
542
-
543
- yield dm
544
-
545
-
546
- @pytest.fixture
547
- def discovery_data(all_fixture_data):
548
- """Return raw discovery file contents as JSON. Used for discovery tests."""
549
- if "discovery_result" in all_fixture_data:
550
- return {"result": all_fixture_data["discovery_result"]}
551
- else:
552
- return {"system": {"get_sysinfo": all_fixture_data["system"]["get_sysinfo"]}}
553
-
554
-
555
- @pytest.fixture(params=FIXTURE_DATA.values(), ids=FIXTURE_DATA.keys(), scope="session")
556
- def all_fixture_data(request):
557
- """Return raw fixture file contents as JSON. Used for discovery tests."""
558
- fixture_data = request.param
559
- return fixture_data
560
-
561
-
562
- @pytest.fixture(params=UNSUPPORTED_DEVICES.values(), ids=UNSUPPORTED_DEVICES.keys())
563
- def unsupported_device_info(request, mocker):
564
- """Return unsupported devices for cli and discovery tests."""
565
- discovery_data = request.param
566
- host = "127.0.0.1"
567
-
568
- def mock_discover(self):
569
- if discovery_data:
570
- data = (
571
- b"\x02\x00\x00\x01\x01[\x00\x00\x00\x00\x00\x00W\xcev\xf8"
572
- + json_dumps(discovery_data).encode()
573
- )
574
- self.datagram_received(data, (host, 20002))
575
-
576
- mocker.patch("kasa.discover._DiscoverProtocol.do_discover", mock_discover)
577
-
578
- yield discovery_data
579
-
580
-
581
- @pytest.fixture()
582
29
  def dummy_protocol():
583
30
  """Return a smart protocol instance with a mocking-ready dummy transport."""
584
31
 
@@ -591,7 +38,7 @@ def dummy_protocol():
591
38
  def credentials_hash(self) -> str:
592
39
  return "dummy hash"
593
40
 
594
- async def send(self, request: str) -> Dict:
41
+ async def send(self, request: str) -> dict:
595
42
  return {}
596
43
 
597
44
  async def close(self) -> None:
@@ -602,8 +49,26 @@ def dummy_protocol():
602
49
 
603
50
  transport = DummyTransport(config=DeviceConfig(host="127.0.0.123"))
604
51
  protocol = SmartProtocol(transport=transport)
52
+ with patch.object(protocol, "BACKOFF_SECONDS_AFTER_TIMEOUT", 0):
53
+ yield protocol
54
+
605
55
 
606
- return protocol
56
+ def pytest_configure():
57
+ pytest.fixtures_missing_methods = {}
58
+
59
+
60
+ def pytest_sessionfinish(session, exitstatus):
61
+ if not pytest.fixtures_missing_methods:
62
+ return
63
+ msg = "\n"
64
+ for fixture, methods in sorted(pytest.fixtures_missing_methods.items()):
65
+ method_list = ", ".join(methods)
66
+ msg += f"Fixture {fixture} missing: {method_list}\n"
67
+
68
+ warnings.warn(
69
+ UserWarning(msg),
70
+ stacklevel=1,
71
+ )
607
72
 
608
73
 
609
74
  def pytest_addoption(parser):