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