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/test_bulb.py CHANGED
@@ -1,28 +1,39 @@
1
+ from __future__ import annotations
2
+
1
3
  import pytest
4
+ from voluptuous import (
5
+ All,
6
+ Boolean,
7
+ Optional,
8
+ Range,
9
+ Schema,
10
+ )
2
11
 
3
- from kasa import DeviceType, SmartBulb, SmartBulbPreset, SmartDeviceException
12
+ from kasa import Device, DeviceType, IotLightPreset, KasaException, Module
13
+ from kasa.iot import IotBulb, IotDimmer
4
14
 
5
15
  from .conftest import (
6
16
  bulb,
7
17
  bulb_iot,
8
18
  color_bulb,
9
19
  color_bulb_iot,
10
- dimmable,
20
+ dimmable_iot,
11
21
  handle_turn_on,
12
22
  non_color_bulb,
13
- non_dimmable,
23
+ non_dimmable_iot,
14
24
  non_variable_temp,
15
25
  turn_on,
16
26
  variable_temp,
17
27
  variable_temp_iot,
28
+ variable_temp_smart,
18
29
  )
19
- from .newfakes import BULB_SCHEMA, LIGHT_STATE_SCHEMA
30
+ from .test_iotdevice import SYSINFO_SCHEMA
20
31
 
21
32
 
22
33
  @bulb
23
- async def test_bulb_sysinfo(dev: SmartBulb):
34
+ async def test_bulb_sysinfo(dev: Device):
24
35
  assert dev.sys_info is not None
25
- BULB_SCHEMA(dev.sys_info)
36
+ SYSINFO_SCHEMA_BULB(dev.sys_info)
26
37
 
27
38
  assert dev.model is not None
28
39
 
@@ -33,17 +44,14 @@ async def test_bulb_sysinfo(dev: SmartBulb):
33
44
 
34
45
 
35
46
  @bulb
36
- async def test_state_attributes(dev: SmartBulb):
37
- assert "Brightness" in dev.state_information
38
- assert dev.state_information["Brightness"] == dev.brightness
39
-
40
- assert "Is dimmable" in dev.state_information
41
- assert dev.state_information["Is dimmable"] == dev.is_dimmable
47
+ async def test_state_attributes(dev: Device):
48
+ assert "Cloud connection" in dev.state_information
49
+ assert isinstance(dev.state_information["Cloud connection"], bool)
42
50
 
43
51
 
44
52
  @bulb_iot
45
- async def test_light_state_without_update(dev: SmartBulb, monkeypatch):
46
- with pytest.raises(SmartDeviceException):
53
+ async def test_light_state_without_update(dev: IotBulb, monkeypatch):
54
+ with pytest.raises(KasaException):
47
55
  monkeypatch.setitem(
48
56
  dev._last_update["system"]["get_sysinfo"], "light_state", None
49
57
  )
@@ -51,33 +59,35 @@ async def test_light_state_without_update(dev: SmartBulb, monkeypatch):
51
59
 
52
60
 
53
61
  @bulb_iot
54
- async def test_get_light_state(dev: SmartBulb):
62
+ async def test_get_light_state(dev: IotBulb):
55
63
  LIGHT_STATE_SCHEMA(await dev.get_light_state())
56
64
 
57
65
 
58
66
  @color_bulb
59
67
  @turn_on
60
- async def test_hsv(dev: SmartBulb, turn_on):
68
+ async def test_hsv(dev: Device, turn_on):
69
+ light = dev.modules.get(Module.Light)
70
+ assert light
61
71
  await handle_turn_on(dev, turn_on)
62
- assert dev.is_color
72
+ assert light.is_color
63
73
 
64
- hue, saturation, brightness = dev.hsv
74
+ hue, saturation, brightness = light.hsv
65
75
  assert 0 <= hue <= 360
66
76
  assert 0 <= saturation <= 100
67
77
  assert 0 <= brightness <= 100
68
78
 
69
- await dev.set_hsv(hue=1, saturation=1, value=1)
79
+ await light.set_hsv(hue=1, saturation=1, value=1)
70
80
 
71
81
  await dev.update()
72
- hue, saturation, brightness = dev.hsv
82
+ hue, saturation, brightness = light.hsv
73
83
  assert hue == 1
74
84
  assert saturation == 1
75
85
  assert brightness == 1
76
86
 
77
87
 
78
88
  @color_bulb_iot
79
- async def test_set_hsv_transition(dev: SmartBulb, mocker):
80
- set_light_state = mocker.patch("kasa.SmartBulb.set_light_state")
89
+ async def test_set_hsv_transition(dev: IotBulb, mocker):
90
+ set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
81
91
  await dev.set_hsv(10, 10, 100, transition=1000)
82
92
 
83
93
  set_light_state.assert_called_with(
@@ -88,100 +98,119 @@ async def test_set_hsv_transition(dev: SmartBulb, mocker):
88
98
 
89
99
  @color_bulb
90
100
  @turn_on
91
- async def test_invalid_hsv(dev: SmartBulb, turn_on):
101
+ async def test_invalid_hsv(dev: Device, turn_on):
102
+ light = dev.modules.get(Module.Light)
103
+ assert light
92
104
  await handle_turn_on(dev, turn_on)
93
- assert dev.is_color
105
+ assert light.is_color
94
106
 
95
107
  for invalid_hue in [-1, 361, 0.5]:
96
108
  with pytest.raises(ValueError):
97
- await dev.set_hsv(invalid_hue, 0, 0)
109
+ await light.set_hsv(invalid_hue, 0, 0) # type: ignore[arg-type]
98
110
 
99
111
  for invalid_saturation in [-1, 101, 0.5]:
100
112
  with pytest.raises(ValueError):
101
- await dev.set_hsv(0, invalid_saturation, 0)
113
+ await light.set_hsv(0, invalid_saturation, 0) # type: ignore[arg-type]
102
114
 
103
115
  for invalid_brightness in [-1, 101, 0.5]:
104
116
  with pytest.raises(ValueError):
105
- await dev.set_hsv(0, 0, invalid_brightness)
117
+ await light.set_hsv(0, 0, invalid_brightness) # type: ignore[arg-type]
106
118
 
107
119
 
108
120
  @color_bulb
109
- async def test_color_state_information(dev: SmartBulb):
121
+ @pytest.mark.skip("requires color feature")
122
+ async def test_color_state_information(dev: Device):
123
+ light = dev.modules.get(Module.Light)
124
+ assert light
110
125
  assert "HSV" in dev.state_information
111
- assert dev.state_information["HSV"] == dev.hsv
126
+ assert dev.state_information["HSV"] == light.hsv
112
127
 
113
128
 
114
129
  @non_color_bulb
115
- async def test_hsv_on_non_color(dev: SmartBulb):
116
- assert not dev.is_color
130
+ async def test_hsv_on_non_color(dev: Device):
131
+ light = dev.modules.get(Module.Light)
132
+ assert light
133
+ assert not light.is_color
117
134
 
118
- with pytest.raises(SmartDeviceException):
119
- await dev.set_hsv(0, 0, 0)
120
- with pytest.raises(SmartDeviceException):
121
- print(dev.hsv)
135
+ with pytest.raises(KasaException):
136
+ await light.set_hsv(0, 0, 0)
137
+ with pytest.raises(KasaException):
138
+ print(light.hsv)
122
139
 
123
140
 
124
141
  @variable_temp
125
- async def test_variable_temp_state_information(dev: SmartBulb):
142
+ @pytest.mark.skip("requires colortemp module")
143
+ async def test_variable_temp_state_information(dev: Device):
144
+ light = dev.modules.get(Module.Light)
145
+ assert light
126
146
  assert "Color temperature" in dev.state_information
127
- assert dev.state_information["Color temperature"] == dev.color_temp
128
-
129
- assert "Valid temperature range" in dev.state_information
130
- assert (
131
- dev.state_information["Valid temperature range"] == dev.valid_temperature_range
132
- )
147
+ assert dev.state_information["Color temperature"] == light.color_temp
133
148
 
134
149
 
135
150
  @variable_temp
136
151
  @turn_on
137
- async def test_try_set_colortemp(dev: SmartBulb, turn_on):
152
+ async def test_try_set_colortemp(dev: Device, turn_on):
153
+ light = dev.modules.get(Module.Light)
154
+ assert light
138
155
  await handle_turn_on(dev, turn_on)
139
- await dev.set_color_temp(2700)
156
+ await light.set_color_temp(2700)
140
157
  await dev.update()
141
- assert dev.color_temp == 2700
158
+ assert light.color_temp == 2700
142
159
 
143
160
 
144
161
  @variable_temp_iot
145
- async def test_set_color_temp_transition(dev: SmartBulb, mocker):
146
- set_light_state = mocker.patch("kasa.SmartBulb.set_light_state")
162
+ async def test_set_color_temp_transition(dev: IotBulb, mocker):
163
+ set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
147
164
  await dev.set_color_temp(2700, transition=100)
148
165
 
149
166
  set_light_state.assert_called_with({"color_temp": 2700}, transition=100)
150
167
 
151
168
 
152
169
  @variable_temp_iot
153
- async def test_unknown_temp_range(dev: SmartBulb, monkeypatch, caplog):
170
+ async def test_unknown_temp_range(dev: IotBulb, monkeypatch, caplog):
154
171
  monkeypatch.setitem(dev._sys_info, "model", "unknown bulb")
155
172
 
156
173
  assert dev.valid_temperature_range == (2700, 5000)
157
174
  assert "Unknown color temperature range, fallback to 2700-5000" in caplog.text
158
175
 
159
176
 
177
+ @variable_temp_smart
178
+ async def test_smart_temp_range(dev: Device):
179
+ light = dev.modules.get(Module.Light)
180
+ assert light
181
+ assert light.valid_temperature_range
182
+
183
+
160
184
  @variable_temp
161
- async def test_out_of_range_temperature(dev: SmartBulb):
185
+ async def test_out_of_range_temperature(dev: Device):
186
+ light = dev.modules.get(Module.Light)
187
+ assert light
162
188
  with pytest.raises(ValueError):
163
- await dev.set_color_temp(1000)
189
+ await light.set_color_temp(1000)
164
190
  with pytest.raises(ValueError):
165
- await dev.set_color_temp(10000)
191
+ await light.set_color_temp(10000)
166
192
 
167
193
 
168
194
  @non_variable_temp
169
- async def test_non_variable_temp(dev: SmartBulb):
170
- with pytest.raises(SmartDeviceException):
171
- await dev.set_color_temp(2700)
195
+ async def test_non_variable_temp(dev: Device):
196
+ light = dev.modules.get(Module.Light)
197
+ assert light
198
+ with pytest.raises(KasaException):
199
+ await light.set_color_temp(2700)
172
200
 
173
- with pytest.raises(SmartDeviceException):
174
- print(dev.valid_temperature_range)
201
+ with pytest.raises(KasaException):
202
+ print(light.valid_temperature_range)
175
203
 
176
- with pytest.raises(SmartDeviceException):
177
- print(dev.color_temp)
204
+ with pytest.raises(KasaException):
205
+ print(light.color_temp)
178
206
 
179
207
 
180
- @dimmable
208
+ @dimmable_iot
181
209
  @turn_on
182
- async def test_dimmable_brightness(dev: SmartBulb, turn_on):
210
+ async def test_dimmable_brightness(dev: IotBulb, turn_on):
211
+ assert isinstance(dev, (IotBulb, IotDimmer))
183
212
  await handle_turn_on(dev, turn_on)
184
- assert dev.is_dimmable
213
+ assert dev._is_dimmable
185
214
 
186
215
  await dev.set_brightness(50)
187
216
  await dev.update()
@@ -192,12 +221,12 @@ async def test_dimmable_brightness(dev: SmartBulb, turn_on):
192
221
  assert dev.brightness == 10
193
222
 
194
223
  with pytest.raises(ValueError):
195
- await dev.set_brightness("foo")
224
+ await dev.set_brightness("foo") # type: ignore[arg-type]
196
225
 
197
226
 
198
227
  @bulb_iot
199
- async def test_turn_on_transition(dev: SmartBulb, mocker):
200
- set_light_state = mocker.patch("kasa.SmartBulb.set_light_state")
228
+ async def test_turn_on_transition(dev: IotBulb, mocker):
229
+ set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
201
230
  await dev.turn_on(transition=1000)
202
231
 
203
232
  set_light_state.assert_called_with({"on_off": 1}, transition=1000)
@@ -208,16 +237,16 @@ async def test_turn_on_transition(dev: SmartBulb, mocker):
208
237
 
209
238
 
210
239
  @bulb_iot
211
- async def test_dimmable_brightness_transition(dev: SmartBulb, mocker):
212
- set_light_state = mocker.patch("kasa.SmartBulb.set_light_state")
240
+ async def test_dimmable_brightness_transition(dev: IotBulb, mocker):
241
+ set_light_state = mocker.patch("kasa.iot.IotBulb._set_light_state")
213
242
  await dev.set_brightness(10, transition=1000)
214
243
 
215
244
  set_light_state.assert_called_with({"brightness": 10}, transition=1000)
216
245
 
217
246
 
218
- @dimmable
219
- async def test_invalid_brightness(dev: SmartBulb):
220
- assert dev.is_dimmable
247
+ @dimmable_iot
248
+ async def test_invalid_brightness(dev: IotBulb):
249
+ assert dev._is_dimmable
221
250
 
222
251
  with pytest.raises(ValueError):
223
252
  await dev.set_brightness(110)
@@ -226,21 +255,21 @@ async def test_invalid_brightness(dev: SmartBulb):
226
255
  await dev.set_brightness(-100)
227
256
 
228
257
 
229
- @non_dimmable
230
- async def test_non_dimmable(dev: SmartBulb):
231
- assert not dev.is_dimmable
258
+ @non_dimmable_iot
259
+ async def test_non_dimmable(dev: IotBulb):
260
+ assert not dev._is_dimmable
232
261
 
233
- with pytest.raises(SmartDeviceException):
262
+ with pytest.raises(KasaException):
234
263
  assert dev.brightness == 0
235
- with pytest.raises(SmartDeviceException):
264
+ with pytest.raises(KasaException):
236
265
  await dev.set_brightness(100)
237
266
 
238
267
 
239
268
  @bulb_iot
240
269
  async def test_ignore_default_not_set_without_color_mode_change_turn_on(
241
- dev: SmartBulb, mocker
270
+ dev: IotBulb, mocker
242
271
  ):
243
- query_helper = mocker.patch("kasa.SmartBulb._query_helper")
272
+ query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
244
273
  # When turning back without settings, ignore default to restore the state
245
274
  await dev.turn_on()
246
275
  args, kwargs = query_helper.call_args_list[0]
@@ -252,7 +281,7 @@ async def test_ignore_default_not_set_without_color_mode_change_turn_on(
252
281
 
253
282
 
254
283
  @bulb_iot
255
- async def test_list_presets(dev: SmartBulb):
284
+ async def test_list_presets(dev: IotBulb):
256
285
  presets = dev.presets
257
286
  assert len(presets) == len(dev.sys_info["preferred_state"])
258
287
 
@@ -265,19 +294,19 @@ async def test_list_presets(dev: SmartBulb):
265
294
 
266
295
 
267
296
  @bulb_iot
268
- async def test_modify_preset(dev: SmartBulb, mocker):
297
+ async def test_modify_preset(dev: IotBulb, mocker):
269
298
  """Verify that modifying preset calls the and exceptions are raised properly."""
270
299
  if not dev.presets:
271
300
  pytest.skip("Some strips do not support presets")
272
301
 
273
- data = {
302
+ data: dict[str, int | None] = {
274
303
  "index": 0,
275
304
  "brightness": 10,
276
305
  "hue": 0,
277
306
  "saturation": 0,
278
307
  "color_temp": 0,
279
308
  }
280
- preset = SmartBulbPreset(**data)
309
+ preset = IotLightPreset(**data) # type: ignore[call-arg, arg-type]
281
310
 
282
311
  assert preset.index == 0
283
312
  assert preset.brightness == 10
@@ -286,11 +315,12 @@ async def test_modify_preset(dev: SmartBulb, mocker):
286
315
  assert preset.color_temp == 0
287
316
 
288
317
  await dev.save_preset(preset)
318
+ await dev.update()
289
319
  assert dev.presets[0].brightness == 10
290
320
 
291
- with pytest.raises(SmartDeviceException):
321
+ with pytest.raises(KasaException):
292
322
  await dev.save_preset(
293
- SmartBulbPreset(index=5, hue=0, brightness=0, saturation=0, color_temp=0)
323
+ IotLightPreset(index=5, hue=0, brightness=0, saturation=0, color_temp=0) # type: ignore[call-arg]
294
324
  )
295
325
 
296
326
 
@@ -299,20 +329,73 @@ async def test_modify_preset(dev: SmartBulb, mocker):
299
329
  ("preset", "payload"),
300
330
  [
301
331
  (
302
- SmartBulbPreset(index=0, hue=0, brightness=1, saturation=0),
332
+ IotLightPreset(index=0, hue=0, brightness=1, saturation=0), # type: ignore[call-arg]
303
333
  {"index": 0, "hue": 0, "brightness": 1, "saturation": 0},
304
334
  ),
305
335
  (
306
- SmartBulbPreset(index=0, brightness=1, id="testid", mode=2, custom=0),
336
+ IotLightPreset(index=0, brightness=1, id="testid", mode=2, custom=0), # type: ignore[call-arg]
307
337
  {"index": 0, "brightness": 1, "id": "testid", "mode": 2, "custom": 0},
308
338
  ),
309
339
  ],
310
340
  )
311
- async def test_modify_preset_payloads(dev: SmartBulb, preset, payload, mocker):
341
+ async def test_modify_preset_payloads(dev: IotBulb, preset, payload, mocker):
312
342
  """Test that modify preset payloads ignore none values."""
313
343
  if not dev.presets:
314
344
  pytest.skip("Some strips do not support presets")
315
345
 
316
- query_helper = mocker.patch("kasa.SmartBulb._query_helper")
346
+ query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
317
347
  await dev.save_preset(preset)
318
348
  query_helper.assert_called_with(dev.LIGHT_SERVICE, "set_preferred_state", payload)
349
+
350
+
351
+ LIGHT_STATE_SCHEMA = Schema(
352
+ {
353
+ "brightness": All(int, Range(min=0, max=100)),
354
+ "color_temp": int,
355
+ "hue": All(int, Range(min=0, max=360)),
356
+ "mode": str,
357
+ "on_off": Boolean,
358
+ "saturation": All(int, Range(min=0, max=100)),
359
+ "dft_on_state": Optional(
360
+ {
361
+ "brightness": All(int, Range(min=0, max=100)),
362
+ "color_temp": All(int, Range(min=0, max=9000)),
363
+ "hue": All(int, Range(min=0, max=360)),
364
+ "mode": str,
365
+ "saturation": All(int, Range(min=0, max=100)),
366
+ }
367
+ ),
368
+ "err_code": int,
369
+ }
370
+ )
371
+
372
+ SYSINFO_SCHEMA_BULB = SYSINFO_SCHEMA.extend(
373
+ {
374
+ "ctrl_protocols": Optional(dict),
375
+ "description": Optional(str), # Seen on LBxxx, similar to dev_name
376
+ "dev_state": str,
377
+ "disco_ver": str,
378
+ "heapsize": int,
379
+ "is_color": Boolean,
380
+ "is_dimmable": Boolean,
381
+ "is_factory": Boolean,
382
+ "is_variable_color_temp": Boolean,
383
+ "light_state": LIGHT_STATE_SCHEMA,
384
+ "preferred_state": [
385
+ {
386
+ "brightness": All(int, Range(min=0, max=100)),
387
+ "color_temp": int,
388
+ "hue": All(int, Range(min=0, max=360)),
389
+ "index": int,
390
+ "saturation": All(int, Range(min=0, max=100)),
391
+ }
392
+ ],
393
+ }
394
+ )
395
+
396
+
397
+ @bulb
398
+ def test_device_type_bulb(dev: Device):
399
+ if dev.is_light_strip:
400
+ pytest.skip("bulb has also lightstrips to test the api")
401
+ assert dev.device_type == DeviceType.Bulb
@@ -0,0 +1,84 @@
1
+ import inspect
2
+ import sys
3
+
4
+ import pytest
5
+
6
+ from kasa.smart.smartchilddevice import SmartChildDevice
7
+ from kasa.smartprotocol import _ChildProtocolWrapper
8
+
9
+ from .conftest import strip_smart
10
+
11
+
12
+ @strip_smart
13
+ def test_childdevice_init(dev, dummy_protocol, mocker):
14
+ """Test that child devices get initialized and use protocol wrapper."""
15
+ assert len(dev.children) > 0
16
+
17
+ first = dev.children[0]
18
+ assert isinstance(first.protocol, _ChildProtocolWrapper)
19
+
20
+ assert first._info["category"] == "plug.powerstrip.sub-plug"
21
+ assert "position" in first._info
22
+
23
+
24
+ @strip_smart
25
+ async def test_childdevice_update(dev, dummy_protocol, mocker):
26
+ """Test that parent update updates children."""
27
+ child_info = dev.internal_state["get_child_device_list"]
28
+ child_list = child_info["child_device_list"]
29
+
30
+ assert len(dev.children) == child_info["sum"]
31
+ first = dev.children[0]
32
+
33
+ await dev.update()
34
+
35
+ assert dev._info != first._info
36
+ assert child_list[0] == first._info
37
+
38
+
39
+ @strip_smart
40
+ @pytest.mark.skipif(
41
+ sys.version_info < (3, 11),
42
+ reason="exceptiongroup requires python3.11+",
43
+ )
44
+ async def test_childdevice_properties(dev: SmartChildDevice):
45
+ """Check that accessing childdevice properties do not raise exceptions."""
46
+ assert len(dev.children) > 0
47
+
48
+ first = dev.children[0]
49
+
50
+ # children do not have children
51
+ assert not first.children
52
+
53
+ def _test_property_getters():
54
+ """Try accessing all properties and return a list of encountered exceptions."""
55
+ exceptions = []
56
+ properties = inspect.getmembers(
57
+ first.__class__, lambda o: isinstance(o, property)
58
+ )
59
+ for prop in properties:
60
+ name, _ = prop
61
+ # Skip emeter and time properties
62
+ # TODO: needs API cleanup, emeter* should probably be removed in favor
63
+ # of access through features/modules, handling of time* needs decision.
64
+ if (
65
+ name.startswith("emeter_")
66
+ or name.startswith("time")
67
+ or name.startswith("fan")
68
+ or name.startswith("color")
69
+ or name.startswith("brightness")
70
+ or name.startswith("valid_temperature_range")
71
+ or name.startswith("hsv")
72
+ or name.startswith("effect")
73
+ ):
74
+ continue
75
+ try:
76
+ _ = getattr(first, name)
77
+ except Exception as ex:
78
+ exceptions.append(ex)
79
+
80
+ return exceptions
81
+
82
+ exceptions = list(_test_property_getters())
83
+ if exceptions:
84
+ raise ExceptionGroup("Accessing child properties caused exceptions", exceptions)