mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Minor refactor of template select (#62091)
This commit is contained in:
parent
e6956acb4b
commit
597045149f
@ -13,22 +13,20 @@ from homeassistant.components.select.const import (
|
|||||||
ATTR_OPTIONS,
|
ATTR_OPTIONS,
|
||||||
DOMAIN as SELECT_DOMAIN,
|
DOMAIN as SELECT_DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_STATE, CONF_UNIQUE_ID
|
||||||
CONF_ICON,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_OPTIMISTIC,
|
|
||||||
CONF_STATE,
|
|
||||||
CONF_UNIQUE_ID,
|
|
||||||
)
|
|
||||||
from homeassistant.core import Config, HomeAssistant
|
from homeassistant.core import Config, HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.template import Template, TemplateError
|
from homeassistant.helpers.template import TemplateError
|
||||||
|
|
||||||
from . import TriggerUpdateCoordinator
|
from . import TriggerUpdateCoordinator
|
||||||
from .const import CONF_AVAILABILITY, DOMAIN
|
from .const import DOMAIN
|
||||||
from .template_entity import TemplateEntity
|
from .template_entity import (
|
||||||
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
|
||||||
|
TEMPLATE_ENTITY_ICON_SCHEMA,
|
||||||
|
TemplateEntity,
|
||||||
|
)
|
||||||
from .trigger_entity import TriggerEntity
|
from .trigger_entity import TriggerEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -38,17 +36,19 @@ CONF_SELECT_OPTION = "select_option"
|
|||||||
DEFAULT_NAME = "Template Select"
|
DEFAULT_NAME = "Template Select"
|
||||||
DEFAULT_OPTIMISTIC = False
|
DEFAULT_OPTIMISTIC = False
|
||||||
|
|
||||||
SELECT_SCHEMA = vol.Schema(
|
SELECT_SCHEMA = (
|
||||||
|
vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
|
||||||
vol.Required(CONF_STATE): cv.template,
|
vol.Required(CONF_STATE): cv.template,
|
||||||
vol.Required(CONF_SELECT_OPTION): cv.SCRIPT_SCHEMA,
|
vol.Required(CONF_SELECT_OPTION): cv.SCRIPT_SCHEMA,
|
||||||
vol.Required(ATTR_OPTIONS): cv.template,
|
vol.Required(ATTR_OPTIONS): cv.template,
|
||||||
vol.Optional(CONF_AVAILABILITY): cv.template,
|
|
||||||
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
||||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_ICON): cv.template,
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
.extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
|
||||||
|
.extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -61,19 +61,7 @@ async def _async_create_entities(
|
|||||||
unique_id = definition.get(CONF_UNIQUE_ID)
|
unique_id = definition.get(CONF_UNIQUE_ID)
|
||||||
if unique_id and unique_id_prefix:
|
if unique_id and unique_id_prefix:
|
||||||
unique_id = f"{unique_id_prefix}-{unique_id}"
|
unique_id = f"{unique_id_prefix}-{unique_id}"
|
||||||
entities.append(
|
entities.append(TemplateSelect(hass, definition, unique_id))
|
||||||
TemplateSelect(
|
|
||||||
hass,
|
|
||||||
definition.get(CONF_NAME, DEFAULT_NAME),
|
|
||||||
definition[CONF_STATE],
|
|
||||||
definition.get(CONF_AVAILABILITY),
|
|
||||||
definition[CONF_SELECT_OPTION],
|
|
||||||
definition[ATTR_OPTIONS],
|
|
||||||
definition.get(CONF_OPTIMISTIC, DEFAULT_OPTIMISTIC),
|
|
||||||
unique_id,
|
|
||||||
definition.get(CONF_ICON),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return entities
|
return entities
|
||||||
|
|
||||||
|
|
||||||
@ -110,30 +98,23 @@ class TemplateSelect(TemplateEntity, SelectEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
name_template: Template | None,
|
config: dict[str, Any],
|
||||||
value_template: Template,
|
|
||||||
availability_template: Template | None,
|
|
||||||
command_select_option: dict[str, Any],
|
|
||||||
options_template: Template,
|
|
||||||
optimistic: bool,
|
|
||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
icon_template: Template | None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the select."""
|
"""Initialize the select."""
|
||||||
super().__init__(
|
super().__init__(config=config)
|
||||||
availability_template=availability_template, icon_template=icon_template
|
|
||||||
)
|
|
||||||
self._attr_name = DEFAULT_NAME
|
self._attr_name = DEFAULT_NAME
|
||||||
|
name_template = config[CONF_NAME]
|
||||||
name_template.hass = hass
|
name_template.hass = hass
|
||||||
with contextlib.suppress(TemplateError):
|
with contextlib.suppress(TemplateError):
|
||||||
self._attr_name = name_template.async_render(parse_result=False)
|
self._attr_name = name_template.async_render(parse_result=False)
|
||||||
self._name_template = name_template
|
self._name_template = name_template
|
||||||
self._value_template = value_template
|
self._value_template = config[CONF_STATE]
|
||||||
self._command_select_option = Script(
|
self._command_select_option = Script(
|
||||||
hass, command_select_option, self._attr_name, DOMAIN
|
hass, config[CONF_SELECT_OPTION], self._attr_name, DOMAIN
|
||||||
)
|
)
|
||||||
self._options_template = options_template
|
self._options_template = config[ATTR_OPTIONS]
|
||||||
self._attr_assumed_state = self._optimistic = optimistic
|
self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC]
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._attr_options = None
|
self._attr_options = None
|
||||||
self._attr_current_option = None
|
self._attr_current_option = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user