-
-
Notifications
You must be signed in to change notification settings - Fork 228
Add Dimmer Configuration Support #1484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,279 @@ | ||||||||
"""Implementation of the dimmer config module found in dimmers.""" | ||||||||
|
||||||||
from __future__ import annotations | ||||||||
|
||||||||
import logging | ||||||||
from datetime import timedelta | ||||||||
from typing import Any, Final, cast | ||||||||
|
||||||||
from ...exceptions import KasaException | ||||||||
from ...feature import Feature | ||||||||
from ..iotmodule import IotModule, merge | ||||||||
|
||||||||
_LOGGER = logging.getLogger(__name__) | ||||||||
|
||||||||
|
||||||||
def _td_to_ms(td: timedelta) -> int: | ||||||||
""" | ||||||||
Convert timedelta to integer milliseconds. | ||||||||
|
||||||||
Uses default float to integer rounding. | ||||||||
""" | ||||||||
return int(td / timedelta(milliseconds=1)) | ||||||||
|
||||||||
|
||||||||
class Dimmer(IotModule): | ||||||||
"""Implements the dimmer config module.""" | ||||||||
|
||||||||
THRESHOLD_ABS_MIN: Final[int] = 0 | ||||||||
THRESHOLD_ABS_MAX: Final[int] = ( | ||||||||
51 # Strange value, but verified against hardware (KS220). | ||||||||
) | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
FADE_TIME_ABS_MIN: Final[timedelta] = timedelta(seconds=0) | ||||||||
FADE_TIME_ABS_MAX: Final[timedelta] = timedelta(seconds=10) # Completely arbitrary. | ||||||||
GENTLE_TIME_ABS_MIN: Final[timedelta] = timedelta(seconds=0) | ||||||||
GENTLE_TIME_ABS_MAX: Final[timedelta] = timedelta( | ||||||||
seconds=120 | ||||||||
) # Completely arbitrary. | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
RAMP_RATE_ABS_MIN: Final[int] = 10 # Verified against KS220. | ||||||||
RAMP_RATE_ABS_MAX: Final[int] = 50 # Verified against KS220. | ||||||||
|
||||||||
def _initialize_features(self) -> None: | ||||||||
"""Initialize features after the initial update.""" | ||||||||
# Only add features if the device supports the module | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if "get_dimmer_parameters" not in self.data: | ||||||||
return | ||||||||
ryenitcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_threshold_min", | ||||||||
name="Minimum dimming level", | ||||||||
icon="mdi:lightbulb-on-20", | ||||||||
attribute_getter="threshold_min", | ||||||||
attribute_setter="set_threshold_min", | ||||||||
ryenitcher marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
range_getter=lambda: (self.THRESHOLD_ABS_MIN, self.THRESHOLD_ABS_MAX), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_fade_off_time", | ||||||||
name="Dimmer fade off time", | ||||||||
icon="mdi:clock-in", | ||||||||
attribute_getter="fade_off_time", | ||||||||
attribute_setter="set_fade_off_time", | ||||||||
range_getter=lambda: ( | ||||||||
_td_to_ms(self.FADE_TIME_ABS_MIN), | ||||||||
_td_to_ms(self.FADE_TIME_ABS_MAX), | ||||||||
), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_fade_on_time", | ||||||||
name="Dimmer fade on time", | ||||||||
icon="mdi:clock-out", | ||||||||
attribute_getter="fade_on_time", | ||||||||
attribute_setter="set_fade_on_time", | ||||||||
range_getter=lambda: ( | ||||||||
_td_to_ms(self.FADE_TIME_ABS_MIN), | ||||||||
_td_to_ms(self.FADE_TIME_ABS_MAX), | ||||||||
), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_gentle_off_time", | ||||||||
name="Dimmer gentle off time", | ||||||||
icon="mdi:clock-in", | ||||||||
attribute_getter="gentle_off_time", | ||||||||
attribute_setter="set_gentle_off_time", | ||||||||
range_getter=lambda: ( | ||||||||
_td_to_ms(self.GENTLE_TIME_ABS_MIN), | ||||||||
_td_to_ms(self.GENTLE_TIME_ABS_MAX), | ||||||||
), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_gentle_on_time", | ||||||||
name="Dimmer gentle on time", | ||||||||
icon="mdi:clock-out", | ||||||||
attribute_getter="gentle_on_time", | ||||||||
attribute_setter="set_gentle_on_time", | ||||||||
range_getter=lambda: ( | ||||||||
_td_to_ms(self.GENTLE_TIME_ABS_MIN), | ||||||||
_td_to_ms(self.GENTLE_TIME_ABS_MAX), | ||||||||
), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
self._add_feature( | ||||||||
Feature( | ||||||||
device=self._device, | ||||||||
container=self, | ||||||||
id="dimmer_ramp_rate", | ||||||||
name="Dimmer ramp rate", | ||||||||
icon="mdi:clock-fast", | ||||||||
attribute_getter="ramp_rate", | ||||||||
attribute_setter="set_ramp_rate", | ||||||||
range_getter=lambda: (self.RAMP_RATE_ABS_MIN, self.RAMP_RATE_ABS_MAX), | ||||||||
type=Feature.Type.Number, | ||||||||
category=Feature.Category.Config, | ||||||||
) | ||||||||
) | ||||||||
|
||||||||
def query(self) -> dict: | ||||||||
"""Request Dimming configuration.""" | ||||||||
req = merge( | ||||||||
self.query_for_command("get_dimmer_parameters"), | ||||||||
self.query_for_command("get_default_behavior"), | ||||||||
) | ||||||||
|
||||||||
return req | ||||||||
|
||||||||
@property | ||||||||
def config(self) -> dict[str, Any]: | ||||||||
"""Return current configuration.""" | ||||||||
return self.data["get_dimmer_parameters"] | ||||||||
|
||||||||
@property | ||||||||
def threshold_min(self) -> int: | ||||||||
"""Return the minimum dimming level for this dimmer.""" | ||||||||
return self.config["minThreshold"] | ||||||||
|
||||||||
async def set_threshold_min(self, min: int) -> dict: | ||||||||
""" | ||||||||
Set the minimum dimming level for this dimmer. | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
The value will depend on the luminaries connected to the dimmer. | ||||||||
|
||||||||
:param min: The minimum dimming level, in the range 0-51. | ||||||||
""" | ||||||||
if (min < self.THRESHOLD_ABS_MIN) or (min > self.THRESHOLD_ABS_MAX): | ||||||||
raise KasaException( | ||||||||
"Minimum dimming threshold is outside the supported range: " | ||||||||
f"{self.THRESHOLD_ABS_MIN}-{self.THRESHOLD_ABS_MAX}" | ||||||||
) | ||||||||
return await self.call("calibrate_brightness", {"minThreshold": min}) | ||||||||
|
||||||||
@property | ||||||||
def fade_off_time(self) -> timedelta: | ||||||||
"""Return the fade off animation duration.""" | ||||||||
return timedelta(milliseconds=cast(int, self.config["fadeOffTime"])) | ||||||||
|
||||||||
async def set_fade_off_time(self, time: int | timedelta) -> dict: | ||||||||
""" | ||||||||
Set the duration of the fade off animation. | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
:param time: The animation duration, in ms. | ||||||||
""" | ||||||||
if isinstance(time, int): | ||||||||
time = timedelta(milliseconds=time) | ||||||||
if (time < self.FADE_TIME_ABS_MIN) or (time > self.FADE_TIME_ABS_MAX): | ||||||||
raise KasaException( | ||||||||
"Fade time is outside the bounds of the supported range:" | ||||||||
f"{self.FADE_TIME_ABS_MIN}-{self.FADE_TIME_ABS_MAX}" | ||||||||
) | ||||||||
return await self.call("set_fade_off_time", {"fadeTime": _td_to_ms(time)}) | ||||||||
|
||||||||
@property | ||||||||
def fade_on_time(self) -> timedelta: | ||||||||
"""Return the fade on animation duration.""" | ||||||||
return timedelta(milliseconds=cast(int, self.config["fadeOnTime"])) | ||||||||
|
||||||||
async def set_fade_on_time(self, time: int | timedelta) -> dict: | ||||||||
""" | ||||||||
Set the duration of the fade on animation. | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
:param time: The animation duration, in ms. | ||||||||
""" | ||||||||
if isinstance(time, int): | ||||||||
time = timedelta(milliseconds=time) | ||||||||
if (time < self.FADE_TIME_ABS_MIN) or (time > self.FADE_TIME_ABS_MAX): | ||||||||
raise KasaException( | ||||||||
"Fade time is outside the bounds of the supported range:" | ||||||||
f"{self.FADE_TIME_ABS_MIN}-{self.FADE_TIME_ABS_MAX}" | ||||||||
) | ||||||||
return await self.call("set_fade_on_time", {"fadeTime": _td_to_ms(time)}) | ||||||||
|
||||||||
@property | ||||||||
def gentle_off_time(self) -> timedelta: | ||||||||
"""Return the gentle fade off animation duration.""" | ||||||||
return timedelta(milliseconds=cast(int, self.config["gentleOffTime"])) | ||||||||
|
||||||||
async def set_gentle_off_time(self, time: int | timedelta) -> dict: | ||||||||
""" | ||||||||
Set the duration of the gentle fade off animation. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
:param time: The animation duration, in ms. | ||||||||
""" | ||||||||
if isinstance(time, int): | ||||||||
time = timedelta(milliseconds=time) | ||||||||
if (time < self.GENTLE_TIME_ABS_MIN) or (time > self.GENTLE_TIME_ABS_MAX): | ||||||||
raise KasaException( | ||||||||
"Gentle off time is outside the bounds of the supported range: " | ||||||||
f"{self.GENTLE_TIME_ABS_MIN}-{self.GENTLE_TIME_ABS_MAX}." | ||||||||
) | ||||||||
return await self.call("set_gentle_off_time", {"duration": _td_to_ms(time)}) | ||||||||
|
||||||||
@property | ||||||||
def gentle_on_time(self) -> timedelta: | ||||||||
"""Return the gentle fade on animation duration.""" | ||||||||
return timedelta(milliseconds=cast(int, self.config["gentleOnTime"])) | ||||||||
|
||||||||
async def set_gentle_on_time(self, time: int | timedelta) -> dict: | ||||||||
""" | ||||||||
Set the duration of the gentle fade on animation. | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
:param time: The animation duration, in ms. | ||||||||
""" | ||||||||
if isinstance(time, int): | ||||||||
time = timedelta(milliseconds=time) | ||||||||
if (time < self.GENTLE_TIME_ABS_MIN) or (time > self.GENTLE_TIME_ABS_MAX): | ||||||||
raise KasaException( | ||||||||
"Gentle off time is outside the bounds of the supported range: " | ||||||||
f"{self.GENTLE_TIME_ABS_MIN}-{self.GENTLE_TIME_ABS_MAX}." | ||||||||
) | ||||||||
return await self.call("set_gentle_on_time", {"duration": _td_to_ms(time)}) | ||||||||
|
||||||||
@property | ||||||||
def ramp_rate(self) -> int: | ||||||||
"""Return the rate that the dimmer buttons increment the dimmer level.""" | ||||||||
return self.config["rampRate"] | ||||||||
|
||||||||
async def set_ramp_rate(self, rate: int) -> dict: | ||||||||
""" | ||||||||
Set how quickly to ramp the dimming level when using the dimmer buttons. | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
:param rate: The rate to increment the dimming level with each press. | ||||||||
""" | ||||||||
if (rate < self.RAMP_RATE_ABS_MIN) or (rate > self.RAMP_RATE_ABS_MAX): | ||||||||
rytilahti marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
raise KasaException( | ||||||||
"Gentle off time is outside the bounds of the supported range:" | ||||||||
f"{self.RAMP_RATE_ABS_MIN}-{self.RAMP_RATE_ABS_MAX}" | ||||||||
) | ||||||||
return await self.call("set_button_ramp_rate", {"rampRate": rate}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.