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.
- kasa/__init__.py +88 -27
- kasa/aestransport.py +51 -34
- kasa/cli.py +326 -130
- kasa/device.py +406 -0
- kasa/device_factory.py +83 -44
- kasa/device_type.py +7 -3
- kasa/deviceconfig.py +16 -9
- kasa/discover.py +100 -81
- kasa/emeterstatus.py +12 -7
- kasa/exceptions.py +41 -26
- kasa/feature.py +201 -0
- kasa/httpclient.py +21 -14
- kasa/interfaces/__init__.py +16 -0
- kasa/interfaces/fan.py +20 -0
- kasa/interfaces/led.py +38 -0
- kasa/interfaces/light.py +133 -0
- kasa/interfaces/lighteffect.py +80 -0
- kasa/interfaces/lightpreset.py +76 -0
- kasa/iot/__init__.py +18 -0
- kasa/{effects.py → iot/effects.py} +4 -2
- kasa/{smartbulb.py → iot/iotbulb.py} +100 -147
- kasa/{smartdevice.py → iot/iotdevice.py} +157 -286
- kasa/{smartdimmer.py → iot/iotdimmer.py} +42 -33
- kasa/iot/iotlightstrip.py +71 -0
- kasa/{modules/module.py → iot/iotmodule.py} +22 -42
- kasa/iot/iotplug.py +94 -0
- kasa/{smartstrip.py → iot/iotstrip.py} +57 -75
- kasa/{modules → iot/modules}/__init__.py +10 -2
- kasa/{modules → iot/modules}/ambientlight.py +31 -3
- kasa/{modules → iot/modules}/antitheft.py +1 -0
- kasa/{modules → iot/modules}/cloud.py +25 -6
- kasa/{modules → iot/modules}/countdown.py +1 -0
- kasa/iot/modules/emeter.py +210 -0
- kasa/iot/modules/led.py +32 -0
- kasa/iot/modules/light.py +221 -0
- kasa/iot/modules/lighteffect.py +122 -0
- kasa/iot/modules/lightpreset.py +151 -0
- kasa/{modules → iot/modules}/motion.py +8 -8
- kasa/{modules → iot/modules}/rulemodule.py +11 -12
- kasa/{modules → iot/modules}/schedule.py +1 -0
- kasa/{modules → iot/modules}/time.py +5 -4
- kasa/{modules → iot/modules}/usage.py +10 -8
- kasa/iotprotocol.py +22 -20
- kasa/klaptransport.py +25 -15
- kasa/module.py +136 -0
- kasa/modulemapping.py +25 -0
- kasa/modulemapping.pyi +96 -0
- kasa/protocol.py +6 -4
- kasa/smart/__init__.py +6 -0
- kasa/smart/effects.py +429 -0
- kasa/smart/modules/__init__.py +57 -0
- kasa/smart/modules/alarm.py +142 -0
- kasa/smart/modules/autooff.py +96 -0
- kasa/smart/modules/batterysensor.py +54 -0
- kasa/smart/modules/brightness.py +66 -0
- kasa/smart/modules/childdevice.py +10 -0
- kasa/smart/modules/cloud.py +42 -0
- kasa/smart/modules/color.py +96 -0
- kasa/smart/modules/colortemperature.py +78 -0
- kasa/smart/modules/contactsensor.py +42 -0
- kasa/smart/modules/devicemodule.py +22 -0
- kasa/smart/modules/energy.py +101 -0
- kasa/smart/modules/fan.py +80 -0
- kasa/smart/modules/firmware.py +216 -0
- kasa/smart/modules/frostprotection.py +59 -0
- kasa/smart/modules/humiditysensor.py +55 -0
- kasa/smart/modules/led.py +49 -0
- kasa/smart/modules/light.py +139 -0
- kasa/smart/modules/lighteffect.py +115 -0
- kasa/smart/modules/lightpreset.py +142 -0
- kasa/smart/modules/lightstripeffect.py +135 -0
- kasa/smart/modules/lighttransition.py +183 -0
- kasa/smart/modules/reportmode.py +36 -0
- kasa/smart/modules/temperaturecontrol.py +177 -0
- kasa/smart/modules/temperaturesensor.py +77 -0
- kasa/smart/modules/time.py +57 -0
- kasa/smart/modules/waterleaksensor.py +66 -0
- kasa/smart/smartchilddevice.py +65 -0
- kasa/smart/smartdevice.py +653 -0
- kasa/smart/smartmodule.py +112 -0
- kasa/smartprotocol.py +107 -41
- kasa/tests/conftest.py +27 -551
- kasa/tests/device_fixtures.py +427 -0
- kasa/tests/discovery_fixtures.py +197 -0
- kasa/tests/{newfakes.py → fakeprotocol_iot.py} +61 -291
- kasa/tests/fakeprotocol_smart.py +318 -0
- kasa/tests/fixtureinfo.py +154 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.2.3.json +221 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.10.json +547 -0
- kasa/tests/fixtures/smart/H100(EU)_1.0_1.5.5.json +391 -0
- kasa/tests/fixtures/smart/KH100(UK)_1.0_1.5.6.json +1557 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.4.json +899 -0
- kasa/tests/fixtures/smart/KS240(US)_1.0_1.0.5.json +479 -0
- kasa/tests/fixtures/smart/L530E(EU)_3.0_1.1.6.json +449 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.1.3.json +173 -0
- kasa/tests/fixtures/smart/P100_1.0.0_1.4.0.json +204 -0
- kasa/tests/fixtures/smart/P110(EU)_1.0_1.0.7.json +566 -0
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.13.json +13 -23
- kasa/tests/fixtures/smart/P300(EU)_1.0_1.0.7.json +6 -6
- kasa/tests/fixtures/smart/TP15(US)_1.0_1.0.3.json +295 -0
- kasa/tests/fixtures/smart/TP25(US)_1.0_1.0.2.json +423 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.4.0.json +170 -0
- kasa/tests/fixtures/smart/child/KE100(EU)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/KE100(UK)_1.0_2.8.0.json +171 -0
- kasa/tests/fixtures/smart/child/T110(EU)_1.0_1.8.0.json +526 -0
- kasa/tests/fixtures/smart/child/T300(EU)_1.0_1.7.0.json +533 -0
- kasa/tests/fixtures/smart/child/T310(EU)_1.0_1.5.0.json +530 -0
- kasa/tests/fixtures/smart/child/T315(EU)_1.0_1.7.0.json +537 -0
- kasa/tests/smart/__init__.py +0 -0
- kasa/tests/smart/features/__init__.py +0 -0
- kasa/tests/smart/features/test_brightness.py +55 -0
- kasa/tests/smart/features/test_colortemp.py +30 -0
- kasa/tests/smart/modules/__init__.py +0 -0
- kasa/tests/smart/modules/test_contact.py +28 -0
- kasa/tests/smart/modules/test_fan.py +79 -0
- kasa/tests/smart/modules/test_firmware.py +108 -0
- kasa/tests/smart/modules/test_humidity.py +28 -0
- kasa/tests/smart/modules/test_light_effect.py +41 -0
- kasa/tests/smart/modules/test_temperature.py +47 -0
- kasa/tests/smart/modules/test_temperaturecontrol.py +137 -0
- kasa/tests/smart/modules/test_waterleak.py +42 -0
- kasa/tests/test_aestransport.py +115 -25
- kasa/tests/test_bulb.py +169 -86
- kasa/tests/test_childdevice.py +84 -0
- kasa/tests/test_cli.py +452 -100
- kasa/tests/test_common_modules.py +216 -0
- kasa/tests/test_device.py +268 -0
- kasa/tests/test_device_factory.py +61 -41
- kasa/tests/test_device_type.py +1 -1
- kasa/tests/test_deviceconfig.py +2 -5
- kasa/tests/test_dimmer.py +33 -24
- kasa/tests/test_discovery.py +112 -69
- kasa/tests/test_emeter.py +39 -12
- kasa/tests/test_feature.py +205 -0
- kasa/tests/test_httpclient.py +9 -9
- kasa/tests/test_iotdevice.py +286 -0
- kasa/tests/test_klapprotocol.py +94 -41
- kasa/tests/test_lightstrip.py +24 -19
- kasa/tests/test_plug.py +51 -9
- kasa/tests/test_protocol.py +8 -12
- kasa/tests/test_readme_examples.py +86 -27
- kasa/tests/test_smartdevice.py +211 -247
- kasa/tests/test_smartprotocol.py +129 -26
- kasa/tests/test_strip.py +15 -6
- kasa/tests/test_usage.py +1 -3
- kasa/xortransport.py +15 -12
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/METADATA +45 -141
- python_kasa-0.7.0.dev0.dist-info/RECORD +239 -0
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/WHEEL +1 -1
- kasa/modules/emeter.py +0 -111
- kasa/smartlightstrip.py +0 -143
- kasa/smartplug.py +0 -91
- kasa/tapo/__init__.py +0 -7
- kasa/tapo/childdevice.py +0 -44
- kasa/tapo/tapobulb.py +0 -262
- kasa/tapo/tapodevice.py +0 -383
- kasa/tapo/tapoplug.py +0 -45
- kasa/tests/fixtures/HS105(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS110(EU)_2.0_mocked.json +0 -50
- kasa/tests/fixtures/HS110(US)_1.0_mocked.json +0 -50
- kasa/tests/fixtures/HS200(US)_1.0_mocked.json +0 -47
- kasa/tests/fixtures/HS220(US)_1.0_mocked.json +0 -75
- kasa/tests/fixtures/LB100(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB120(US)_1.0_mocked.json +0 -103
- kasa/tests/fixtures/LB130(US)_1.0_mocked.json +0 -104
- python_kasa-0.6.2.dist-info/RECORD +0 -163
- {python_kasa-0.6.2.dist-info → python_kasa-0.7.0.dev0.dist-info}/LICENSE +0 -0
- {python_kasa-0.6.2.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,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
|