Allow multiple template.select platform entries (#55908)

This commit is contained in:
Raman Gupta
2021-09-07 17:50:07 -04:00
committed by GitHub
parent 42f586c585
commit 37d75e8a03
2 changed files with 47 additions and 15 deletions

View File

@ -46,27 +46,27 @@ SELECT_SCHEMA = vol.Schema(
async def _async_create_entities(
hass: HomeAssistant, entities: list[dict[str, Any]], unique_id_prefix: str | None
hass: HomeAssistant, definitions: list[dict[str, Any]], unique_id_prefix: str | None
) -> list[TemplateSelect]:
"""Create the Template select."""
for entity in entities:
unique_id = entity.get(CONF_UNIQUE_ID)
entities = []
for definition in definitions:
unique_id = definition.get(CONF_UNIQUE_ID)
if unique_id and unique_id_prefix:
unique_id = f"{unique_id_prefix}-{unique_id}"
return [
entities.append(
TemplateSelect(
hass,
entity.get(CONF_NAME, DEFAULT_NAME),
entity[CONF_STATE],
entity.get(CONF_AVAILABILITY),
entity[CONF_SELECT_OPTION],
entity[ATTR_OPTIONS],
entity.get(CONF_OPTIMISTIC, DEFAULT_OPTIMISTIC),
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,
)
]
)
return entities
async def async_setup_platform(

View File

@ -60,6 +60,38 @@ async def test_missing_optional_config(hass, calls):
_verify(hass, "a", ["a", "b"])
async def test_multiple_configs(hass, calls):
"""Test: multiple select entities get created."""
with assert_setup_component(1, "template"):
assert await setup.async_setup_component(
hass,
"template",
{
"template": {
"select": [
{
"state": "{{ 'a' }}",
"select_option": {"service": "script.select_option"},
"options": "{{ ['a', 'b'] }}",
},
{
"state": "{{ 'a' }}",
"select_option": {"service": "script.select_option"},
"options": "{{ ['a', 'b'] }}",
},
]
}
},
)
await hass.async_block_till_done()
await hass.async_start()
await hass.async_block_till_done()
_verify(hass, "a", ["a", "b"])
_verify(hass, "a", ["a", "b"], f"{_TEST_SELECT}_2")
async def test_missing_required_keys(hass, calls):
"""Test: missing required fields will fail."""
with assert_setup_component(0, "template"):
@ -250,9 +282,9 @@ async def test_trigger_select(hass):
assert events[0].event_type == "test_number_event"
def _verify(hass, expected_current_option, expected_options):
def _verify(hass, expected_current_option, expected_options, entity_name=_TEST_SELECT):
"""Verify select's state."""
state = hass.states.get(_TEST_SELECT)
state = hass.states.get(entity_name)
attributes = state.attributes
assert state.state == str(expected_current_option)
assert attributes.get(SELECT_ATTR_OPTIONS) == expected_options