Merge stop & error script actions (#70109)

This commit is contained in:
Franck Nijhof 2022-04-20 23:22:37 +02:00 committed by GitHub
parent bddfbe01f3
commit 35687def02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 29 deletions

View File

@ -1545,17 +1545,10 @@ _SCRIPT_STOP_SCHEMA = vol.Schema(
{
**SCRIPT_ACTION_BASE_SCHEMA,
vol.Required(CONF_STOP): vol.Any(None, string),
vol.Optional(CONF_ERROR, default=False): boolean,
}
)
_SCRIPT_ERROR_SCHEMA = vol.Schema(
{
**SCRIPT_ACTION_BASE_SCHEMA,
vol.Optional(CONF_ERROR): vol.Any(None, string),
}
)
_SCRIPT_PARALLEL_SEQUENCE = vol.Schema(
{
**SCRIPT_ACTION_BASE_SCHEMA,
@ -1593,7 +1586,6 @@ SCRIPT_ACTION_CHOOSE = "choose"
SCRIPT_ACTION_WAIT_FOR_TRIGGER = "wait_for_trigger"
SCRIPT_ACTION_VARIABLES = "variables"
SCRIPT_ACTION_STOP = "stop"
SCRIPT_ACTION_ERROR = "error"
SCRIPT_ACTION_IF = "if"
SCRIPT_ACTION_PARALLEL = "parallel"
@ -1639,9 +1631,6 @@ def determine_script_action(action: dict[str, Any]) -> str:
if CONF_STOP in action:
return SCRIPT_ACTION_STOP
if CONF_ERROR in action:
return SCRIPT_ACTION_ERROR
if CONF_PARALLEL in action:
return SCRIPT_ACTION_PARALLEL
@ -1661,7 +1650,6 @@ ACTION_TYPE_SCHEMAS: dict[str, Callable[[Any], dict]] = {
SCRIPT_ACTION_WAIT_FOR_TRIGGER: _SCRIPT_WAIT_FOR_TRIGGER_SCHEMA,
SCRIPT_ACTION_VARIABLES: _SCRIPT_SET_SCHEMA,
SCRIPT_ACTION_STOP: _SCRIPT_STOP_SCHEMA,
SCRIPT_ACTION_ERROR: _SCRIPT_ERROR_SCHEMA,
SCRIPT_ACTION_IF: _SCRIPT_IF_SCHEMA,
SCRIPT_ACTION_PARALLEL: _SCRIPT_PARALLEL_SCHEMA,
}

View File

@ -240,7 +240,6 @@ STATIC_VALIDATION_ACTION_TYPES = (
cv.SCRIPT_ACTION_FIRE_EVENT,
cv.SCRIPT_ACTION_ACTIVATE_SCENE,
cv.SCRIPT_ACTION_VARIABLES,
cv.SCRIPT_ACTION_ERROR,
cv.SCRIPT_ACTION_STOP,
)
@ -972,16 +971,13 @@ class _ScriptRun:
async def _async_stop_step(self):
"""Stop script execution."""
stop = self._action[CONF_STOP]
self._log("Stop script sequence: %s", stop)
trace_set_result(stop=stop)
raise _StopScript(stop)
async def _async_error_step(self):
"""Abort and error script execution."""
error = self._action[CONF_ERROR]
self._log("Error script sequence: %s", error)
trace_set_result(error=error)
raise _AbortScript(error)
trace_set_result(stop=stop, error=error)
if error:
self._log("Error script sequence: %s", stop)
raise _AbortScript(stop)
self._log("Stop script sequence: %s", stop)
raise _StopScript(stop)
@async_trace_path("parallel")
async def _async_parallel_step(self) -> None:

View File

@ -4164,7 +4164,6 @@ async def test_validate_action_config(hass):
},
cv.SCRIPT_ACTION_VARIABLES: {"variables": {"hello": "world"}},
cv.SCRIPT_ACTION_STOP: {"stop": "Stop it right there buddy..."},
cv.SCRIPT_ACTION_ERROR: {"error": "Stand up, and try again!"},
cv.SCRIPT_ACTION_IF: {
"if": [
{
@ -4481,12 +4480,12 @@ async def test_stop_action(hass, caplog):
assert_action_trace(
{
"0": [{"result": {"event": "test_event", "event_data": {}}}],
"1": [{"result": {"stop": "In the name of love"}}],
"1": [{"result": {"stop": "In the name of love", "error": False}}],
}
)
async def test_error_action(hass, caplog):
async def test_stop_action_with_error(hass, caplog):
"""Test if automation fails on calling the error action."""
event = "test_event"
events = async_capture_events(hass, event)
@ -4497,7 +4496,8 @@ async def test_error_action(hass, caplog):
{"event": event},
{
"alias": alias,
"error": "Epic one...",
"stop": "Epic one...",
"error": True,
},
{"event": event},
]
@ -4515,7 +4515,10 @@ async def test_error_action(hass, caplog):
{
"0": [{"result": {"event": "test_event", "event_data": {}}}],
"1": [
{"error_type": script._AbortScript, "result": {"error": "Epic one..."}}
{
"error_type": script._AbortScript,
"result": {"stop": "Epic one...", "error": True},
}
],
},
expected_script_execution="aborted",
@ -4609,7 +4612,7 @@ async def test_continue_on_error_with_stop(hass: HomeAssistant) -> None:
assert_action_trace(
{
"0": [{"result": {"stop": "Stop it!"}}],
"0": [{"result": {"stop": "Stop it!", "error": False}}],
},
expected_script_execution="finished",
)