mirror of
https://github.com/home-assistant/core.git
synced 2026-02-15 07:36:16 +00:00
Log script condition warnings with the instance logger (#154966)
This commit is contained in:
@@ -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]} "
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user