From 8ea6cbc257a1d68054cff5028caece9dd9684f45 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Mon, 21 Feb 2022 12:56:20 -0500 Subject: [PATCH] Support variables in templates with timeout (#66990) --- .../components/websocket_api/commands.py | 2 +- .../components/websocket_api/test_commands.py | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 4b64e028f97..0b7e355ef24 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -346,7 +346,7 @@ async def handle_render_template( if timeout: try: timed_out = await template_obj.async_render_will_timeout( - timeout, strict=msg["strict"] + timeout, variables, strict=msg["strict"] ) except TemplateError as ex: connection.send_error(msg["id"], const.ERR_TEMPLATE_ERROR, str(ex)) diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index 58c9b414d5a..8304b093a14 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -706,6 +706,38 @@ async def test_render_template_renders_template(hass, websocket_client): } +async def test_render_template_with_timeout_and_variables(hass, websocket_client): + """Test a template with a timeout and variables renders without error.""" + await websocket_client.send_json( + { + "id": 5, + "type": "render_template", + "timeout": 10, + "variables": {"test": {"value": "hello"}}, + "template": "{{ test.value }}", + } + ) + + msg = await websocket_client.receive_json() + assert msg["id"] == 5 + assert msg["type"] == const.TYPE_RESULT + assert msg["success"] + + msg = await websocket_client.receive_json() + assert msg["id"] == 5 + assert msg["type"] == "event" + event = msg["event"] + assert event == { + "result": "hello", + "listeners": { + "all": False, + "domains": [], + "entities": [], + "time": False, + }, + } + + async def test_render_template_manual_entity_ids_no_longer_needed( hass, websocket_client ):