From 520b6bbae36fb2c3269db0395e4952cb62be8687 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Mon, 19 Feb 2024 20:39:20 +0100 Subject: [PATCH] Add smartdevice module for smooth transitions (#759) * Add smart module for smooth transitions * Fix tests * Fix linting --- kasa/smart/modules/__init__.py | 9 ++++- kasa/smart/modules/lighttransitionmodule.py | 41 +++++++++++++++++++++ kasa/smart/smartdevice.py | 1 + kasa/tests/fakeprotocol_smart.py | 1 + 4 files changed, 51 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 56436322..69a3c572 100644 --- a/kasa/smart/modules/__init__.py +++ b/kasa/smart/modules/__init__.py @@ -2,6 +2,13 @@ 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 00000000..ef8739bc --- /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 f5e41dc1..7542078a 100644 --- a/kasa/smart/smartdevice.py +++ b/kasa/smart/smartdevice.py @@ -16,6 +16,7 @@ from .modules import ( # noqa: F401 ChildDeviceModule, DeviceModule, EnergyModule, + LightTransitionModule, TimeModule, ) from .smartmodule import SmartModule diff --git a/kasa/tests/fakeprotocol_smart.py b/kasa/tests/fakeprotocol_smart.py index bbadec0a..2945d167 100644 --- a/kasa/tests/fakeprotocol_smart.py +++ b/kasa/tests/fakeprotocol_smart.py @@ -46,6 +46,7 @@ class FakeSmartTransport(BaseTransport): 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):