Minor refactor of template light (#61857)
parent
1fb69fb69a
commit
a0b6edc894
|
@ -21,9 +21,7 @@ from homeassistant.components.light import (
|
|||
)
|
||||
from homeassistant.const import (
|
||||
CONF_ENTITY_ID,
|
||||
CONF_ENTITY_PICTURE_TEMPLATE,
|
||||
CONF_FRIENDLY_NAME,
|
||||
CONF_ICON_TEMPLATE,
|
||||
CONF_LIGHTS,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
|
@ -37,8 +35,12 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
|
|||
from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.helpers.script import Script
|
||||
|
||||
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN
|
||||
from .template_entity import TemplateEntity
|
||||
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"]
|
||||
|
@ -67,9 +69,6 @@ LIGHT_SCHEMA = vol.All(
|
|||
vol.Required(CONF_ON_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_LEVEL_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(CONF_LEVEL_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_FRIENDLY_NAME): cv.string,
|
||||
|
@ -88,7 +87,7 @@ LIGHT_SCHEMA = vol.All(
|
|||
vol.Optional(CONF_SUPPORTS_TRANSITION): cv.template,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
}
|
||||
),
|
||||
).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
|
@ -100,64 +99,15 @@ async def _async_create_entities(hass, config):
|
|||
"""Create the Template Lights."""
|
||||
lights = []
|
||||
|
||||
for device, device_config in config[CONF_LIGHTS].items():
|
||||
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device)
|
||||
|
||||
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
||||
icon_template = device_config.get(CONF_ICON_TEMPLATE)
|
||||
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
|
||||
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
|
||||
unique_id = device_config.get(CONF_UNIQUE_ID)
|
||||
|
||||
on_action = device_config[CONF_ON_ACTION]
|
||||
off_action = device_config[CONF_OFF_ACTION]
|
||||
|
||||
level_action = device_config.get(CONF_LEVEL_ACTION)
|
||||
level_template = device_config.get(CONF_LEVEL_TEMPLATE)
|
||||
|
||||
temperature_action = device_config.get(CONF_TEMPERATURE_ACTION)
|
||||
temperature_template = device_config.get(CONF_TEMPERATURE_TEMPLATE)
|
||||
|
||||
color_action = device_config.get(CONF_COLOR_ACTION)
|
||||
color_template = device_config.get(CONF_COLOR_TEMPLATE)
|
||||
|
||||
white_value_action = device_config.get(CONF_WHITE_VALUE_ACTION)
|
||||
white_value_template = device_config.get(CONF_WHITE_VALUE_TEMPLATE)
|
||||
|
||||
effect_action = device_config.get(CONF_EFFECT_ACTION)
|
||||
effect_list_template = device_config.get(CONF_EFFECT_LIST_TEMPLATE)
|
||||
effect_template = device_config.get(CONF_EFFECT_TEMPLATE)
|
||||
|
||||
max_mireds_template = device_config.get(CONF_MAX_MIREDS_TEMPLATE)
|
||||
min_mireds_template = device_config.get(CONF_MIN_MIREDS_TEMPLATE)
|
||||
|
||||
supports_transition_template = device_config.get(CONF_SUPPORTS_TRANSITION)
|
||||
for object_id, entity_config in config[CONF_LIGHTS].items():
|
||||
entity_config = rewrite_common_legacy_to_modern_conf(entity_config)
|
||||
unique_id = entity_config.get(CONF_UNIQUE_ID)
|
||||
|
||||
lights.append(
|
||||
LightTemplate(
|
||||
hass,
|
||||
device,
|
||||
friendly_name,
|
||||
state_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
on_action,
|
||||
off_action,
|
||||
level_action,
|
||||
level_template,
|
||||
temperature_action,
|
||||
temperature_template,
|
||||
color_action,
|
||||
color_template,
|
||||
white_value_action,
|
||||
white_value_template,
|
||||
effect_action,
|
||||
effect_list_template,
|
||||
effect_template,
|
||||
max_mireds_template,
|
||||
min_mireds_template,
|
||||
supports_transition_template,
|
||||
object_id,
|
||||
entity_config,
|
||||
unique_id,
|
||||
)
|
||||
)
|
||||
|
@ -176,71 +126,47 @@ class LightTemplate(TemplateEntity, LightEntity):
|
|||
def __init__(
|
||||
self,
|
||||
hass,
|
||||
device_id,
|
||||
friendly_name,
|
||||
state_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
on_action,
|
||||
off_action,
|
||||
level_action,
|
||||
level_template,
|
||||
temperature_action,
|
||||
temperature_template,
|
||||
color_action,
|
||||
color_template,
|
||||
white_value_action,
|
||||
white_value_template,
|
||||
effect_action,
|
||||
effect_list_template,
|
||||
effect_template,
|
||||
max_mireds_template,
|
||||
min_mireds_template,
|
||||
supports_transition_template,
|
||||
object_id,
|
||||
config,
|
||||
unique_id,
|
||||
):
|
||||
"""Initialize the light."""
|
||||
super().__init__(
|
||||
availability_template=availability_template,
|
||||
icon_template=icon_template,
|
||||
entity_picture_template=entity_picture_template,
|
||||
)
|
||||
super().__init__(config=config)
|
||||
self.entity_id = async_generate_entity_id(
|
||||
ENTITY_ID_FORMAT, device_id, hass=hass
|
||||
ENTITY_ID_FORMAT, object_id, hass=hass
|
||||
)
|
||||
self._name = friendly_name
|
||||
self._template = state_template
|
||||
self._on_script = Script(hass, on_action, friendly_name, DOMAIN)
|
||||
self._off_script = Script(hass, off_action, friendly_name, DOMAIN)
|
||||
self._name = friendly_name = config.get(CONF_FRIENDLY_NAME, object_id)
|
||||
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 is not 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 = level_template
|
||||
self._level_template = config.get(CONF_LEVEL_TEMPLATE)
|
||||
self._temperature_script = None
|
||||
if temperature_action is not None:
|
||||
if (temperature_action := config.get(CONF_TEMPERATURE_ACTION)) is not None:
|
||||
self._temperature_script = Script(
|
||||
hass, temperature_action, friendly_name, DOMAIN
|
||||
)
|
||||
self._temperature_template = temperature_template
|
||||
self._temperature_template = config.get(CONF_TEMPERATURE_TEMPLATE)
|
||||
self._color_script = None
|
||||
if color_action is not None:
|
||||
if (color_action := config.get(CONF_COLOR_ACTION)) is not None:
|
||||
self._color_script = Script(hass, color_action, friendly_name, DOMAIN)
|
||||
self._color_template = color_template
|
||||
self._color_template = config.get(CONF_COLOR_TEMPLATE)
|
||||
self._white_value_script = None
|
||||
if white_value_action is not None:
|
||||
if (white_value_action := config.get(CONF_WHITE_VALUE_ACTION)) is not None:
|
||||
self._white_value_script = Script(
|
||||
hass, white_value_action, friendly_name, DOMAIN
|
||||
)
|
||||
self._white_value_template = white_value_template
|
||||
self._white_value_template = config.get(CONF_WHITE_VALUE_TEMPLATE)
|
||||
self._effect_script = None
|
||||
if effect_action is not 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 = effect_list_template
|
||||
self._effect_template = effect_template
|
||||
self._max_mireds_template = max_mireds_template
|
||||
self._min_mireds_template = min_mireds_template
|
||||
self._supports_transition_template = supports_transition_template
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue