1 """Support for selects which integrates with other components."""
3 from __future__
import annotations
8 import voluptuous
as vol
13 DOMAIN
as SELECT_DOMAIN,
31 from .
import TriggerUpdateCoordinator
32 from .const
import DOMAIN
33 from .template_entity
import (
34 TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
35 TEMPLATE_ENTITY_ICON_SCHEMA,
38 from .trigger_entity
import TriggerEntity
40 _LOGGER = logging.getLogger(__name__)
42 CONF_OPTIONS =
"options"
43 CONF_SELECT_OPTION =
"select_option"
45 DEFAULT_NAME =
"Template Select"
46 DEFAULT_OPTIMISTIC =
False
51 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
52 vol.Required(CONF_STATE): cv.template,
53 vol.Required(CONF_SELECT_OPTION): cv.SCRIPT_SCHEMA,
54 vol.Required(ATTR_OPTIONS): cv.template,
55 vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
56 vol.Optional(CONF_UNIQUE_ID): cv.string,
59 .extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
60 .extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema)
64 SELECT_CONFIG_SCHEMA = vol.Schema(
66 vol.Required(CONF_NAME): cv.template,
67 vol.Required(CONF_STATE): cv.template,
68 vol.Required(CONF_OPTIONS): cv.template,
69 vol.Optional(CONF_SELECT_OPTION): cv.SCRIPT_SCHEMA,
70 vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(),
76 hass: HomeAssistant, definitions: list[dict[str, Any]], unique_id_prefix: str |
None
77 ) -> list[TemplateSelect]:
78 """Create the Template select."""
80 for definition
in definitions:
81 unique_id = definition.get(CONF_UNIQUE_ID)
82 if unique_id
and unique_id_prefix:
83 unique_id = f
"{unique_id_prefix}-{unique_id}"
91 async_add_entities: AddEntitiesCallback,
92 discovery_info: DiscoveryInfoType |
None =
None,
94 """Set up the template select."""
95 if discovery_info
is None:
97 "Template select entities can only be configured under template:"
101 if "coordinator" in discovery_info:
104 for config
in discovery_info[
"entities"]
110 hass, discovery_info[
"entities"], discovery_info[
"unique_id"]
117 config_entry: ConfigEntry,
118 async_add_entities: AddEntitiesCallback,
120 """Initialize config entry."""
121 _options =
dict(config_entry.options)
122 _options.pop(
"template_type")
128 """Representation of a template select."""
130 _attr_should_poll =
False
135 config: dict[str, Any],
136 unique_id: str |
None,
138 """Initialize the select."""
139 super().
__init__(hass, config=config, unique_id=unique_id)
142 if (selection_option := config.get(CONF_SELECT_OPTION))
is not None:
144 hass, selection_option, self.
_attr_name_attr_name, DOMAIN
152 config.get(CONF_DEVICE_ID),
157 """Set up templates."""
159 "_attr_current_option",
162 none_on_template_error=
True,
167 validator=vol.All(cv.ensure_list, [cv.string]),
168 none_on_template_error=
True,
173 """Change the selected option."""
180 run_variables={ATTR_OPTION: option},
186 """Select entity based on trigger data."""
188 domain = SELECT_DOMAIN
189 extra_template_keys = (CONF_STATE,)
190 extra_template_keys_complex = (ATTR_OPTIONS,)
195 coordinator: TriggerUpdateCoordinator,
198 """Initialize the entity."""
199 super().
__init__(hass, coordinator, config)
202 config[CONF_SELECT_OPTION],
203 self._rendered.
get(CONF_NAME, DEFAULT_NAME),
209 """Return the currently selected option."""
210 return self._rendered.
get(CONF_STATE)
214 """Return the list of available options."""
215 return self._rendered.
get(ATTR_OPTIONS, [])
218 """Change the selected option."""
219 if self._config[CONF_OPTIMISTIC]:
223 {ATTR_OPTION: option}, context=self.
_context_context
None async_select_option(self, str option)
None __init__(self, HomeAssistant hass, dict[str, Any] config, str|None unique_id)
None _async_setup_templates(self)
str|None current_option(self)
None __init__(self, HomeAssistant hass, TriggerUpdateCoordinator coordinator, dict config)
None async_select_option(self, str option)
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)
web.Response get(self, web.Request request, str config_key)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
list[TemplateSelect] _async_create_entities(HomeAssistant hass, list[dict[str, Any]] definitions, str|None unique_id_prefix)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
dr.DeviceInfo|None async_device_info_to_link_from_device_id(HomeAssistant hass, str|None device_id)
def async_run(config_dir)