diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 2b4da38b15e..7ed12467c6f 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -698,7 +698,13 @@ class _ScriptRun: if cond(hass, variables) is False: return False except exceptions.ConditionError as ex: - _LOGGER.warning("Error in '%s[%s]' evaluation: %s", name, idx, ex) + self._log( + "Error in '%s[%s]' evaluation: %s", + name, + idx, + ex, + level=logging.WARNING, + ) return None return True @@ -719,7 +725,11 @@ class _ScriptRun: await self._async_run_script(script) return except exceptions.ConditionError as ex: - _LOGGER.warning("Error in 'choose' evaluation:\n%s", ex) + self._log( + "Error in 'choose' evaluation:\n%s", + ex, + level=logging.WARNING, + ) if choose_data["default"] is not None: trace_set_result(choice="default") @@ -738,7 +748,7 @@ class _ScriptRun: trace_element.reuse_by_child = True check = cond(self._hass, self._variables) except exceptions.ConditionError as ex: - _LOGGER.warning("Error in 'condition' evaluation:\n%s", ex) + self._log("Error in 'condition' evaluation:\n%s", ex, level=logging.WARNING) check = False self._log("Test condition %s: %s", self._script.last_action, check) @@ -751,13 +761,10 @@ class _ScriptRun: if_data = await self._script._async_get_if_data(self._step) # noqa: SLF001 test_conditions: bool | None = False - try: - with trace_path("if"): - test_conditions = self._test_conditions( - if_data["if_conditions"], "if", "condition" - ) - except exceptions.ConditionError as ex: - _LOGGER.warning("Error in 'if' evaluation:\n%s", ex) + with trace_path("if"): + test_conditions = self._test_conditions( + if_data["if_conditions"], "if", "condition" + ) if test_conditions: trace_set_result(choice="then") @@ -848,33 +855,28 @@ class _ScriptRun: ] for iteration in itertools.count(1): set_repeat_var(iteration) - try: - if self._stop.done(): - break - if not self._test_conditions(conditions, "while"): - break - except exceptions.ConditionError as ex: - _LOGGER.warning("Error in 'while' evaluation:\n%s", ex) + if self._stop.done(): + break + if not self._test_conditions(conditions, "while"): break if iteration > 1: if iteration > REPEAT_WARN_ITERATIONS: if not warned_too_many_loops: warned_too_many_loops = True - _LOGGER.warning( - "While condition %s in script `%s` looped %s times", + self._log( + "While condition %s looped %s times", repeat[CONF_WHILE], - self._script.name, REPEAT_WARN_ITERATIONS, + level=logging.WARNING, ) if iteration > REPEAT_TERMINATE_ITERATIONS: - _LOGGER.critical( - "While condition %s in script `%s` " - "terminated because it looped %s times", + self._log( + "While condition %s terminated because it looped %s times", repeat[CONF_WHILE], - self._script.name, REPEAT_TERMINATE_ITERATIONS, + level=logging.CRITICAL, ) raise _AbortScript( f"While condition {repeat[CONF_WHILE]} " @@ -896,32 +898,27 @@ class _ScriptRun: for iteration in itertools.count(1): set_repeat_var(iteration) await async_run_sequence(iteration) - try: - if self._stop.done(): - break - if self._test_conditions(conditions, "until") in [True, None]: - break - except exceptions.ConditionError as ex: - _LOGGER.warning("Error in 'until' evaluation:\n%s", ex) + if self._stop.done(): + break + if self._test_conditions(conditions, "until") in [True, None]: break if iteration >= REPEAT_WARN_ITERATIONS: if not warned_too_many_loops: warned_too_many_loops = True - _LOGGER.warning( - "Until condition %s in script `%s` looped %s times", + self._log( + "Until condition %s looped %s times", repeat[CONF_UNTIL], - self._script.name, REPEAT_WARN_ITERATIONS, + level=logging.WARNING, ) if iteration >= REPEAT_TERMINATE_ITERATIONS: - _LOGGER.critical( - "Until condition %s in script `%s` " - "terminated because it looped %s times", + self._log( + "Until condition %s terminated because it looped %s times", repeat[CONF_UNTIL], - self._script.name, REPEAT_TERMINATE_ITERATIONS, + level=logging.CRITICAL, ) raise _AbortScript( f"Until condition {repeat[CONF_UNTIL]} " diff --git a/tests/helpers/test_script.py b/tests/helpers/test_script.py index 4a50cb9399f..74e81ebeb6e 100644 --- a/tests/helpers/test_script.py +++ b/tests/helpers/test_script.py @@ -3181,12 +3181,9 @@ async def test_repeat_limits( title_condition = condition.title() - assert f"{title_condition} condition" in caplog.text - assert f"in script `Test {condition}` looped 5 times" in caplog.text - assert ( - f"script `Test {condition}` terminated because it looped 10 times" - in caplog.text - ) + assert f"Test {condition}: {title_condition} condition" in caplog.text + assert "looped 5 times" in caplog.text + assert "terminated because it looped 10 times" in caplog.text assert len(events) == 10