mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Minor refactor of template switch (#61859)
This commit is contained in:
parent
597045149f
commit
6acf45566e
@ -10,8 +10,6 @@ from homeassistant.components.switch import (
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
ATTR_FRIENDLY_NAME,
|
||||
CONF_ENTITY_PICTURE_TEMPLATE,
|
||||
CONF_ICON_TEMPLATE,
|
||||
CONF_SWITCHES,
|
||||
CONF_UNIQUE_ID,
|
||||
CONF_VALUE_TEMPLATE,
|
||||
@ -25,8 +23,12 @@ from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
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,
|
||||
)
|
||||
|
||||
_VALID_STATES = [STATE_ON, STATE_OFF, "true", "false"]
|
||||
|
||||
@ -38,16 +40,13 @@ SWITCH_SCHEMA = vol.All(
|
||||
vol.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.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Required(OFF_ACTION): cv.SCRIPT_SCHEMA,
|
||||
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
}
|
||||
),
|
||||
).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
@ -59,27 +58,15 @@ async def _async_create_entities(hass, config):
|
||||
"""Create the Template switches."""
|
||||
switches = []
|
||||
|
||||
for device, device_config in config[CONF_SWITCHES].items():
|
||||
friendly_name = device_config.get(ATTR_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)
|
||||
on_action = device_config[ON_ACTION]
|
||||
off_action = device_config[OFF_ACTION]
|
||||
unique_id = device_config.get(CONF_UNIQUE_ID)
|
||||
for object_id, entity_config in config[CONF_SWITCHES].items():
|
||||
entity_config = rewrite_common_legacy_to_modern_conf(entity_config)
|
||||
unique_id = entity_config.get(CONF_UNIQUE_ID)
|
||||
|
||||
switches.append(
|
||||
SwitchTemplate(
|
||||
hass,
|
||||
device,
|
||||
friendly_name,
|
||||
state_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
on_action,
|
||||
off_action,
|
||||
object_id,
|
||||
entity_config,
|
||||
unique_id,
|
||||
)
|
||||
)
|
||||
@ -98,29 +85,19 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
|
||||
def __init__(
|
||||
self,
|
||||
hass,
|
||||
device_id,
|
||||
friendly_name,
|
||||
state_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
on_action,
|
||||
off_action,
|
||||
object_id,
|
||||
config,
|
||||
unique_id,
|
||||
):
|
||||
"""Initialize the Template switch."""
|
||||
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(ATTR_FRIENDLY_NAME, object_id)
|
||||
self._template = config.get(CONF_VALUE_TEMPLATE)
|
||||
self._on_script = Script(hass, config[ON_ACTION], friendly_name, DOMAIN)
|
||||
self._off_script = Script(hass, config[OFF_ACTION], friendly_name, DOMAIN)
|
||||
self._state = False
|
||||
self._unique_id = unique_id
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user