1 """Support for switches which integrates with other components."""
3 from __future__
import annotations
7 import voluptuous
as vol
11 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
36 from .const
import CONF_TURN_OFF, CONF_TURN_ON, DOMAIN
37 from .template_entity
import (
38 TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY,
40 rewrite_common_legacy_to_modern_conf,
43 _VALID_STATES = [STATE_ON, STATE_OFF,
"true",
"false"]
45 SWITCH_SCHEMA = vol.All(
46 cv.deprecated(ATTR_ENTITY_ID),
49 vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
50 vol.Required(CONF_TURN_ON): cv.SCRIPT_SCHEMA,
51 vol.Required(CONF_TURN_OFF): cv.SCRIPT_SCHEMA,
52 vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
53 vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
54 vol.Optional(CONF_UNIQUE_ID): cv.string,
56 ).extend(TEMPLATE_ENTITY_COMMON_SCHEMA_LEGACY.schema),
59 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
60 {vol.Required(CONF_SWITCHES): cv.schema_with_slug_keys(SWITCH_SCHEMA)}
63 SWITCH_CONFIG_SCHEMA = vol.Schema(
65 vol.Required(CONF_NAME): cv.template,
66 vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
67 vol.Optional(CONF_TURN_ON): cv.SCRIPT_SCHEMA,
68 vol.Optional(CONF_TURN_OFF): cv.SCRIPT_SCHEMA,
69 vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(),
75 """Create the Template switches."""
78 for object_id, entity_config
in config[CONF_SWITCHES].items():
80 unique_id = entity_config.get(CONF_UNIQUE_ID)
97 async_add_entities: AddEntitiesCallback,
98 discovery_info: DiscoveryInfoType |
None =
None,
100 """Set up the template switches."""
106 config_entry: ConfigEntry,
107 async_add_entities: AddEntitiesCallback,
109 """Initialize config entry."""
110 _options =
dict(config_entry.options)
111 _options.pop(
"template_type")
114 [
SwitchTemplate(hass,
None, validated_config, config_entry.entry_id)]
120 hass: HomeAssistant, name: str, config: dict[str, Any]
122 """Create a preview switch."""
128 """Representation of a Template switch."""
130 _attr_should_poll =
False
139 """Initialize the Template switch."""
141 hass, config=config, fallback_name=object_id, unique_id=unique_id
143 if object_id
is not None:
145 ENTITY_ID_FORMAT, object_id, hass=hass
148 self.
_template_template = config.get(CONF_VALUE_TEMPLATE)
150 Script(hass, config.get(CONF_TURN_ON), friendly_name, DOMAIN)
151 if config.get(CONF_TURN_ON)
is not None
155 Script(hass, config.get(CONF_TURN_OFF), friendly_name, DOMAIN)
156 if config.get(CONF_TURN_OFF)
is not None
159 self.
_state_state: bool |
None =
False
163 config.get(CONF_DEVICE_ID),
169 if isinstance(result, TemplateError):
173 if isinstance(result, bool):
174 self.
_state_state = result
177 if isinstance(result, str):
178 self.
_state_state = result.lower()
in (
"true", STATE_ON)
184 """Register callbacks."""
189 self.
_state_state = state.state == STATE_ON
194 """Set up templates."""
204 """Return true if device is on."""
208 """Fire the on action."""
216 """Fire the off action."""
None _async_setup_templates(self)
def __init__(self, hass, object_id, config, unique_id)
None async_turn_off(self, **Any kwargs)
def _update_state(self, result)
None async_turn_on(self, **Any kwargs)
None async_added_to_hass(self)
None _update_state(self, str|TemplateError result)
None async_run_script(self, Script script, *_VarsType|None run_variables=None, Context|None context=None)
None add_template_attribute(self, str attribute, Template template, Callable[[Any], Any]|None validator=None, Callable[[Any], None]|None on_update=None, bool none_on_template_error=False)
None async_write_ha_state(self)
State|None async_get_last_state(self)
def _async_create_entities(hass, config)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
SwitchTemplate async_create_preview_switch(HomeAssistant hass, str name, dict[str, Any] config)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
dict[str, Any] rewrite_common_legacy_to_modern_conf(HomeAssistant hass, dict[str, Any] entity_cfg, dict[str, str]|None extra_legacy_fields=None)
dr.DeviceInfo|None async_device_info_to_link_from_device_id(HomeAssistant hass, str|None device_id)
str async_generate_entity_id(str entity_id_format, str|None name, Iterable[str]|None current_ids=None, HomeAssistant|None hass=None)