From adde9130a1d7db0ad9cc1c6054f07616bc1dc288 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 13 May 2022 18:32:45 +0200 Subject: [PATCH] Support this variable in template select actions (#71798) --- homeassistant/components/template/select.py | 6 +++-- tests/components/template/test_select.py | 30 ++++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/template/select.py b/homeassistant/components/template/select.py index fa2668c1e81..19f17096178 100644 --- a/homeassistant/components/template/select.py +++ b/homeassistant/components/template/select.py @@ -133,8 +133,10 @@ class TemplateSelect(TemplateEntity, SelectEntity): if self._optimistic: self._attr_current_option = option self.async_write_ha_state() - await self._command_select_option.async_run( - {ATTR_OPTION: option}, context=self._context + await self.async_run_script( + self._command_select_option, + run_variables={ATTR_OPTION: option}, + context=self._context, ) diff --git a/tests/components/template/test_select.py b/tests/components/template/test_select.py index de41ccedbb1..a58de63f186 100644 --- a/tests/components/template/test_select.py +++ b/tests/components/template/test_select.py @@ -131,7 +131,7 @@ async def test_missing_required_keys(hass): assert hass.states.async_all("select") == [] -async def test_templates_with_entities(hass): +async def test_templates_with_entities(hass, calls): """Test templates with values from other entities.""" with assert_setup_component(1, "input_select"): assert await setup.async_setup_component( @@ -158,13 +158,23 @@ async def test_templates_with_entities(hass): "select": { "state": f"{{{{ states('{_OPTION_INPUT_SELECT}') }}}}", "options": f"{{{{ state_attr('{_OPTION_INPUT_SELECT}', '{INPUT_SELECT_ATTR_OPTIONS}') }}}}", - "select_option": { - "service": "input_select.select_option", - "data_template": { - "entity_id": _OPTION_INPUT_SELECT, - "option": "{{ option }}", + "select_option": [ + { + "service": "input_select.select_option", + "data_template": { + "entity_id": _OPTION_INPUT_SELECT, + "option": "{{ option }}", + }, }, - }, + { + "service": "test.automation", + "data_template": { + "action": "select_option", + "caller": "{{ this.entity_id }}", + "option": "{{ option }}", + }, + }, + ], "optimistic": True, "unique_id": "a", }, @@ -212,6 +222,12 @@ async def test_templates_with_entities(hass): ) _verify(hass, "c", ["a", "b", "c"]) + # Check this variable can be used in set_value script + assert len(calls) == 1 + assert calls[-1].data["action"] == "select_option" + assert calls[-1].data["caller"] == _TEST_SELECT + assert calls[-1].data["option"] == "c" + async def test_trigger_select(hass): """Test trigger based template select."""