mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 02:49:40 +00:00
Allow scripts to capture service response data in variables (#94757)
* Allow scripts service actions to save return values * Simplify script service response data * Rename result_variable to response_variable based on feedback
This commit is contained in:
@@ -27,6 +27,7 @@ from homeassistant.core import (
|
||||
CoreState,
|
||||
HomeAssistant,
|
||||
ServiceCall,
|
||||
ServiceResult,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.exceptions import ConditionError, HomeAssistantError, ServiceNotFound
|
||||
@@ -329,6 +330,80 @@ async def test_calling_service_template(hass: HomeAssistant) -> None:
|
||||
)
|
||||
|
||||
|
||||
async def test_calling_service_return_values(
|
||||
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
|
||||
) -> None:
|
||||
"""Test the calling of a service with return values."""
|
||||
context = Context()
|
||||
|
||||
def mock_service(call: ServiceCall) -> ServiceResult:
|
||||
"""Mock service call."""
|
||||
if call.return_values:
|
||||
return {"data": "value-12345"}
|
||||
return None
|
||||
|
||||
hass.services.async_register("test", "script", mock_service)
|
||||
sequence = cv.SCRIPT_SCHEMA(
|
||||
[
|
||||
{
|
||||
"alias": "service step1",
|
||||
"service": "test.script",
|
||||
# Store the result of the service call as a variable
|
||||
"response_variable": "my_response",
|
||||
},
|
||||
{
|
||||
"alias": "service step2",
|
||||
"service": "test.script",
|
||||
"data_template": {
|
||||
# Result of previous service call
|
||||
"key": "{{ my_response.data }}"
|
||||
},
|
||||
},
|
||||
]
|
||||
)
|
||||
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
|
||||
|
||||
await script_obj.async_run(context=context)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert "Executing step service step1" in caplog.text
|
||||
assert "Executing step service step2" in caplog.text
|
||||
|
||||
assert_action_trace(
|
||||
{
|
||||
"0": [
|
||||
{
|
||||
"result": {
|
||||
"params": {
|
||||
"domain": "test",
|
||||
"service": "script",
|
||||
"service_data": {},
|
||||
"target": {},
|
||||
},
|
||||
"running_script": False,
|
||||
}
|
||||
}
|
||||
],
|
||||
"1": [
|
||||
{
|
||||
"result": {
|
||||
"params": {
|
||||
"domain": "test",
|
||||
"service": "script",
|
||||
"service_data": {"key": "value-12345"},
|
||||
"target": {},
|
||||
},
|
||||
"running_script": False,
|
||||
},
|
||||
"variables": {
|
||||
"my_response": {"data": "value-12345"},
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def test_data_template_with_templated_key(hass: HomeAssistant) -> None:
|
||||
"""Test the calling of a service with a data_template with a templated key."""
|
||||
context = Context()
|
||||
|
||||
Reference in New Issue
Block a user