From 35687def022155796f5a43aaaa45e0bb918d1582 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 20 Apr 2022 23:22:37 +0200 Subject: [PATCH] Merge stop & error script actions (#70109) --- homeassistant/helpers/config_validation.py | 14 +------------- homeassistant/helpers/script.py | 16 ++++++---------- tests/helpers/test_script.py | 15 +++++++++------ 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 342c9867527..b8c42a60b52 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -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, } diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index d5aa76a98a1..70f73f065ab 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -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: diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 80a7a163181..7f1b35e4e4a 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -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", )