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
@@ -0,0 +1,427 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import AsyncGenerator
4
+
5
+ import pytest
6
+
7
+ from kasa import (
8
+ Credentials,
9
+ Device,
10
+ DeviceType,
11
+ Discover,
12
+ )
13
+ from kasa.iot import IotBulb, IotDimmer, IotLightStrip, IotPlug, IotStrip, IotWallSwitch
14
+ from kasa.smart import SmartDevice
15
+
16
+ from .fakeprotocol_iot import FakeIotProtocol
17
+ from .fakeprotocol_smart import FakeSmartProtocol
18
+ from .fixtureinfo import FIXTURE_DATA, FixtureInfo, filter_fixtures, idgenerator
19
+
20
+ # Tapo bulbs
21
+ BULBS_SMART_VARIABLE_TEMP = {"L530E", "L930-5"}
22
+ BULBS_SMART_LIGHT_STRIP = {"L900-5", "L900-10", "L920-5", "L930-5"}
23
+ BULBS_SMART_COLOR = {"L530E", *BULBS_SMART_LIGHT_STRIP}
24
+ BULBS_SMART_DIMMABLE = {"L510B", "L510E"}
25
+ BULBS_SMART = (
26
+ BULBS_SMART_VARIABLE_TEMP.union(BULBS_SMART_COLOR)
27
+ .union(BULBS_SMART_DIMMABLE)
28
+ .union(BULBS_SMART_LIGHT_STRIP)
29
+ )
30
+
31
+ # Kasa (IOT-prefixed) bulbs
32
+ BULBS_IOT_LIGHT_STRIP = {"KL400L5", "KL430", "KL420L5"}
33
+ BULBS_IOT_VARIABLE_TEMP = {
34
+ "LB120",
35
+ "LB130",
36
+ "KL120",
37
+ "KL125",
38
+ "KL130",
39
+ "KL135",
40
+ "KL430",
41
+ }
42
+ BULBS_IOT_COLOR = {"LB130", "KL125", "KL130", "KL135", *BULBS_IOT_LIGHT_STRIP}
43
+ BULBS_IOT_DIMMABLE = {"KL50", "KL60", "LB100", "LB110", "KL110"}
44
+ BULBS_IOT = (
45
+ BULBS_IOT_VARIABLE_TEMP.union(BULBS_IOT_COLOR)
46
+ .union(BULBS_IOT_DIMMABLE)
47
+ .union(BULBS_IOT_LIGHT_STRIP)
48
+ )
49
+
50
+ BULBS_VARIABLE_TEMP = {*BULBS_SMART_VARIABLE_TEMP, *BULBS_IOT_VARIABLE_TEMP}
51
+ BULBS_COLOR = {*BULBS_SMART_COLOR, *BULBS_IOT_COLOR}
52
+
53
+
54
+ LIGHT_STRIPS = {*BULBS_SMART_LIGHT_STRIP, *BULBS_IOT_LIGHT_STRIP}
55
+ BULBS = {
56
+ *BULBS_IOT,
57
+ *BULBS_SMART,
58
+ }
59
+
60
+
61
+ PLUGS_IOT = {
62
+ "HS100",
63
+ "HS103",
64
+ "HS105",
65
+ "HS110",
66
+ "EP10",
67
+ "KP100",
68
+ "KP105",
69
+ "KP115",
70
+ "KP125",
71
+ "KP401",
72
+ }
73
+ # P135 supports dimming, but its not currently support
74
+ # by the library
75
+ PLUGS_SMART = {
76
+ "P100",
77
+ "P110",
78
+ "KP125M",
79
+ "EP25",
80
+ "P125M",
81
+ "TP15",
82
+ }
83
+ PLUGS = {
84
+ *PLUGS_IOT,
85
+ *PLUGS_SMART,
86
+ }
87
+ SWITCHES_IOT = {
88
+ "HS200",
89
+ "HS210",
90
+ "KS200M",
91
+ }
92
+ SWITCHES_SMART = {
93
+ "KS205",
94
+ "KS225",
95
+ "KS240",
96
+ "S500D",
97
+ "S505",
98
+ }
99
+ SWITCHES = {*SWITCHES_IOT, *SWITCHES_SMART}
100
+ STRIPS_IOT = {"HS107", "HS300", "KP303", "KP200", "KP400", "EP40"}
101
+ STRIPS_SMART = {"P300", "TP25"}
102
+ STRIPS = {*STRIPS_IOT, *STRIPS_SMART}
103
+
104
+ DIMMERS_IOT = {"ES20M", "HS220", "KS220M", "KS230", "KP405"}
105
+ DIMMERS_SMART = {"KS225", "S500D", "P135"}
106
+ DIMMERS = {
107
+ *DIMMERS_IOT,
108
+ *DIMMERS_SMART,
109
+ }
110
+
111
+ HUBS_SMART = {"H100", "KH100"}
112
+ SENSORS_SMART = {"T310", "T315", "T300", "T110"}
113
+ THERMOSTATS_SMART = {"KE100"}
114
+
115
+ WITH_EMETER_IOT = {"HS110", "HS300", "KP115", "KP125", *BULBS_IOT}
116
+ WITH_EMETER_SMART = {"P110", "KP125M", "EP25"}
117
+ WITH_EMETER = {*WITH_EMETER_IOT, *WITH_EMETER_SMART}
118
+
119
+ DIMMABLE = {*BULBS, *DIMMERS}
120
+
121
+ ALL_DEVICES_IOT = (
122
+ BULBS_IOT.union(PLUGS_IOT).union(STRIPS_IOT).union(DIMMERS_IOT).union(SWITCHES_IOT)
123
+ )
124
+ ALL_DEVICES_SMART = (
125
+ BULBS_SMART.union(PLUGS_SMART)
126
+ .union(STRIPS_SMART)
127
+ .union(DIMMERS_SMART)
128
+ .union(HUBS_SMART)
129
+ .union(SENSORS_SMART)
130
+ .union(SWITCHES_SMART)
131
+ .union(THERMOSTATS_SMART)
132
+ )
133
+ ALL_DEVICES = ALL_DEVICES_IOT.union(ALL_DEVICES_SMART)
134
+
135
+ IP_MODEL_CACHE: dict[str, str] = {}
136
+
137
+
138
+ def parametrize_combine(parametrized: list[pytest.MarkDecorator]):
139
+ """Combine multiple pytest parametrize dev marks into one set of fixtures."""
140
+ fixtures = set()
141
+ for param in parametrized:
142
+ if param.args[0] != "dev":
143
+ raise Exception(f"Supplied mark is not for dev fixture: {param.args[0]}")
144
+ fixtures.update(param.args[1])
145
+ return pytest.mark.parametrize(
146
+ "dev",
147
+ sorted(list(fixtures)),
148
+ indirect=True,
149
+ ids=idgenerator,
150
+ )
151
+
152
+
153
+ def parametrize(
154
+ desc,
155
+ *,
156
+ model_filter=None,
157
+ protocol_filter=None,
158
+ component_filter=None,
159
+ data_root_filter=None,
160
+ device_type_filter=None,
161
+ ids=None,
162
+ ):
163
+ if ids is None:
164
+ ids = idgenerator
165
+ return pytest.mark.parametrize(
166
+ "dev",
167
+ filter_fixtures(
168
+ desc,
169
+ model_filter=model_filter,
170
+ protocol_filter=protocol_filter,
171
+ component_filter=component_filter,
172
+ data_root_filter=data_root_filter,
173
+ device_type_filter=device_type_filter,
174
+ ),
175
+ indirect=True,
176
+ ids=ids,
177
+ )
178
+
179
+
180
+ has_emeter = parametrize(
181
+ "has emeter", model_filter=WITH_EMETER, protocol_filter={"SMART", "IOT"}
182
+ )
183
+ no_emeter = parametrize(
184
+ "no emeter",
185
+ model_filter=ALL_DEVICES - WITH_EMETER,
186
+ protocol_filter={"SMART", "IOT"},
187
+ )
188
+ has_emeter_iot = parametrize(
189
+ "has emeter iot", model_filter=WITH_EMETER_IOT, protocol_filter={"IOT"}
190
+ )
191
+ no_emeter_iot = parametrize(
192
+ "no emeter iot",
193
+ model_filter=ALL_DEVICES_IOT - WITH_EMETER_IOT,
194
+ protocol_filter={"IOT"},
195
+ )
196
+
197
+ plug = parametrize("plugs", model_filter=PLUGS, protocol_filter={"IOT", "SMART"})
198
+ plug_iot = parametrize("plugs iot", model_filter=PLUGS, protocol_filter={"IOT"})
199
+ wallswitch = parametrize(
200
+ "wall switches", model_filter=SWITCHES, protocol_filter={"IOT", "SMART"}
201
+ )
202
+ wallswitch_iot = parametrize(
203
+ "wall switches iot", model_filter=SWITCHES, protocol_filter={"IOT"}
204
+ )
205
+ strip = parametrize("strips", model_filter=STRIPS, protocol_filter={"SMART", "IOT"})
206
+ dimmer_iot = parametrize("dimmers", model_filter=DIMMERS, protocol_filter={"IOT"})
207
+ lightstrip_iot = parametrize(
208
+ "lightstrips", model_filter=LIGHT_STRIPS, protocol_filter={"IOT"}
209
+ )
210
+
211
+ # bulb types
212
+ dimmable_iot = parametrize("dimmable", model_filter=DIMMABLE, protocol_filter={"IOT"})
213
+ non_dimmable_iot = parametrize(
214
+ "non-dimmable", model_filter=BULBS - DIMMABLE, protocol_filter={"IOT"}
215
+ )
216
+ variable_temp = parametrize(
217
+ "variable color temp",
218
+ model_filter=BULBS_VARIABLE_TEMP,
219
+ protocol_filter={"SMART", "IOT"},
220
+ )
221
+ non_variable_temp = parametrize(
222
+ "non-variable color temp",
223
+ model_filter=BULBS - BULBS_VARIABLE_TEMP,
224
+ protocol_filter={"SMART", "IOT"},
225
+ )
226
+ color_bulb = parametrize(
227
+ "color bulbs", model_filter=BULBS_COLOR, protocol_filter={"SMART", "IOT"}
228
+ )
229
+ non_color_bulb = parametrize(
230
+ "non-color bulbs",
231
+ model_filter=BULBS - BULBS_COLOR,
232
+ protocol_filter={"SMART", "IOT"},
233
+ )
234
+
235
+ color_bulb_iot = parametrize(
236
+ "color bulbs iot", model_filter=BULBS_IOT_COLOR, protocol_filter={"IOT"}
237
+ )
238
+ variable_temp_iot = parametrize(
239
+ "variable color temp iot",
240
+ model_filter=BULBS_IOT_VARIABLE_TEMP,
241
+ protocol_filter={"IOT"},
242
+ )
243
+ variable_temp_smart = parametrize(
244
+ "variable color temp smart",
245
+ model_filter=BULBS_SMART_VARIABLE_TEMP,
246
+ protocol_filter={"SMART"},
247
+ )
248
+
249
+ bulb_smart = parametrize(
250
+ "bulb devices smart",
251
+ device_type_filter=[DeviceType.Bulb, DeviceType.LightStrip],
252
+ protocol_filter={"SMART"},
253
+ )
254
+ bulb_iot = parametrize(
255
+ "bulb devices iot", model_filter=BULBS_IOT, protocol_filter={"IOT"}
256
+ )
257
+ bulb = parametrize_combine([bulb_smart, bulb_iot])
258
+
259
+ strip_iot = parametrize(
260
+ "strip devices iot", model_filter=STRIPS_IOT, protocol_filter={"IOT"}
261
+ )
262
+ strip_smart = parametrize(
263
+ "strip devices smart", model_filter=STRIPS_SMART, protocol_filter={"SMART"}
264
+ )
265
+
266
+ plug_smart = parametrize(
267
+ "plug devices smart", model_filter=PLUGS_SMART, protocol_filter={"SMART"}
268
+ )
269
+ switch_smart = parametrize(
270
+ "switch devices smart", model_filter=SWITCHES_SMART, protocol_filter={"SMART"}
271
+ )
272
+ dimmers_smart = parametrize(
273
+ "dimmer devices smart", model_filter=DIMMERS_SMART, protocol_filter={"SMART"}
274
+ )
275
+ hubs_smart = parametrize(
276
+ "hubs smart", model_filter=HUBS_SMART, protocol_filter={"SMART"}
277
+ )
278
+ sensors_smart = parametrize(
279
+ "sensors smart", model_filter=SENSORS_SMART, protocol_filter={"SMART.CHILD"}
280
+ )
281
+ thermostats_smart = parametrize(
282
+ "thermostats smart", model_filter=THERMOSTATS_SMART, protocol_filter={"SMART.CHILD"}
283
+ )
284
+ device_smart = parametrize(
285
+ "devices smart", model_filter=ALL_DEVICES_SMART, protocol_filter={"SMART"}
286
+ )
287
+ device_iot = parametrize(
288
+ "devices iot", model_filter=ALL_DEVICES_IOT, protocol_filter={"IOT"}
289
+ )
290
+
291
+
292
+ def check_categories():
293
+ """Check that every fixture file is categorized."""
294
+ categorized_fixtures = set(
295
+ dimmer_iot.args[1]
296
+ + strip.args[1]
297
+ + plug.args[1]
298
+ + bulb.args[1]
299
+ + wallswitch.args[1]
300
+ + lightstrip_iot.args[1]
301
+ + bulb_smart.args[1]
302
+ + dimmers_smart.args[1]
303
+ + hubs_smart.args[1]
304
+ + sensors_smart.args[1]
305
+ + thermostats_smart.args[1]
306
+ )
307
+ diffs: set[FixtureInfo] = set(FIXTURE_DATA) - set(categorized_fixtures)
308
+ if diffs:
309
+ print(diffs)
310
+ for diff in diffs:
311
+ print(
312
+ f"No category for file {diff.name} protocol {diff.protocol}, add to the corresponding set (BULBS, PLUGS, ..)"
313
+ )
314
+ raise Exception(f"Missing category for {diff.name}")
315
+
316
+
317
+ check_categories()
318
+
319
+
320
+ def device_for_fixture_name(model, protocol):
321
+ if "SMART" in protocol:
322
+ return SmartDevice
323
+ else:
324
+ for d in STRIPS_IOT:
325
+ if d in model:
326
+ return IotStrip
327
+
328
+ for d in PLUGS_IOT:
329
+ if d in model:
330
+ return IotPlug
331
+ for d in SWITCHES_IOT:
332
+ if d in model:
333
+ return IotWallSwitch
334
+
335
+ # Light strips are recognized also as bulbs, so this has to go first
336
+ for d in BULBS_IOT_LIGHT_STRIP:
337
+ if d in model:
338
+ return IotLightStrip
339
+
340
+ for d in BULBS_IOT:
341
+ if d in model:
342
+ return IotBulb
343
+
344
+ for d in DIMMERS_IOT:
345
+ if d in model:
346
+ return IotDimmer
347
+
348
+ raise Exception("Unable to find type for %s", model)
349
+
350
+
351
+ async def _update_and_close(d) -> Device:
352
+ await d.update()
353
+ await d.protocol.close()
354
+ return d
355
+
356
+
357
+ async def _discover_update_and_close(ip, username, password) -> Device:
358
+ if username and password:
359
+ credentials = Credentials(username=username, password=password)
360
+ else:
361
+ credentials = None
362
+ d = await Discover.discover_single(ip, timeout=10, credentials=credentials)
363
+ return await _update_and_close(d)
364
+
365
+
366
+ async def get_device_for_fixture(fixture_data: FixtureInfo) -> Device:
367
+ # if the wanted file is not an absolute path, prepend the fixtures directory
368
+
369
+ d = device_for_fixture_name(fixture_data.name, fixture_data.protocol)(
370
+ host="127.0.0.123"
371
+ )
372
+ if "SMART" in fixture_data.protocol:
373
+ d.protocol = FakeSmartProtocol(fixture_data.data, fixture_data.name)
374
+ else:
375
+ d.protocol = FakeIotProtocol(fixture_data.data)
376
+
377
+ discovery_data = None
378
+ if "discovery_result" in fixture_data.data:
379
+ discovery_data = {"result": fixture_data.data["discovery_result"]}
380
+ elif "system" in fixture_data.data:
381
+ discovery_data = {
382
+ "system": {"get_sysinfo": fixture_data.data["system"]["get_sysinfo"]}
383
+ }
384
+
385
+ if discovery_data: # Child devices do not have discovery info
386
+ d.update_from_discover_info(discovery_data)
387
+
388
+ await _update_and_close(d)
389
+ return d
390
+
391
+
392
+ async def get_device_for_fixture_protocol(fixture, protocol):
393
+ finfo = FixtureInfo(name=fixture, protocol=protocol, data={})
394
+ for fixture_info in FIXTURE_DATA:
395
+ if finfo == fixture_info:
396
+ return await get_device_for_fixture(fixture_info)
397
+
398
+
399
+ @pytest.fixture(params=filter_fixtures("main devices"), ids=idgenerator)
400
+ async def dev(request) -> AsyncGenerator[Device, None]:
401
+ """Device fixture.
402
+
403
+ Provides a device (given --ip) or parametrized fixture for the supported devices.
404
+ The initial update is called automatically before returning the device.
405
+ """
406
+ fixture_data: FixtureInfo = request.param
407
+ dev: Device
408
+
409
+ ip = request.config.getoption("--ip")
410
+ username = request.config.getoption("--username")
411
+ password = request.config.getoption("--password")
412
+ if ip:
413
+ model = IP_MODEL_CACHE.get(ip)
414
+ d = None
415
+ if not model:
416
+ d = await _discover_update_and_close(ip, username, password)
417
+ IP_MODEL_CACHE[ip] = model = d.model
418
+
419
+ if model not in fixture_data.name:
420
+ pytest.skip(f"skipping file {fixture_data.name}")
421
+ dev = d if d else await _discover_update_and_close(ip, username, password)
422
+ else:
423
+ dev = await get_device_for_fixture(fixture_data)
424
+
425
+ yield dev
426
+
427
+ await dev.disconnect()
@@ -0,0 +1,197 @@
1
+ from __future__ import annotations
2
+
3
+ import copy
4
+ from dataclasses import dataclass
5
+ from json import dumps as json_dumps
6
+
7
+ import pytest
8
+
9
+ from kasa.xortransport import XorEncryption
10
+
11
+ from .fakeprotocol_iot import FakeIotProtocol
12
+ from .fakeprotocol_smart import FakeSmartProtocol, FakeSmartTransport
13
+ from .fixtureinfo import FixtureInfo, filter_fixtures, idgenerator
14
+
15
+
16
+ def _make_unsupported(device_family, encrypt_type):
17
+ return {
18
+ "result": {
19
+ "device_id": "xx",
20
+ "owner": "xx",
21
+ "device_type": device_family,
22
+ "device_model": "P110(EU)",
23
+ "ip": "127.0.0.1",
24
+ "mac": "48-22xxx",
25
+ "is_support_iot_cloud": True,
26
+ "obd_src": "tplink",
27
+ "factory_default": False,
28
+ "mgt_encrypt_schm": {
29
+ "is_support_https": False,
30
+ "encrypt_type": encrypt_type,
31
+ "http_port": 80,
32
+ "lv": 2,
33
+ },
34
+ },
35
+ "error_code": 0,
36
+ }
37
+
38
+
39
+ UNSUPPORTED_DEVICES = {
40
+ "unknown_device_family": _make_unsupported("SMART.TAPOXMASTREE", "AES"),
41
+ "wrong_encryption_iot": _make_unsupported("IOT.SMARTPLUGSWITCH", "AES"),
42
+ "wrong_encryption_smart": _make_unsupported("SMART.TAPOBULB", "IOT"),
43
+ "unknown_encryption": _make_unsupported("IOT.SMARTPLUGSWITCH", "FOO"),
44
+ }
45
+
46
+
47
+ def parametrize_discovery(desc, *, data_root_filter, protocol_filter=None):
48
+ filtered_fixtures = filter_fixtures(
49
+ desc, data_root_filter=data_root_filter, protocol_filter=protocol_filter
50
+ )
51
+ return pytest.mark.parametrize(
52
+ "discovery_mock",
53
+ filtered_fixtures,
54
+ indirect=True,
55
+ ids=idgenerator,
56
+ )
57
+
58
+
59
+ new_discovery = parametrize_discovery(
60
+ "new discovery", data_root_filter="discovery_result"
61
+ )
62
+
63
+
64
+ @pytest.fixture(
65
+ params=filter_fixtures("discoverable", protocol_filter={"SMART", "IOT"}),
66
+ ids=idgenerator,
67
+ )
68
+ def discovery_mock(request, mocker):
69
+ """Mock discovery and patch protocol queries to use Fake protocols."""
70
+ fixture_info: FixtureInfo = request.param
71
+ fixture_data = fixture_info.data
72
+
73
+ @dataclass
74
+ class _DiscoveryMock:
75
+ ip: str
76
+ default_port: int
77
+ discovery_port: int
78
+ discovery_data: dict
79
+ query_data: dict
80
+ device_type: str
81
+ encrypt_type: str
82
+ login_version: int | None = None
83
+ port_override: int | None = None
84
+
85
+ if "discovery_result" in fixture_data:
86
+ discovery_data = {"result": fixture_data["discovery_result"]}
87
+ device_type = fixture_data["discovery_result"]["device_type"]
88
+ encrypt_type = fixture_data["discovery_result"]["mgt_encrypt_schm"][
89
+ "encrypt_type"
90
+ ]
91
+ login_version = fixture_data["discovery_result"]["mgt_encrypt_schm"].get("lv")
92
+ datagram = (
93
+ b"\x02\x00\x00\x01\x01[\x00\x00\x00\x00\x00\x00W\xcev\xf8"
94
+ + json_dumps(discovery_data).encode()
95
+ )
96
+ dm = _DiscoveryMock(
97
+ "127.0.0.123",
98
+ 80,
99
+ 20002,
100
+ discovery_data,
101
+ fixture_data,
102
+ device_type,
103
+ encrypt_type,
104
+ login_version,
105
+ )
106
+ else:
107
+ sys_info = fixture_data["system"]["get_sysinfo"]
108
+ discovery_data = {"system": {"get_sysinfo": sys_info}}
109
+ device_type = sys_info.get("mic_type") or sys_info.get("type")
110
+ encrypt_type = "XOR"
111
+ login_version = None
112
+ datagram = XorEncryption.encrypt(json_dumps(discovery_data))[4:]
113
+ dm = _DiscoveryMock(
114
+ "127.0.0.123",
115
+ 9999,
116
+ 9999,
117
+ discovery_data,
118
+ fixture_data,
119
+ device_type,
120
+ encrypt_type,
121
+ login_version,
122
+ )
123
+
124
+ async def mock_discover(self):
125
+ port = (
126
+ dm.port_override
127
+ if dm.port_override and dm.discovery_port != 20002
128
+ else dm.discovery_port
129
+ )
130
+ self.datagram_received(
131
+ datagram,
132
+ (dm.ip, port),
133
+ )
134
+
135
+ mocker.patch("kasa.discover._DiscoverProtocol.do_discover", mock_discover)
136
+ mocker.patch(
137
+ "socket.getaddrinfo",
138
+ side_effect=lambda *_, **__: [(None, None, None, None, (dm.ip, 0))],
139
+ )
140
+
141
+ if "SMART" in fixture_info.protocol:
142
+ proto = FakeSmartProtocol(fixture_data, fixture_info.name)
143
+ else:
144
+ proto = FakeIotProtocol(fixture_data)
145
+
146
+ async def _query(request, retry_count: int = 3):
147
+ return await proto.query(request)
148
+
149
+ mocker.patch("kasa.IotProtocol.query", side_effect=_query)
150
+ mocker.patch("kasa.SmartProtocol.query", side_effect=_query)
151
+
152
+ yield dm
153
+
154
+
155
+ @pytest.fixture(
156
+ params=filter_fixtures("discoverable", protocol_filter={"SMART", "IOT"}),
157
+ ids=idgenerator,
158
+ )
159
+ def discovery_data(request, mocker):
160
+ """Return raw discovery file contents as JSON. Used for discovery tests."""
161
+ fixture_info = request.param
162
+ fixture_data = copy.deepcopy(fixture_info.data)
163
+ # Add missing queries to fixture data
164
+ if "component_nego" in fixture_data:
165
+ components = {
166
+ comp["id"]: int(comp["ver_code"])
167
+ for comp in fixture_data["component_nego"]["component_list"]
168
+ }
169
+ for k, v in FakeSmartTransport.FIXTURE_MISSING_MAP.items():
170
+ # Value is a tuple of component,reponse
171
+ if k not in fixture_data and v[0] in components:
172
+ fixture_data[k] = v[1]
173
+ mocker.patch("kasa.IotProtocol.query", return_value=fixture_data)
174
+ mocker.patch("kasa.SmartProtocol.query", return_value=fixture_data)
175
+ if "discovery_result" in fixture_data:
176
+ return {"result": fixture_data["discovery_result"]}
177
+ else:
178
+ return {"system": {"get_sysinfo": fixture_data["system"]["get_sysinfo"]}}
179
+
180
+
181
+ @pytest.fixture(params=UNSUPPORTED_DEVICES.values(), ids=UNSUPPORTED_DEVICES.keys())
182
+ def unsupported_device_info(request, mocker):
183
+ """Return unsupported devices for cli and discovery tests."""
184
+ discovery_data = request.param
185
+ host = "127.0.0.1"
186
+
187
+ async def mock_discover(self):
188
+ if discovery_data:
189
+ data = (
190
+ b"\x02\x00\x00\x01\x01[\x00\x00\x00\x00\x00\x00W\xcev\xf8"
191
+ + json_dumps(discovery_data).encode()
192
+ )
193
+ self.datagram_received(data, (host, 20002))
194
+
195
+ mocker.patch("kasa.discover._DiscoverProtocol.do_discover", mock_discover)
196
+
197
+ yield discovery_data