python-kasa 0.6.2.1__py3-none-any.whl → 0.7.0.dev1__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 +90 -27
  2. kasa/aestransport.py +51 -34
  3. kasa/cli.py +326 -130
  4. kasa/device.py +414 -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 +138 -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} +103 -147
  22. kasa/{smartdevice.py → iot/iotdevice.py} +176 -286
  23. kasa/{smartdimmer.py → iot/iotdimmer.py} +45 -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 +263 -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 +166 -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 +116 -86
  120. kasa/tests/test_childdevice.py +19 -7
  121. kasa/tests/test_cli.py +453 -99
  122. kasa/tests/test_common_modules.py +242 -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 +42 -33
  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.dev1.dist-info}/METADATA +45 -143
  144. python_kasa-0.7.0.dev1.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.dev1.dist-info}/LICENSE +0 -0
  163. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev1.dist-info}/WHEEL +0 -0
  164. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev1.dist-info}/entry_points.txt +0 -0
@@ -1,4 +1,4 @@
1
- from itertools import chain
1
+ import logging
2
2
 
3
3
  import pytest
4
4
 
@@ -6,11 +6,11 @@ from ..credentials import Credentials
6
6
  from ..deviceconfig import DeviceConfig
7
7
  from ..exceptions import (
8
8
  SMART_RETRYABLE_ERRORS,
9
- SMART_TIMEOUT_ERRORS,
10
- SmartDeviceException,
9
+ KasaException,
11
10
  SmartErrorCode,
12
11
  )
13
- from ..smartprotocol import _ChildProtocolWrapper
12
+ from ..smartprotocol import SmartProtocol, _ChildProtocolWrapper
13
+ from .fakeprotocol_smart import FakeSmartTransport
14
14
 
15
15
  DUMMY_QUERY = {"foobar": {"foo": "bar", "bar": "foo"}}
16
16
  DUMMY_MULTIPLE_QUERY = {
@@ -28,13 +28,10 @@ async def test_smart_device_errors(dummy_protocol, mocker, error_code):
28
28
  dummy_protocol._transport, "send", return_value=mock_response
29
29
  )
30
30
 
31
- with pytest.raises(SmartDeviceException):
31
+ with pytest.raises(KasaException):
32
32
  await dummy_protocol.query(DUMMY_QUERY, retry_count=2)
33
33
 
34
- if error_code in chain(SMART_TIMEOUT_ERRORS, SMART_RETRYABLE_ERRORS):
35
- expected_calls = 3
36
- else:
37
- expected_calls = 1
34
+ expected_calls = 3 if error_code in SMART_RETRYABLE_ERRORS else 1
38
35
  assert send_mock.call_count == expected_calls
39
36
 
40
37
 
@@ -42,6 +39,11 @@ async def test_smart_device_errors(dummy_protocol, mocker, error_code):
42
39
  async def test_smart_device_errors_in_multiple_request(
43
40
  dummy_protocol, mocker, error_code
44
41
  ):
42
+ mock_request = {
43
+ "foobar1": {"foo": "bar", "bar": "foo"},
44
+ "foobar2": {"foo": "bar", "bar": "foo"},
45
+ "foobar3": {"foo": "bar", "bar": "foo"},
46
+ }
45
47
  mock_response = {
46
48
  "result": {
47
49
  "responses": [
@@ -60,13 +62,11 @@ async def test_smart_device_errors_in_multiple_request(
60
62
  send_mock = mocker.patch.object(
61
63
  dummy_protocol._transport, "send", return_value=mock_response
62
64
  )
63
- with pytest.raises(SmartDeviceException):
64
- await dummy_protocol.query(DUMMY_MULTIPLE_QUERY, retry_count=2)
65
- if error_code in chain(SMART_TIMEOUT_ERRORS, SMART_RETRYABLE_ERRORS):
66
- expected_calls = 3
67
- else:
68
- expected_calls = 1
69
- assert send_mock.call_count == expected_calls
65
+
66
+ resp_dict = await dummy_protocol.query(mock_request, retry_count=2)
67
+ assert resp_dict["foobar2"] == error_code
68
+ assert send_mock.call_count == 1
69
+ assert len(resp_dict) == len(mock_request)
70
70
 
71
71
 
72
72
  @pytest.mark.parametrize("request_size", [1, 3, 5, 10])
@@ -127,11 +127,10 @@ async def test_childdevicewrapper_error(dummy_protocol, mocker):
127
127
  mock_response = {"error_code": 0, "result": {"responseData": {"error_code": -1001}}}
128
128
 
129
129
  mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
130
- with pytest.raises(SmartDeviceException):
130
+ with pytest.raises(KasaException):
131
131
  await wrapped_protocol.query(DUMMY_QUERY)
132
132
 
133
133
 
134
- @pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
135
134
  async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, mocker):
136
135
  """Test that unwrapping multiplerequest works correctly."""
137
136
  mock_response = {
@@ -155,13 +154,12 @@ async def test_childdevicewrapper_unwrapping_multiplerequest(dummy_protocol, moc
155
154
  }
156
155
  },
157
156
  }
158
-
159
- mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response)
160
- resp = await dummy_protocol.query(DUMMY_QUERY)
157
+ wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
158
+ mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
159
+ resp = await wrapped_protocol.query(DUMMY_QUERY)
161
160
  assert resp == {"get_device_info": {"foo": "bar"}, "second_command": {"bar": "foo"}}
162
161
 
163
162
 
164
- @pytest.mark.skip("childprotocolwrapper does not yet support multirequests")
165
163
  async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
166
164
  """Test that errors inside multipleRequest response of responseData raise an exception."""
167
165
  mock_response = {
@@ -181,7 +179,114 @@ async def test_childdevicewrapper_multiplerequest_error(dummy_protocol, mocker):
181
179
  }
182
180
  },
183
181
  }
182
+ wrapped_protocol = _ChildProtocolWrapper("dummyid", dummy_protocol)
183
+ mocker.patch.object(wrapped_protocol._transport, "send", return_value=mock_response)
184
+ with pytest.raises(KasaException):
185
+ await wrapped_protocol.query(DUMMY_QUERY)
186
+
187
+
188
+ @pytest.mark.parametrize("list_sum", [5, 10, 30])
189
+ @pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
190
+ async def test_smart_protocol_lists_single_request(mocker, list_sum, batch_size):
191
+ child_device_list = [{"foo": i} for i in range(list_sum)]
192
+ response = {
193
+ "get_child_device_list": {
194
+ "child_device_list": child_device_list,
195
+ "start_index": 0,
196
+ "sum": list_sum,
197
+ }
198
+ }
199
+ request = {"get_child_device_list": None}
200
+
201
+ ft = FakeSmartTransport(
202
+ response,
203
+ "foobar",
204
+ list_return_size=batch_size,
205
+ component_nego_not_included=True,
206
+ )
207
+ protocol = SmartProtocol(transport=ft)
208
+ query_spy = mocker.spy(protocol, "_execute_query")
209
+ resp = await protocol.query(request)
210
+ expected_count = int(list_sum / batch_size) + (1 if list_sum % batch_size else 0)
211
+ assert query_spy.call_count == expected_count
212
+ assert resp == response
213
+
214
+
215
+ @pytest.mark.parametrize("list_sum", [5, 10, 30])
216
+ @pytest.mark.parametrize("batch_size", [1, 2, 3, 50])
217
+ async def test_smart_protocol_lists_multiple_request(mocker, list_sum, batch_size):
218
+ child_list = [{"foo": i} for i in range(list_sum)]
219
+ response = {
220
+ "get_child_device_list": {
221
+ "child_device_list": child_list,
222
+ "start_index": 0,
223
+ "sum": list_sum,
224
+ },
225
+ "get_child_device_component_list": {
226
+ "child_component_list": child_list,
227
+ "start_index": 0,
228
+ "sum": list_sum,
229
+ },
230
+ }
231
+ request = {"get_child_device_list": None, "get_child_device_component_list": None}
232
+
233
+ ft = FakeSmartTransport(
234
+ response,
235
+ "foobar",
236
+ list_return_size=batch_size,
237
+ component_nego_not_included=True,
238
+ )
239
+ protocol = SmartProtocol(transport=ft)
240
+ query_spy = mocker.spy(protocol, "_execute_query")
241
+ resp = await protocol.query(request)
242
+ expected_count = 1 + 2 * (
243
+ int(list_sum / batch_size) + (0 if list_sum % batch_size else -1)
244
+ )
245
+ assert query_spy.call_count == expected_count
246
+ assert resp == response
247
+
248
+
249
+ async def test_incomplete_list(mocker, caplog):
250
+ """Test for handling incomplete lists returned from queries."""
251
+ info = {
252
+ "get_preset_rules": {
253
+ "start_index": 0,
254
+ "states": [
255
+ {
256
+ "brightness": 50,
257
+ },
258
+ {
259
+ "brightness": 100,
260
+ },
261
+ ],
262
+ "sum": 7,
263
+ }
264
+ }
265
+ caplog.set_level(logging.ERROR)
266
+ transport = FakeSmartTransport(
267
+ info,
268
+ "dummy-name",
269
+ component_nego_not_included=True,
270
+ warn_fixture_missing_methods=False,
271
+ )
272
+ protocol = SmartProtocol(transport=transport)
273
+ resp = await protocol.query({"get_preset_rules": None})
274
+ assert resp
275
+ assert resp["get_preset_rules"]["sum"] == 2 # FakeTransport fixes sum
276
+ assert caplog.text == ""
184
277
 
185
- mocker.patch.object(dummy_protocol._transport, "send", return_value=mock_response)
186
- with pytest.raises(SmartDeviceException):
187
- await dummy_protocol.query(DUMMY_QUERY)
278
+ # Test behaviour without FakeTranport fix
279
+ transport = FakeSmartTransport(
280
+ info,
281
+ "dummy-name",
282
+ component_nego_not_included=True,
283
+ warn_fixture_missing_methods=False,
284
+ fix_incomplete_fixture_lists=False,
285
+ )
286
+ protocol = SmartProtocol(transport=transport)
287
+ resp = await protocol.query({"get_preset_rules": None})
288
+ assert resp["get_preset_rules"]["sum"] == 7
289
+ assert (
290
+ "Device 127.0.0.123 returned empty results list for method get_preset_rules"
291
+ in caplog.text
292
+ )
kasa/tests/test_strip.py CHANGED
@@ -2,7 +2,8 @@ from datetime import datetime
2
2
 
3
3
  import pytest
4
4
 
5
- from kasa import SmartDeviceException, SmartStrip
5
+ from kasa import KasaException
6
+ from kasa.iot import IotStrip
6
7
 
7
8
  from .conftest import handle_turn_on, strip, turn_on
8
9
 
@@ -68,22 +69,22 @@ async def test_children_on_since(dev):
68
69
 
69
70
 
70
71
  @strip
71
- async def test_get_plug_by_name(dev: SmartStrip):
72
+ async def test_get_plug_by_name(dev: IotStrip):
72
73
  name = dev.children[0].alias
73
74
  assert dev.get_plug_by_name(name) == dev.children[0] # type: ignore[arg-type]
74
75
 
75
- with pytest.raises(SmartDeviceException):
76
+ with pytest.raises(KasaException):
76
77
  dev.get_plug_by_name("NONEXISTING NAME")
77
78
 
78
79
 
79
80
  @strip
80
- async def test_get_plug_by_index(dev: SmartStrip):
81
+ async def test_get_plug_by_index(dev: IotStrip):
81
82
  assert dev.get_plug_by_index(0) == dev.children[0]
82
83
 
83
- with pytest.raises(SmartDeviceException):
84
+ with pytest.raises(KasaException):
84
85
  dev.get_plug_by_index(-1)
85
86
 
86
- with pytest.raises(SmartDeviceException):
87
+ with pytest.raises(KasaException):
87
88
  dev.get_plug_by_index(len(dev.children))
88
89
 
89
90
 
@@ -130,3 +131,11 @@ async def test_all_binary_states(dev):
130
131
  # original state map should be restored
131
132
  for index, state in dev.is_on.items():
132
133
  assert state == state_map[index]
134
+
135
+
136
+ @strip
137
+ def test_children_api(dev):
138
+ """Test the child device API."""
139
+ first = dev.children[0]
140
+ first_by_get_child_device = dev.get_child_device(first.device_id)
141
+ assert first == first_by_get_child_device
kasa/tests/test_usage.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import datetime
2
2
  from unittest.mock import Mock
3
3
 
4
- from kasa.modules import Usage
4
+ from kasa.iot.modules import Usage
5
5
 
6
6
 
7
7
  def test_usage_convert_stat_data():
kasa/xortransport.py CHANGED
@@ -9,6 +9,9 @@ https://github.com/softScheck/tplink-smartplug/
9
9
  which are licensed under the Apache License, Version 2.0
10
10
  http://www.apache.org/licenses/LICENSE-2.0
11
11
  """
12
+
13
+ from __future__ import annotations
14
+
12
15
  import asyncio
13
16
  import contextlib
14
17
  import errno
@@ -16,14 +19,14 @@ import logging
16
19
  import socket
17
20
  import struct
18
21
  from pprint import pformat as pf
19
- from typing import Dict, Generator, Optional
22
+ from typing import Generator
20
23
 
21
24
  # When support for cpython older than 3.11 is dropped
22
25
  # async_timeout can be replaced with asyncio.timeout
23
26
  from async_timeout import timeout as asyncio_timeout
24
27
 
25
28
  from .deviceconfig import DeviceConfig
26
- from .exceptions import RetryableException, SmartDeviceException
29
+ from .exceptions import KasaException, _RetryableError
27
30
  from .json import loads as json_loads
28
31
  from .protocol import BaseTransport
29
32
 
@@ -40,10 +43,10 @@ class XorTransport(BaseTransport):
40
43
 
41
44
  def __init__(self, *, config: DeviceConfig) -> None:
42
45
  super().__init__(config=config)
43
- self.reader: Optional[asyncio.StreamReader] = None
44
- self.writer: Optional[asyncio.StreamWriter] = None
46
+ self.reader: asyncio.StreamReader | None = None
47
+ self.writer: asyncio.StreamWriter | None = None
45
48
  self.query_lock = asyncio.Lock()
46
- self.loop: Optional[asyncio.AbstractEventLoop] = None
49
+ self.loop: asyncio.AbstractEventLoop | None = None
47
50
 
48
51
  @property
49
52
  def default_port(self):
@@ -71,7 +74,7 @@ class XorTransport(BaseTransport):
71
74
  # the buffer on the device
72
75
  sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
73
76
 
74
- async def _execute_send(self, request: str) -> Dict:
77
+ async def _execute_send(self, request: str) -> dict:
75
78
  """Execute a query on the device and wait for the response."""
76
79
  assert self.writer is not None # noqa: S101
77
80
  assert self.reader is not None # noqa: S101
@@ -114,7 +117,7 @@ class XorTransport(BaseTransport):
114
117
  """
115
118
  await self.close()
116
119
 
117
- async def send(self, request: str) -> Dict:
120
+ async def send(self, request: str) -> dict:
118
121
  """Send a message to the device and return a response."""
119
122
  #
120
123
  # Most of the time we will already be connected if the device is online
@@ -129,24 +132,24 @@ class XorTransport(BaseTransport):
129
132
  await self._connect(self._timeout)
130
133
  except ConnectionRefusedError as ex:
131
134
  await self.reset()
132
- raise SmartDeviceException(
135
+ raise KasaException(
133
136
  f"Unable to connect to the device: {self._host}:{self._port}: {ex}"
134
137
  ) from ex
135
138
  except OSError as ex:
136
139
  await self.reset()
137
140
  if ex.errno in _NO_RETRY_ERRORS:
138
- raise SmartDeviceException(
141
+ raise KasaException(
139
142
  f"Unable to connect to the device:"
140
143
  f" {self._host}:{self._port}: {ex}"
141
144
  ) from ex
142
145
  else:
143
- raise RetryableException(
146
+ raise _RetryableError(
144
147
  f"Unable to connect to the device:"
145
148
  f" {self._host}:{self._port}: {ex}"
146
149
  ) from ex
147
150
  except Exception as ex:
148
151
  await self.reset()
149
- raise RetryableException(
152
+ raise _RetryableError(
150
153
  f"Unable to connect to the device:" f" {self._host}:{self._port}: {ex}"
151
154
  ) from ex
152
155
  except BaseException:
@@ -162,7 +165,7 @@ class XorTransport(BaseTransport):
162
165
  return await self._execute_send(request)
163
166
  except Exception as ex:
164
167
  await self.reset()
165
- raise RetryableException(
168
+ raise _RetryableError(
166
169
  f"Unable to query the device {self._host}:{self._port}: {ex}"
167
170
  ) from ex
168
171
  except BaseException:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-kasa
3
- Version: 0.6.2.1
3
+ Version: 0.7.0.dev1
4
4
  Summary: Python API for TP-Link Kasa Smarthome devices
5
5
  Home-page: https://github.com/python-kasa/python-kasa
6
6
  License: GPL-3.0-or-later
@@ -14,6 +14,7 @@ Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Programming Language :: Python :: 3.12
16
16
  Provides-Extra: docs
17
+ Provides-Extra: shell
17
18
  Provides-Extra: speedups
18
19
  Requires-Dist: aiohttp (>=3)
19
20
  Requires-Dist: anyio
@@ -24,9 +25,11 @@ Requires-Dist: docutils (>=0.17) ; extra == "docs"
24
25
  Requires-Dist: kasa-crypt (>=0.2.0) ; extra == "speedups"
25
26
  Requires-Dist: myst-parser ; extra == "docs"
26
27
  Requires-Dist: orjson (>=3.9.1) ; extra == "speedups"
27
- Requires-Dist: pydantic (>=1)
28
+ Requires-Dist: ptpython ; extra == "shell"
29
+ Requires-Dist: pydantic (>=1.10.15)
30
+ Requires-Dist: rich ; extra == "shell"
28
31
  Requires-Dist: sphinx (>=5,<6) ; extra == "docs"
29
- Requires-Dist: sphinx_rtd_theme (>=0,<1) ; extra == "docs"
32
+ Requires-Dist: sphinx_rtd_theme (>=2,<3) ; extra == "docs"
30
33
  Requires-Dist: sphinxcontrib-programoutput (>=0,<1) ; extra == "docs"
31
34
  Project-URL: Bug Tracker, https://github.com/python-kasa/python-kasa/issues
32
35
  Project-URL: Documentation, https://python-kasa.readthedocs.io
@@ -55,11 +58,19 @@ You can install the most recent release using pip:
55
58
  pip install python-kasa
56
59
  ```
57
60
 
61
+ For enhanced cli tool support (coloring, embedded shell) install with `[shell]`:
62
+ ```
63
+ pip install python-kasa[shell]
64
+ ```
65
+
58
66
  If you are using cpython, it is recommended to install with `[speedups]` to enable orjson (faster json support):
59
67
  ```
60
68
  pip install python-kasa[speedups]
61
69
  ```
62
-
70
+ or for both:
71
+ ```
72
+ pip install python-kasa[speedups, shell]
73
+ ```
63
74
  With `[speedups]`, the protocol overhead is roughly an order of magnitude lower (benchmarks available in devtools).
64
75
 
65
76
  Alternatively, you can clone this repository and use poetry to install the development version:
@@ -198,7 +209,8 @@ Current state: {'total': 133.105, 'power': 108.223577, 'current': 0.54463, 'volt
198
209
 
199
210
  # Library usage
200
211
 
201
- If you want to use this library in your own project, a good starting point is to check [the documentation on discovering devices](https://python-kasa.readthedocs.io/en/latest/discover.html).
212
+ If you want to use this library in your own project, a good starting point is [the tutorial in the documentation](https://python-kasa.readthedocs.io/en/latest/tutorial.html).
213
+
202
214
  You can find several code examples in the API documentation of each of the implementation base classes, check out the [documentation for the base class shared by all supported devices](https://python-kasa.readthedocs.io/en/latest/smartdevice.html).
203
215
 
204
216
  [The library design and module structure is described in a separate page](https://python-kasa.readthedocs.io/en/latest/design.html).
@@ -212,152 +224,41 @@ The device type specific documentation can be found in their separate pages:
212
224
 
213
225
  ## Contributing
214
226
 
215
- Contributions are very welcome! To simplify the process, we are leveraging automated checks and tests for contributions.
216
-
217
- ### Setting up development environment
218
-
219
- To get started, simply clone this repository and initialize the development environment.
220
- We are using [poetry](https://python-poetry.org) for dependency management, so after cloning the repository simply execute
221
- `poetry install` which will install all necessary packages and create a virtual environment for you.
222
-
223
- ### Code-style checks
224
-
225
- We use several tools to automatically check all contributions. The simplest way to verify that everything is formatted properly
226
- before creating a pull request, consider activating the pre-commit hooks by executing `pre-commit install`.
227
- This will make sure that the checks are passing when you do a commit.
228
-
229
- You can also execute the checks by running either `tox -e lint` to only do the linting checks, or `tox` to also execute the tests.
230
-
231
- ### Running tests
232
-
233
- You can run tests on the library by executing `pytest` in the source directory.
234
- This will run the tests against contributed example responses, but you can also execute the tests against a real device:
235
- ```
236
- $ pytest --ip <address>
237
- ```
238
- Note that this will perform state changes on the device.
239
-
240
- ### Analyzing network captures
241
-
242
- The simplest way to add support for a new device or to improve existing ones is to capture traffic between the mobile app and the device.
243
- After capturing the traffic, you can either use the [softScheck's wireshark dissector](https://github.com/softScheck/tplink-smartplug#wireshark-dissector)
244
- or the `parse_pcap.py` script contained inside the `devtools` directory.
245
- Note, that this works currently only on kasa-branded devices which use port 9999 for communications.
246
-
227
+ Contributions are very welcome! The easiest way to contribute is by [creating a fixture file](https://python-kasa.readthedocs.io/en/latest/contribute.html#contributing-fixture-files) for the automated test suite if your device hardware and firmware version is not currently listed as supported.
228
+ Please refer to [our contributing guidelines](https://python-kasa.readthedocs.io/en/latest/contribute.html).
247
229
 
248
230
  ## Supported devices
249
231
 
250
- In principle, most kasa-branded devices that are locally controllable using the official Kasa mobile app work with this library.
251
-
252
- The following lists the devices that have been manually verified to work.
253
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
254
-
255
- ### Plugs
256
-
257
- * HS100
258
- * HS103
259
- * HS105
260
- * HS107
261
- * HS110
262
- * KP100
263
- * KP105
264
- * KP115
265
- * KP125
266
- * KP125M [See note below](#newer-kasa-branded-devices)
267
- * KP401
268
- * EP10
269
- * EP25 [See note below](#newer-kasa-branded-devices)
270
-
271
- ### Power Strips
272
-
273
- * EP40
274
- * HS300
275
- * KP303
276
- * KP200 (in wall)
277
- * KP400
278
- * KP405 (dimmer)
279
-
280
- ### Wall switches
281
-
282
- * ES20M
283
- * HS200
284
- * HS210
285
- * HS220
286
- * KS200M (partial support, no motion, no daylight detection)
287
- * KS220M (partial support, no motion, no daylight detection)
288
- * KS230
289
-
290
- ### Bulbs
291
-
292
- * LB100
293
- * LB110
294
- * LB120
295
- * LB130
296
- * LB230
297
- * KL50
298
- * KL60
299
- * KL110
300
- * KL120
301
- * KL125
302
- * KL130
303
- * KL135
304
-
305
- ### Light strips
306
-
307
- * KL400L5
308
- * KL420L5
309
- * KL430
310
-
311
- ### Tapo branded devices
312
-
313
- The library has recently added a limited supported for devices that carry Tapo branding.
314
-
315
- At the moment, the following devices have been confirmed to work:
316
-
317
- #### Plugs
318
-
319
- * Tapo P110
320
- * Tapo P125M
321
- * Tapo P135 (dimming not yet supported)
322
- * Tapo TP15
323
-
324
- #### Bulbs
325
-
326
- * Tapo L510B
327
- * Tapo L510E
328
- * Tapo L530E
329
-
330
- #### Light strips
331
-
332
- * Tapo L900-5
333
- * Tapo L900-10
334
- * Tapo L920-5
335
- * Tapo L930-5
336
-
337
- #### Wall switches
338
-
339
- * Tapo S500D
340
- * Tapo S505
341
-
342
- #### Power strips
343
-
344
- * Tapo P300
345
- * Tapo TP25
232
+ The following devices have been tested and confirmed as working. If your device is unlisted but working, please consider [contributing a fixture file](https://python-kasa.readthedocs.io/en/latest/contribute.html#contributing-fixture-files).
346
233
 
234
+ <!--Do not edit text inside the SUPPORTED section below -->
235
+ <!--SUPPORTED_START-->
236
+ ### Supported Kasa devices
347
237
 
348
- ### Newer Kasa branded devices
238
+ - **Plugs**: EP10, EP25<sup>\*</sup>, HS100<sup>\*\*</sup>, HS103, HS105, HS110, KP100, KP105, KP115, KP125, KP125M<sup>\*</sup>, KP401
239
+ - **Power Strips**: EP40, HS107, HS300, KP200, KP303, KP400
240
+ - **Wall Switches**: ES20M, HS200, HS210, HS220, KP405, KS200M, KS205<sup>\*</sup>, KS220M, KS225<sup>\*</sup>, KS230, KS240<sup>\*</sup>
241
+ - **Bulbs**: KL110, KL120, KL125, KL130, KL135, KL50, KL60, LB110
242
+ - **Light Strips**: KL400L5, KL420L5, KL430
243
+ - **Hubs**: KH100<sup>\*</sup>
244
+ - **Hub-Connected Devices<sup>\*\*\*</sup>**: KE100<sup>\*</sup>
349
245
 
350
- Some newer hardware versions of Kasa branded devices are now using the same protocol as
351
- Tapo branded devices. Support for these devices is currently limited as per TAPO branded
352
- devices:
246
+ ### Supported Tapo<sup>\*</sup> devices
353
247
 
354
- * Kasa EP25 (plug) hw_version 2.6
355
- * Kasa KP125M (plug)
356
- * Kasa KS205 (Wifi/Matter Wall Switch)
357
- * Kasa KS225 (Wifi/Matter Wall Dimmer Switch)
248
+ - **Plugs**: P100, P110, P125M, P135, TP15
249
+ - **Power Strips**: P300, TP25
250
+ - **Wall Switches**: S500D, S505
251
+ - **Bulbs**: L510B, L510E, L530E
252
+ - **Light Strips**: L900-10, L900-5, L920-5, L930-5
253
+ - **Hubs**: H100
254
+ - **Hub-Connected Devices<sup>\*\*\*</sup>**: T110, T300, T310, T315
358
255
 
256
+ <!--SUPPORTED_END-->
257
+ <sup>\*</sup>&nbsp;&nbsp; Model requires authentication<br>
258
+ <sup>\*\*</sup>&nbsp; Newer versions require authentication<br>
259
+ <sup>\*\*\*</sup> Devices may work across TAPO/KASA branded hubs
359
260
 
360
- **If your device is unlisted but working, please open a pull request to update the list and add a fixture file (use `python -m devtools.dump_devinfo` to generate one).**
261
+ See [supported devices in our documentation](SUPPORTED.md) for more detailed information about tested hardware and software versions.
361
262
 
362
263
  ## Resources
363
264
 
@@ -378,7 +279,7 @@ devices:
378
279
  ### TP-Link Tapo support
379
280
 
380
281
  This library has recently added a limited supported for devices that carry Tapo branding.
381
- That support is currently limited to the cli. The package `kasa.tapo` is in flux and if you
282
+ That support is currently limited to the cli. The package `kasa.smart` is in flux and if you
382
283
  use it directly you should expect it could break in future releases until this statement is removed.
383
284
 
384
285
  Other TAPO libraries are:
@@ -388,4 +289,5 @@ Other TAPO libraries are:
388
289
  * [Home Assistant integration](https://github.com/fishbigger/HomeAssistant-Tapo-P100-Control)
389
290
  * [plugp100, another tapo library](https://github.com/petretiandrea/plugp100)
390
291
  * [Home Assistant integration](https://github.com/petretiandrea/home-assistant-tapo-p100)
292
+ * [rust and python implementation](https://github.com/mihai-dinculescu/tapo/)
391
293