finall - lightB is Bresponse ver
This commit is contained in:
997
home-assistant/homeassistant/components/template/lightB.py
Normal file
997
home-assistant/homeassistant/components/template/lightB.py
Normal file
@@ -0,0 +1,997 @@
|
||||
"""Support for Template lights."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
ATTR_COLOR_TEMP_KELVIN,
|
||||
ATTR_EFFECT,
|
||||
ATTR_HS_COLOR,
|
||||
ATTR_RGB_COLOR,
|
||||
ATTR_RGBW_COLOR,
|
||||
ATTR_RGBWW_COLOR,
|
||||
ATTR_TRANSITION,
|
||||
DEFAULT_MAX_KELVIN,
|
||||
DEFAULT_MIN_KELVIN,
|
||||
ENTITY_ID_FORMAT,
|
||||
PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
|
||||
ColorMode,
|
||||
LightEntity,
|
||||
LightEntityFeature,
|
||||
filter_supported_color_modes,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
CONF_ENTITY_ID,
|
||||
CONF_FRIENDLY_NAME,
|
||||
CONF_LIGHTS,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import TemplateError
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.script import Script
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import color as color_util
|
||||
|
||||
from .const import DOMAIN
|
||||
from .template_entity import (
|
||||
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
|
||||
TemplateEntity,
|
||||
rewrite_common_legacy_to_modern_conf,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
|
||||
|
||||
# Legacy
|
||||
CONF_COLOR_ACTION = "set_color"
|
||||
CONF_COLOR_TEMPLATE = "color_template"
|
||||
|
||||
CONF_HS_ACTION = "set_hs"
|
||||
CONF_HS_TEMPLATE = "hs_template"
|
||||
CONF_RGB_ACTION = "set_rgb"
|
||||
CONF_RGB_TEMPLATE = "rgb_template"
|
||||
CONF_RGBW_ACTION = "set_rgbw"
|
||||
CONF_RGBW_TEMPLATE = "rgbw_template"
|
||||
CONF_RGBWW_ACTION = "set_rgbww"
|
||||
CONF_RGBWW_TEMPLATE = "rgbww_template"
|
||||
CONF_EFFECT_ACTION = "set_effect"
|
||||
CONF_EFFECT_LIST_TEMPLATE = "effect_list_template"
|
||||
CONF_EFFECT_TEMPLATE = "effect_template"
|
||||
CONF_LEVEL_ACTION = "set_level"
|
||||
CONF_LEVEL_TEMPLATE = "level_template"
|
||||
CONF_MAX_MIREDS_TEMPLATE = "max_mireds_template"
|
||||
CONF_MIN_MIREDS_TEMPLATE = "min_mireds_template"
|
||||
CONF_OFF_ACTION = "turn_off"
|
||||
CONF_ON_ACTION = "turn_on"
|
||||
CONF_SUPPORTS_TRANSITION = "supports_transition_template"
|
||||
CONF_TEMPERATURE_ACTION = "set_temperature"
|
||||
CONF_TEMPERATURE_TEMPLATE = "temperature_template"
|
||||
CONF_WHITE_VALUE_ACTION = "set_white_value"
|
||||
CONF_WHITE_VALUE_TEMPLATE = "white_value_template"
|
||||
|
||||
DEFAULT_MIN_MIREDS = 153
|
||||
DEFAULT_MAX_MIREDS = 500
|
||||
|
||||
|
||||
@dataclass
|
||||
class ColorModeHandler:
|
||||
"""Describe how one color mode of a template light is configured.
|
||||
|
||||
One handler exists per color mode that has a script and/or a template
|
||||
configured. It carries everything needed to set up the template, to run
|
||||
the script and to optimistically track the state for that mode.
|
||||
"""
|
||||
|
||||
color_mode: ColorMode
|
||||
"""The color mode this handler represents."""
|
||||
service_attr: str
|
||||
"""The `light.turn_on` service data key that selects this mode."""
|
||||
state_attr: str
|
||||
"""Name of the entity attribute holding the current value."""
|
||||
property_name: str
|
||||
"""Name of the LightEntity property exposing the value."""
|
||||
script: Script | None
|
||||
"""The script that sets the color, if configured."""
|
||||
template: Template | None
|
||||
"""The template that reports the color, if configured."""
|
||||
update_callback: Callable[[Any], None]
|
||||
"""Called with the rendered template to update the entity state."""
|
||||
script_variables: Callable[[Any], dict[str, Any]]
|
||||
"""Builds the script run variables from the state value."""
|
||||
to_state: Callable[[Any], Any] = lambda value: value
|
||||
"""Converts the service data value to the value kept in `state_attr`."""
|
||||
|
||||
|
||||
def _hs_variables(value: Any) -> dict[str, Any]:
|
||||
return {"hs": value, "h": int(value[0]), "s": int(value[1])}
|
||||
|
||||
|
||||
def _rgb_variables(value: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"rgb": value,
|
||||
"r": int(value[0]),
|
||||
"g": int(value[1]),
|
||||
"b": int(value[2]),
|
||||
}
|
||||
|
||||
|
||||
def _rgbw_variables(value: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"rgbw": value,
|
||||
"rgb": (int(value[0]), int(value[1]), int(value[2])),
|
||||
"r": int(value[0]),
|
||||
"g": int(value[1]),
|
||||
"b": int(value[2]),
|
||||
"w": int(value[3]),
|
||||
}
|
||||
|
||||
|
||||
def _rgbww_variables(value: Any) -> dict[str, Any]:
|
||||
return {
|
||||
"rgbww": value,
|
||||
"rgb": (int(value[0]), int(value[1]), int(value[2])),
|
||||
"r": int(value[0]),
|
||||
"g": int(value[1]),
|
||||
"b": int(value[2]),
|
||||
"cw": int(value[3]),
|
||||
"ww": int(value[4]),
|
||||
}
|
||||
|
||||
|
||||
LIGHT_SCHEMA = vol.All(
|
||||
cv.deprecated(CONF_ENTITY_ID),
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Exclusive(CONF_COLOR_ACTION, "hs_legacy_action"): cv.SCRIPT_SCHEMA,
|
||||
vol.Exclusive(CONF_COLOR_TEMPLATE, "hs_legacy_template"): cv.template,
|
||||
vol.Exclusive(CONF_HS_ACTION, "hs_legacy_action"): cv.SCRIPT_SCHEMA,
|
||||
vol.Exclusive(CONF_HS_TEMPLATE, "hs_legacy_template"): cv.template,
|
||||
vol.Optional(CONF_RGB_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_RGB_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_RGBW_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_RGBW_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_RGBWW_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_RGBWW_TEMPLATE): cv.template,
|
||||
vol.Inclusive(CONF_EFFECT_ACTION, "effect"): cv.SCRIPT_SCHEMA,
|
||||
vol.Inclusive(CONF_EFFECT_LIST_TEMPLATE, "effect"): cv.template,
|
||||
vol.Inclusive(CONF_EFFECT_TEMPLATE, "effect"): cv.template,
|
||||
vol.Optional(CONF_ENTITY_ID): cv.entity_ids,
|
||||
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
||||
vol.Optional(CONF_LEVEL_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_LEVEL_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_MAX_MIREDS_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_MIN_MIREDS_TEMPLATE): cv.template,
|
||||
vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Required(CONF_ON_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_SUPPORTS_TRANSITION): cv.template,
|
||||
vol.Optional(CONF_TEMPERATURE_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_TEMPERATURE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||
}
|
||||
).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = vol.All(
|
||||
# CONF_WHITE_VALUE_* is deprecated, support will be removed in release 2022.9
|
||||
cv.removed(CONF_WHITE_VALUE_ACTION),
|
||||
cv.removed(CONF_WHITE_VALUE_TEMPLATE),
|
||||
LIGHT_PLATFORM_SCHEMA.extend(
|
||||
{vol.Required(CONF_LIGHTS): cv.schema_with_slug_keys(LIGHT_SCHEMA)}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def _async_create_entities(hass, config):
|
||||
"""Create the Template Lights."""
|
||||
lights = []
|
||||
|
||||
for object_id, entity_config in config[CONF_LIGHTS].items():
|
||||
entity_config = rewrite_common_legacy_to_modern_conf(hass, entity_config)
|
||||
unique_id = entity_config.get(CONF_UNIQUE_ID)
|
||||
|
||||
lights.append(
|
||||
LightTemplate(
|
||||
hass,
|
||||
object_id,
|
||||
entity_config,
|
||||
unique_id,
|
||||
)
|
||||
)
|
||||
|
||||
return lights
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the template lights."""
|
||||
async_add_entities(await _async_create_entities(hass, config))
|
||||
|
||||
|
||||
class LightTemplate(TemplateEntity, LightEntity):
|
||||
"""Representation of a templated Light, including dimmable."""
|
||||
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass,
|
||||
object_id,
|
||||
config,
|
||||
unique_id,
|
||||
):
|
||||
"""Initialize the light."""
|
||||
super().__init__(
|
||||
hass, config=config, fallback_name=object_id, unique_id=unique_id
|
||||
)
|
||||
self.entity_id = async_generate_entity_id(
|
||||
ENTITY_ID_FORMAT, object_id, hass=hass
|
||||
)
|
||||
friendly_name = self._attr_name
|
||||
self._template = config.get(CONF_VALUE_TEMPLATE)
|
||||
self._on_script = Script(hass, config[CONF_ON_ACTION], friendly_name, DOMAIN)
|
||||
self._off_script = Script(hass, config[CONF_OFF_ACTION], friendly_name, DOMAIN)
|
||||
self._level_script = None
|
||||
if (level_action := config.get(CONF_LEVEL_ACTION)) is not None:
|
||||
self._level_script = Script(hass, level_action, friendly_name, DOMAIN)
|
||||
self._level_template = config.get(CONF_LEVEL_TEMPLATE)
|
||||
self._color_handlers: list[ColorModeHandler] = []
|
||||
self._setup_color_handlers(hass, config, friendly_name)
|
||||
self._effect_script = None
|
||||
if (effect_action := config.get(CONF_EFFECT_ACTION)) is not None:
|
||||
self._effect_script = Script(hass, effect_action, friendly_name, DOMAIN)
|
||||
self._effect_list_template = config.get(CONF_EFFECT_LIST_TEMPLATE)
|
||||
self._effect_template = config.get(CONF_EFFECT_TEMPLATE)
|
||||
self._max_mireds_template = config.get(CONF_MAX_MIREDS_TEMPLATE)
|
||||
self._min_mireds_template = config.get(CONF_MIN_MIREDS_TEMPLATE)
|
||||
self._supports_transition_template = config.get(CONF_SUPPORTS_TRANSITION)
|
||||
|
||||
self._state = False
|
||||
self._brightness = None
|
||||
self._temperature = None
|
||||
self._hs_color = None
|
||||
self._rgb_color = None
|
||||
self._rgbw_color = None
|
||||
self._rgbww_color = None
|
||||
self._effect = None
|
||||
self._effect_list = None
|
||||
self._color_mode = None
|
||||
self._max_mireds = None
|
||||
self._min_mireds = None
|
||||
self._supports_transition = False
|
||||
|
||||
supported_color_modes = self.supported_color_modes
|
||||
if len(supported_color_modes) > 1:
|
||||
self._color_mode = ColorMode.UNKNOWN
|
||||
if len(supported_color_modes) == 1:
|
||||
self._color_mode = next(iter(supported_color_modes))
|
||||
|
||||
self._attr_supported_features = LightEntityFeature(0)
|
||||
if self._effect_script is not None:
|
||||
self._attr_supported_features |= LightEntityFeature.EFFECT
|
||||
if self._supports_transition is True:
|
||||
self._attr_supported_features |= LightEntityFeature.TRANSITION
|
||||
|
||||
def _setup_color_handlers(
|
||||
self, hass: HomeAssistant, config: ConfigType, friendly_name: str
|
||||
) -> None:
|
||||
"""Create a handler for every color mode that is configured."""
|
||||
|
||||
def script(*keys: str) -> Script | None:
|
||||
for key in keys:
|
||||
if (action := config.get(key)) is not None:
|
||||
return Script(hass, action, friendly_name, DOMAIN)
|
||||
return None
|
||||
|
||||
def template(*keys: str) -> Template | None:
|
||||
for key in keys:
|
||||
if (tpl := config.get(key)) is not None:
|
||||
return tpl
|
||||
return None
|
||||
|
||||
# (handler, configured action keys, configured template keys).
|
||||
# The legacy color options are an alias of the hs options.
|
||||
candidates = (
|
||||
(
|
||||
ColorModeHandler(
|
||||
color_mode=ColorMode.COLOR_TEMP,
|
||||
service_attr=ATTR_COLOR_TEMP_KELVIN,
|
||||
state_attr="_temperature",
|
||||
property_name="color_temp_kelvin",
|
||||
script=None,
|
||||
template=None,
|
||||
update_callback=self._update_temperature,
|
||||
script_variables=lambda mireds: {"color_temp": mireds},
|
||||
to_state=color_util.color_temperature_kelvin_to_mired,
|
||||
),
|
||||
(CONF_TEMPERATURE_ACTION,),
|
||||
(CONF_TEMPERATURE_TEMPLATE,),
|
||||
),
|
||||
(
|
||||
ColorModeHandler(
|
||||
color_mode=ColorMode.HS,
|
||||
service_attr=ATTR_HS_COLOR,
|
||||
state_attr="_hs_color",
|
||||
property_name="hs_color",
|
||||
script=None,
|
||||
template=None,
|
||||
update_callback=self._update_hs,
|
||||
script_variables=_hs_variables,
|
||||
),
|
||||
(CONF_HS_ACTION, CONF_COLOR_ACTION),
|
||||
(CONF_HS_TEMPLATE, CONF_COLOR_TEMPLATE),
|
||||
),
|
||||
(
|
||||
ColorModeHandler(
|
||||
color_mode=ColorMode.RGB,
|
||||
service_attr=ATTR_RGB_COLOR,
|
||||
state_attr="_rgb_color",
|
||||
property_name="rgb_color",
|
||||
script=None,
|
||||
template=None,
|
||||
update_callback=self._update_rgb,
|
||||
script_variables=_rgb_variables,
|
||||
),
|
||||
(CONF_RGB_ACTION,),
|
||||
(CONF_RGB_TEMPLATE,),
|
||||
),
|
||||
(
|
||||
ColorModeHandler(
|
||||
color_mode=ColorMode.RGBW,
|
||||
service_attr=ATTR_RGBW_COLOR,
|
||||
state_attr="_rgbw_color",
|
||||
property_name="rgbw_color",
|
||||
script=None,
|
||||
template=None,
|
||||
update_callback=self._update_rgbw,
|
||||
script_variables=_rgbw_variables,
|
||||
),
|
||||
(CONF_RGBW_ACTION,),
|
||||
(CONF_RGBW_TEMPLATE,),
|
||||
),
|
||||
(
|
||||
ColorModeHandler(
|
||||
color_mode=ColorMode.RGBWW,
|
||||
service_attr=ATTR_RGBWW_COLOR,
|
||||
state_attr="_rgbww_color",
|
||||
property_name="rgbww_color",
|
||||
script=None,
|
||||
template=None,
|
||||
update_callback=self._update_rgbww,
|
||||
script_variables=_rgbww_variables,
|
||||
),
|
||||
(CONF_RGBWW_ACTION,),
|
||||
(CONF_RGBWW_TEMPLATE,),
|
||||
),
|
||||
)
|
||||
for handler, action_keys, template_keys in candidates:
|
||||
handler.script = script(*action_keys)
|
||||
handler.template = template(*template_keys)
|
||||
if handler.script is not None or handler.template is not None:
|
||||
self._color_handlers.append(handler)
|
||||
|
||||
@property
|
||||
def brightness(self) -> int | None:
|
||||
"""Return the brightness of the light."""
|
||||
return self._brightness
|
||||
|
||||
@property
|
||||
def color_temp_kelvin(self) -> int | None:
|
||||
"""Return the color temperature value in Kelvin."""
|
||||
if self._temperature is None:
|
||||
return None
|
||||
return color_util.color_temperature_mired_to_kelvin(self._temperature)
|
||||
|
||||
@property
|
||||
def min_color_temp_kelvin(self) -> int:
|
||||
"""Return the warmest color_temp_kelvin that this light supports."""
|
||||
if self._max_mireds is not None:
|
||||
return color_util.color_temperature_mired_to_kelvin(self._max_mireds)
|
||||
|
||||
return DEFAULT_MIN_KELVIN
|
||||
|
||||
@property
|
||||
def max_color_temp_kelvin(self) -> int:
|
||||
"""Return the coldest color_temp_kelvin that this light supports."""
|
||||
if self._min_mireds is not None:
|
||||
return color_util.color_temperature_mired_to_kelvin(self._min_mireds)
|
||||
|
||||
return DEFAULT_MAX_KELVIN
|
||||
|
||||
@property
|
||||
def hs_color(self) -> tuple[float, float] | None:
|
||||
"""Return the hue and saturation color value [float, float]."""
|
||||
return self._hs_color
|
||||
|
||||
@property
|
||||
def rgb_color(self) -> tuple[int, int, int] | None:
|
||||
"""Return the rgb color value."""
|
||||
return self._rgb_color
|
||||
|
||||
@property
|
||||
def rgbw_color(self) -> tuple[int, int, int, int] | None:
|
||||
"""Return the rgbw color value."""
|
||||
return self._rgbw_color
|
||||
|
||||
@property
|
||||
def rgbww_color(self) -> tuple[int, int, int, int, int] | None:
|
||||
"""Return the rgbww color value."""
|
||||
return self._rgbww_color
|
||||
|
||||
@property
|
||||
def effect(self) -> str | None:
|
||||
"""Return the effect."""
|
||||
return self._effect
|
||||
|
||||
@property
|
||||
def effect_list(self) -> list[str] | None:
|
||||
"""Return the effect list."""
|
||||
return self._effect_list
|
||||
|
||||
@property
|
||||
def color_mode(self):
|
||||
"""Return current color mode."""
|
||||
return self._color_mode
|
||||
|
||||
@property
|
||||
def supported_color_modes(self):
|
||||
"""Flag supported color modes, derived from the configured handlers."""
|
||||
color_modes = {ColorMode.ONOFF}
|
||||
if self._level_script is not None:
|
||||
color_modes.add(ColorMode.BRIGHTNESS)
|
||||
color_modes.update(
|
||||
handler.color_mode
|
||||
for handler in self._color_handlers
|
||||
if handler.script is not None
|
||||
)
|
||||
return filter_supported_color_modes(color_modes)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if device is on."""
|
||||
return self._state
|
||||
|
||||
@callback
|
||||
def _async_setup_templates(self) -> None:
|
||||
"""Set up templates."""
|
||||
if self._template:
|
||||
self.add_template_attribute(
|
||||
"_state", self._template, None, self._update_state
|
||||
)
|
||||
if self._level_template:
|
||||
self.add_template_attribute(
|
||||
"_brightness",
|
||||
self._level_template,
|
||||
None,
|
||||
self._update_brightness,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._max_mireds_template:
|
||||
self.add_template_attribute(
|
||||
"_max_mireds_template",
|
||||
self._max_mireds_template,
|
||||
None,
|
||||
self._update_max_mireds,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._min_mireds_template:
|
||||
self.add_template_attribute(
|
||||
"_min_mireds_template",
|
||||
self._min_mireds_template,
|
||||
None,
|
||||
self._update_min_mireds,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
for handler in self._color_handlers:
|
||||
if handler.template:
|
||||
self.add_template_attribute(
|
||||
handler.state_attr,
|
||||
handler.template,
|
||||
None,
|
||||
handler.update_callback,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._effect_list_template:
|
||||
self.add_template_attribute(
|
||||
"_effect_list",
|
||||
self._effect_list_template,
|
||||
None,
|
||||
self._update_effect_list,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._effect_template:
|
||||
self.add_template_attribute(
|
||||
"_effect",
|
||||
self._effect_template,
|
||||
None,
|
||||
self._update_effect,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
if self._supports_transition_template:
|
||||
self.add_template_attribute(
|
||||
"_supports_transition_template",
|
||||
self._supports_transition_template,
|
||||
None,
|
||||
self._update_supports_transition,
|
||||
none_on_template_error=True,
|
||||
)
|
||||
super()._async_setup_templates()
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None: # noqa: C901
|
||||
"""Turn the light on."""
|
||||
optimistic_set = False
|
||||
# set optimistic states
|
||||
if self._template is None:
|
||||
self._state = True
|
||||
optimistic_set = True
|
||||
|
||||
if self._level_template is None and ATTR_BRIGHTNESS in kwargs:
|
||||
_LOGGER.debug(
|
||||
"Optimistically setting brightness to %s", kwargs[ATTR_BRIGHTNESS]
|
||||
)
|
||||
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||
optimistic_set = True
|
||||
|
||||
for handler in self._color_handlers:
|
||||
if handler.template is not None or handler.service_attr not in kwargs:
|
||||
continue
|
||||
value = handler.to_state(kwargs[handler.service_attr])
|
||||
_LOGGER.debug(
|
||||
"Optimistically setting %s to %s", handler.property_name, value
|
||||
)
|
||||
self._color_mode = handler.color_mode
|
||||
setattr(self, handler.state_attr, value)
|
||||
# Only one color representation is valid at a time
|
||||
for other in self._color_handlers:
|
||||
if other is not handler and other.template is None:
|
||||
setattr(self, other.state_attr, None)
|
||||
optimistic_set = True
|
||||
|
||||
common_params = {}
|
||||
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
common_params["brightness"] = kwargs[ATTR_BRIGHTNESS]
|
||||
|
||||
if ATTR_TRANSITION in kwargs and self._supports_transition is True:
|
||||
common_params["transition"] = kwargs[ATTR_TRANSITION]
|
||||
|
||||
color_handler = next(
|
||||
(
|
||||
handler
|
||||
for handler in self._color_handlers
|
||||
if handler.script is not None and handler.service_attr in kwargs
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
if (
|
||||
color_handler is not None
|
||||
and color_handler.color_mode == ColorMode.COLOR_TEMP
|
||||
):
|
||||
await self._async_run_color_script(color_handler, kwargs, common_params)
|
||||
elif ATTR_EFFECT in kwargs and self._effect_script:
|
||||
effect = kwargs[ATTR_EFFECT]
|
||||
if effect not in self._effect_list:
|
||||
_LOGGER.error(
|
||||
"Received invalid effect: %s for entity %s. Expected one of: %s",
|
||||
effect,
|
||||
self.entity_id,
|
||||
self._effect_list,
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
common_params["effect"] = effect
|
||||
|
||||
await self.async_run_script(
|
||||
self._effect_script, run_variables=common_params, context=self._context
|
||||
)
|
||||
elif color_handler is not None:
|
||||
await self._async_run_color_script(color_handler, kwargs, common_params)
|
||||
elif ATTR_BRIGHTNESS in kwargs and self._level_script:
|
||||
await self.async_run_script(
|
||||
self._level_script, run_variables=common_params, context=self._context
|
||||
)
|
||||
else:
|
||||
await self.async_run_script(
|
||||
self._on_script, run_variables=common_params, context=self._context
|
||||
)
|
||||
|
||||
if optimistic_set:
|
||||
self.async_write_ha_state()
|
||||
|
||||
async def _async_run_color_script(
|
||||
self,
|
||||
handler: ColorModeHandler,
|
||||
kwargs: dict[str, Any],
|
||||
common_params: dict[str, Any],
|
||||
) -> None:
|
||||
"""Run the script of a color mode handler."""
|
||||
assert handler.script is not None
|
||||
common_params.update(
|
||||
handler.script_variables(handler.to_state(kwargs[handler.service_attr]))
|
||||
)
|
||||
await self.async_run_script(
|
||||
handler.script, run_variables=common_params, context=self._context
|
||||
)
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn the light off."""
|
||||
if ATTR_TRANSITION in kwargs and self._supports_transition is True:
|
||||
await self.async_run_script(
|
||||
self._off_script,
|
||||
run_variables={"transition": kwargs[ATTR_TRANSITION]},
|
||||
context=self._context,
|
||||
)
|
||||
else:
|
||||
await self.async_run_script(self._off_script, context=self._context)
|
||||
if self._template is None:
|
||||
self._state = False
|
||||
self.async_write_ha_state()
|
||||
|
||||
@callback
|
||||
def _update_brightness(self, brightness):
|
||||
"""Update the brightness from the template."""
|
||||
try:
|
||||
if brightness in (None, "None", ""):
|
||||
self._brightness = None
|
||||
return
|
||||
if 0 <= int(brightness) <= 255:
|
||||
self._brightness = int(brightness)
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Received invalid brightness : %s for entity %s. Expected: 0-255",
|
||||
brightness,
|
||||
self.entity_id,
|
||||
)
|
||||
self._brightness = None
|
||||
except ValueError:
|
||||
_LOGGER.exception(
|
||||
"Template must supply an integer brightness from 0-255, or 'None'"
|
||||
)
|
||||
self._brightness = None
|
||||
|
||||
@callback
|
||||
def _update_effect_list(self, effect_list):
|
||||
"""Update the effect list from the template."""
|
||||
if effect_list in (None, "None", ""):
|
||||
self._effect_list = None
|
||||
return
|
||||
|
||||
if not isinstance(effect_list, list):
|
||||
_LOGGER.error(
|
||||
(
|
||||
"Received invalid effect list: %s for entity %s. Expected list of"
|
||||
" strings"
|
||||
),
|
||||
effect_list,
|
||||
self.entity_id,
|
||||
)
|
||||
self._effect_list = None
|
||||
return
|
||||
|
||||
if len(effect_list) == 0:
|
||||
self._effect_list = None
|
||||
return
|
||||
|
||||
self._effect_list = effect_list
|
||||
|
||||
@callback
|
||||
def _update_effect(self, effect):
|
||||
"""Update the effect from the template."""
|
||||
if effect in (None, "None", ""):
|
||||
self._effect = None
|
||||
return
|
||||
|
||||
if effect not in self._effect_list:
|
||||
_LOGGER.error(
|
||||
"Received invalid effect: %s for entity %s. Expected one of: %s",
|
||||
effect,
|
||||
self.entity_id,
|
||||
self._effect_list,
|
||||
)
|
||||
self._effect = None
|
||||
return
|
||||
|
||||
self._effect = effect
|
||||
|
||||
@callback
|
||||
def _update_state(self, result):
|
||||
"""Update the state from the template."""
|
||||
if isinstance(result, TemplateError):
|
||||
# This behavior is legacy
|
||||
self._state = False
|
||||
if not self._availability_template:
|
||||
self._attr_available = True
|
||||
return
|
||||
|
||||
if isinstance(result, bool):
|
||||
self._state = result
|
||||
return
|
||||
|
||||
state = str(result).lower()
|
||||
if state in _VALID_STATES:
|
||||
self._state = state in ("true", STATE_ON)
|
||||
return
|
||||
|
||||
_LOGGER.error(
|
||||
"Received invalid light is_on state: %s for entity %s. Expected: %s",
|
||||
state,
|
||||
self.entity_id,
|
||||
", ".join(_VALID_STATES),
|
||||
)
|
||||
self._state = None
|
||||
|
||||
@callback
|
||||
def _update_temperature(self, render):
|
||||
"""Update the temperature from the template."""
|
||||
try:
|
||||
if render in (None, "None", ""):
|
||||
self._temperature = None
|
||||
return
|
||||
temperature = int(render)
|
||||
min_mireds = self._min_mireds or DEFAULT_MIN_MIREDS
|
||||
max_mireds = self._max_mireds or DEFAULT_MAX_MIREDS
|
||||
if min_mireds <= temperature <= max_mireds:
|
||||
self._temperature = temperature
|
||||
else:
|
||||
_LOGGER.error(
|
||||
(
|
||||
"Received invalid color temperature : %s for entity %s."
|
||||
" Expected: %s-%s"
|
||||
),
|
||||
temperature,
|
||||
self.entity_id,
|
||||
min_mireds,
|
||||
max_mireds,
|
||||
)
|
||||
self._temperature = None
|
||||
except ValueError:
|
||||
_LOGGER.exception(
|
||||
"Template must supply an integer temperature within the range for"
|
||||
" this light, or 'None'"
|
||||
)
|
||||
self._temperature = None
|
||||
self._color_mode = ColorMode.COLOR_TEMP
|
||||
|
||||
@callback
|
||||
def _update_hs(self, render):
|
||||
"""Update the color from the template."""
|
||||
if render is None:
|
||||
self._hs_color = None
|
||||
return
|
||||
|
||||
h_str = s_str = None
|
||||
if isinstance(render, str):
|
||||
if render in ("None", ""):
|
||||
self._hs_color = None
|
||||
return
|
||||
h_str, s_str = map(
|
||||
float, render.replace("(", "").replace(")", "").split(",", 1)
|
||||
)
|
||||
elif isinstance(render, (list, tuple)) and len(render) == 2:
|
||||
h_str, s_str = render
|
||||
|
||||
if (
|
||||
h_str is not None
|
||||
and s_str is not None
|
||||
and isinstance(h_str, (int, float))
|
||||
and isinstance(s_str, (int, float))
|
||||
and 0 <= h_str <= 360
|
||||
and 0 <= s_str <= 100
|
||||
):
|
||||
self._hs_color = (h_str, s_str)
|
||||
elif h_str is not None and s_str is not None:
|
||||
_LOGGER.error(
|
||||
(
|
||||
"Received invalid hs_color : (%s, %s) for entity %s. Expected:"
|
||||
" (0-360, 0-100)"
|
||||
),
|
||||
h_str,
|
||||
s_str,
|
||||
self.entity_id,
|
||||
)
|
||||
self._hs_color = None
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Received invalid hs_color : (%s) for entity %s", render, self.entity_id
|
||||
)
|
||||
self._hs_color = None
|
||||
self._color_mode = ColorMode.HS
|
||||
|
||||
@callback
|
||||
def _update_rgb(self, render):
|
||||
"""Update the color from the template."""
|
||||
if render is None:
|
||||
self._rgb_color = None
|
||||
return
|
||||
|
||||
r_int = g_int = b_int = None
|
||||
if isinstance(render, str):
|
||||
if render in ("None", ""):
|
||||
self._rgb_color = None
|
||||
return
|
||||
cleanup_char = ["(", ")", "[", "]", " "]
|
||||
for char in cleanup_char:
|
||||
render = render.replace(char, "")
|
||||
r_int, g_int, b_int = map(int, render.split(",", 3))
|
||||
elif isinstance(render, (list, tuple)) and len(render) == 3:
|
||||
r_int, g_int, b_int = render
|
||||
|
||||
if all(
|
||||
value is not None and isinstance(value, (int, float)) and 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int)
|
||||
):
|
||||
self._rgb_color = (r_int, g_int, b_int)
|
||||
elif any(
|
||||
isinstance(value, (int, float)) and not 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int)
|
||||
):
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s, %s, %s) for entity %s. Expected: (0-255, 0-255, 0-255)",
|
||||
r_int,
|
||||
g_int,
|
||||
b_int,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgb_color = None
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s) for entity %s",
|
||||
render,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgb_color = None
|
||||
self._color_mode = ColorMode.RGB
|
||||
|
||||
@callback
|
||||
def _update_rgbw(self, render):
|
||||
"""Update the color from the template."""
|
||||
if render is None:
|
||||
self._rgbw_color = None
|
||||
return
|
||||
|
||||
r_int = g_int = b_int = w_int = None
|
||||
if isinstance(render, str):
|
||||
if render in ("None", ""):
|
||||
self._rgb_color = None
|
||||
return
|
||||
cleanup_char = ["(", ")", "[", "]", " "]
|
||||
for char in cleanup_char:
|
||||
render = render.replace(char, "")
|
||||
r_int, g_int, b_int, w_int = map(int, render.split(",", 4))
|
||||
elif isinstance(render, (list, tuple)) and len(render) == 4:
|
||||
r_int, g_int, b_int, w_int = render
|
||||
|
||||
if all(
|
||||
value is not None and isinstance(value, (int, float)) and 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int, w_int)
|
||||
):
|
||||
self._rgbw_color = (r_int, g_int, b_int, w_int)
|
||||
elif any(
|
||||
isinstance(value, (int, float)) and not 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int, w_int)
|
||||
):
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s, %s, %s, %s) for entity %s. Expected: (0-255, 0-255, 0-255, 0-255)",
|
||||
r_int,
|
||||
g_int,
|
||||
b_int,
|
||||
w_int,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgbw_color = None
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s) for entity %s",
|
||||
render,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgbw_color = None
|
||||
self._color_mode = ColorMode.RGBW
|
||||
|
||||
@callback
|
||||
def _update_rgbww(self, render):
|
||||
"""Update the color from the template."""
|
||||
if render is None:
|
||||
self._rgbww_color = None
|
||||
return
|
||||
|
||||
r_int = g_int = b_int = cw_int = ww_int = None
|
||||
if isinstance(render, str):
|
||||
if render in ("None", ""):
|
||||
self._rgb_color = None
|
||||
return
|
||||
cleanup_char = ["(", ")", "[", "]", " "]
|
||||
for char in cleanup_char:
|
||||
render = render.replace(char, "")
|
||||
r_int, g_int, b_int, cw_int, ww_int = map(int, render.split(",", 5))
|
||||
elif isinstance(render, (list, tuple)) and len(render) == 5:
|
||||
r_int, g_int, b_int, cw_int, ww_int = render
|
||||
|
||||
if all(
|
||||
value is not None and isinstance(value, (int, float)) and 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int, cw_int, ww_int)
|
||||
):
|
||||
self._rgbww_color = (r_int, g_int, b_int, cw_int, ww_int)
|
||||
elif any(
|
||||
isinstance(value, (int, float)) and not 0 <= value <= 255
|
||||
for value in (r_int, g_int, b_int, cw_int, ww_int)
|
||||
):
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s, %s, %s, %s, %s) for entity %s. Expected: (0-255, 0-255, 0-255, 0-255)",
|
||||
r_int,
|
||||
g_int,
|
||||
b_int,
|
||||
cw_int,
|
||||
ww_int,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgbww_color = None
|
||||
else:
|
||||
_LOGGER.error(
|
||||
"Received invalid rgb_color : (%s) for entity %s",
|
||||
render,
|
||||
self.entity_id,
|
||||
)
|
||||
self._rgbww_color = None
|
||||
self._color_mode = ColorMode.RGBWW
|
||||
|
||||
@callback
|
||||
def _update_max_mireds(self, render):
|
||||
"""Update the max mireds from the template."""
|
||||
|
||||
try:
|
||||
if render in (None, "None", ""):
|
||||
self._max_mireds = None
|
||||
return
|
||||
self._max_mireds = int(render)
|
||||
except ValueError:
|
||||
_LOGGER.exception(
|
||||
"Template must supply an integer temperature within the range for"
|
||||
" this light, or 'None'"
|
||||
)
|
||||
self._max_mireds = None
|
||||
|
||||
@callback
|
||||
def _update_min_mireds(self, render):
|
||||
"""Update the min mireds from the template."""
|
||||
try:
|
||||
if render in (None, "None", ""):
|
||||
self._min_mireds = None
|
||||
return
|
||||
self._min_mireds = int(render)
|
||||
except ValueError:
|
||||
_LOGGER.exception(
|
||||
"Template must supply an integer temperature within the range for"
|
||||
" this light, or 'None'"
|
||||
)
|
||||
self._min_mireds = None
|
||||
|
||||
@callback
|
||||
def _update_supports_transition(self, render):
|
||||
"""Update the supports transition from the template."""
|
||||
if render in (None, "None", ""):
|
||||
self._supports_transition = False
|
||||
return
|
||||
self._attr_supported_features &= LightEntityFeature.EFFECT
|
||||
self._supports_transition = bool(render)
|
||||
if self._supports_transition:
|
||||
self._attr_supported_features |= LightEntityFeature.TRANSITION
|
||||
0
sources/FinalPrompt.md
Normal file
0
sources/FinalPrompt.md
Normal file
@@ -11,12 +11,20 @@ Create a `ColorModeHandler` dataclass/protocol that encapsulates per-mode config
|
||||
## Suggested 5-Turn Plan
|
||||
|
||||
### Turn 1: Define ColorModeHandler and Refactor __init__
|
||||
Original instructions
|
||||
- Create `ColorModeHandler` dataclass with fields: `color_mode`, `script_attr`, `template_attr`, `state_attr`, `property_name`, `update_callback`, `validation_fn`, `turn_on_priority`, `script_param_builder`
|
||||
- In `__init__`, build a list `self._color_mode_handlers` by checking which scripts/templates are configured
|
||||
- Replace individual `_rgb_script`, `_hs_script`, etc. with handler-based lookup
|
||||
- Compute `supported_color_modes` from handlers dynamically
|
||||
|
||||
Actual instructions
|
||||
We're refactoring the component light.py working to reduce the duplication. Create `ColorModeHandler` data class (a DTO) with fields like: color_mode, script_attr, state_attr, property_name and others you deem needed.
|
||||
In `__init__` create a list of color_mode handlers by checking which scripts or template are configured.
|
||||
Replace `_rgb_script`, `hs_script`, etc. with handler-based lookups.
|
||||
Finally, compute `supported_color_modes` from the handlers dynamically.
|
||||
|
||||
### Turn 2: Refactor _async_setup_templates
|
||||
This is effectively complete - do not implement
|
||||
- Replace 8 separate `if self._xxx_template:` blocks with a loop over `self._color_mode_handlers`
|
||||
- Each handler registers its template with `add_template_attribute` using its update callback
|
||||
- Handle special cases (min/max mireds, transition, effect_list) separately or as additional handlers
|
||||
|
||||
Reference in New Issue
Block a user