From 66ec4564f4b5c07debaac4e164d5bcae3a7bfe81 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 13 May 2022 18:33:00 +0200 Subject: [PATCH] Support this variable in template number actions (#71797) --- homeassistant/components/template/number.py | 6 +++-- tests/components/template/test_number.py | 30 ++++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/template/number.py b/homeassistant/components/template/number.py index 89adb45a6d0..54131990a26 100644 --- a/homeassistant/components/template/number.py +++ b/homeassistant/components/template/number.py @@ -157,8 +157,10 @@ class TemplateNumber(TemplateEntity, NumberEntity): if self._optimistic: self._attr_value = value self.async_write_ha_state() - await self._command_set_value.async_run( - {ATTR_VALUE: value}, context=self._context + await self.async_run_script( + self._command_set_value, + run_variables={ATTR_VALUE: value}, + context=self._context, ) diff --git a/tests/components/template/test_number.py b/tests/components/template/test_number.py index 6c47fdd2abc..ea29bb303df 100644 --- a/tests/components/template/test_number.py +++ b/tests/components/template/test_number.py @@ -127,7 +127,7 @@ async def test_all_optional_config(hass): _verify(hass, 4, 1, 3, 5) -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(4, "input_number"): assert await setup.async_setup_component( @@ -173,13 +173,23 @@ async def test_templates_with_entities(hass): "step": f"{{{{ states('{_STEP_INPUT_NUMBER}') }}}}", "min": f"{{{{ states('{_MINIMUM_INPUT_NUMBER}') }}}}", "max": f"{{{{ states('{_MAXIMUM_INPUT_NUMBER}') }}}}", - "set_value": { - "service": "input_number.set_value", - "data_template": { - "entity_id": _VALUE_INPUT_NUMBER, - "value": "{{ value }}", + "set_value": [ + { + "service": "input_number.set_value", + "data_template": { + "entity_id": _VALUE_INPUT_NUMBER, + "value": "{{ value }}", + }, }, - }, + { + "service": "test.automation", + "data_template": { + "action": "set_value", + "caller": "{{ this.entity_id }}", + "value": "{{ value }}", + }, + }, + ], "optimistic": True, "unique_id": "a", }, @@ -247,6 +257,12 @@ async def test_templates_with_entities(hass): ) _verify(hass, 2, 2, 2, 6) + # Check this variable can be used in set_value script + assert len(calls) == 1 + assert calls[-1].data["action"] == "set_value" + assert calls[-1].data["caller"] == _TEST_NUMBER + assert calls[-1].data["value"] == 2 + async def test_trigger_number(hass): """Test trigger based template number."""