Add variables to execute script (#48613)

This commit is contained in:
Paulus Schoutsen 2021-04-02 06:27:41 -07:00 committed by GitHub
parent e76b653246
commit bdbb4f939f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 10 deletions

View file

@ -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}))

View file

@ -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
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"]