From ce58cc1a6ac298f8dd9c06b5b0b4687f70ceeeb8 Mon Sep 17 00:00:00 2001 From: Teemu R Date: Wed, 17 May 2023 20:10:39 +0200 Subject: [PATCH] Add methods to configure dimmer settings (#429) --- kasa/smartdimmer.py | 58 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/kasa/smartdimmer.py b/kasa/smartdimmer.py index 74d9221a..9565437d 100644 --- a/kasa/smartdimmer.py +++ b/kasa/smartdimmer.py @@ -1,4 +1,5 @@ """Module for dimmers (currently only HS220).""" +from enum import Enum from typing import Any, Dict, Optional from kasa.modules import AmbientLight, Motion @@ -6,6 +7,29 @@ from kasa.smartdevice import DeviceType, SmartDeviceException, requires_update from kasa.smartplug import SmartPlug +class ButtonAction(Enum): + """Button action.""" + + NoAction = "none" + Instant = "instant_on_off" + Gentle = "gentle_on_off" + Preset = "customize_preset" + + +class ActionType(Enum): + """Button action.""" + + DoubleClick = "double_click_action" + LongPress = "long_press_action" + + +class FadeType(Enum): + """Fade on/off setting.""" + + FadeOn = "fade_on" + FadeOff = "fade_off" + + class SmartDimmer(SmartPlug): r"""Representation of a TP-Link Smart Dimmer. @@ -140,6 +164,40 @@ class SmartDimmer(SmartPlug): {"brightness": brightness, "duration": transition}, ) + @requires_update + async def get_behaviors(self): + """Return button behavior settings.""" + behaviors = await self._query_helper( + self.DIMMER_SERVICE, "get_default_behavior", {} + ) + return behaviors + + @requires_update + async def set_button_action( + self, action_type: ActionType, action: ButtonAction, index: Optional[int] = None + ): + """Set action to perform on button click/hold. + + :param action_type ActionType: whether to control double click or hold action. + :param action ButtonAction: what should the button do (nothing, instant, gentle, change preset) + :param index int: in case of preset change, the preset to select + """ + action_type_setter = f"set_{action_type}" + + payload: Dict[str, Any] = {"mode": str(action)} + if index is not None: + payload["index"] = index + + await self._query_helper(self.DIMMER_SERVICE, action_type_setter, payload) + + @requires_update + async def set_fade_time(self, fade_type: FadeType, time: int): + """Set time for fade in / fade out.""" + fade_type_setter = f"set_{fade_type}_time" + payload = {"fadeTime": time} + + await self._query_helper(self.DIMMER_SERVICE, fade_type_setter, payload) + @property # type: ignore @requires_update def is_dimmable(self) -> bool: