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 kasa.smartdevice import DeviceType
1
+ from kasa.device_type import DeviceType
2
2
 
3
3
 
4
4
  async def test_device_type_from_value():
@@ -8,7 +8,7 @@ from kasa.credentials import Credentials
8
8
  from kasa.deviceconfig import (
9
9
  DeviceConfig,
10
10
  )
11
- from kasa.exceptions import SmartDeviceException
11
+ from kasa.exceptions import KasaException
12
12
 
13
13
 
14
14
  async def test_serialization():
@@ -29,7 +29,7 @@ async def test_serialization():
29
29
  ids=["invalid-dict", "not-dict"],
30
30
  )
31
31
  def test_deserialization_errors(input_value, expected_msg):
32
- with pytest.raises(SmartDeviceException, match=expected_msg):
32
+ with pytest.raises(KasaException, match=expected_msg):
33
33
  DeviceConfig.from_dict(input_value)
34
34
 
35
35
 
kasa/tests/test_dimmer.py CHANGED
@@ -1,77 +1,81 @@
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
- @turn_on
10
- async def test_set_brightness(dev, turn_on):
11
- await handle_turn_on(dev, turn_on)
9
+ @dimmer_iot
10
+ async def test_set_brightness(dev):
11
+ await handle_turn_on(dev, False)
12
+ assert dev.is_on is False
12
13
 
13
14
  await dev.set_brightness(99)
15
+ await dev.update()
14
16
  assert dev.brightness == 99
15
- assert dev.is_on == turn_on
17
+ assert dev.is_on is True
16
18
 
17
19
  await dev.set_brightness(0)
18
- assert dev.brightness == 1
19
- assert dev.is_on == turn_on
20
+ await dev.update()
21
+ assert dev.brightness == 99
22
+ assert dev.is_on is False
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)
40
- assert dev.brightness == 1
43
+ await dev.update()
44
+ assert dev.is_on is False
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):
47
51
  await dev.set_brightness(invalid_brightness)
48
52
 
49
- for invalid_transition in [-1, 0, 0.5]:
53
+ for invalid_transition in [-1, 0.5]:
50
54
  with pytest.raises(ValueError):
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,12 +127,17 @@ 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):
130
134
  await dev.set_dimmer_transition(invalid_brightness, 1000)
131
135
 
132
- for invalid_transition in [-1, 0, 0.5]:
136
+ for invalid_transition in [-1, 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
@@ -10,26 +10,28 @@ from async_timeout import timeout as asyncio_timeout
10
10
 
11
11
  from kasa import (
12
12
  Credentials,
13
+ Device,
13
14
  DeviceType,
14
15
  Discover,
15
- SmartDevice,
16
- SmartDeviceException,
16
+ KasaException,
17
17
  )
18
18
  from kasa.deviceconfig import (
19
19
  ConnectionType,
20
20
  DeviceConfig,
21
21
  )
22
22
  from kasa.discover import DiscoveryResult, _DiscoverProtocol, json_dumps
23
- from kasa.exceptions import AuthenticationException, UnsupportedDeviceException
23
+ from kasa.exceptions import AuthenticationError, UnsupportedDeviceError
24
+ from kasa.iot import IotDevice
24
25
  from kasa.xortransport import XorEncryption
25
26
 
26
27
  from .conftest import (
27
28
  bulb_iot,
28
- dimmer,
29
- lightstrip,
29
+ dimmer_iot,
30
+ lightstrip_iot,
30
31
  new_discovery,
31
- plug,
32
+ plug_iot,
32
33
  strip_iot,
34
+ wallswitch_iot,
33
35
  )
34
36
 
35
37
  UNSUPPORTED = {
@@ -54,15 +56,22 @@ UNSUPPORTED = {
54
56
  }
55
57
 
56
58
 
57
- @plug
58
- 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):
59
68
  d = Discover._get_device_class(dev._last_update)("localhost")
60
69
  assert d.is_plug
61
70
  assert d.device_type == DeviceType.Plug
62
71
 
63
72
 
64
73
  @bulb_iot
65
- async def test_type_detection_bulb(dev: SmartDevice):
74
+ async def test_type_detection_bulb(dev: Device):
66
75
  d = Discover._get_device_class(dev._last_update)("localhost")
67
76
  # TODO: light_strip is a special case for now to force bulb tests on it
68
77
  if not d.is_light_strip:
@@ -71,21 +80,21 @@ async def test_type_detection_bulb(dev: SmartDevice):
71
80
 
72
81
 
73
82
  @strip_iot
74
- async def test_type_detection_strip(dev: SmartDevice):
83
+ async def test_type_detection_strip(dev: Device):
75
84
  d = Discover._get_device_class(dev._last_update)("localhost")
76
85
  assert d.is_strip
77
86
  assert d.device_type == DeviceType.Strip
78
87
 
79
88
 
80
- @dimmer
81
- async def test_type_detection_dimmer(dev: SmartDevice):
89
+ @dimmer_iot
90
+ async def test_type_detection_dimmer(dev: Device):
82
91
  d = Discover._get_device_class(dev._last_update)("localhost")
83
92
  assert d.is_dimmer
84
93
  assert d.device_type == DeviceType.Dimmer
85
94
 
86
95
 
87
- @lightstrip
88
- async def test_type_detection_lightstrip(dev: SmartDevice):
96
+ @lightstrip_iot
97
+ async def test_type_detection_lightstrip(dev: Device):
89
98
  d = Discover._get_device_class(dev._last_update)("localhost")
90
99
  assert d.is_light_strip
91
100
  assert d.device_type == DeviceType.LightStrip
@@ -93,7 +102,7 @@ async def test_type_detection_lightstrip(dev: SmartDevice):
93
102
 
94
103
  async def test_type_unknown():
95
104
  invalid_info = {"system": {"get_sysinfo": {"type": "nosuchtype"}}}
96
- with pytest.raises(UnsupportedDeviceException):
105
+ with pytest.raises(UnsupportedDeviceError):
97
106
  Discover._get_device_class(invalid_info)
98
107
 
99
108
 
@@ -111,7 +120,7 @@ async def test_discover_single(discovery_mock, custom_port, mocker):
111
120
  x = await Discover.discover_single(
112
121
  host, port=custom_port, credentials=Credentials()
113
122
  )
114
- assert issubclass(x.__class__, SmartDevice)
123
+ assert issubclass(x.__class__, Device)
115
124
  assert x._discovery_info is not None
116
125
  assert x.port == custom_port or x.port == discovery_mock.default_port
117
126
  assert update_mock.call_count == 0
@@ -144,13 +153,13 @@ async def test_discover_single_hostname(discovery_mock, mocker):
144
153
  update_mock = mocker.patch.object(device_class, "update")
145
154
 
146
155
  x = await Discover.discover_single(host, credentials=Credentials())
147
- assert issubclass(x.__class__, SmartDevice)
156
+ assert issubclass(x.__class__, Device)
148
157
  assert x._discovery_info is not None
149
158
  assert x.host == host
150
159
  assert update_mock.call_count == 0
151
160
 
152
161
  mocker.patch("socket.getaddrinfo", side_effect=socket.gaierror())
153
- with pytest.raises(SmartDeviceException):
162
+ with pytest.raises(KasaException):
154
163
  x = await Discover.discover_single(host, credentials=Credentials())
155
164
 
156
165
 
@@ -160,7 +169,7 @@ async def test_discover_single_unsupported(unsupported_device_info, mocker):
160
169
 
161
170
  # Test with a valid unsupported response
162
171
  with pytest.raises(
163
- UnsupportedDeviceException,
172
+ UnsupportedDeviceError,
164
173
  ):
165
174
  await Discover.discover_single(host)
166
175
 
@@ -170,7 +179,7 @@ async def test_discover_single_no_response(mocker):
170
179
  host = "127.0.0.1"
171
180
  mocker.patch.object(_DiscoverProtocol, "do_discover")
172
181
  with pytest.raises(
173
- SmartDeviceException, match=f"Timed out getting discovery response for {host}"
182
+ KasaException, match=f"Timed out getting discovery response for {host}"
174
183
  ):
175
184
  await Discover.discover_single(host, discovery_timeout=0)
176
185
 
@@ -190,20 +199,21 @@ async def test_discover_invalid_info(msg, data, mocker):
190
199
  """Make sure that invalid discovery information raises an exception."""
191
200
  host = "127.0.0.1"
192
201
 
193
- def mock_discover(self):
202
+ async def mock_discover(self):
194
203
  self.datagram_received(
195
204
  XorEncryption.encrypt(json_dumps(data))[4:], (host, 9999)
196
205
  )
197
206
 
198
207
  mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
199
208
 
200
- with pytest.raises(SmartDeviceException, match=msg):
209
+ with pytest.raises(KasaException, match=msg):
201
210
  await Discover.discover_single(host)
202
211
 
203
212
 
204
213
  async def test_discover_send(mocker):
205
214
  """Test discovery parameters."""
206
- proto = _DiscoverProtocol()
215
+ discovery_timeout = 0
216
+ proto = _DiscoverProtocol(discovery_timeout=discovery_timeout)
207
217
  assert proto.discovery_packets == 3
208
218
  assert proto.target_1 == ("255.255.255.255", 9999)
209
219
  transport = mocker.patch.object(proto, "transport")
@@ -232,7 +242,7 @@ async def test_discover_datagram_received(mocker, discovery_data):
232
242
  # Check that unsupported device is 1
233
243
  assert len(proto.unsupported_device_exceptions) == 1
234
244
  dev = proto.discovered_devices[addr]
235
- assert issubclass(dev.__class__, SmartDevice)
245
+ assert issubclass(dev.__class__, Device)
236
246
  assert dev.host == addr
237
247
 
238
248
 
@@ -278,11 +288,11 @@ async def test_discover_single_authentication(discovery_mock, mocker):
278
288
  mocker.patch.object(
279
289
  device_class,
280
290
  "update",
281
- side_effect=AuthenticationException("Failed to authenticate"),
291
+ side_effect=AuthenticationError("Failed to authenticate"),
282
292
  )
283
293
 
284
294
  with pytest.raises(
285
- AuthenticationException,
295
+ AuthenticationError,
286
296
  match="Failed to authenticate",
287
297
  ):
288
298
  device = await Discover.discover_single(
@@ -297,23 +307,27 @@ async def test_discover_single_authentication(discovery_mock, mocker):
297
307
 
298
308
 
299
309
  @new_discovery
300
- async def test_device_update_from_new_discovery_info(discovery_data):
301
- 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")
302
315
  discover_info = DiscoveryResult(**discovery_data["result"])
303
316
  discover_dump = discover_info.get_dict()
304
- discover_dump["alias"] = "foobar"
305
- discover_dump["model"] = discover_dump["device_model"]
317
+ model, _, _ = discover_dump["device_model"].partition("(")
318
+ discover_dump["model"] = model
306
319
  device.update_from_discover_info(discover_dump)
307
320
 
308
- assert device.alias == "foobar"
309
321
  assert device.mac == discover_dump["mac"].replace("-", ":")
310
- assert device.model == discover_dump["device_model"]
322
+ assert device.model == model
311
323
 
312
- with pytest.raises(
313
- SmartDeviceException,
314
- match=re.escape("You need to await update() to access the data"),
315
- ):
316
- 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
317
331
 
318
332
 
319
333
  async def test_discover_single_http_client(discovery_mock, mocker):
@@ -323,7 +337,7 @@ async def test_discover_single_http_client(discovery_mock, mocker):
323
337
 
324
338
  http_client = aiohttp.ClientSession()
325
339
 
326
- x: SmartDevice = await Discover.discover_single(host)
340
+ x: Device = await Discover.discover_single(host)
327
341
 
328
342
  assert x.config.uses_http == (discovery_mock.default_port == 80)
329
343
 
@@ -334,14 +348,14 @@ async def test_discover_single_http_client(discovery_mock, mocker):
334
348
 
335
349
 
336
350
  async def test_discover_http_client(discovery_mock, mocker):
337
- """Make sure that discover_single returns an initialized SmartDevice instance."""
351
+ """Make sure that discover returns an initialized SmartDevice instance."""
338
352
  host = "127.0.0.1"
339
353
  discovery_mock.ip = host
340
354
 
341
355
  http_client = aiohttp.ClientSession()
342
356
 
343
357
  devices = await Discover.discover(discovery_timeout=0)
344
- x: SmartDevice = devices[host]
358
+ x: Device = devices[host]
345
359
  assert x.config.uses_http == (discovery_mock.default_port == 80)
346
360
 
347
361
  if discovery_mock.default_port == 80:
@@ -402,31 +416,24 @@ class FakeDatagramTransport(asyncio.DatagramTransport):
402
416
  @pytest.mark.parametrize("port", [9999, 20002])
403
417
  @pytest.mark.parametrize("do_not_reply_count", [0, 1, 2, 3, 4])
404
418
  async def test_do_discover_drop_packets(mocker, port, do_not_reply_count):
405
- """Make sure that discover_single handles authenticating devices correctly."""
419
+ """Make sure that _DiscoverProtocol handles authenticating devices correctly."""
406
420
  host = "127.0.0.1"
407
- discovery_timeout = 1
421
+ discovery_timeout = 0
408
422
 
409
- event = asyncio.Event()
410
423
  dp = _DiscoverProtocol(
411
424
  target=host,
412
425
  discovery_timeout=discovery_timeout,
413
426
  discovery_packets=5,
414
- discovered_event=event,
415
427
  )
416
428
  ft = FakeDatagramTransport(dp, port, do_not_reply_count)
417
429
  dp.connection_made(ft)
418
430
 
419
- timed_out = False
420
- try:
421
- async with asyncio_timeout(discovery_timeout):
422
- await event.wait()
423
- except asyncio.TimeoutError:
424
- timed_out = True
431
+ await dp.wait_for_discovery_to_complete()
425
432
 
426
433
  await asyncio.sleep(0)
427
434
  assert ft.send_count == do_not_reply_count + 1
428
435
  assert dp.discover_task.done()
429
- assert timed_out is False
436
+ assert dp.discover_task.cancelled()
430
437
 
431
438
 
432
439
  @pytest.mark.parametrize(
@@ -435,27 +442,69 @@ async def test_do_discover_drop_packets(mocker, port, do_not_reply_count):
435
442
  ids=["unknownport", "unsupporteddevice"],
436
443
  )
437
444
  async def test_do_discover_invalid(mocker, port, will_timeout):
438
- """Make sure that discover_single handles authenticating devices correctly."""
445
+ """Make sure that _DiscoverProtocol handles invalid devices correctly."""
439
446
  host = "127.0.0.1"
440
- discovery_timeout = 1
447
+ discovery_timeout = 0
441
448
 
442
- event = asyncio.Event()
443
449
  dp = _DiscoverProtocol(
444
450
  target=host,
445
451
  discovery_timeout=discovery_timeout,
446
452
  discovery_packets=5,
447
- discovered_event=event,
448
453
  )
449
454
  ft = FakeDatagramTransport(dp, port, 0, unsupported=True)
450
455
  dp.connection_made(ft)
451
456
 
452
- timed_out = False
453
- try:
454
- async with asyncio_timeout(15):
455
- await event.wait()
456
- except asyncio.TimeoutError:
457
- timed_out = True
458
-
457
+ await dp.wait_for_discovery_to_complete()
459
458
  await asyncio.sleep(0)
460
459
  assert dp.discover_task.done()
461
- 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
@@ -10,8 +10,9 @@ from voluptuous import (
10
10
  Schema,
11
11
  )
12
12
 
13
- from kasa import EmeterStatus, SmartDeviceException
14
- from kasa.modules.emeter import Emeter
13
+ from kasa import EmeterStatus, KasaException
14
+ from kasa.iot import IotDevice
15
+ from kasa.iot.modules.emeter import Emeter
15
16
 
16
17
  from .conftest import has_emeter, has_emeter_iot, no_emeter
17
18
 
@@ -37,14 +38,17 @@ CURRENT_CONSUMPTION_SCHEMA = Schema(
37
38
  async def test_no_emeter(dev):
38
39
  assert not dev.has_emeter
39
40
 
40
- with pytest.raises(SmartDeviceException):
41
+ with pytest.raises(KasaException):
41
42
  await dev.get_emeter_realtime()
42
- with pytest.raises(SmartDeviceException):
43
- await dev.get_emeter_daily()
44
- with pytest.raises(SmartDeviceException):
45
- await dev.get_emeter_monthly()
46
- with pytest.raises(SmartDeviceException):
47
- 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()
48
52
 
49
53
 
50
54
  @has_emeter
@@ -121,7 +125,7 @@ async def test_erase_emeter_stats(dev):
121
125
  await dev.erase_emeter()
122
126
 
123
127
 
124
- @has_emeter
128
+ @has_emeter_iot
125
129
  async def test_current_consumption(dev):
126
130
  if dev.has_emeter:
127
131
  x = await dev.current_consumption()