2024-03-05 14:41:40 +00:00
|
|
|
"""Implementation of brightness module."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-24 16:38:52 +00:00
|
|
|
from ...feature import Feature
|
2024-07-01 11:59:24 +00:00
|
|
|
from ..smartmodule import Module, SmartModule
|
2024-03-05 14:41:40 +00:00
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
BRIGHTNESS_MIN = 0
|
2024-04-20 18:29:07 +00:00
|
|
|
BRIGHTNESS_MAX = 100
|
|
|
|
|
|
|
|
|
2024-03-05 14:41:40 +00:00
|
|
|
class Brightness(SmartModule):
|
|
|
|
"""Implementation of brightness module."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "brightness"
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
def _initialize_features(self):
|
|
|
|
"""Initialize features."""
|
|
|
|
super()._initialize_features()
|
|
|
|
|
|
|
|
device = self._device
|
2024-03-05 14:41:40 +00:00
|
|
|
self._add_feature(
|
|
|
|
Feature(
|
|
|
|
device,
|
2024-05-07 09:13:35 +00:00
|
|
|
id="brightness",
|
|
|
|
name="Brightness",
|
2024-03-05 14:41:40 +00:00
|
|
|
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-04-24 16:38:52 +00:00
|
|
|
type=Feature.Type.Number,
|
2024-04-23 17:20:12 +00:00
|
|
|
category=Feature.Category.Primary,
|
2024-03-05 14:41:40 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
def query(self) -> dict:
|
2024-03-05 14:41:40 +00:00
|
|
|
"""Query to execute during the update cycle."""
|
|
|
|
# Brightness is contained in the main device info response.
|
|
|
|
return {}
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return current brightness."""
|
2024-07-01 11:59:24 +00:00
|
|
|
# If the device supports effects and one is active, use its brightness
|
|
|
|
if (
|
|
|
|
light_effect := self._device.modules.get(Module.SmartLightEffect)
|
|
|
|
) is not None and light_effect.is_active:
|
|
|
|
return light_effect.brightness
|
|
|
|
|
2024-03-05 14:41:40 +00:00
|
|
|
return self.data["brightness"]
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
async def set_brightness(self, brightness: int, *, transition: int | None = None):
|
|
|
|
"""Set the brightness. A brightness value of 0 will turn off the light.
|
|
|
|
|
|
|
|
Note, transition is not supported and will be ignored.
|
|
|
|
"""
|
2024-04-20 18:29:07 +00:00
|
|
|
if not isinstance(brightness, int) or not (
|
|
|
|
BRIGHTNESS_MIN <= brightness <= BRIGHTNESS_MAX
|
|
|
|
):
|
|
|
|
raise ValueError(
|
|
|
|
f"Invalid brightness value: {brightness} "
|
|
|
|
f"(valid range: {BRIGHTNESS_MIN}-{BRIGHTNESS_MAX}%)"
|
|
|
|
)
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
if brightness == 0:
|
|
|
|
return await self._device.turn_off()
|
2024-07-01 11:59:24 +00:00
|
|
|
|
|
|
|
# If the device supports effects and one is active, we adjust its brightness
|
|
|
|
if (
|
|
|
|
light_effect := self._device.modules.get(Module.SmartLightEffect)
|
|
|
|
) is not None and light_effect.is_active:
|
|
|
|
return await light_effect.set_brightness(brightness)
|
|
|
|
|
2024-03-05 14:41:40 +00:00
|
|
|
return await self.call("set_device_info", {"brightness": brightness})
|
2024-04-24 18:17:49 +00:00
|
|
|
|
|
|
|
async def _check_supported(self):
|
|
|
|
"""Additional check to see if the module is supported by the device."""
|
|
|
|
return "brightness" in self.data
|