2024-05-02 14:31:12 +00:00
|
|
|
"""Module for light effects."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import base64
|
2024-07-01 11:59:24 +00:00
|
|
|
import binascii
|
|
|
|
import contextlib
|
2024-05-02 14:31:12 +00:00
|
|
|
import copy
|
2024-05-19 09:18:17 +00:00
|
|
|
from typing import Any
|
2024-05-02 14:31:12 +00:00
|
|
|
|
2024-07-01 11:59:24 +00:00
|
|
|
from ..effects import SmartLightEffect
|
2024-07-11 15:21:59 +00:00
|
|
|
from ..smartmodule import Module, SmartModule, allow_update_after
|
2024-05-02 14:31:12 +00:00
|
|
|
|
|
|
|
|
2024-07-01 11:59:24 +00:00
|
|
|
class LightEffect(SmartModule, SmartLightEffect):
|
2024-05-02 14:31:12 +00:00
|
|
|
"""Implementation of dynamic light effects."""
|
|
|
|
|
|
|
|
REQUIRED_COMPONENT = "light_effect"
|
|
|
|
QUERY_GETTER_NAME = "get_dynamic_light_effect_rules"
|
2024-07-17 07:28:11 +00:00
|
|
|
MINIMUM_UPDATE_INTERVAL_SECS = 60 * 60 * 24
|
2024-05-02 14:31:12 +00:00
|
|
|
AVAILABLE_BULB_EFFECTS = {
|
|
|
|
"L1": "Party",
|
|
|
|
"L2": "Relax",
|
|
|
|
}
|
|
|
|
|
2024-05-19 09:18:17 +00:00
|
|
|
_effect: str
|
|
|
|
_effect_state_list: dict[str, dict[str, Any]]
|
|
|
|
_effect_list: list[str]
|
|
|
|
_scenes_names_to_id: dict[str, str]
|
2024-05-02 14:31:12 +00:00
|
|
|
|
2024-10-08 07:16:51 +00:00
|
|
|
async def _post_update_hook(self) -> None:
|
2024-05-19 09:18:17 +00:00
|
|
|
"""Update internal effect state."""
|
2024-05-02 14:31:12 +00:00
|
|
|
# Copy the effects so scene name updates do not update the underlying dict.
|
|
|
|
effects = copy.deepcopy(
|
|
|
|
{effect["id"]: effect for effect in self.data["rule_list"]}
|
|
|
|
)
|
|
|
|
for effect in effects.values():
|
|
|
|
if not effect["scene_name"]:
|
|
|
|
# If the name has not been edited scene_name will be an empty string
|
|
|
|
effect["scene_name"] = self.AVAILABLE_BULB_EFFECTS[effect["id"]]
|
|
|
|
else:
|
2024-07-01 11:59:24 +00:00
|
|
|
# Otherwise it might be b64 encoded or raw string
|
|
|
|
with contextlib.suppress(binascii.Error):
|
|
|
|
effect["scene_name"] = base64.b64decode(
|
|
|
|
effect["scene_name"]
|
|
|
|
).decode()
|
2024-05-19 09:18:17 +00:00
|
|
|
|
|
|
|
self._effect_state_list = effects
|
|
|
|
self._effect_list = [self.LIGHT_EFFECTS_OFF]
|
|
|
|
self._effect_list.extend([effect["scene_name"] for effect in effects.values()])
|
2024-05-02 14:31:12 +00:00
|
|
|
self._scenes_names_to_id = {
|
|
|
|
effect["scene_name"]: effect["id"] for effect in effects.values()
|
|
|
|
}
|
2024-05-19 09:18:17 +00:00
|
|
|
# get_dynamic_light_effect_rules also has an enable property and current_rule_id
|
|
|
|
# property that could be used here as an alternative
|
|
|
|
if self._device._info["dynamic_light_effect_enable"]:
|
|
|
|
self._effect = self._effect_state_list[
|
|
|
|
self._device._info["dynamic_light_effect_id"]
|
|
|
|
]["scene_name"]
|
|
|
|
else:
|
|
|
|
self._effect = self.LIGHT_EFFECTS_OFF
|
2024-05-02 14:31:12 +00:00
|
|
|
|
|
|
|
@property
|
2024-05-10 18:29:28 +00:00
|
|
|
def effect_list(self) -> list[str]:
|
2024-05-02 14:31:12 +00:00
|
|
|
"""Return built-in effects list.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
['Party', 'Relax', ...]
|
|
|
|
"""
|
2024-05-19 09:18:17 +00:00
|
|
|
return self._effect_list
|
2024-05-02 14:31:12 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def effect(self) -> str:
|
|
|
|
"""Return effect name."""
|
2024-05-19 09:18:17 +00:00
|
|
|
return self._effect
|
2024-05-02 14:31:12 +00:00
|
|
|
|
2024-07-17 07:28:11 +00:00
|
|
|
@allow_update_after
|
2024-05-02 14:31:12 +00:00
|
|
|
async def set_effect(
|
|
|
|
self,
|
|
|
|
effect: str,
|
2024-05-10 18:29:28 +00:00
|
|
|
*,
|
|
|
|
brightness: int | None = None,
|
|
|
|
transition: int | None = None,
|
2024-11-10 18:55:13 +00:00
|
|
|
) -> dict:
|
2024-05-02 14:31:12 +00:00
|
|
|
"""Set an effect for the device.
|
|
|
|
|
2024-07-01 11:59:24 +00:00
|
|
|
Calling this will modify the brightness of the effect on the device.
|
|
|
|
|
2024-05-02 14:31:12 +00:00
|
|
|
The device doesn't store an active effect while not enabled so store locally.
|
|
|
|
"""
|
|
|
|
if effect != self.LIGHT_EFFECTS_OFF and effect not in self._scenes_names_to_id:
|
|
|
|
raise ValueError(
|
2024-08-30 15:30:07 +00:00
|
|
|
f"The effect {effect} is not a built in effect. Possible values "
|
2024-05-02 14:31:12 +00:00
|
|
|
f"are: {self.LIGHT_EFFECTS_OFF} "
|
|
|
|
f"{' '.join(self._scenes_names_to_id.keys())}"
|
|
|
|
)
|
|
|
|
enable = effect != self.LIGHT_EFFECTS_OFF
|
|
|
|
params: dict[str, bool | str] = {"enable": enable}
|
|
|
|
if enable:
|
|
|
|
effect_id = self._scenes_names_to_id[effect]
|
|
|
|
params["id"] = effect_id
|
2024-07-01 11:59:24 +00:00
|
|
|
|
|
|
|
# We set the wanted brightness before activating the effect
|
|
|
|
brightness_module = self._device.modules[Module.Brightness]
|
|
|
|
brightness = (
|
|
|
|
brightness if brightness is not None else brightness_module.brightness
|
|
|
|
)
|
|
|
|
await self.set_brightness(brightness, effect_id=effect_id)
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
return await self.call("set_dynamic_light_effect_rule_enable", params)
|
2024-07-01 11:59:24 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_active(self) -> bool:
|
|
|
|
"""Return True if effect is active."""
|
|
|
|
return bool(self._device._info["dynamic_light_effect_enable"])
|
|
|
|
|
|
|
|
def _get_effect_data(self, effect_id: str | None = None) -> dict[str, Any]:
|
|
|
|
"""Return effect data for the *effect_id*.
|
|
|
|
|
|
|
|
If *effect_id* is None, return the data for active effect.
|
|
|
|
"""
|
|
|
|
if effect_id is None:
|
|
|
|
effect_id = self.data["current_rule_id"]
|
|
|
|
|
|
|
|
return self._effect_state_list[effect_id]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self) -> int:
|
|
|
|
"""Return effect brightness."""
|
|
|
|
first_color_status = self._get_effect_data()["color_status_list"][0]
|
|
|
|
brightness = first_color_status[0]
|
|
|
|
|
|
|
|
return brightness
|
|
|
|
|
2024-07-11 15:21:59 +00:00
|
|
|
@allow_update_after
|
2024-07-01 11:59:24 +00:00
|
|
|
async def set_brightness(
|
|
|
|
self,
|
|
|
|
brightness: int,
|
|
|
|
*,
|
|
|
|
transition: int | None = None,
|
|
|
|
effect_id: str | None = None,
|
2024-11-10 18:55:13 +00:00
|
|
|
) -> dict:
|
2024-07-01 11:59:24 +00:00
|
|
|
"""Set effect brightness."""
|
|
|
|
new_effect = self._get_effect_data(effect_id=effect_id).copy()
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def _replace_brightness(data: list[int], new_brightness: int) -> list[int]:
|
2024-07-01 11:59:24 +00:00
|
|
|
"""Replace brightness.
|
|
|
|
|
|
|
|
The first element is the brightness, the rest are unknown.
|
|
|
|
[[33, 0, 0, 2700], [33, 321, 99, 0], [33, 196, 99, 0], .. ]
|
|
|
|
"""
|
|
|
|
return [new_brightness, data[1], data[2], data[3]]
|
|
|
|
|
|
|
|
new_color_status_list = [
|
|
|
|
_replace_brightness(state, brightness)
|
|
|
|
for state in new_effect["color_status_list"]
|
|
|
|
]
|
|
|
|
new_effect["color_status_list"] = new_color_status_list
|
|
|
|
|
|
|
|
return await self.call("edit_dynamic_light_effect_rule", new_effect)
|
2024-05-02 14:31:12 +00:00
|
|
|
|
2024-07-11 15:21:59 +00:00
|
|
|
@allow_update_after
|
2024-05-10 18:29:28 +00:00
|
|
|
async def set_custom_effect(
|
|
|
|
self,
|
|
|
|
effect_dict: dict,
|
2024-11-10 18:55:13 +00:00
|
|
|
) -> dict:
|
2024-05-10 18:29:28 +00:00
|
|
|
"""Set a custom effect on the device.
|
|
|
|
|
|
|
|
:param str effect_dict: The custom effect dict to set
|
|
|
|
"""
|
|
|
|
raise NotImplementedError(
|
|
|
|
"Device does not support setting custom effects. "
|
|
|
|
"Use has_custom_effects to check for support."
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def has_custom_effects(self) -> bool:
|
|
|
|
"""Return True if the device supports setting custom effects."""
|
|
|
|
return False
|
|
|
|
|
2024-05-02 14:31:12 +00:00
|
|
|
def query(self) -> dict:
|
|
|
|
"""Query to execute during the update cycle."""
|
|
|
|
return {self.QUERY_GETTER_NAME: {"start_index": 0}}
|