Minor refactor of template light (#61857)

This commit is contained in:
Erik Montnemery 2021-12-16 16:11:23 +01:00 committed by GitHub
parent 1fb69fb69a
commit a0b6edc894
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,9 +21,7 @@ from homeassistant.components.light import (
) )
from homeassistant.const import ( from homeassistant.const import (
CONF_ENTITY_ID, CONF_ENTITY_ID,
CONF_ENTITY_PICTURE_TEMPLATE,
CONF_FRIENDLY_NAME, CONF_FRIENDLY_NAME,
CONF_ICON_TEMPLATE,
CONF_LIGHTS, CONF_LIGHTS,
CONF_UNIQUE_ID, CONF_UNIQUE_ID,
CONF_VALUE_TEMPLATE, 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.entity import async_generate_entity_id
from homeassistant.helpers.script import Script from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE, DOMAIN from .const import DOMAIN
from .template_entity import TemplateEntity from .template_entity import (
TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
TemplateEntity,
rewrite_common_legacy_to_modern_conf,
)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"] _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_ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA, vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template, 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_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_LEVEL_TEMPLATE): cv.template, vol.Optional(CONF_LEVEL_TEMPLATE): cv.template,
vol.Optional(CONF_FRIENDLY_NAME): cv.string, 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_SUPPORTS_TRANSITION): cv.template,
vol.Optional(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_UNIQUE_ID): cv.string,
} }
), ).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
) )
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -100,64 +99,15 @@ async def _async_create_entities(hass, config):
"""Create the Template Lights.""" """Create the Template Lights."""
lights = [] lights = []
for device, device_config in config[CONF_LIGHTS].items(): for object_id, entity_config in config[CONF_LIGHTS].items():
friendly_name = device_config.get(CONF_FRIENDLY_NAME, device) entity_config = rewrite_common_legacy_to_modern_conf(entity_config)
unique_id = entity_config.get(CONF_UNIQUE_ID)
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)
lights.append( lights.append(
LightTemplate( LightTemplate(
hass, hass,
device, object_id,
friendly_name, entity_config,
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,
unique_id, unique_id,
) )
) )
@ -176,71 +126,47 @@ class LightTemplate(TemplateEntity, LightEntity):
def __init__( def __init__(
self, self,
hass, hass,
device_id, object_id,
friendly_name, config,
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,
unique_id, unique_id,
): ):
"""Initialize the light.""" """Initialize the light."""
super().__init__( super().__init__(config=config)
availability_template=availability_template,
icon_template=icon_template,
entity_picture_template=entity_picture_template,
)
self.entity_id = async_generate_entity_id( 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._name = friendly_name = config.get(CONF_FRIENDLY_NAME, object_id)
self._template = state_template self._template = config.get(CONF_VALUE_TEMPLATE)
self._on_script = Script(hass, on_action, friendly_name, DOMAIN) self._on_script = Script(hass, config[CONF_ON_ACTION], friendly_name, DOMAIN)
self._off_script = Script(hass, off_action, friendly_name, DOMAIN) self._off_script = Script(hass, config[CONF_OFF_ACTION], friendly_name, DOMAIN)
self._level_script = None 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_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 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( self._temperature_script = Script(
hass, temperature_action, friendly_name, DOMAIN hass, temperature_action, friendly_name, DOMAIN
) )
self._temperature_template = temperature_template self._temperature_template = config.get(CONF_TEMPERATURE_TEMPLATE)
self._color_script = None 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_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 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( self._white_value_script = Script(
hass, white_value_action, friendly_name, DOMAIN 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 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_script = Script(hass, effect_action, friendly_name, DOMAIN)
self._effect_list_template = effect_list_template self._effect_list_template = config.get(CONF_EFFECT_LIST_TEMPLATE)
self._effect_template = effect_template self._effect_template = config.get(CONF_EFFECT_TEMPLATE)
self._max_mireds_template = max_mireds_template self._max_mireds_template = config.get(CONF_MAX_MIREDS_TEMPLATE)
self._min_mireds_template = min_mireds_template self._min_mireds_template = config.get(CONF_MIN_MIREDS_TEMPLATE)
self._supports_transition_template = supports_transition_template self._supports_transition_template = config.get(CONF_SUPPORTS_TRANSITION)
self._state = False self._state = False
self._brightness = None self._brightness = None