python-kasa 0.6.2.1__py3-none-any.whl → 0.7.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) hide show
  1. kasa/__init__.py +88 -27
  2. kasa/aestransport.py +51 -34
  3. kasa/cli.py +326 -130
  4. kasa/device.py +406 -0
  5. kasa/device_factory.py +83 -44
  6. kasa/device_type.py +7 -3
  7. kasa/deviceconfig.py +16 -9
  8. kasa/discover.py +100 -81
  9. kasa/emeterstatus.py +7 -5
  10. kasa/exceptions.py +41 -26
  11. kasa/feature.py +201 -0
  12. kasa/httpclient.py +21 -14
  13. kasa/interfaces/__init__.py +16 -0
  14. kasa/interfaces/fan.py +20 -0
  15. kasa/interfaces/led.py +38 -0
  16. kasa/interfaces/light.py +133 -0
  17. kasa/interfaces/lighteffect.py +80 -0
  18. kasa/interfaces/lightpreset.py +76 -0
  19. kasa/iot/__init__.py +18 -0
  20. kasa/{effects.py → iot/effects.py} +4 -2
  21. kasa/{smartbulb.py → iot/iotbulb.py} +100 -147
  22. kasa/{smartdevice.py → iot/iotdevice.py} +157 -286
  23. kasa/{smartdimmer.py → iot/iotdimmer.py} +42 -33
  24. kasa/iot/iotlightstrip.py +71 -0
  25. kasa/{modules/module.py → iot/iotmodule.py} +22 -42
  26. kasa/iot/iotplug.py +94 -0
  27. kasa/{smartstrip.py → iot/iotstrip.py} +57 -75
  28. kasa/{modules → iot/modules}/__init__.py +10 -2
  29. kasa/{modules → iot/modules}/ambientlight.py +31 -3
  30. kasa/{modules → iot/modules}/antitheft.py +1 -0
  31. kasa/{modules → iot/modules}/cloud.py +25 -6
  32. kasa/{modules → iot/modules}/countdown.py +1 -0
  33. kasa/iot/modules/emeter.py +210 -0
  34. kasa/iot/modules/led.py +32 -0
  35. kasa/iot/modules/light.py +221 -0
  36. kasa/iot/modules/lighteffect.py +122 -0
  37. kasa/iot/modules/lightpreset.py +151 -0
  38. kasa/{modules → iot/modules}/motion.py +8 -8
  39. kasa/{modules → iot/modules}/rulemodule.py +11 -12
  40. kasa/{modules → iot/modules}/schedule.py +1 -0
  41. kasa/{modules → iot/modules}/time.py +5 -4
  42. kasa/{modules → iot/modules}/usage.py +10 -8
  43. kasa/iotprotocol.py +22 -20
  44. kasa/klaptransport.py +25 -15
  45. kasa/module.py +136 -0
  46. kasa/modulemapping.py +25 -0
  47. kasa/modulemapping.pyi +96 -0
  48. kasa/protocol.py +6 -4
  49. kasa/smart/__init__.py +6 -0
  50. kasa/smart/effects.py +429 -0
  51. kasa/smart/modules/__init__.py +57 -0
  52. kasa/smart/modules/alarm.py +142 -0
  53. kasa/smart/modules/autooff.py +96 -0
  54. kasa/smart/modules/batterysensor.py +54 -0
  55. kasa/smart/modules/brightness.py +66 -0
  56. kasa/smart/modules/childdevice.py +10 -0
  57. kasa/smart/modules/cloud.py +42 -0
  58. kasa/smart/modules/color.py +96 -0
  59. kasa/smart/modules/colortemperature.py +78 -0
  60. kasa/smart/modules/contactsensor.py +42 -0
  61. kasa/smart/modules/devicemodule.py +22 -0
  62. kasa/smart/modules/energy.py +101 -0
  63. kasa/smart/modules/fan.py +80 -0
  64. kasa/smart/modules/firmware.py +216 -0
  65. kasa/smart/modules/frostprotection.py +59 -0
  66. kasa/smart/modules/humiditysensor.py +55 -0
  67. kasa/smart/modules/led.py +49 -0
  68. kasa/smart/modules/light.py +139 -0
  69. kasa/smart/modules/lighteffect.py +115 -0
  70. kasa/smart/modules/lightpreset.py +142 -0
  71. kasa/smart/modules/lightstripeffect.py +135 -0
  72. kasa/smart/modules/lighttransition.py +183 -0
  73. kasa/smart/modules/reportmode.py +36 -0
  74. kasa/smart/modules/temperaturecontrol.py +177 -0
  75. kasa/smart/modules/temperaturesensor.py +77 -0
  76. kasa/smart/modules/time.py +57 -0
  77. kasa/smart/modules/waterleaksensor.py +66 -0
  78. kasa/smart/smartchilddevice.py +65 -0
  79. kasa/smart/smartdevice.py +653 -0
  80. kasa/smart/smartmodule.py +112 -0
  81. kasa/smartprotocol.py +107 -41
  82. kasa/tests/conftest.py +27 -562
  83. kasa/tests/device_fixtures.py +427 -0
  84. kasa/tests/discovery_fixtures.py +197 -0
  85. kasa/tests/fakeprotocol_iot.py +64 -6
  86. kasa/tests/fakeprotocol_smart.py +215 -22
  87. kasa/tests/fixtureinfo.py +154 -0
  88. kasa/tests/fixtures/smart/H100(EU)_1.0_1.2.3.json +221 -0
  89. kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.10.json +547 -0
  90. kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.5.json +391 -0
  91. kasa/tests/fixtures/smart/KH100(UK)_1.0_1.5.6.json +1557 -0
  92. kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.4.json +899 -0
  93. kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.5.json +479 -0
  94. kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +449 -0
  95. kasa/tests/fixtures/smart/P100_1.0.0_1.1.3.json +173 -0
  96. kasa/tests/fixtures/smart/P100_1.0.0_1.4.0.json +204 -0
  97. kasa/tests/fixtures/smart/P110(EU)_1.0_1.0.7.json +566 -0
  98. kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.4.0.json +170 -0
  99. kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.8.0.json +171 -0
  100. kasa/tests/fixtures/smart/child/KE100(UK)_1.0_2.8.0.json +171 -0
  101. kasa/tests/fixtures/smart/child/T110(EU)_1.0_1.8.0.json +526 -0
  102. kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json +533 -0
  103. kasa/tests/fixtures/smart/child/T310(EU)_1.0_1.5.0.json +530 -0
  104. kasa/tests/fixtures/smart/child/T315(EU)_1.0_1.7.0.json +537 -0
  105. kasa/tests/smart/__init__.py +0 -0
  106. kasa/tests/smart/features/__init__.py +0 -0
  107. kasa/tests/smart/features/test_brightness.py +55 -0
  108. kasa/tests/smart/features/test_colortemp.py +30 -0
  109. kasa/tests/smart/modules/__init__.py +0 -0
  110. kasa/tests/smart/modules/test_contact.py +28 -0
  111. kasa/tests/smart/modules/test_fan.py +79 -0
  112. kasa/tests/smart/modules/test_firmware.py +108 -0
  113. kasa/tests/smart/modules/test_humidity.py +28 -0
  114. kasa/tests/smart/modules/test_light_effect.py +41 -0
  115. kasa/tests/smart/modules/test_temperature.py +47 -0
  116. kasa/tests/smart/modules/test_temperaturecontrol.py +137 -0
  117. kasa/tests/smart/modules/test_waterleak.py +42 -0
  118. kasa/tests/test_aestransport.py +115 -23
  119. kasa/tests/test_bulb.py +115 -85
  120. kasa/tests/test_childdevice.py +19 -7
  121. kasa/tests/test_cli.py +453 -99
  122. kasa/tests/test_common_modules.py +216 -0
  123. kasa/tests/test_device.py +268 -0
  124. kasa/tests/test_device_factory.py +61 -35
  125. kasa/tests/test_device_type.py +1 -1
  126. kasa/tests/test_deviceconfig.py +2 -2
  127. kasa/tests/test_dimmer.py +33 -24
  128. kasa/tests/test_discovery.py +112 -63
  129. kasa/tests/test_emeter.py +14 -10
  130. kasa/tests/test_feature.py +205 -0
  131. kasa/tests/test_httpclient.py +9 -9
  132. kasa/tests/test_iotdevice.py +286 -0
  133. kasa/tests/test_klapprotocol.py +94 -37
  134. kasa/tests/test_lightstrip.py +24 -19
  135. kasa/tests/test_plug.py +46 -8
  136. kasa/tests/test_protocol.py +8 -10
  137. kasa/tests/test_readme_examples.py +87 -26
  138. kasa/tests/test_smartdevice.py +203 -311
  139. kasa/tests/test_smartprotocol.py +130 -25
  140. kasa/tests/test_strip.py +15 -6
  141. kasa/tests/test_usage.py +1 -1
  142. kasa/xortransport.py +15 -12
  143. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -143
  144. python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
  145. kasa/modules/emeter.py +0 -111
  146. kasa/smartlightstrip.py +0 -143
  147. kasa/smartplug.py +0 -91
  148. kasa/tapo/__init__.py +0 -7
  149. kasa/tapo/childdevice.py +0 -41
  150. kasa/tapo/tapobulb.py +0 -262
  151. kasa/tapo/tapodevice.py +0 -409
  152. kasa/tapo/tapoplug.py +0 -45
  153. kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -47
  154. kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -50
  155. kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -50
  156. kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -47
  157. kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -75
  158. kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -103
  159. kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -103
  160. kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -104
  161. python_kasa-0.6.2.1.dist-info/RECORD +0 -167
  162. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
  163. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +0 -0
  164. {python_kasa-0.6.2.1.dist-info → python_kasa-0.7.0.dev0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,96 @@
1
+ """Implementation of auto off module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from datetime import datetime, timedelta
6
+ from typing import TYPE_CHECKING
7
+
8
+ from ...feature import Feature
9
+ from ..smartmodule import SmartModule
10
+
11
+ if TYPE_CHECKING:
12
+ from ..smartdevice import SmartDevice
13
+
14
+
15
+ class AutoOff(SmartModule):
16
+ """Implementation of auto off module."""
17
+
18
+ REQUIRED_COMPONENT = "auto_off"
19
+ QUERY_GETTER_NAME = "get_auto_off_config"
20
+
21
+ def __init__(self, device: SmartDevice, module: str):
22
+ super().__init__(device, module)
23
+ self._add_feature(
24
+ Feature(
25
+ device,
26
+ id="auto_off_enabled",
27
+ name="Auto off enabled",
28
+ container=self,
29
+ attribute_getter="enabled",
30
+ attribute_setter="set_enabled",
31
+ type=Feature.Type.Switch,
32
+ )
33
+ )
34
+ self._add_feature(
35
+ Feature(
36
+ device,
37
+ id="auto_off_minutes",
38
+ name="Auto off minutes",
39
+ container=self,
40
+ attribute_getter="delay",
41
+ attribute_setter="set_delay",
42
+ type=Feature.Type.Number,
43
+ )
44
+ )
45
+ self._add_feature(
46
+ Feature(
47
+ device,
48
+ id="auto_off_at",
49
+ name="Auto off at",
50
+ container=self,
51
+ attribute_getter="auto_off_at",
52
+ category=Feature.Category.Info,
53
+ )
54
+ )
55
+
56
+ def query(self) -> dict:
57
+ """Query to execute during the update cycle."""
58
+ return {self.QUERY_GETTER_NAME: {"start_index": 0}}
59
+
60
+ @property
61
+ def enabled(self) -> bool:
62
+ """Return True if enabled."""
63
+ return self.data["enable"]
64
+
65
+ async def set_enabled(self, enable: bool):
66
+ """Enable/disable auto off."""
67
+ return await self.call(
68
+ "set_auto_off_config",
69
+ {"enable": enable, "delay_min": self.data["delay_min"]},
70
+ )
71
+
72
+ @property
73
+ def delay(self) -> int:
74
+ """Return time until auto off."""
75
+ return self.data["delay_min"]
76
+
77
+ async def set_delay(self, delay: int):
78
+ """Set time until auto off."""
79
+ return await self.call(
80
+ "set_auto_off_config", {"delay_min": delay, "enable": self.data["enable"]}
81
+ )
82
+
83
+ @property
84
+ def is_timer_active(self) -> bool:
85
+ """Return True is auto-off timer is active."""
86
+ return self._device.sys_info["auto_off_status"] == "on"
87
+
88
+ @property
89
+ def auto_off_at(self) -> datetime | None:
90
+ """Return when the device will be turned off automatically."""
91
+ if not self.is_timer_active:
92
+ return None
93
+
94
+ sysinfo = self._device.sys_info
95
+
96
+ return self._device.time + timedelta(seconds=sysinfo["auto_off_remain_time"])
@@ -0,0 +1,54 @@
1
+ """Implementation of battery module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from ...feature import Feature
6
+ from ..smartmodule import SmartModule
7
+
8
+
9
+ class BatterySensor(SmartModule):
10
+ """Implementation of battery module."""
11
+
12
+ REQUIRED_COMPONENT = "battery_detect"
13
+ QUERY_GETTER_NAME = "get_battery_detect_info"
14
+
15
+ def _initialize_features(self):
16
+ """Initialize features."""
17
+ self._add_feature(
18
+ Feature(
19
+ self._device,
20
+ "battery_low",
21
+ "Battery low",
22
+ container=self,
23
+ attribute_getter="battery_low",
24
+ icon="mdi:alert",
25
+ type=Feature.Type.BinarySensor,
26
+ category=Feature.Category.Debug,
27
+ )
28
+ )
29
+
30
+ # Some devices, like T110 contact sensor do not report the battery percentage
31
+ if "battery_percentage" in self._device.sys_info:
32
+ self._add_feature(
33
+ Feature(
34
+ self._device,
35
+ "battery_level",
36
+ "Battery level",
37
+ container=self,
38
+ attribute_getter="battery",
39
+ icon="mdi:battery",
40
+ unit="%",
41
+ category=Feature.Category.Info,
42
+ type=Feature.Type.Sensor,
43
+ )
44
+ )
45
+
46
+ @property
47
+ def battery(self):
48
+ """Return battery level."""
49
+ return self._device.sys_info["battery_percentage"]
50
+
51
+ @property
52
+ def battery_low(self):
53
+ """Return True if battery is low."""
54
+ return self._device.sys_info["at_low_battery"]
@@ -0,0 +1,66 @@
1
+ """Implementation of brightness module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from ...feature import Feature
6
+ from ..smartmodule import SmartModule
7
+
8
+ BRIGHTNESS_MIN = 0
9
+ BRIGHTNESS_MAX = 100
10
+
11
+
12
+ class Brightness(SmartModule):
13
+ """Implementation of brightness module."""
14
+
15
+ REQUIRED_COMPONENT = "brightness"
16
+
17
+ def _initialize_features(self):
18
+ """Initialize features."""
19
+ super()._initialize_features()
20
+
21
+ device = self._device
22
+ self._add_feature(
23
+ Feature(
24
+ device,
25
+ id="brightness",
26
+ name="Brightness",
27
+ container=self,
28
+ attribute_getter="brightness",
29
+ attribute_setter="set_brightness",
30
+ minimum_value=BRIGHTNESS_MIN,
31
+ maximum_value=BRIGHTNESS_MAX,
32
+ type=Feature.Type.Number,
33
+ category=Feature.Category.Primary,
34
+ )
35
+ )
36
+
37
+ def query(self) -> dict:
38
+ """Query to execute during the update cycle."""
39
+ # Brightness is contained in the main device info response.
40
+ return {}
41
+
42
+ @property
43
+ def brightness(self):
44
+ """Return current brightness."""
45
+ return self.data["brightness"]
46
+
47
+ async def set_brightness(self, brightness: int, *, transition: int | None = None):
48
+ """Set the brightness. A brightness value of 0 will turn off the light.
49
+
50
+ Note, transition is not supported and will be ignored.
51
+ """
52
+ if not isinstance(brightness, int) or not (
53
+ BRIGHTNESS_MIN <= brightness <= BRIGHTNESS_MAX
54
+ ):
55
+ raise ValueError(
56
+ f"Invalid brightness value: {brightness} "
57
+ f"(valid range: {BRIGHTNESS_MIN}-{BRIGHTNESS_MAX}%)"
58
+ )
59
+
60
+ if brightness == 0:
61
+ return await self._device.turn_off()
62
+ return await self.call("set_device_info", {"brightness": brightness})
63
+
64
+ async def _check_supported(self):
65
+ """Additional check to see if the module is supported by the device."""
66
+ return "brightness" in self.data
@@ -0,0 +1,10 @@
1
+ """Implementation for child devices."""
2
+
3
+ from ..smartmodule import SmartModule
4
+
5
+
6
+ class ChildDevice(SmartModule):
7
+ """Implementation for child devices."""
8
+
9
+ REQUIRED_COMPONENT = "child_device"
10
+ QUERY_GETTER_NAME = "get_child_device_list"
@@ -0,0 +1,42 @@
1
+ """Implementation of cloud module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from ...exceptions import SmartErrorCode
8
+ from ...feature import Feature
9
+ from ..smartmodule import SmartModule
10
+
11
+ if TYPE_CHECKING:
12
+ from ..smartdevice import SmartDevice
13
+
14
+
15
+ class Cloud(SmartModule):
16
+ """Implementation of cloud module."""
17
+
18
+ QUERY_GETTER_NAME = "get_connect_cloud_state"
19
+ REQUIRED_COMPONENT = "cloud_connect"
20
+
21
+ def __init__(self, device: SmartDevice, module: str):
22
+ super().__init__(device, module)
23
+
24
+ self._add_feature(
25
+ Feature(
26
+ device,
27
+ id="cloud_connection",
28
+ name="Cloud connection",
29
+ container=self,
30
+ attribute_getter="is_connected",
31
+ icon="mdi:cloud",
32
+ type=Feature.Type.BinarySensor,
33
+ category=Feature.Category.Info,
34
+ )
35
+ )
36
+
37
+ @property
38
+ def is_connected(self):
39
+ """Return True if device is connected to the cloud."""
40
+ if isinstance(self.data, SmartErrorCode):
41
+ return False
42
+ return self.data["status"] == 0
@@ -0,0 +1,96 @@
1
+ """Implementation of color module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from ...feature import Feature
8
+ from ...interfaces.light import HSV
9
+ from ..smartmodule import SmartModule
10
+
11
+ if TYPE_CHECKING:
12
+ from ..smartdevice import SmartDevice
13
+
14
+
15
+ class Color(SmartModule):
16
+ """Implementation of color module."""
17
+
18
+ REQUIRED_COMPONENT = "color"
19
+
20
+ def __init__(self, device: SmartDevice, module: str):
21
+ super().__init__(device, module)
22
+ self._add_feature(
23
+ Feature(
24
+ device,
25
+ "hsv",
26
+ "HSV",
27
+ container=self,
28
+ attribute_getter="hsv",
29
+ attribute_setter="set_hsv",
30
+ # TODO proper type for setting hsv
31
+ type=Feature.Type.Unknown,
32
+ )
33
+ )
34
+
35
+ def query(self) -> dict:
36
+ """Query to execute during the update cycle."""
37
+ # HSV is contained in the main device info response.
38
+ return {}
39
+
40
+ @property
41
+ def hsv(self) -> HSV:
42
+ """Return the current HSV state of the bulb.
43
+
44
+ :return: hue, saturation and value (degrees, %, 1-100)
45
+ """
46
+ h, s, v = (
47
+ self.data.get("hue", 0),
48
+ self.data.get("saturation", 0),
49
+ self.data.get("brightness", 0),
50
+ )
51
+
52
+ return HSV(hue=h, saturation=s, value=v)
53
+
54
+ def _raise_for_invalid_brightness(self, value: int):
55
+ """Raise error on invalid brightness value."""
56
+ if not isinstance(value, int) or not (1 <= value <= 100):
57
+ raise ValueError(f"Invalid brightness value: {value} (valid range: 1-100%)")
58
+
59
+ async def set_hsv(
60
+ self,
61
+ hue: int,
62
+ saturation: int,
63
+ value: int | None = None,
64
+ *,
65
+ transition: int | None = None,
66
+ ) -> dict:
67
+ """Set new HSV.
68
+
69
+ Note, transition is not supported and will be ignored.
70
+
71
+ :param int hue: hue in degrees
72
+ :param int saturation: saturation in percentage [0,100]
73
+ :param int value: value in percentage [0, 100]
74
+ :param int transition: transition in milliseconds.
75
+ """
76
+ if not isinstance(hue, int) or not (0 <= hue <= 360):
77
+ raise ValueError(f"Invalid hue value: {hue} (valid range: 0-360)")
78
+
79
+ if not isinstance(saturation, int) or not (0 <= saturation <= 100):
80
+ raise ValueError(
81
+ f"Invalid saturation value: {saturation} (valid range: 0-100%)"
82
+ )
83
+
84
+ if value is not None:
85
+ self._raise_for_invalid_brightness(value)
86
+
87
+ request_payload = {
88
+ "color_temp": 0, # If set, color_temp takes precedence over hue&sat
89
+ "hue": hue,
90
+ "saturation": saturation,
91
+ }
92
+ # The device errors on invalid brightness values.
93
+ if value is not None:
94
+ request_payload["brightness"] = value
95
+
96
+ return await self.call("set_device_info", {**request_payload})
@@ -0,0 +1,78 @@
1
+ """Implementation of color temp module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ from typing import TYPE_CHECKING
7
+
8
+ from ...feature import Feature
9
+ from ...interfaces.light import ColorTempRange
10
+ from ..smartmodule import SmartModule
11
+
12
+ if TYPE_CHECKING:
13
+ from ..smartdevice import SmartDevice
14
+
15
+
16
+ _LOGGER = logging.getLogger(__name__)
17
+
18
+ DEFAULT_TEMP_RANGE = [2500, 6500]
19
+
20
+
21
+ class ColorTemperature(SmartModule):
22
+ """Implementation of color temp module."""
23
+
24
+ REQUIRED_COMPONENT = "color_temperature"
25
+
26
+ def __init__(self, device: SmartDevice, module: str):
27
+ super().__init__(device, module)
28
+ self._add_feature(
29
+ Feature(
30
+ device,
31
+ "color_temperature",
32
+ "Color temperature",
33
+ container=self,
34
+ attribute_getter="color_temp",
35
+ attribute_setter="set_color_temp",
36
+ range_getter="valid_temperature_range",
37
+ category=Feature.Category.Primary,
38
+ type=Feature.Type.Number,
39
+ )
40
+ )
41
+
42
+ def query(self) -> dict:
43
+ """Query to execute during the update cycle."""
44
+ # Color temp is contained in the main device info response.
45
+ return {}
46
+
47
+ @property
48
+ def valid_temperature_range(self) -> ColorTempRange:
49
+ """Return valid color-temp range."""
50
+ if (ct_range := self.data.get("color_temp_range")) is None:
51
+ _LOGGER.debug(
52
+ "Device doesn't report color temperature range, "
53
+ "falling back to default %s",
54
+ DEFAULT_TEMP_RANGE,
55
+ )
56
+ ct_range = DEFAULT_TEMP_RANGE
57
+ return ColorTempRange(*ct_range)
58
+
59
+ @property
60
+ def color_temp(self):
61
+ """Return current color temperature."""
62
+ return self.data["color_temp"]
63
+
64
+ async def set_color_temp(self, temp: int):
65
+ """Set the color temperature."""
66
+ valid_temperature_range = self.valid_temperature_range
67
+ if temp < valid_temperature_range[0] or temp > valid_temperature_range[1]:
68
+ raise ValueError(
69
+ "Temperature should be between {} and {}, was {}".format(
70
+ *valid_temperature_range, temp
71
+ )
72
+ )
73
+
74
+ return await self.call("set_device_info", {"color_temp": temp})
75
+
76
+ async def _check_supported(self) -> bool:
77
+ """Check the color_temp_range has more than one value."""
78
+ return self.valid_temperature_range.min != self.valid_temperature_range.max
@@ -0,0 +1,42 @@
1
+ """Implementation of contact sensor module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from ...feature import Feature
8
+ from ..smartmodule import SmartModule
9
+
10
+ if TYPE_CHECKING:
11
+ from ..smartdevice import SmartDevice
12
+
13
+
14
+ class ContactSensor(SmartModule):
15
+ """Implementation of contact sensor module."""
16
+
17
+ REQUIRED_COMPONENT = None # we depend on availability of key
18
+ REQUIRED_KEY_ON_PARENT = "open"
19
+
20
+ def __init__(self, device: SmartDevice, module: str):
21
+ super().__init__(device, module)
22
+ self._add_feature(
23
+ Feature(
24
+ device,
25
+ id="is_open",
26
+ name="Open",
27
+ container=self,
28
+ attribute_getter="is_open",
29
+ icon="mdi:door",
30
+ category=Feature.Category.Primary,
31
+ type=Feature.Type.BinarySensor,
32
+ )
33
+ )
34
+
35
+ def query(self) -> dict:
36
+ """Query to execute during the update cycle."""
37
+ return {}
38
+
39
+ @property
40
+ def is_open(self):
41
+ """Return True if the contact sensor is open."""
42
+ return self._device.sys_info["open"]
@@ -0,0 +1,22 @@
1
+ """Implementation of device module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from ..smartmodule import SmartModule
6
+
7
+
8
+ class DeviceModule(SmartModule):
9
+ """Implementation of device module."""
10
+
11
+ REQUIRED_COMPONENT = "device"
12
+
13
+ def query(self) -> dict:
14
+ """Query to execute during the update cycle."""
15
+ query = {
16
+ "get_device_info": None,
17
+ }
18
+ # Device usage is not available on older firmware versions
19
+ if self.supported_version >= 2:
20
+ query["get_device_usage"] = None
21
+
22
+ return query
@@ -0,0 +1,101 @@
1
+ """Implementation of energy monitoring module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from ...emeterstatus import EmeterStatus
8
+ from ...feature import Feature
9
+ from ..smartmodule import SmartModule
10
+
11
+ if TYPE_CHECKING:
12
+ from ..smartdevice import SmartDevice
13
+
14
+
15
+ class Energy(SmartModule):
16
+ """Implementation of energy monitoring module."""
17
+
18
+ REQUIRED_COMPONENT = "energy_monitoring"
19
+
20
+ def __init__(self, device: SmartDevice, module: str):
21
+ super().__init__(device, module)
22
+ self._add_feature(
23
+ Feature(
24
+ device,
25
+ "consumption_current",
26
+ name="Current consumption",
27
+ attribute_getter="current_power",
28
+ container=self,
29
+ unit="W",
30
+ precision_hint=1,
31
+ category=Feature.Category.Primary,
32
+ )
33
+ )
34
+ self._add_feature(
35
+ Feature(
36
+ device,
37
+ "consumption_today",
38
+ name="Today's consumption",
39
+ attribute_getter="emeter_today",
40
+ container=self,
41
+ unit="Wh",
42
+ precision_hint=2,
43
+ category=Feature.Category.Info,
44
+ )
45
+ )
46
+ self._add_feature(
47
+ Feature(
48
+ device,
49
+ "consumption_this_month",
50
+ name="This month's consumption",
51
+ attribute_getter="emeter_this_month",
52
+ container=self,
53
+ unit="Wh",
54
+ precision_hint=2,
55
+ category=Feature.Category.Info,
56
+ )
57
+ )
58
+
59
+ def query(self) -> dict:
60
+ """Query to execute during the update cycle."""
61
+ req = {
62
+ "get_energy_usage": None,
63
+ }
64
+ if self.supported_version > 1:
65
+ req["get_current_power"] = None
66
+ return req
67
+
68
+ @property
69
+ def current_power(self) -> float | None:
70
+ """Current power in watts."""
71
+ if power := self.energy.get("current_power"):
72
+ return power / 1_000
73
+ return None
74
+
75
+ @property
76
+ def energy(self):
77
+ """Return get_energy_usage results."""
78
+ if en := self.data.get("get_energy_usage"):
79
+ return en
80
+ return self.data
81
+
82
+ @property
83
+ def emeter_realtime(self):
84
+ """Get the emeter status."""
85
+ # TODO: Perhaps we should get rid of emeterstatus altogether for smartdevices
86
+ return EmeterStatus(
87
+ {
88
+ "power_mw": self.energy.get("current_power"),
89
+ "total": self.energy.get("today_energy") / 1_000,
90
+ }
91
+ )
92
+
93
+ @property
94
+ def emeter_this_month(self) -> float | None:
95
+ """Get the emeter value for this month."""
96
+ return self.energy.get("month_energy")
97
+
98
+ @property
99
+ def emeter_today(self) -> float | None:
100
+ """Get the emeter value for today."""
101
+ return self.energy.get("today_energy")
@@ -0,0 +1,80 @@
1
+ """Implementation of fan_control module."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING
6
+
7
+ from ...feature import Feature
8
+ from ..smartmodule import SmartModule
9
+
10
+ if TYPE_CHECKING:
11
+ from ..smartdevice import SmartDevice
12
+
13
+
14
+ class Fan(SmartModule):
15
+ """Implementation of fan_control module."""
16
+
17
+ REQUIRED_COMPONENT = "fan_control"
18
+
19
+ def __init__(self, device: SmartDevice, module: str):
20
+ super().__init__(device, module)
21
+
22
+ self._add_feature(
23
+ Feature(
24
+ device,
25
+ id="fan_speed_level",
26
+ name="Fan speed level",
27
+ container=self,
28
+ attribute_getter="fan_speed_level",
29
+ attribute_setter="set_fan_speed_level",
30
+ icon="mdi:fan",
31
+ type=Feature.Type.Number,
32
+ minimum_value=0,
33
+ maximum_value=4,
34
+ category=Feature.Category.Primary,
35
+ )
36
+ )
37
+ self._add_feature(
38
+ Feature(
39
+ device,
40
+ id="fan_sleep_mode",
41
+ name="Fan sleep mode",
42
+ container=self,
43
+ attribute_getter="sleep_mode",
44
+ attribute_setter="set_sleep_mode",
45
+ icon="mdi:sleep",
46
+ type=Feature.Type.Switch,
47
+ )
48
+ )
49
+
50
+ def query(self) -> dict:
51
+ """Query to execute during the update cycle."""
52
+ return {}
53
+
54
+ @property
55
+ def fan_speed_level(self) -> int:
56
+ """Return fan speed level."""
57
+ return self.data["fan_speed_level"]
58
+
59
+ async def set_fan_speed_level(self, level: int):
60
+ """Set fan speed level, 0 for off, 1-4 for on."""
61
+ if level < 0 or level > 4:
62
+ raise ValueError("Invalid level, should be in range 0-4.")
63
+ if level == 0:
64
+ return await self.call("set_device_info", {"device_on": False})
65
+ return await self.call(
66
+ "set_device_info", {"device_on": True, "fan_speed_level": level}
67
+ )
68
+
69
+ @property
70
+ def sleep_mode(self) -> bool:
71
+ """Return sleep mode status."""
72
+ return self.data["fan_sleep_mode_on"]
73
+
74
+ async def set_sleep_mode(self, on: bool):
75
+ """Set sleep mode."""
76
+ return await self.call("set_device_info", {"fan_sleep_mode_on": on})
77
+
78
+ async def _check_supported(self):
79
+ """Is the module available on this device."""
80
+ return "fan_speed_level" in self.data