Support this variable in template select actions (#71798)

This commit is contained in:
Erik Montnemery 2022-05-13 18:32:45 +02:00 committed by GitHub
parent 6f7a465347
commit adde9130a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 9 deletions

View File

@ -133,8 +133,10 @@ class TemplateSelect(TemplateEntity, SelectEntity):
if self._optimistic: if self._optimistic:
self._attr_current_option = option self._attr_current_option = option
self.async_write_ha_state() self.async_write_ha_state()
await self._command_select_option.async_run( await self.async_run_script(
{ATTR_OPTION: option}, context=self._context self._command_select_option,
run_variables={ATTR_OPTION: option},
context=self._context,
) )

View File

@ -131,7 +131,7 @@ async def test_missing_required_keys(hass):
assert hass.states.async_all("select") == [] 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.""" """Test templates with values from other entities."""
with assert_setup_component(1, "input_select"): with assert_setup_component(1, "input_select"):
assert await setup.async_setup_component( assert await setup.async_setup_component(
@ -158,13 +158,23 @@ async def test_templates_with_entities(hass):
"select": { "select": {
"state": f"{{{{ states('{_OPTION_INPUT_SELECT}') }}}}", "state": f"{{{{ states('{_OPTION_INPUT_SELECT}') }}}}",
"options": f"{{{{ state_attr('{_OPTION_INPUT_SELECT}', '{INPUT_SELECT_ATTR_OPTIONS}') }}}}", "options": f"{{{{ state_attr('{_OPTION_INPUT_SELECT}', '{INPUT_SELECT_ATTR_OPTIONS}') }}}}",
"select_option": { "select_option": [
{
"service": "input_select.select_option", "service": "input_select.select_option",
"data_template": { "data_template": {
"entity_id": _OPTION_INPUT_SELECT, "entity_id": _OPTION_INPUT_SELECT,
"option": "{{ option }}", "option": "{{ option }}",
}, },
}, },
{
"service": "test.automation",
"data_template": {
"action": "select_option",
"caller": "{{ this.entity_id }}",
"option": "{{ option }}",
},
},
],
"optimistic": True, "optimistic": True,
"unique_id": "a", "unique_id": "a",
}, },
@ -212,6 +222,12 @@ async def test_templates_with_entities(hass):
) )
_verify(hass, "c", ["a", "b", "c"]) _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): async def test_trigger_select(hass):
"""Test trigger based template select.""" """Test trigger based template select."""