1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-20 19:09:45 +00:00

Add script specific error messages for response_variable (#95188)

This commit is contained in:
Allen Porter
2023-06-24 21:34:57 -07:00
committed by GitHub
parent ef2e55ecec
commit 528c206094
3 changed files with 81 additions and 4 deletions

View File

@@ -28,6 +28,7 @@ from homeassistant.core import (
HomeAssistant,
ServiceCall,
ServiceResponse,
SupportsResponse,
callback,
)
from homeassistant.exceptions import ConditionError, HomeAssistantError, ServiceNotFound
@@ -333,7 +334,7 @@ async def test_calling_service_template(hass: HomeAssistant) -> None:
async def test_calling_service_response_data(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test the calling of a service with return values."""
"""Test the calling of a service with response data."""
context = Context()
def mock_service(call: ServiceCall) -> ServiceResponse:
@@ -342,7 +343,9 @@ async def test_calling_service_response_data(
return {"data": "value-12345"}
return None
hass.services.async_register("test", "script", mock_service, supports_response=True)
hass.services.async_register(
"test", "script", mock_service, supports_response=SupportsResponse.OPTIONAL
)
sequence = cv.SCRIPT_SCHEMA(
[
{
@@ -404,6 +407,50 @@ async def test_calling_service_response_data(
)
@pytest.mark.parametrize(
("supports_response", "params", "expected_error"),
[
(
SupportsResponse.NONE,
{"response_variable": "foo"},
"does not support 'response_variable'",
),
(SupportsResponse.ONLY, {}, "requires 'response_variable'"),
],
)
async def test_service_response_data_errors(
hass: HomeAssistant,
supports_response: SupportsResponse,
params: dict[str, str],
expected_error: str,
) -> None:
"""Test the calling of a service with response data error cases."""
context = Context()
def mock_service(call: ServiceCall) -> ServiceResponse:
"""Mock service call."""
raise ValueError("Never invoked")
hass.services.async_register(
"test", "script", mock_service, supports_response=supports_response
)
sequence = cv.SCRIPT_SCHEMA(
[
{
"alias": "service step1",
"service": "test.script",
**params,
},
]
)
script_obj = script.Script(hass, sequence, "Test Name", "test_domain")
with pytest.raises(vol.Invalid, match=expected_error):
await script_obj.async_run(context=context)
await hass.async_block_till_done()
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()