From 333e6a215a021c87dea472cee756c6931f08de3f Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 16 Mar 2021 08:51:00 +0100 Subject: [PATCH] Add execute_script WS API (#47964) * Add execute_script WS API * Improve tests --- .../components/websocket_api/commands.py | 35 +++++++++++++---- .../components/websocket_api/test_commands.py | 39 +++++++++++++++++++ 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 53531cf9ba9..f85281d10c1 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -26,19 +26,20 @@ from . import const, decorators, messages @callback def async_register_commands(hass, async_reg): """Register commands.""" - async_reg(hass, handle_subscribe_events) - async_reg(hass, handle_unsubscribe_events) async_reg(hass, handle_call_service) - async_reg(hass, handle_get_states) - async_reg(hass, handle_get_services) + async_reg(hass, handle_entity_source) + async_reg(hass, handle_execute_script) async_reg(hass, handle_get_config) + async_reg(hass, handle_get_services) + async_reg(hass, handle_get_states) + async_reg(hass, handle_manifest_get) + async_reg(hass, handle_manifest_list) async_reg(hass, handle_ping) async_reg(hass, handle_render_template) - async_reg(hass, handle_manifest_list) - async_reg(hass, handle_manifest_get) - async_reg(hass, handle_entity_source) + async_reg(hass, handle_subscribe_events) async_reg(hass, handle_subscribe_trigger) async_reg(hass, handle_test_condition) + async_reg(hass, handle_unsubscribe_events) def pong_message(iden): @@ -420,3 +421,23 @@ async def handle_test_condition(hass, connection, msg): connection.send_result( msg["id"], {"result": check_condition(hass, msg.get("variables"))} ) + + +@decorators.websocket_command( + { + vol.Required("type"): "execute_script", + vol.Required("sequence"): cv.SCRIPT_SCHEMA, + } +) +@decorators.require_admin +@decorators.async_response +async def handle_execute_script(hass, connection, msg): + """Handle execute script command.""" + # Circular dep + # pylint: disable=import-outside-toplevel + from homeassistant.helpers.script import Script + + context = connection.context(msg) + script_obj = Script(hass, msg["sequence"], f"{const.DOMAIN} script", const.DOMAIN) + await script_obj.async_run(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 f596db63c5e..a83f9509d01 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -44,6 +44,7 @@ async def test_call_service(hass, websocket_client): assert call.domain == "domain_test" assert call.service == "test_service" assert call.data == {"hello": "world"} + assert call.context.as_dict() == msg["result"]["context"] async def test_call_service_target(hass, websocket_client): @@ -79,6 +80,7 @@ async def test_call_service_target(hass, websocket_client): "entity_id": ["entity.one", "entity.two"], "device_id": ["deviceid"], } + assert call.context.as_dict() == msg["result"]["context"] async def test_call_service_target_template(hass, websocket_client): @@ -985,3 +987,40 @@ async def test_test_condition(hass, websocket_client): assert msg["type"] == const.TYPE_RESULT assert msg["success"] assert msg["result"]["result"] is True + + +async def test_execute_script(hass, websocket_client): + """Test testing a condition.""" + calls = async_mock_service(hass, "domain_test", "test_service") + + await websocket_client.send_json( + { + "id": 5, + "type": "execute_script", + "sequence": [ + { + "service": "domain_test.test_service", + "data": {"hello": "world"}, + } + ], + } + ) + + await hass.async_block_till_done() + await hass.async_block_till_done() + + msg = await websocket_client.receive_json() + assert msg["id"] == 5 + assert msg["type"] == const.TYPE_RESULT + assert msg["success"] + + await hass.async_block_till_done() + await hass.async_block_till_done() + + assert len(calls) == 1 + 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"]