diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 74251e1bf24b2f..2912512fa62bf2 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -424,6 +424,7 @@ async def handle_test_condition(hass, connection, msg): { vol.Required("type"): "execute_script", vol.Required("sequence"): cv.SCRIPT_SCHEMA, + vol.Optional("variables"): dict, } ) @decorators.require_admin @@ -436,5 +437,5 @@ async def handle_execute_script(hass, connection, msg): context = connection.context(msg) script_obj = Script(hass, msg["sequence"], f"{const.DOMAIN} script", const.DOMAIN) - await script_obj.async_run(context=context) + await script_obj.async_run(msg.get("variables"), context=context) connection.send_message(messages.result_message(msg["id"], {"context": context})) diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index b9e1e149dd1914..da42e175ff360a 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -1086,21 +1086,41 @@ async def test_execute_script(hass, websocket_client): } ) - await hass.async_block_till_done() - await hass.async_block_till_done() + msg_no_var = await websocket_client.receive_json() + assert msg_no_var["id"] == 5 + assert msg_no_var["type"] == const.TYPE_RESULT + assert msg_no_var["success"] - msg = await websocket_client.receive_json() - assert msg["id"] == 5 - assert msg["type"] == const.TYPE_RESULT - assert msg["success"] + await websocket_client.send_json( + { + "id": 6, + "type": "execute_script", + "sequence": { + "service": "domain_test.test_service", + "data": {"hello": "{{ name }}"}, + }, + "variables": {"name": "From variable"}, + } + ) + + msg_var = await websocket_client.receive_json() + assert msg_var["id"] == 6 + assert msg_var["type"] == const.TYPE_RESULT + assert msg_var["success"] await hass.async_block_till_done() await hass.async_block_till_done() - assert len(calls) == 1 - call = calls[0] + assert len(calls) == 2 + call = calls[0] assert call.domain == "domain_test" assert call.service == "test_service" assert call.data == {"hello": "world"} - assert call.context.as_dict() == msg["result"]["context"] + assert call.context.as_dict() == msg_no_var["result"]["context"] + + call = calls[1] + assert call.domain == "domain_test" + assert call.service == "test_service" + assert call.data == {"hello": "From variable"} + assert call.context.as_dict() == msg_var["result"]["context"]