mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Add execute_script WS API (#47964)
* Add execute_script WS API * Improve tests
This commit is contained in:
parent
1cde1074c9
commit
333e6a215a
@ -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}))
|
||||
|
@ -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"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user