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
@@ -1,12 +1,15 @@
1
+ from __future__ import annotations
2
+
1
3
  import base64
2
4
  import json
5
+ import logging
3
6
  import random
4
7
  import string
5
8
  import time
6
9
  from contextlib import nullcontext as does_not_raise
7
10
  from json import dumps as json_dumps
8
11
  from json import loads as json_loads
9
- from typing import Any, Dict
12
+ from typing import Any
10
13
 
11
14
  import aiohttp
12
15
  import pytest
@@ -18,8 +21,8 @@ from ..aestransport import AesEncyptionSession, AesTransport, TransportState
18
21
  from ..credentials import Credentials
19
22
  from ..deviceconfig import DeviceConfig
20
23
  from ..exceptions import (
21
- AuthenticationException,
22
- SmartDeviceException,
24
+ AuthenticationError,
25
+ KasaException,
23
26
  SmartErrorCode,
24
27
  )
25
28
  from ..httpclient import HttpClient
@@ -48,8 +51,8 @@ status_parameters = pytest.mark.parametrize(
48
51
  "status_code, error_code, inner_error_code, expectation",
49
52
  [
50
53
  (200, 0, 0, does_not_raise()),
51
- (400, 0, 0, pytest.raises(SmartDeviceException)),
52
- (200, -1, 0, pytest.raises(SmartDeviceException)),
54
+ (400, 0, 0, pytest.raises(KasaException)),
55
+ (200, -1, 0, pytest.raises(KasaException)),
53
56
  ],
54
57
  ids=("success", "status_code", "error_code"),
55
58
  )
@@ -100,17 +103,17 @@ async def test_login(mocker, status_code, error_code, inner_error_code, expectat
100
103
  ([SmartErrorCode.LOGIN_ERROR, 0, 0, 0], does_not_raise(), 4),
101
104
  (
102
105
  [SmartErrorCode.LOGIN_ERROR, SmartErrorCode.LOGIN_ERROR],
103
- pytest.raises(AuthenticationException),
106
+ pytest.raises(AuthenticationError),
104
107
  3,
105
108
  ),
106
109
  (
107
110
  [SmartErrorCode.LOGIN_FAILED_ERROR],
108
- pytest.raises(AuthenticationException),
111
+ pytest.raises(AuthenticationError),
109
112
  1,
110
113
  ),
111
114
  (
112
115
  [SmartErrorCode.LOGIN_ERROR, SmartErrorCode.SESSION_TIMEOUT_ERROR],
113
- pytest.raises(SmartDeviceException),
116
+ pytest.raises(KasaException),
114
117
  3,
115
118
  ),
116
119
  ],
@@ -134,6 +137,7 @@ async def test_login_errors(mocker, inner_error_codes, expectation, call_count):
134
137
  transport._state = TransportState.LOGIN_REQUIRED
135
138
  transport._session_expire_at = time.time() + 86400
136
139
  transport._encryption_session = mock_aes_device.encryption_session
140
+ mocker.patch.object(transport, "BACKOFF_SECONDS_AFTER_LOGIN_ERROR", 0)
137
141
 
138
142
  assert transport._token_url is None
139
143
 
@@ -180,6 +184,67 @@ async def test_send(mocker, status_code, error_code, inner_error_code, expectati
180
184
  assert "result" in res
181
185
 
182
186
 
187
+ async def test_unencrypted_response(mocker, caplog):
188
+ host = "127.0.0.1"
189
+ mock_aes_device = MockAesDevice(host, 200, 0, 0, do_not_encrypt_response=True)
190
+ mocker.patch.object(aiohttp.ClientSession, "post", side_effect=mock_aes_device.post)
191
+
192
+ transport = AesTransport(
193
+ config=DeviceConfig(host, credentials=Credentials("foo", "bar"))
194
+ )
195
+ transport._state = TransportState.ESTABLISHED
196
+ transport._session_expire_at = time.time() + 86400
197
+ transport._encryption_session = mock_aes_device.encryption_session
198
+ transport._token_url = transport._app_url.with_query(
199
+ f"token={mock_aes_device.token}"
200
+ )
201
+
202
+ request = {
203
+ "method": "get_device_info",
204
+ "params": None,
205
+ "request_time_milis": round(time.time() * 1000),
206
+ "requestID": 1,
207
+ "terminal_uuid": "foobar",
208
+ }
209
+ caplog.set_level(logging.DEBUG)
210
+ res = await transport.send(json_dumps(request))
211
+ assert "result" in res
212
+ assert (
213
+ "Received unencrypted response over secure passthrough from 127.0.0.1"
214
+ in caplog.text
215
+ )
216
+
217
+
218
+ async def test_unencrypted_response_invalid_json(mocker, caplog):
219
+ host = "127.0.0.1"
220
+ mock_aes_device = MockAesDevice(
221
+ host, 200, 0, 0, do_not_encrypt_response=True, send_response=b"Foobar"
222
+ )
223
+ mocker.patch.object(aiohttp.ClientSession, "post", side_effect=mock_aes_device.post)
224
+
225
+ transport = AesTransport(
226
+ config=DeviceConfig(host, credentials=Credentials("foo", "bar"))
227
+ )
228
+ transport._state = TransportState.ESTABLISHED
229
+ transport._session_expire_at = time.time() + 86400
230
+ transport._encryption_session = mock_aes_device.encryption_session
231
+ transport._token_url = transport._app_url.with_query(
232
+ f"token={mock_aes_device.token}"
233
+ )
234
+
235
+ request = {
236
+ "method": "get_device_info",
237
+ "params": None,
238
+ "request_time_milis": round(time.time() * 1000),
239
+ "requestID": 1,
240
+ "terminal_uuid": "foobar",
241
+ }
242
+ caplog.set_level(logging.DEBUG)
243
+ msg = f"Unable to decrypt response from {host}, error: Incorrect padding, response: Foobar"
244
+ with pytest.raises(KasaException, match=msg):
245
+ await transport.send(json_dumps(request))
246
+
247
+
183
248
  ERRORS = [e for e in SmartErrorCode if e != 0]
184
249
 
185
250
 
@@ -205,10 +270,21 @@ async def test_passthrough_errors(mocker, error_code):
205
270
  "requestID": 1,
206
271
  "terminal_uuid": "foobar",
207
272
  }
208
- with pytest.raises(SmartDeviceException):
273
+ with pytest.raises(KasaException):
209
274
  await transport.send(json_dumps(request))
210
275
 
211
276
 
277
+ async def test_port_override():
278
+ """Test that port override sets the app_url."""
279
+ host = "127.0.0.1"
280
+ config = DeviceConfig(
281
+ host, credentials=Credentials("foo", "bar"), port_override=12345
282
+ )
283
+ transport = AesTransport(config=config)
284
+
285
+ assert str(transport._app_url) == "http://127.0.0.1:12345/app"
286
+
287
+
212
288
  class MockAesDevice:
213
289
  class _mock_response:
214
290
  def __init__(self, status, json: dict):
@@ -222,15 +298,28 @@ class MockAesDevice:
222
298
  pass
223
299
 
224
300
  async def read(self):
225
- return json_dumps(self._json).encode()
301
+ if isinstance(self._json, dict):
302
+ return json_dumps(self._json).encode()
303
+ return self._json
226
304
 
227
305
  encryption_session = AesEncyptionSession(KEY_IV[:16], KEY_IV[16:])
228
306
 
229
- def __init__(self, host, status_code=200, error_code=0, inner_error_code=0):
307
+ def __init__(
308
+ self,
309
+ host,
310
+ status_code=200,
311
+ error_code=0,
312
+ inner_error_code=0,
313
+ *,
314
+ do_not_encrypt_response=False,
315
+ send_response=None,
316
+ ):
230
317
  self.host = host
231
318
  self.status_code = status_code
232
319
  self.error_code = error_code
233
320
  self._inner_error_code = inner_error_code
321
+ self.do_not_encrypt_response = do_not_encrypt_response
322
+ self.send_response = send_response
234
323
  self.http_client = HttpClient(DeviceConfig(self.host))
235
324
  self.inner_call_count = 0
236
325
  self.token = "".join(random.choices(string.ascii_uppercase, k=32)) # noqa: S311
@@ -248,7 +337,7 @@ class MockAesDevice:
248
337
  json = json_loads(item.decode())
249
338
  return await self._post(url, json)
250
339
 
251
- async def _post(self, url: URL, json: Dict[str, Any]):
340
+ async def _post(self, url: URL, json: dict[str, Any]):
252
341
  if json["method"] == "handshake":
253
342
  return await self._return_handshake_response(url, json)
254
343
  elif json["method"] == "securePassthrough":
@@ -256,10 +345,10 @@ class MockAesDevice:
256
345
  elif json["method"] == "login_device":
257
346
  return await self._return_login_response(url, json)
258
347
  else:
259
- assert str(url) == f"http://{self.host}/app?token={self.token}"
348
+ assert str(url) == f"http://{self.host}:80/app?token={self.token}"
260
349
  return await self._return_send_response(url, json)
261
350
 
262
- async def _return_handshake_response(self, url: URL, json: Dict[str, Any]):
351
+ async def _return_handshake_response(self, url: URL, json: dict[str, Any]):
263
352
  start = len("-----BEGIN PUBLIC KEY-----\n")
264
353
  end = len("\n-----END PUBLIC KEY-----\n")
265
354
  client_pub_key = json["params"]["key"][start:-end]
@@ -272,24 +361,26 @@ class MockAesDevice:
272
361
  self.status_code, {"result": {"key": key_64}, "error_code": self.error_code}
273
362
  )
274
363
 
275
- async def _return_secure_passthrough_response(self, url: URL, json: Dict[str, Any]):
364
+ async def _return_secure_passthrough_response(self, url: URL, json: dict[str, Any]):
276
365
  encrypted_request = json["params"]["request"]
277
366
  decrypted_request = self.encryption_session.decrypt(encrypted_request.encode())
278
367
  decrypted_request_dict = json_loads(decrypted_request)
279
368
  decrypted_response = await self._post(url, decrypted_request_dict)
280
369
  async with decrypted_response:
281
- response_data = await decrypted_response.read()
282
- decrypted_response_dict = json_loads(response_data.decode())
283
- encrypted_response = self.encryption_session.encrypt(
284
- json_dumps(decrypted_response_dict).encode()
370
+ decrypted_response_data = await decrypted_response.read()
371
+ encrypted_response = self.encryption_session.encrypt(decrypted_response_data)
372
+ response = (
373
+ decrypted_response_data
374
+ if self.do_not_encrypt_response
375
+ else encrypted_response
285
376
  )
286
377
  result = {
287
- "result": {"response": encrypted_response.decode()},
378
+ "result": {"response": response.decode()},
288
379
  "error_code": self.error_code,
289
380
  }
290
381
  return self._mock_response(self.status_code, result)
291
382
 
292
- async def _return_login_response(self, url: URL, json: Dict[str, Any]):
383
+ async def _return_login_response(self, url: URL, json: dict[str, Any]):
293
384
  if "token=" in str(url):
294
385
  raise Exception("token should not be in url for a login request")
295
386
  self.token = "".join(random.choices(string.ascii_uppercase, k=32)) # noqa: S311
@@ -297,7 +388,8 @@ class MockAesDevice:
297
388
  self.inner_call_count += 1
298
389
  return self._mock_response(self.status_code, result)
299
390
 
300
- async def _return_send_response(self, url: URL, json: Dict[str, Any]):
391
+ async def _return_send_response(self, url: URL, json: dict[str, Any]):
301
392
  result = {"result": {"method": None}, "error_code": self.inner_error_code}
393
+ response = self.send_response if self.send_response else result
302
394
  self.inner_call_count += 1
303
- return self._mock_response(self.status_code, result)
395
+ return self._mock_response(self.status_code, response)
kasa/tests/test_bulb.py CHANGED
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  import pytest
2
4
  from voluptuous import (
3
5
  All,
@@ -7,27 +9,29 @@ from voluptuous import (
7
9
  Schema,
8
10
  )
9
11
 
10
- from kasa import DeviceType, SmartBulb, SmartBulbPreset, SmartDeviceException
12
+ from kasa import Device, DeviceType, IotLightPreset, KasaException, Module
13
+ from kasa.iot import IotBulb, IotDimmer
11
14
 
12
15
  from .conftest import (
13
16
  bulb,
14
17
  bulb_iot,
15
18
  color_bulb,
16
19
  color_bulb_iot,
17
- dimmable,
20
+ dimmable_iot,
18
21
  handle_turn_on,
19
22
  non_color_bulb,
20
- non_dimmable,
23
+ non_dimmable_iot,
21
24
  non_variable_temp,
22
25
  turn_on,
23
26
  variable_temp,
24
27
  variable_temp_iot,
28
+ variable_temp_smart,
25
29
  )
26
- from .test_smartdevice import SYSINFO_SCHEMA
30
+ from .test_iotdevice import SYSINFO_SCHEMA
27
31
 
28
32
 
29
33
  @bulb
30
- async def test_bulb_sysinfo(dev: SmartBulb):
34
+ async def test_bulb_sysinfo(dev: Device):
31
35
  assert dev.sys_info is not None
32
36
  SYSINFO_SCHEMA_BULB(dev.sys_info)
33
37
 
@@ -40,17 +44,14 @@ async def test_bulb_sysinfo(dev: SmartBulb):
40
44
 
41
45
 
42
46
  @bulb
43
- async def test_state_attributes(dev: SmartBulb):
44
- assert "Brightness" in dev.state_information
45
- assert dev.state_information["Brightness"] == dev.brightness
46
-
47
- assert "Is dimmable" in dev.state_information
48
- 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)
49
50
 
50
51
 
51
52
  @bulb_iot
52
- async def test_light_state_without_update(dev: SmartBulb, monkeypatch):
53
- with pytest.raises(SmartDeviceException):
53
+ async def test_light_state_without_update(dev: IotBulb, monkeypatch):
54
+ with pytest.raises(KasaException):
54
55
  monkeypatch.setitem(
55
56
  dev._last_update["system"]["get_sysinfo"], "light_state", None
56
57
  )
@@ -58,33 +59,35 @@ async def test_light_state_without_update(dev: SmartBulb, monkeypatch):
58
59
 
59
60
 
60
61
  @bulb_iot
61
- async def test_get_light_state(dev: SmartBulb):
62
+ async def test_get_light_state(dev: IotBulb):
62
63
  LIGHT_STATE_SCHEMA(await dev.get_light_state())
63
64
 
64
65
 
65
66
  @color_bulb
66
67
  @turn_on
67
- 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
68
71
  await handle_turn_on(dev, turn_on)
69
- assert dev.is_color
72
+ assert light.is_color
70
73
 
71
- hue, saturation, brightness = dev.hsv
74
+ hue, saturation, brightness = light.hsv
72
75
  assert 0 <= hue <= 360
73
76
  assert 0 <= saturation <= 100
74
77
  assert 0 <= brightness <= 100
75
78
 
76
- await dev.set_hsv(hue=1, saturation=1, value=1)
79
+ await light.set_hsv(hue=1, saturation=1, value=1)
77
80
 
78
81
  await dev.update()
79
- hue, saturation, brightness = dev.hsv
82
+ hue, saturation, brightness = light.hsv
80
83
  assert hue == 1
81
84
  assert saturation == 1
82
85
  assert brightness == 1
83
86
 
84
87
 
85
88
  @color_bulb_iot
86
- async def test_set_hsv_transition(dev: SmartBulb, mocker):
87
- 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")
88
91
  await dev.set_hsv(10, 10, 100, transition=1000)
89
92
 
90
93
  set_light_state.assert_called_with(
@@ -95,100 +98,119 @@ async def test_set_hsv_transition(dev: SmartBulb, mocker):
95
98
 
96
99
  @color_bulb
97
100
  @turn_on
98
- 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
99
104
  await handle_turn_on(dev, turn_on)
100
- assert dev.is_color
105
+ assert light.is_color
101
106
 
102
107
  for invalid_hue in [-1, 361, 0.5]:
103
108
  with pytest.raises(ValueError):
104
- await dev.set_hsv(invalid_hue, 0, 0)
109
+ await light.set_hsv(invalid_hue, 0, 0) # type: ignore[arg-type]
105
110
 
106
111
  for invalid_saturation in [-1, 101, 0.5]:
107
112
  with pytest.raises(ValueError):
108
- await dev.set_hsv(0, invalid_saturation, 0)
113
+ await light.set_hsv(0, invalid_saturation, 0) # type: ignore[arg-type]
109
114
 
110
115
  for invalid_brightness in [-1, 101, 0.5]:
111
116
  with pytest.raises(ValueError):
112
- await dev.set_hsv(0, 0, invalid_brightness)
117
+ await light.set_hsv(0, 0, invalid_brightness) # type: ignore[arg-type]
113
118
 
114
119
 
115
120
  @color_bulb
116
- 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
117
125
  assert "HSV" in dev.state_information
118
- assert dev.state_information["HSV"] == dev.hsv
126
+ assert dev.state_information["HSV"] == light.hsv
119
127
 
120
128
 
121
129
  @non_color_bulb
122
- async def test_hsv_on_non_color(dev: SmartBulb):
123
- 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
124
134
 
125
- with pytest.raises(SmartDeviceException):
126
- await dev.set_hsv(0, 0, 0)
127
- with pytest.raises(SmartDeviceException):
128
- 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)
129
139
 
130
140
 
131
141
  @variable_temp
132
- 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
133
146
  assert "Color temperature" in dev.state_information
134
- assert dev.state_information["Color temperature"] == dev.color_temp
135
-
136
- assert "Valid temperature range" in dev.state_information
137
- assert (
138
- dev.state_information["Valid temperature range"] == dev.valid_temperature_range
139
- )
147
+ assert dev.state_information["Color temperature"] == light.color_temp
140
148
 
141
149
 
142
150
  @variable_temp
143
151
  @turn_on
144
- 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
145
155
  await handle_turn_on(dev, turn_on)
146
- await dev.set_color_temp(2700)
156
+ await light.set_color_temp(2700)
147
157
  await dev.update()
148
- assert dev.color_temp == 2700
158
+ assert light.color_temp == 2700
149
159
 
150
160
 
151
161
  @variable_temp_iot
152
- async def test_set_color_temp_transition(dev: SmartBulb, mocker):
153
- 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")
154
164
  await dev.set_color_temp(2700, transition=100)
155
165
 
156
166
  set_light_state.assert_called_with({"color_temp": 2700}, transition=100)
157
167
 
158
168
 
159
169
  @variable_temp_iot
160
- async def test_unknown_temp_range(dev: SmartBulb, monkeypatch, caplog):
170
+ async def test_unknown_temp_range(dev: IotBulb, monkeypatch, caplog):
161
171
  monkeypatch.setitem(dev._sys_info, "model", "unknown bulb")
162
172
 
163
173
  assert dev.valid_temperature_range == (2700, 5000)
164
174
  assert "Unknown color temperature range, fallback to 2700-5000" in caplog.text
165
175
 
166
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
+
167
184
  @variable_temp
168
- 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
169
188
  with pytest.raises(ValueError):
170
- await dev.set_color_temp(1000)
189
+ await light.set_color_temp(1000)
171
190
  with pytest.raises(ValueError):
172
- await dev.set_color_temp(10000)
191
+ await light.set_color_temp(10000)
173
192
 
174
193
 
175
194
  @non_variable_temp
176
- async def test_non_variable_temp(dev: SmartBulb):
177
- with pytest.raises(SmartDeviceException):
178
- 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)
179
200
 
180
- with pytest.raises(SmartDeviceException):
181
- print(dev.valid_temperature_range)
201
+ with pytest.raises(KasaException):
202
+ print(light.valid_temperature_range)
182
203
 
183
- with pytest.raises(SmartDeviceException):
184
- print(dev.color_temp)
204
+ with pytest.raises(KasaException):
205
+ print(light.color_temp)
185
206
 
186
207
 
187
- @dimmable
208
+ @dimmable_iot
188
209
  @turn_on
189
- 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))
190
212
  await handle_turn_on(dev, turn_on)
191
- assert dev.is_dimmable
213
+ assert dev._is_dimmable
192
214
 
193
215
  await dev.set_brightness(50)
194
216
  await dev.update()
@@ -199,12 +221,12 @@ async def test_dimmable_brightness(dev: SmartBulb, turn_on):
199
221
  assert dev.brightness == 10
200
222
 
201
223
  with pytest.raises(ValueError):
202
- await dev.set_brightness("foo")
224
+ await dev.set_brightness("foo") # type: ignore[arg-type]
203
225
 
204
226
 
205
227
  @bulb_iot
206
- async def test_turn_on_transition(dev: SmartBulb, mocker):
207
- 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")
208
230
  await dev.turn_on(transition=1000)
209
231
 
210
232
  set_light_state.assert_called_with({"on_off": 1}, transition=1000)
@@ -215,16 +237,16 @@ async def test_turn_on_transition(dev: SmartBulb, mocker):
215
237
 
216
238
 
217
239
  @bulb_iot
218
- async def test_dimmable_brightness_transition(dev: SmartBulb, mocker):
219
- 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")
220
242
  await dev.set_brightness(10, transition=1000)
221
243
 
222
244
  set_light_state.assert_called_with({"brightness": 10}, transition=1000)
223
245
 
224
246
 
225
- @dimmable
226
- async def test_invalid_brightness(dev: SmartBulb):
227
- assert dev.is_dimmable
247
+ @dimmable_iot
248
+ async def test_invalid_brightness(dev: IotBulb):
249
+ assert dev._is_dimmable
228
250
 
229
251
  with pytest.raises(ValueError):
230
252
  await dev.set_brightness(110)
@@ -233,21 +255,21 @@ async def test_invalid_brightness(dev: SmartBulb):
233
255
  await dev.set_brightness(-100)
234
256
 
235
257
 
236
- @non_dimmable
237
- async def test_non_dimmable(dev: SmartBulb):
238
- assert not dev.is_dimmable
258
+ @non_dimmable_iot
259
+ async def test_non_dimmable(dev: IotBulb):
260
+ assert not dev._is_dimmable
239
261
 
240
- with pytest.raises(SmartDeviceException):
262
+ with pytest.raises(KasaException):
241
263
  assert dev.brightness == 0
242
- with pytest.raises(SmartDeviceException):
264
+ with pytest.raises(KasaException):
243
265
  await dev.set_brightness(100)
244
266
 
245
267
 
246
268
  @bulb_iot
247
269
  async def test_ignore_default_not_set_without_color_mode_change_turn_on(
248
- dev: SmartBulb, mocker
270
+ dev: IotBulb, mocker
249
271
  ):
250
- query_helper = mocker.patch("kasa.SmartBulb._query_helper")
272
+ query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
251
273
  # When turning back without settings, ignore default to restore the state
252
274
  await dev.turn_on()
253
275
  args, kwargs = query_helper.call_args_list[0]
@@ -259,7 +281,7 @@ async def test_ignore_default_not_set_without_color_mode_change_turn_on(
259
281
 
260
282
 
261
283
  @bulb_iot
262
- async def test_list_presets(dev: SmartBulb):
284
+ async def test_list_presets(dev: IotBulb):
263
285
  presets = dev.presets
264
286
  assert len(presets) == len(dev.sys_info["preferred_state"])
265
287
 
@@ -272,19 +294,19 @@ async def test_list_presets(dev: SmartBulb):
272
294
 
273
295
 
274
296
  @bulb_iot
275
- async def test_modify_preset(dev: SmartBulb, mocker):
297
+ async def test_modify_preset(dev: IotBulb, mocker):
276
298
  """Verify that modifying preset calls the and exceptions are raised properly."""
277
299
  if not dev.presets:
278
300
  pytest.skip("Some strips do not support presets")
279
301
 
280
- data = {
302
+ data: dict[str, int | None] = {
281
303
  "index": 0,
282
304
  "brightness": 10,
283
305
  "hue": 0,
284
306
  "saturation": 0,
285
307
  "color_temp": 0,
286
308
  }
287
- preset = SmartBulbPreset(**data)
309
+ preset = IotLightPreset(**data) # type: ignore[call-arg, arg-type]
288
310
 
289
311
  assert preset.index == 0
290
312
  assert preset.brightness == 10
@@ -293,11 +315,12 @@ async def test_modify_preset(dev: SmartBulb, mocker):
293
315
  assert preset.color_temp == 0
294
316
 
295
317
  await dev.save_preset(preset)
318
+ await dev.update()
296
319
  assert dev.presets[0].brightness == 10
297
320
 
298
- with pytest.raises(SmartDeviceException):
321
+ with pytest.raises(KasaException):
299
322
  await dev.save_preset(
300
- 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]
301
324
  )
302
325
 
303
326
 
@@ -306,21 +329,21 @@ async def test_modify_preset(dev: SmartBulb, mocker):
306
329
  ("preset", "payload"),
307
330
  [
308
331
  (
309
- SmartBulbPreset(index=0, hue=0, brightness=1, saturation=0),
332
+ IotLightPreset(index=0, hue=0, brightness=1, saturation=0), # type: ignore[call-arg]
310
333
  {"index": 0, "hue": 0, "brightness": 1, "saturation": 0},
311
334
  ),
312
335
  (
313
- 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]
314
337
  {"index": 0, "brightness": 1, "id": "testid", "mode": 2, "custom": 0},
315
338
  ),
316
339
  ],
317
340
  )
318
- async def test_modify_preset_payloads(dev: SmartBulb, preset, payload, mocker):
341
+ async def test_modify_preset_payloads(dev: IotBulb, preset, payload, mocker):
319
342
  """Test that modify preset payloads ignore none values."""
320
343
  if not dev.presets:
321
344
  pytest.skip("Some strips do not support presets")
322
345
 
323
- query_helper = mocker.patch("kasa.SmartBulb._query_helper")
346
+ query_helper = mocker.patch("kasa.iot.IotBulb._query_helper")
324
347
  await dev.save_preset(preset)
325
348
  query_helper.assert_called_with(dev.LIGHT_SERVICE, "set_preferred_state", payload)
326
349
 
@@ -369,3 +392,10 @@ SYSINFO_SCHEMA_BULB = SYSINFO_SCHEMA.extend(
369
392
  ],
370
393
  }
371
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