From 66ceed6122f945215fbb6b8a410d2f6ab1fdb272 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Fri, 16 Feb 2024 18:20:57 +0100 Subject: [PATCH 1/3] Add smart module for smooth transitions --- kasa/smart/modules/__init__.py | 3 +- kasa/smart/modules/lighttransitionmodule.py | 41 +++++++++++++++++++++ kasa/smart/smartdevice.py | 1 + 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 kasa/smart/modules/lighttransitionmodule.py diff --git a/kasa/smart/modules/__init__.py b/kasa/smart/modules/__init__.py index 564363222..05f742d8c 100644 --- a/kasa/smart/modules/__init__.py +++ b/kasa/smart/modules/__init__.py @@ -2,6 +2,7 @@ from .childdevicemodule import ChildDeviceModule from .devicemodule import DeviceModule from .energymodule import EnergyModule +from .lighttransitionmodule import LightTransitionModule from .timemodule import TimeModule -__all__ = ["TimeModule", "EnergyModule", "DeviceModule", "ChildDeviceModule"] +__all__ = ["TimeModule", "EnergyModule", "DeviceModule", "ChildDeviceModule", "LightTransitionModule"] diff --git a/kasa/smart/modules/lighttransitionmodule.py b/kasa/smart/modules/lighttransitionmodule.py new file mode 100644 index 000000000..ef8739bcf --- /dev/null +++ b/kasa/smart/modules/lighttransitionmodule.py @@ -0,0 +1,41 @@ +"""Module for smooth light transitions.""" +from typing import TYPE_CHECKING + +from ...feature import Feature, FeatureType +from ..smartmodule import SmartModule + +if TYPE_CHECKING: + from ..smartdevice import SmartDevice + + +class LightTransitionModule(SmartModule): + """Implementation of gradual on/off.""" + + REQUIRED_COMPONENT = "on_off_gradually" + QUERY_GETTER_NAME = "get_on_off_gradually_info" + + def __init__(self, device: "SmartDevice", module: str): + super().__init__(device, module) + self._add_feature( + Feature( + device=device, + container=self, + name="Smooth transitions", + icon="mdi:transition", + attribute_getter="enabled", + attribute_setter="set_enabled", + type=FeatureType.Switch, + ) + ) + + def set_enabled(self, enable: bool): + """Enable gradual on/off.""" + return self.call("set_on_off_gradually_info", {"enable": enable}) + + @property + def enabled(self) -> bool: + """Return True if gradual on/off is enabled.""" + return bool(self.data["enable"]) + + def __cli_output__(self): + return f"Gradual on/off enabled: {self.enabled}" diff --git a/kasa/smart/smartdevice.py b/kasa/smart/smartdevice.py index f5e41dc1b..3dc2fe2f7 100644 --- a/kasa/smart/smartdevice.py +++ b/kasa/smart/smartdevice.py @@ -17,6 +17,7 @@ DeviceModule, EnergyModule, TimeModule, + LightTransitionModule, ) from .smartmodule import SmartModule From e1e02c41d41785be31bcd8787a10ff636e551934 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Mon, 19 Feb 2024 18:55:57 +0100 Subject: [PATCH 2/3] Fix tests --- kasa/tests/fakeprotocol_smart.py | 1 + 1 file changed, 1 insertion(+) diff --git a/kasa/tests/fakeprotocol_smart.py b/kasa/tests/fakeprotocol_smart.py index bbadec0af..e7c90c436 100644 --- a/kasa/tests/fakeprotocol_smart.py +++ b/kasa/tests/fakeprotocol_smart.py @@ -46,6 +46,7 @@ def credentials_hash(self): FIXTURE_MISSING_MAP = { "get_wireless_scan_info": ("wireless", {"ap_list": [], "wep_supported": False}), + "get_on_off_gradually_info": ("on_off_gradually", {'enable': True}), } async def send(self, request: str): From 6c6c6fa3c7725b3f494e96336a00be5a77d59399 Mon Sep 17 00:00:00 2001 From: Teemu Rytilahti Date: Mon, 19 Feb 2024 19:00:51 +0100 Subject: [PATCH 3/3] Fix linting --- kasa/smart/modules/__init__.py | 8 +++++++- kasa/smart/smartdevice.py | 2 +- kasa/tests/fakeprotocol_smart.py | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/kasa/smart/modules/__init__.py b/kasa/smart/modules/__init__.py index 05f742d8c..69a3c5727 100644 --- a/kasa/smart/modules/__init__.py +++ b/kasa/smart/modules/__init__.py @@ -5,4 +5,10 @@ from .lighttransitionmodule import LightTransitionModule from .timemodule import TimeModule -__all__ = ["TimeModule", "EnergyModule", "DeviceModule", "ChildDeviceModule", "LightTransitionModule"] +__all__ = [ + "TimeModule", + "EnergyModule", + "DeviceModule", + "ChildDeviceModule", + "LightTransitionModule", +] diff --git a/kasa/smart/smartdevice.py b/kasa/smart/smartdevice.py index 3dc2fe2f7..7542078ac 100644 --- a/kasa/smart/smartdevice.py +++ b/kasa/smart/smartdevice.py @@ -16,8 +16,8 @@ ChildDeviceModule, DeviceModule, EnergyModule, - TimeModule, LightTransitionModule, + TimeModule, ) from .smartmodule import SmartModule diff --git a/kasa/tests/fakeprotocol_smart.py b/kasa/tests/fakeprotocol_smart.py index e7c90c436..2945d1677 100644 --- a/kasa/tests/fakeprotocol_smart.py +++ b/kasa/tests/fakeprotocol_smart.py @@ -46,7 +46,7 @@ def credentials_hash(self): FIXTURE_MISSING_MAP = { "get_wireless_scan_info": ("wireless", {"ap_list": [], "wep_supported": False}), - "get_on_off_gradually_info": ("on_off_gradually", {'enable': True}), + "get_on_off_gradually_info": ("on_off_gradually", {"enable": True}), } async def send(self, request: str):