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
@@ -6,12 +6,9 @@ import pytest
6
6
 
7
7
  from kasa.credentials import Credentials
8
8
  from kasa.deviceconfig import (
9
- ConnectionType,
10
9
  DeviceConfig,
11
- DeviceFamilyType,
12
- EncryptType,
13
10
  )
14
- from kasa.exceptions import SmartDeviceException
11
+ from kasa.exceptions import KasaException
15
12
 
16
13
 
17
14
  async def test_serialization():
@@ -32,7 +29,7 @@ async def test_serialization():
32
29
  ids=["invalid-dict", "not-dict"],
33
30
  )
34
31
  def test_deserialization_errors(input_value, expected_msg):
35
- with pytest.raises(SmartDeviceException, match=expected_msg):
32
+ with pytest.raises(KasaException, match=expected_msg):
36
33
  DeviceConfig.from_dict(input_value)
37
34
 
38
35
 
kasa/tests/test_dimmer.py CHANGED
@@ -1,46 +1,50 @@
1
1
  import pytest
2
2
 
3
- from kasa import SmartDimmer
3
+ from kasa import DeviceType
4
+ from kasa.iot import IotDimmer
4
5
 
5
- from .conftest import dimmer, handle_turn_on, turn_on
6
+ from .conftest import dimmer_iot, handle_turn_on, turn_on
6
7
 
7
8
 
8
- @dimmer
9
+ @dimmer_iot
9
10
  @turn_on
10
11
  async def test_set_brightness(dev, turn_on):
11
12
  await handle_turn_on(dev, turn_on)
12
13
 
13
14
  await dev.set_brightness(99)
15
+ await dev.update()
14
16
  assert dev.brightness == 99
15
17
  assert dev.is_on == turn_on
16
18
 
17
19
  await dev.set_brightness(0)
20
+ await dev.update()
18
21
  assert dev.brightness == 1
19
22
  assert dev.is_on == turn_on
20
23
 
21
24
 
22
- @dimmer
25
+ @dimmer_iot
23
26
  @turn_on
24
27
  async def test_set_brightness_transition(dev, turn_on, mocker):
25
28
  await handle_turn_on(dev, turn_on)
26
- query_helper = mocker.spy(SmartDimmer, "_query_helper")
29
+ query_helper = mocker.spy(IotDimmer, "_query_helper")
27
30
 
28
31
  await dev.set_brightness(99, transition=1000)
29
-
30
- assert dev.brightness == 99
31
- assert dev.is_on
32
32
  query_helper.assert_called_with(
33
33
  mocker.ANY,
34
34
  "smartlife.iot.dimmer",
35
35
  "set_dimmer_transition",
36
36
  {"brightness": 99, "duration": 1000},
37
37
  )
38
+ await dev.update()
39
+ assert dev.brightness == 99
40
+ assert dev.is_on
38
41
 
39
42
  await dev.set_brightness(0, transition=1000)
43
+ await dev.update()
40
44
  assert dev.brightness == 1
41
45
 
42
46
 
43
- @dimmer
47
+ @dimmer_iot
44
48
  async def test_set_brightness_invalid(dev):
45
49
  for invalid_brightness in [-1, 101, 0.5]:
46
50
  with pytest.raises(ValueError):
@@ -51,27 +55,27 @@ async def test_set_brightness_invalid(dev):
51
55
  await dev.set_brightness(1, transition=invalid_transition)
52
56
 
53
57
 
54
- @dimmer
58
+ @dimmer_iot
55
59
  async def test_turn_on_transition(dev, mocker):
56
- query_helper = mocker.spy(SmartDimmer, "_query_helper")
60
+ query_helper = mocker.spy(IotDimmer, "_query_helper")
57
61
  original_brightness = dev.brightness
58
62
 
59
63
  await dev.turn_on(transition=1000)
60
-
61
- assert dev.is_on
62
- assert dev.brightness == original_brightness
63
64
  query_helper.assert_called_with(
64
65
  mocker.ANY,
65
66
  "smartlife.iot.dimmer",
66
67
  "set_dimmer_transition",
67
68
  {"brightness": original_brightness, "duration": 1000},
68
69
  )
70
+ await dev.update()
71
+ assert dev.is_on
72
+ assert dev.brightness == original_brightness
69
73
 
70
74
 
71
- @dimmer
75
+ @dimmer_iot
72
76
  async def test_turn_off_transition(dev, mocker):
73
77
  await handle_turn_on(dev, True)
74
- query_helper = mocker.spy(SmartDimmer, "_query_helper")
78
+ query_helper = mocker.spy(IotDimmer, "_query_helper")
75
79
  original_brightness = dev.brightness
76
80
 
77
81
  await dev.turn_off(transition=1000)
@@ -86,30 +90,30 @@ async def test_turn_off_transition(dev, mocker):
86
90
  )
87
91
 
88
92
 
89
- @dimmer
93
+ @dimmer_iot
90
94
  @turn_on
91
95
  async def test_set_dimmer_transition(dev, turn_on, mocker):
92
96
  await handle_turn_on(dev, turn_on)
93
- query_helper = mocker.spy(SmartDimmer, "_query_helper")
97
+ query_helper = mocker.spy(IotDimmer, "_query_helper")
94
98
 
95
99
  await dev.set_dimmer_transition(99, 1000)
96
-
97
- assert dev.is_on
98
- assert dev.brightness == 99
99
100
  query_helper.assert_called_with(
100
101
  mocker.ANY,
101
102
  "smartlife.iot.dimmer",
102
103
  "set_dimmer_transition",
103
104
  {"brightness": 99, "duration": 1000},
104
105
  )
106
+ await dev.update()
107
+ assert dev.is_on
108
+ assert dev.brightness == 99
105
109
 
106
110
 
107
- @dimmer
111
+ @dimmer_iot
108
112
  @turn_on
109
113
  async def test_set_dimmer_transition_to_off(dev, turn_on, mocker):
110
114
  await handle_turn_on(dev, turn_on)
111
115
  original_brightness = dev.brightness
112
- query_helper = mocker.spy(SmartDimmer, "_query_helper")
116
+ query_helper = mocker.spy(IotDimmer, "_query_helper")
113
117
 
114
118
  await dev.set_dimmer_transition(0, 1000)
115
119
 
@@ -123,7 +127,7 @@ async def test_set_dimmer_transition_to_off(dev, turn_on, mocker):
123
127
  )
124
128
 
125
129
 
126
- @dimmer
130
+ @dimmer_iot
127
131
  async def test_set_dimmer_transition_invalid(dev):
128
132
  for invalid_brightness in [-1, 101, 0.5]:
129
133
  with pytest.raises(ValueError):
@@ -132,3 +136,8 @@ async def test_set_dimmer_transition_invalid(dev):
132
136
  for invalid_transition in [-1, 0, 0.5]:
133
137
  with pytest.raises(ValueError):
134
138
  await dev.set_dimmer_transition(1, invalid_transition)
139
+
140
+
141
+ @dimmer_iot
142
+ def test_device_type_dimmer(dev):
143
+ assert dev.device_type == DeviceType.Dimmer
@@ -1,6 +1,5 @@
1
1
  # type: ignore
2
2
  import asyncio
3
- import logging
4
3
  import re
5
4
  import socket
6
5
  from unittest.mock import MagicMock
@@ -11,31 +10,28 @@ from async_timeout import timeout as asyncio_timeout
11
10
 
12
11
  from kasa import (
13
12
  Credentials,
13
+ Device,
14
14
  DeviceType,
15
15
  Discover,
16
- SmartDevice,
17
- SmartDeviceException,
18
- protocol,
16
+ KasaException,
19
17
  )
20
18
  from kasa.deviceconfig import (
21
19
  ConnectionType,
22
20
  DeviceConfig,
23
- DeviceFamilyType,
24
- EncryptType,
25
21
  )
26
22
  from kasa.discover import DiscoveryResult, _DiscoverProtocol, json_dumps
27
- from kasa.exceptions import AuthenticationException, UnsupportedDeviceException
23
+ from kasa.exceptions import AuthenticationError, UnsupportedDeviceError
24
+ from kasa.iot import IotDevice
28
25
  from kasa.xortransport import XorEncryption
29
26
 
30
27
  from .conftest import (
31
- bulb,
32
28
  bulb_iot,
33
- dimmer,
34
- lightstrip,
29
+ dimmer_iot,
30
+ lightstrip_iot,
35
31
  new_discovery,
36
- plug,
37
- strip,
32
+ plug_iot,
38
33
  strip_iot,
34
+ wallswitch_iot,
39
35
  )
40
36
 
41
37
  UNSUPPORTED = {
@@ -60,15 +56,22 @@ UNSUPPORTED = {
60
56
  }
61
57
 
62
58
 
63
- @plug
64
- async def test_type_detection_plug(dev: SmartDevice):
59
+ @wallswitch_iot
60
+ async def test_type_detection_switch(dev: Device):
61
+ d = Discover._get_device_class(dev._last_update)("localhost")
62
+ assert d.is_wallswitch
63
+ assert d.device_type == DeviceType.WallSwitch
64
+
65
+
66
+ @plug_iot
67
+ async def test_type_detection_plug(dev: Device):
65
68
  d = Discover._get_device_class(dev._last_update)("localhost")
66
69
  assert d.is_plug
67
70
  assert d.device_type == DeviceType.Plug
68
71
 
69
72
 
70
73
  @bulb_iot
71
- async def test_type_detection_bulb(dev: SmartDevice):
74
+ async def test_type_detection_bulb(dev: Device):
72
75
  d = Discover._get_device_class(dev._last_update)("localhost")
73
76
  # TODO: light_strip is a special case for now to force bulb tests on it
74
77
  if not d.is_light_strip:
@@ -77,21 +80,21 @@ async def test_type_detection_bulb(dev: SmartDevice):
77
80
 
78
81
 
79
82
  @strip_iot
80
- async def test_type_detection_strip(dev: SmartDevice):
83
+ async def test_type_detection_strip(dev: Device):
81
84
  d = Discover._get_device_class(dev._last_update)("localhost")
82
85
  assert d.is_strip
83
86
  assert d.device_type == DeviceType.Strip
84
87
 
85
88
 
86
- @dimmer
87
- async def test_type_detection_dimmer(dev: SmartDevice):
89
+ @dimmer_iot
90
+ async def test_type_detection_dimmer(dev: Device):
88
91
  d = Discover._get_device_class(dev._last_update)("localhost")
89
92
  assert d.is_dimmer
90
93
  assert d.device_type == DeviceType.Dimmer
91
94
 
92
95
 
93
- @lightstrip
94
- async def test_type_detection_lightstrip(dev: SmartDevice):
96
+ @lightstrip_iot
97
+ async def test_type_detection_lightstrip(dev: Device):
95
98
  d = Discover._get_device_class(dev._last_update)("localhost")
96
99
  assert d.is_light_strip
97
100
  assert d.device_type == DeviceType.LightStrip
@@ -99,7 +102,7 @@ async def test_type_detection_lightstrip(dev: SmartDevice):
99
102
 
100
103
  async def test_type_unknown():
101
104
  invalid_info = {"system": {"get_sysinfo": {"type": "nosuchtype"}}}
102
- with pytest.raises(UnsupportedDeviceException):
105
+ with pytest.raises(UnsupportedDeviceError):
103
106
  Discover._get_device_class(invalid_info)
104
107
 
105
108
 
@@ -117,7 +120,7 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
117
120
  x = await Discover.discover_single(
118
121
  host, port=custom_port, credentials=Credentials()
119
122
  )
120
- assert issubclass(x.__class__, SmartDevice)
123
+ assert issubclass(x.__class__, Device)
121
124
  assert x._discovery_info is not None
122
125
  assert x.port == custom_port or x.port == discovery_mock.default_port
123
126
  assert update_mock.call_count == 0
@@ -150,13 +153,13 @@ async def test_discover_single_hostname(discovery_mock, mocker):
150
153
  update_mock = mocker.patch.object(device_class, "update")
151
154
 
152
155
  x = await Discover.discover_single(host, credentials=Credentials())
153
- assert issubclass(x.__class__, SmartDevice)
156
+ assert issubclass(x.__class__, Device)
154
157
  assert x._discovery_info is not None
155
158
  assert x.host == host
156
159
  assert update_mock.call_count == 0
157
160
 
158
161
  mocker.patch("socket.getaddrinfo", side_effect=socket.gaierror())
159
- with pytest.raises(SmartDeviceException):
162
+ with pytest.raises(KasaException):
160
163
  x = await Discover.discover_single(host, credentials=Credentials())
161
164
 
162
165
 
@@ -166,7 +169,7 @@ async def test_discover_single_unsupported(unsupported_device_info, mocker):
166
169
 
167
170
  # Test with a valid unsupported response
168
171
  with pytest.raises(
169
- UnsupportedDeviceException,
172
+ UnsupportedDeviceError,
170
173
  ):
171
174
  await Discover.discover_single(host)
172
175
 
@@ -176,7 +179,7 @@ async def test_discover_single_no_response(mocker):
176
179
  host = "127.0.0.1"
177
180
  mocker.patch.object(_DiscoverProtocol, "do_discover")
178
181
  with pytest.raises(
179
- SmartDeviceException, match=f"Timed out getting discovery response for {host}"
182
+ KasaException, match=f"Timed out getting discovery response for {host}"
180
183
  ):
181
184
  await Discover.discover_single(host, discovery_timeout=0)
182
185
 
@@ -196,20 +199,21 @@ async def test_discover_invalid_info(msg, data, mocker):
196
199
  """Make sure that invalid discovery information raises an exception."""
197
200
  host = "127.0.0.1"
198
201
 
199
- def mock_discover(self):
202
+ async def mock_discover(self):
200
203
  self.datagram_received(
201
204
  XorEncryption.encrypt(json_dumps(data))[4:], (host, 9999)
202
205
  )
203
206
 
204
207
  mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
205
208
 
206
- with pytest.raises(SmartDeviceException, match=msg):
209
+ with pytest.raises(KasaException, match=msg):
207
210
  await Discover.discover_single(host)
208
211
 
209
212
 
210
213
  async def test_discover_send(mocker):
211
214
  """Test discovery parameters."""
212
- proto = _DiscoverProtocol()
215
+ discovery_timeout = 0
216
+ proto = _DiscoverProtocol(discovery_timeout=discovery_timeout)
213
217
  assert proto.discovery_packets == 3
214
218
  assert proto.target_1 == ("255.255.255.255", 9999)
215
219
  transport = mocker.patch.object(proto, "transport")
@@ -238,7 +242,7 @@ async def test_discover_datagram_received(mocker, discovery_data):
238
242
  # Check that unsupported device is 1
239
243
  assert len(proto.unsupported_device_exceptions) == 1
240
244
  dev = proto.discovered_devices[addr]
241
- assert issubclass(dev.__class__, SmartDevice)
245
+ assert issubclass(dev.__class__, Device)
242
246
  assert dev.host == addr
243
247
 
244
248
 
@@ -284,11 +288,11 @@ async def test_discover_single_authentication(discovery_mock, mocker):
284
288
  mocker.patch.object(
285
289
  device_class,
286
290
  "update",
287
- side_effect=AuthenticationException("Failed to authenticate"),
291
+ side_effect=AuthenticationError("Failed to authenticate"),
288
292
  )
289
293
 
290
294
  with pytest.raises(
291
- AuthenticationException,
295
+ AuthenticationError,
292
296
  match="Failed to authenticate",
293
297
  ):
294
298
  device = await Discover.discover_single(
@@ -303,23 +307,27 @@ async def test_discover_single_authentication(discovery_mock, mocker):
303
307
 
304
308
 
305
309
  @new_discovery
306
- async def test_device_update_from_new_discovery_info(discovery_data):
307
- device = SmartDevice("127.0.0.7")
310
+ async def test_device_update_from_new_discovery_info(discovery_mock):
311
+ """Make sure that new discovery devices update from discovery info correctly."""
312
+ discovery_data = discovery_mock.discovery_data
313
+ device_class = Discover._get_device_class(discovery_data)
314
+ device = device_class("127.0.0.1")
308
315
  discover_info = DiscoveryResult(**discovery_data["result"])
309
316
  discover_dump = discover_info.get_dict()
310
- discover_dump["alias"] = "foobar"
311
- discover_dump["model"] = discover_dump["device_model"]
317
+ model, _, _ = discover_dump["device_model"].partition("(")
318
+ discover_dump["model"] = model
312
319
  device.update_from_discover_info(discover_dump)
313
320
 
314
- assert device.alias == "foobar"
315
321
  assert device.mac == discover_dump["mac"].replace("-", ":")
316
- assert device.model == discover_dump["device_model"]
322
+ assert device.model == model
317
323
 
318
- with pytest.raises(
319
- SmartDeviceException,
320
- match=re.escape("You need to await update() to access the data"),
321
- ):
322
- assert device.supported_modules
324
+ # TODO implement requires_update for SmartDevice
325
+ if isinstance(device, IotDevice):
326
+ with pytest.raises(
327
+ KasaException,
328
+ match=re.escape("You need to await update() to access the data"),
329
+ ):
330
+ assert device.supported_modules
323
331
 
324
332
 
325
333
  async def test_discover_single_http_client(discovery_mock, mocker):
@@ -329,7 +337,7 @@ async def test_discover_single_http_client(discovery_mock, mocker):
329
337
 
330
338
  http_client = aiohttp.ClientSession()
331
339
 
332
- x: SmartDevice = await Discover.discover_single(host)
340
+ x: Device = await Discover.discover_single(host)
333
341
 
334
342
  assert x.config.uses_http == (discovery_mock.default_port == 80)
335
343
 
@@ -340,14 +348,14 @@ async def test_discover_single_http_client(discovery_mock, mocker):
340
348
 
341
349
 
342
350
  async def test_discover_http_client(discovery_mock, mocker):
343
- """Make sure that discover_single returns an initialized SmartDevice instance."""
351
+ """Make sure that discover returns an initialized SmartDevice instance."""
344
352
  host = "127.0.0.1"
345
353
  discovery_mock.ip = host
346
354
 
347
355
  http_client = aiohttp.ClientSession()
348
356
 
349
357
  devices = await Discover.discover(discovery_timeout=0)
350
- x: SmartDevice = devices[host]
358
+ x: Device = devices[host]
351
359
  assert x.config.uses_http == (discovery_mock.default_port == 80)
352
360
 
353
361
  if discovery_mock.default_port == 80:
@@ -408,31 +416,24 @@ class FakeDatagramTransport(asyncio.DatagramTransport):
408
416
  @pytest.mark.parametrize("port", [9999, 20002])
409
417
  @pytest.mark.parametrize("do_not_reply_count", [0, 1, 2, 3, 4])
410
418
  async def test_do_discover_drop_packets(mocker, port, do_not_reply_count):
411
- """Make sure that discover_single handles authenticating devices correctly."""
419
+ """Make sure that _DiscoverProtocol handles authenticating devices correctly."""
412
420
  host = "127.0.0.1"
413
- discovery_timeout = 1
421
+ discovery_timeout = 0
414
422
 
415
- event = asyncio.Event()
416
423
  dp = _DiscoverProtocol(
417
424
  target=host,
418
425
  discovery_timeout=discovery_timeout,
419
426
  discovery_packets=5,
420
- discovered_event=event,
421
427
  )
422
428
  ft = FakeDatagramTransport(dp, port, do_not_reply_count)
423
429
  dp.connection_made(ft)
424
430
 
425
- timed_out = False
426
- try:
427
- async with asyncio_timeout(discovery_timeout):
428
- await event.wait()
429
- except asyncio.TimeoutError:
430
- timed_out = True
431
+ await dp.wait_for_discovery_to_complete()
431
432
 
432
433
  await asyncio.sleep(0)
433
434
  assert ft.send_count == do_not_reply_count + 1
434
435
  assert dp.discover_task.done()
435
- assert timed_out is False
436
+ assert dp.discover_task.cancelled()
436
437
 
437
438
 
438
439
  @pytest.mark.parametrize(
@@ -441,27 +442,69 @@ async def test_do_discover_drop_packets(mocker, port, do_not_reply_count):
441
442
  ids=["unknownport", "unsupporteddevice"],
442
443
  )
443
444
  async def test_do_discover_invalid(mocker, port, will_timeout):
444
- """Make sure that discover_single handles authenticating devices correctly."""
445
+ """Make sure that _DiscoverProtocol handles invalid devices correctly."""
445
446
  host = "127.0.0.1"
446
- discovery_timeout = 1
447
+ discovery_timeout = 0
447
448
 
448
- event = asyncio.Event()
449
449
  dp = _DiscoverProtocol(
450
450
  target=host,
451
451
  discovery_timeout=discovery_timeout,
452
452
  discovery_packets=5,
453
- discovered_event=event,
454
453
  )
455
454
  ft = FakeDatagramTransport(dp, port, 0, unsupported=True)
456
455
  dp.connection_made(ft)
457
456
 
458
- timed_out = False
459
- try:
460
- async with asyncio_timeout(15):
461
- await event.wait()
462
- except asyncio.TimeoutError:
463
- timed_out = True
464
-
457
+ await dp.wait_for_discovery_to_complete()
465
458
  await asyncio.sleep(0)
466
459
  assert dp.discover_task.done()
467
- assert timed_out is will_timeout
460
+ assert dp.discover_task.cancelled() != will_timeout
461
+
462
+
463
+ async def test_discover_propogates_task_exceptions(discovery_mock):
464
+ """Make sure that discover propogates callback exceptions."""
465
+ discovery_timeout = 0
466
+
467
+ async def on_discovered(dev):
468
+ raise KasaException("Dummy exception")
469
+
470
+ with pytest.raises(KasaException):
471
+ await Discover.discover(
472
+ discovery_timeout=discovery_timeout, on_discovered=on_discovered
473
+ )
474
+
475
+
476
+ async def test_do_discover_no_connection(mocker):
477
+ """Make sure that if the datagram connection doesnt start a TimeoutError is raised."""
478
+ host = "127.0.0.1"
479
+ discovery_timeout = 0
480
+ mocker.patch.object(_DiscoverProtocol, "DISCOVERY_START_TIMEOUT", 0)
481
+ dp = _DiscoverProtocol(
482
+ target=host,
483
+ discovery_timeout=discovery_timeout,
484
+ discovery_packets=5,
485
+ )
486
+ # Normally tests would simulate connection as per below
487
+ # ft = FakeDatagramTransport(dp, port, 0, unsupported=True)
488
+ # dp.connection_made(ft)
489
+
490
+ with pytest.raises(asyncio.TimeoutError):
491
+ await dp.wait_for_discovery_to_complete()
492
+
493
+
494
+ async def test_do_discover_external_cancel(mocker):
495
+ """Make sure that a cancel other than when target is discovered propogates."""
496
+ host = "127.0.0.1"
497
+ discovery_timeout = 1
498
+
499
+ dp = _DiscoverProtocol(
500
+ target=host,
501
+ discovery_timeout=discovery_timeout,
502
+ discovery_packets=1,
503
+ )
504
+ # Normally tests would simulate connection as per below
505
+ ft = FakeDatagramTransport(dp, 9999, 1, unsupported=True)
506
+ dp.connection_made(ft)
507
+
508
+ with pytest.raises(asyncio.TimeoutError):
509
+ async with asyncio_timeout(0):
510
+ await dp.wait_for_discovery_to_complete()
kasa/tests/test_emeter.py CHANGED
@@ -2,26 +2,53 @@ import datetime
2
2
  from unittest.mock import Mock
3
3
 
4
4
  import pytest
5
-
6
- from kasa import EmeterStatus, SmartDeviceException
7
- from kasa.modules.emeter import Emeter
5
+ from voluptuous import (
6
+ All,
7
+ Any,
8
+ Coerce, # type: ignore
9
+ Range,
10
+ Schema,
11
+ )
12
+
13
+ from kasa import EmeterStatus, KasaException
14
+ from kasa.iot import IotDevice
15
+ from kasa.iot.modules.emeter import Emeter
8
16
 
9
17
  from .conftest import has_emeter, has_emeter_iot, no_emeter
10
- from .newfakes import CURRENT_CONSUMPTION_SCHEMA
18
+
19
+ CURRENT_CONSUMPTION_SCHEMA = Schema(
20
+ Any(
21
+ {
22
+ "voltage": Any(All(float, Range(min=0, max=300)), None),
23
+ "power": Any(Coerce(float, Range(min=0)), None),
24
+ "total": Any(Coerce(float, Range(min=0)), None),
25
+ "current": Any(All(float, Range(min=0)), None),
26
+ "voltage_mv": Any(All(float, Range(min=0, max=300000)), int, None),
27
+ "power_mw": Any(Coerce(float, Range(min=0)), None),
28
+ "total_wh": Any(Coerce(float, Range(min=0)), None),
29
+ "current_ma": Any(All(float, Range(min=0)), int, None),
30
+ "slot_id": Any(Coerce(int, Range(min=0)), None),
31
+ },
32
+ None,
33
+ )
34
+ )
11
35
 
12
36
 
13
37
  @no_emeter
14
38
  async def test_no_emeter(dev):
15
39
  assert not dev.has_emeter
16
40
 
17
- with pytest.raises(SmartDeviceException):
41
+ with pytest.raises(KasaException):
18
42
  await dev.get_emeter_realtime()
19
- with pytest.raises(SmartDeviceException):
20
- await dev.get_emeter_daily()
21
- with pytest.raises(SmartDeviceException):
22
- await dev.get_emeter_monthly()
23
- with pytest.raises(SmartDeviceException):
24
- await dev.erase_emeter_stats()
43
+ # Only iot devices support the historical stats so other
44
+ # devices will not implement the methods below
45
+ if isinstance(dev, IotDevice):
46
+ with pytest.raises(KasaException):
47
+ await dev.get_emeter_daily()
48
+ with pytest.raises(KasaException):
49
+ await dev.get_emeter_monthly()
50
+ with pytest.raises(KasaException):
51
+ await dev.erase_emeter_stats()
25
52
 
26
53
 
27
54
  @has_emeter
@@ -98,7 +125,7 @@ async def test_erase_emeter_stats(dev):
98
125
  await dev.erase_emeter()
99
126
 
100
127
 
101
- @has_emeter
128
+ @has_emeter_iot
102
129
  async def test_current_consumption(dev):
103
130
  if dev.has_emeter:
104
131
  x = await dev.current_consumption()