Add methods to configure dimmer settings (#429)

This commit is contained in:
Teemu R 2023-05-17 20:10:39 +02:00 committed by GitHub
parent 18ce40b6bb
commit ce58cc1a6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
"""Module for dimmers (currently only HS220).""" """Module for dimmers (currently only HS220)."""
from enum import Enum
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from kasa.modules import AmbientLight, Motion from kasa.modules import AmbientLight, Motion
@ -6,6 +7,29 @@ from kasa.smartdevice import DeviceType, SmartDeviceException, requires_update
from kasa.smartplug import SmartPlug 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): class SmartDimmer(SmartPlug):
r"""Representation of a TP-Link Smart Dimmer. r"""Representation of a TP-Link Smart Dimmer.
@ -140,6 +164,40 @@ class SmartDimmer(SmartPlug):
{"brightness": brightness, "duration": transition}, {"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 @property # type: ignore
@requires_update @requires_update
def is_dimmable(self) -> bool: def is_dimmable(self) -> bool: