forked from home-assistant/core
Allow multiple template.select platform entries (#55908)
This commit is contained in:
@ -46,27 +46,27 @@ SELECT_SCHEMA = vol.Schema(
|
|||||||
|
|
||||||
|
|
||||||
async def _async_create_entities(
|
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]:
|
) -> list[TemplateSelect]:
|
||||||
"""Create the Template select."""
|
"""Create the Template select."""
|
||||||
for entity in entities:
|
entities = []
|
||||||
unique_id = entity.get(CONF_UNIQUE_ID)
|
for definition in definitions:
|
||||||
|
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(
|
||||||
return [
|
|
||||||
TemplateSelect(
|
TemplateSelect(
|
||||||
hass,
|
hass,
|
||||||
entity.get(CONF_NAME, DEFAULT_NAME),
|
definition.get(CONF_NAME, DEFAULT_NAME),
|
||||||
entity[CONF_STATE],
|
definition[CONF_STATE],
|
||||||
entity.get(CONF_AVAILABILITY),
|
definition.get(CONF_AVAILABILITY),
|
||||||
entity[CONF_SELECT_OPTION],
|
definition[CONF_SELECT_OPTION],
|
||||||
entity[ATTR_OPTIONS],
|
definition[ATTR_OPTIONS],
|
||||||
entity.get(CONF_OPTIMISTIC, DEFAULT_OPTIMISTIC),
|
definition.get(CONF_OPTIMISTIC, DEFAULT_OPTIMISTIC),
|
||||||
unique_id,
|
unique_id,
|
||||||
)
|
)
|
||||||
]
|
)
|
||||||
|
return entities
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_platform(
|
async def async_setup_platform(
|
||||||
|
@ -60,6 +60,38 @@ async def test_missing_optional_config(hass, calls):
|
|||||||
_verify(hass, "a", ["a", "b"])
|
_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):
|
async def test_missing_required_keys(hass, calls):
|
||||||
"""Test: missing required fields will fail."""
|
"""Test: missing required fields will fail."""
|
||||||
with assert_setup_component(0, "template"):
|
with assert_setup_component(0, "template"):
|
||||||
@ -250,9 +282,9 @@ async def test_trigger_select(hass):
|
|||||||
assert events[0].event_type == "test_number_event"
|
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."""
|
"""Verify select's state."""
|
||||||
state = hass.states.get(_TEST_SELECT)
|
state = hass.states.get(entity_name)
|
||||||
attributes = state.attributes
|
attributes = state.attributes
|
||||||
assert state.state == str(expected_current_option)
|
assert state.state == str(expected_current_option)
|
||||||
assert attributes.get(SELECT_ATTR_OPTIONS) == expected_options
|
assert attributes.get(SELECT_ATTR_OPTIONS) == expected_options
|
||||||
|
Reference in New Issue
Block a user