diff --git a/homeassistant/components/template/select.py b/homeassistant/components/template/select.py index 944c80cbfa4..96e86e8caec 100644 --- a/homeassistant/components/template/select.py +++ b/homeassistant/components/template/select.py @@ -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( diff --git a/tests/components/template/test_select.py b/tests/components/template/test_select.py index eb94a9284f4..ca4a30b1cd6 100644 --- a/tests/components/template/test_select.py +++ b/tests/components/template/test_select.py @@ -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