2024-05-13 16:34:44 +00:00
|
|
|
"""Implementation of brightness module."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-05-19 10:20:18 +00:00
|
|
|
from dataclasses import asdict
|
2024-12-11 15:53:35 +00:00
|
|
|
from typing import TYPE_CHECKING, Annotated, cast
|
2024-05-13 16:34:44 +00:00
|
|
|
|
2024-05-13 17:52:08 +00:00
|
|
|
from ...device_type import DeviceType
|
2024-05-13 16:34:44 +00:00
|
|
|
from ...exceptions import KasaException
|
|
|
|
from ...feature import Feature
|
2024-05-19 10:20:18 +00:00
|
|
|
from ...interfaces.light import HSV, ColorTempRange, LightState
|
2024-05-13 16:34:44 +00:00
|
|
|
from ...interfaces.light import Light as LightInterface
|
2024-12-11 15:53:35 +00:00
|
|
|
from ...module import FeatureAttribute
|
2024-05-13 16:34:44 +00:00
|
|
|
from ..iotmodule import IotModule
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from ..iotbulb import IotBulb
|
|
|
|
from ..iotdimmer import IotDimmer
|
|
|
|
|
|
|
|
|
|
|
|
BRIGHTNESS_MIN = 0
|
|
|
|
BRIGHTNESS_MAX = 100
|
|
|
|
|
|
|
|
|
|
|
|
class Light(IotModule, LightInterface):
|
|
|
|
"""Implementation of brightness module."""
|
|
|
|
|
|
|
|
_device: IotBulb | IotDimmer
|
2024-05-22 13:33:55 +00:00
|
|
|
_light_state: LightState
|
2024-05-13 16:34:44 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def _initialize_features(self) -> None:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Initialize features."""
|
|
|
|
super()._initialize_features()
|
|
|
|
device = self._device
|
|
|
|
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_dimmable:
|
2024-05-13 16:34:44 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
|
|
|
id="brightness",
|
|
|
|
name="Brightness",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="brightness",
|
|
|
|
attribute_setter="set_brightness",
|
2024-07-30 18:23:07 +00:00
|
|
|
range_getter=lambda: (BRIGHTNESS_MIN, BRIGHTNESS_MAX),
|
2024-05-13 16:34:44 +00:00
|
|
|
type=Feature.Type.Number,
|
|
|
|
category=Feature.Category.Primary,
|
|
|
|
)
|
|
|
|
)
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_variable_color_temp:
|
2024-05-13 16:34:44 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device=device,
|
|
|
|
id="color_temperature",
|
|
|
|
name="Color temperature",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="color_temp",
|
|
|
|
attribute_setter="set_color_temp",
|
|
|
|
range_getter="valid_temperature_range",
|
|
|
|
category=Feature.Category.Primary,
|
|
|
|
type=Feature.Type.Number,
|
|
|
|
)
|
|
|
|
)
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_color:
|
2024-05-13 16:34:44 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device=device,
|
|
|
|
id="hsv",
|
|
|
|
name="HSV",
|
|
|
|
container=self,
|
|
|
|
attribute_getter="hsv",
|
|
|
|
attribute_setter="set_hsv",
|
|
|
|
# TODO proper type for setting hsv
|
|
|
|
type=Feature.Type.Unknown,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def query(self) -> dict:
|
|
|
|
"""Query to execute during the update cycle."""
|
|
|
|
# Brightness is contained in the main device info response.
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def _get_bulb_device(self) -> IotBulb | None:
|
2024-05-13 17:52:08 +00:00
|
|
|
"""For type checker this gets an IotBulb.
|
|
|
|
|
|
|
|
IotDimmer is not a subclass of IotBulb and using isinstance
|
|
|
|
here at runtime would create a circular import.
|
|
|
|
"""
|
|
|
|
if self._device.device_type in {DeviceType.Bulb, DeviceType.LightStrip}:
|
2024-05-13 16:34:44 +00:00
|
|
|
return cast("IotBulb", self._device)
|
|
|
|
return None
|
|
|
|
|
|
|
|
@property # type: ignore
|
|
|
|
def is_dimmable(self) -> int:
|
|
|
|
"""Whether the bulb supports brightness changes."""
|
2024-05-13 17:52:08 +00:00
|
|
|
return self._device._is_dimmable
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property # type: ignore
|
2024-12-11 15:53:35 +00:00
|
|
|
def brightness(self) -> Annotated[int, FeatureAttribute()]:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Return the current brightness in percentage."""
|
2024-05-15 17:49:08 +00:00
|
|
|
return self._device._brightness
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
async def set_brightness(
|
|
|
|
self, brightness: int, *, transition: int | None = None
|
2024-12-11 15:53:35 +00:00
|
|
|
) -> Annotated[dict, FeatureAttribute()]:
|
2024-05-22 13:33:55 +00:00
|
|
|
"""Set the brightness in percentage. A value of 0 will turn off the light.
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
:param int brightness: brightness in percent
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
2024-05-22 13:33:55 +00:00
|
|
|
return await self.set_state(
|
|
|
|
LightState(brightness=brightness, transition=transition)
|
|
|
|
)
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_color(self) -> bool:
|
|
|
|
"""Whether the light supports color changes."""
|
|
|
|
if (bulb := self._get_bulb_device()) is None:
|
|
|
|
return False
|
2024-05-13 17:52:08 +00:00
|
|
|
return bulb._is_color
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_variable_color_temp(self) -> bool:
|
|
|
|
"""Whether the bulb supports color temperature changes."""
|
|
|
|
if (bulb := self._get_bulb_device()) is None:
|
|
|
|
return False
|
2024-05-13 17:52:08 +00:00
|
|
|
return bulb._is_variable_color_temp
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def has_effects(self) -> bool:
|
|
|
|
"""Return True if the device supports effects."""
|
|
|
|
if (bulb := self._get_bulb_device()) is None:
|
|
|
|
return False
|
2024-05-15 17:49:08 +00:00
|
|
|
return bulb._has_effects
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
2024-12-11 15:53:35 +00:00
|
|
|
def hsv(self) -> Annotated[HSV, FeatureAttribute()]:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Return the current HSV state of the bulb.
|
|
|
|
|
|
|
|
:return: hue, saturation and value (degrees, %, %)
|
|
|
|
"""
|
2024-05-13 17:52:08 +00:00
|
|
|
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
|
2024-05-13 16:34:44 +00:00
|
|
|
raise KasaException("Light does not support color.")
|
2024-05-15 17:49:08 +00:00
|
|
|
return bulb._hsv
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
async def set_hsv(
|
|
|
|
self,
|
|
|
|
hue: int,
|
|
|
|
saturation: int,
|
|
|
|
value: int | None = None,
|
|
|
|
*,
|
|
|
|
transition: int | None = None,
|
2024-12-11 15:53:35 +00:00
|
|
|
) -> Annotated[dict, FeatureAttribute()]:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Set new HSV.
|
|
|
|
|
|
|
|
Note, transition is not supported and will be ignored.
|
|
|
|
|
|
|
|
:param int hue: hue in degrees
|
|
|
|
:param int saturation: saturation in percentage [0,100]
|
|
|
|
:param int value: value in percentage [0, 100]
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
2024-05-13 17:52:08 +00:00
|
|
|
if (bulb := self._get_bulb_device()) is None or not bulb._is_color:
|
2024-05-13 16:34:44 +00:00
|
|
|
raise KasaException("Light does not support color.")
|
2024-05-15 17:49:08 +00:00
|
|
|
return await bulb._set_hsv(hue, saturation, value, transition=transition)
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def valid_temperature_range(self) -> ColorTempRange:
|
|
|
|
"""Return the device-specific white temperature range (in Kelvin).
|
|
|
|
|
|
|
|
:return: White temperature range in Kelvin (minimum, maximum)
|
|
|
|
"""
|
2024-05-13 17:52:08 +00:00
|
|
|
if (
|
|
|
|
bulb := self._get_bulb_device()
|
|
|
|
) is None or not bulb._is_variable_color_temp:
|
2024-05-13 16:34:44 +00:00
|
|
|
raise KasaException("Light does not support colortemp.")
|
2024-05-15 17:49:08 +00:00
|
|
|
return bulb._valid_temperature_range
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
@property
|
2024-12-11 15:53:35 +00:00
|
|
|
def color_temp(self) -> Annotated[int, FeatureAttribute()]:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Whether the bulb supports color temperature changes."""
|
2024-05-13 17:52:08 +00:00
|
|
|
if (
|
|
|
|
bulb := self._get_bulb_device()
|
|
|
|
) is None or not bulb._is_variable_color_temp:
|
2024-05-13 16:34:44 +00:00
|
|
|
raise KasaException("Light does not support colortemp.")
|
2024-05-15 17:49:08 +00:00
|
|
|
return bulb._color_temp
|
2024-05-13 16:34:44 +00:00
|
|
|
|
|
|
|
async def set_color_temp(
|
2024-11-10 18:55:13 +00:00
|
|
|
self, temp: int, *, brightness: int | None = None, transition: int | None = None
|
2024-12-11 15:53:35 +00:00
|
|
|
) -> Annotated[dict, FeatureAttribute()]:
|
2024-05-13 16:34:44 +00:00
|
|
|
"""Set the color temperature of the device in kelvin.
|
|
|
|
|
|
|
|
Note, transition is not supported and will be ignored.
|
|
|
|
|
|
|
|
:param int temp: The new color temperature, in Kelvin
|
|
|
|
:param int transition: transition in milliseconds.
|
|
|
|
"""
|
2024-05-13 17:52:08 +00:00
|
|
|
if (
|
|
|
|
bulb := self._get_bulb_device()
|
|
|
|
) is None or not bulb._is_variable_color_temp:
|
2024-05-13 16:34:44 +00:00
|
|
|
raise KasaException("Light does not support colortemp.")
|
2024-05-15 17:49:08 +00:00
|
|
|
return await bulb._set_color_temp(
|
2024-05-13 16:34:44 +00:00
|
|
|
temp, brightness=brightness, transition=transition
|
|
|
|
)
|
2024-05-19 10:20:18 +00:00
|
|
|
|
|
|
|
async def set_state(self, state: LightState) -> dict:
|
|
|
|
"""Set the light state."""
|
2024-05-22 13:33:55 +00:00
|
|
|
# iot protocol Dimmers and smart protocol devices do not support
|
|
|
|
# brightness of 0 so 0 will turn off all devices for consistency
|
|
|
|
if (bulb := self._get_bulb_device()) is None: # Dimmer
|
2024-11-21 18:40:13 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert isinstance(self._device, IotDimmer)
|
2024-05-22 13:33:55 +00:00
|
|
|
if state.brightness == 0 or state.light_on is False:
|
|
|
|
return await self._device.turn_off(transition=state.transition)
|
|
|
|
elif state.brightness:
|
|
|
|
# set_dimmer_transition will turn on the device
|
|
|
|
return await self._device.set_dimmer_transition(
|
|
|
|
state.brightness, state.transition or 0
|
|
|
|
)
|
|
|
|
return await self._device.turn_on(transition=state.transition)
|
2024-05-19 10:20:18 +00:00
|
|
|
else:
|
|
|
|
transition = state.transition
|
|
|
|
state_dict = asdict(state)
|
|
|
|
state_dict = {k: v for k, v in state_dict.items() if v is not None}
|
2024-05-22 13:33:55 +00:00
|
|
|
if "transition" in state_dict:
|
|
|
|
del state_dict["transition"]
|
2024-05-19 10:20:18 +00:00
|
|
|
state_dict["on_off"] = 1 if state.light_on is None else int(state.light_on)
|
2024-05-22 13:33:55 +00:00
|
|
|
if state_dict.get("brightness") == 0:
|
|
|
|
state_dict["on_off"] = 0
|
|
|
|
del state_dict["brightness"]
|
|
|
|
# If light on state not set default to on.
|
|
|
|
elif state.light_on is None:
|
|
|
|
state_dict["on_off"] = 1
|
|
|
|
else:
|
|
|
|
state_dict["on_off"] = int(state.light_on)
|
2024-07-31 14:56:07 +00:00
|
|
|
# Remove the light_on from the dict
|
|
|
|
state_dict.pop("light_on", None)
|
2024-05-19 10:20:18 +00:00
|
|
|
return await bulb._set_light_state(state_dict, transition=transition)
|
|
|
|
|
2024-05-22 13:33:55 +00:00
|
|
|
@property
|
|
|
|
def state(self) -> LightState:
|
|
|
|
"""Return the current light state."""
|
|
|
|
return self._light_state
|
|
|
|
|
2024-10-08 07:16:51 +00:00
|
|
|
async def _post_update_hook(self) -> None:
|
2024-12-11 15:53:35 +00:00
|
|
|
device = self._device
|
|
|
|
if device.is_on is False:
|
2024-05-22 13:33:55 +00:00
|
|
|
state = LightState(light_on=False)
|
|
|
|
else:
|
|
|
|
state = LightState(light_on=True)
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_dimmable:
|
2024-05-22 13:33:55 +00:00
|
|
|
state.brightness = self.brightness
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_color:
|
2024-05-22 13:33:55 +00:00
|
|
|
hsv = self.hsv
|
|
|
|
state.hue = hsv.hue
|
|
|
|
state.saturation = hsv.saturation
|
2024-12-11 15:53:35 +00:00
|
|
|
if device._is_variable_color_temp:
|
2024-05-22 13:33:55 +00:00
|
|
|
state.color_temp = self.color_temp
|
|
|
|
self._light_state = state
|
|
|
|
|
2024-05-19 10:20:18 +00:00
|
|
|
async def _deprecated_set_light_state(
|
|
|
|
self, state: dict, *, transition: int | None = None
|
|
|
|
) -> dict:
|
|
|
|
"""Set the light state."""
|
|
|
|
if (bulb := self._get_bulb_device()) is None:
|
|
|
|
raise KasaException("Device does not support set_light_state")
|
|
|
|
else:
|
|
|
|
return await bulb._set_light_state(state, transition=transition)
|