Allow returning a script variable from a script (#95346)

* Allow returning a script variable from a script

* Don't allow returning a template result

* Raise if response variable is undefined

* Add test

* Update homeassistant/helpers/script.py

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>

* Format code

---------

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Erik Montnemery 2023-06-27 17:13:53 +02:00 committed by GitHub
parent e19b29d6ae
commit cb22fb16f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 19 deletions

View file

@ -24,7 +24,10 @@ from homeassistant.setup import DATA_SETUP_TIME, async_setup_component
from homeassistant.util.json import json_loads
from tests.common import MockEntity, MockEntityPlatform, MockUser, async_mock_service
from tests.typing import ClientSessionGenerator, WebSocketGenerator
from tests.typing import (
ClientSessionGenerator,
WebSocketGenerator,
)
STATE_KEY_SHORT_NAMES = {
"entity_id": "e",
@ -1686,7 +1689,7 @@ async def test_execute_script(hass: HomeAssistant, websocket_client) -> None:
"data": {"hello": "world"},
"response_variable": "service_result",
},
{"stop": "done", "response": "{{ service_result }}"},
{"stop": "done", "response_variable": "service_result"},
],
}
)
@ -1732,6 +1735,48 @@ async def test_execute_script(hass: HomeAssistant, websocket_client) -> None:
assert call.context.as_dict() == msg_var["result"]["context"]
async def test_execute_script_complex_response(
hass: HomeAssistant, hass_ws_client: WebSocketGenerator
) -> None:
"""Test testing a condition."""
await async_setup_component(hass, "calendar", {"calendar": {"platform": "demo"}})
await hass.async_block_till_done()
ws_client = await hass_ws_client(hass)
await ws_client.send_json_auto_id(
{
"type": "execute_script",
"sequence": [
{
"service": "calendar.list_events",
"data": {"duration": {"hours": 24, "minutes": 0, "seconds": 0}},
"target": {"entity_id": "calendar.calendar_1"},
"response_variable": "service_result",
},
{"stop": "done", "response_variable": "service_result"},
],
}
)
msg_no_var = await ws_client.receive_json()
assert msg_no_var["type"] == const.TYPE_RESULT
assert msg_no_var["success"]
assert msg_no_var["result"]["response"] == {
"events": [
{
"start": ANY,
"end": ANY,
"summary": "Future Event",
"description": "Future Description",
"location": "Future Location",
"uid": None,
"recurrence_id": None,
"rrule": None,
}
]
}
async def test_subscribe_unsubscribe_bootstrap_integrations(
hass: HomeAssistant, websocket_client, hass_admin_user: MockUser
) -> None: