From f36ee88a878239cf02ecad6e657ee38a073066d6 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Thu, 22 May 2025 01:23:44 -0400 Subject: [PATCH] Clean up AbstractTemplateEntity (#145409) Clean up abstract templates --- homeassistant/components/template/light.py | 28 +++++++++---------- .../components/template/template_entity.py | 2 +- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/template/light.py b/homeassistant/components/template/light.py index 3b64cca26b4..ac751d46cf7 100644 --- a/homeassistant/components/template/light.py +++ b/homeassistant/components/template/light.py @@ -50,6 +50,7 @@ from homeassistant.util import color as color_util from . import TriggerUpdateCoordinator from .const import CONF_OBJECT_ID, CONF_PICTURE, DOMAIN +from .entity import AbstractTemplateEntity from .template_entity import ( LEGACY_FIELDS as TEMPLATE_ENTITY_LEGACY_FIELDS, TEMPLATE_ENTITY_AVAILABILITY_SCHEMA, @@ -272,16 +273,14 @@ async def async_setup_platform( ) -class AbstractTemplateLight(LightEntity): +class AbstractTemplateLight(AbstractTemplateEntity, LightEntity): """Representation of a template lights features.""" - def __init__( + def __init__( # pylint: disable=super-init-not-called self, config: dict[str, Any], initial_state: bool | None = False ) -> None: """Initialize the features.""" - self._registered_scripts: list[str] = [] - # Template attributes self._template = config.get(CONF_STATE) self._level_template = config.get(CONF_LEVEL) @@ -312,7 +311,7 @@ class AbstractTemplateLight(LightEntity): self._color_mode: ColorMode | None = None self._supported_color_modes: set[ColorMode] | None = None - def _register_scripts( + def _iterate_scripts( self, config: dict[str, Any] ) -> Generator[tuple[str, Sequence[dict[str, Any]], ColorMode | None]]: for action_id, color_mode in ( @@ -327,7 +326,6 @@ class AbstractTemplateLight(LightEntity): (CONF_RGBWW_ACTION, ColorMode.RGBWW), ): if (action_config := config.get(action_id)) is not None: - self._registered_scripts.append(action_id) yield (action_id, action_config, color_mode) @property @@ -522,7 +520,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_COLOR_TEMP_KELVIN in kwargs - and (script := CONF_TEMPERATURE_ACTION) in self._registered_scripts + and (script := CONF_TEMPERATURE_ACTION) in self._action_scripts ): common_params["color_temp"] = color_util.color_temperature_kelvin_to_mired( kwargs[ATTR_COLOR_TEMP_KELVIN] @@ -532,7 +530,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_EFFECT in kwargs - and (script := CONF_EFFECT_ACTION) in self._registered_scripts + and (script := CONF_EFFECT_ACTION) in self._action_scripts ): assert self._effect_list is not None effect = kwargs[ATTR_EFFECT] @@ -551,7 +549,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_HS_COLOR in kwargs - and (script := CONF_HS_ACTION) in self._registered_scripts + and (script := CONF_HS_ACTION) in self._action_scripts ): hs_value = kwargs[ATTR_HS_COLOR] common_params["hs"] = hs_value @@ -562,7 +560,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_RGBWW_COLOR in kwargs - and (script := CONF_RGBWW_ACTION) in self._registered_scripts + and (script := CONF_RGBWW_ACTION) in self._action_scripts ): rgbww_value = kwargs[ATTR_RGBWW_COLOR] common_params["rgbww"] = rgbww_value @@ -581,7 +579,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_RGBW_COLOR in kwargs - and (script := CONF_RGBW_ACTION) in self._registered_scripts + and (script := CONF_RGBW_ACTION) in self._action_scripts ): rgbw_value = kwargs[ATTR_RGBW_COLOR] common_params["rgbw"] = rgbw_value @@ -599,7 +597,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_RGB_COLOR in kwargs - and (script := CONF_RGB_ACTION) in self._registered_scripts + and (script := CONF_RGB_ACTION) in self._action_scripts ): rgb_value = kwargs[ATTR_RGB_COLOR] common_params["rgb"] = rgb_value @@ -611,7 +609,7 @@ class AbstractTemplateLight(LightEntity): if ( ATTR_BRIGHTNESS in kwargs - and (script := CONF_LEVEL_ACTION) in self._registered_scripts + and (script := CONF_LEVEL_ACTION) in self._action_scripts ): return (script, common_params) @@ -966,7 +964,7 @@ class LightTemplate(TemplateEntity, AbstractTemplateLight): assert name is not None color_modes = {ColorMode.ONOFF} - for action_id, action_config, color_mode in self._register_scripts(config): + for action_id, action_config, color_mode in self._iterate_scripts(config): self.add_script(action_id, action_config, name, DOMAIN) if color_mode: color_modes.add(color_mode) @@ -1180,7 +1178,7 @@ class TriggerLightEntity(TriggerEntity, AbstractTemplateLight): self._parse_result.add(key) color_modes = {ColorMode.ONOFF} - for action_id, action_config, color_mode in self._register_scripts(config): + for action_id, action_config, color_mode in self._iterate_scripts(config): self.add_script(action_id, action_config, name, DOMAIN) if color_mode: color_modes.add(color_mode) diff --git a/homeassistant/components/template/template_entity.py b/homeassistant/components/template/template_entity.py index 41ebf5bc1be..f879c60ed9e 100644 --- a/homeassistant/components/template/template_entity.py +++ b/homeassistant/components/template/template_entity.py @@ -280,7 +280,7 @@ class TemplateEntity(AbstractTemplateEntity): unique_id: str | None = None, ) -> None: """Template Entity.""" - super().__init__(hass) + AbstractTemplateEntity.__init__(self, hass) self._template_attrs: dict[Template, list[_TemplateAttribute]] = {} self._template_result_info: TrackTemplateResultInfo | None = None self._attr_extra_state_attributes = {}