Wait for all triggers when one fails to attach (#45361)
This commit is contained in:
parent
da4404e8cf
commit
a7741be9bb
4 changed files with 52 additions and 13 deletions
|
@ -462,8 +462,8 @@ class AutomationEntity(ToggleEntity, RestoreEntity):
|
||||||
) -> Optional[Callable[[], None]]:
|
) -> Optional[Callable[[], None]]:
|
||||||
"""Set up the triggers."""
|
"""Set up the triggers."""
|
||||||
|
|
||||||
def log_cb(level, msg):
|
def log_cb(level, msg, **kwargs):
|
||||||
self._logger.log(level, "%s %s", msg, self._name)
|
self._logger.log(level, "%s %s", msg, self._name, **kwargs)
|
||||||
|
|
||||||
return await async_initialize_triggers(
|
return await async_initialize_triggers(
|
||||||
cast(HomeAssistant, self.hass),
|
cast(HomeAssistant, self.hass),
|
||||||
|
|
|
@ -227,8 +227,12 @@ class _ScriptRun:
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
return await self._script._async_get_condition(config)
|
return await self._script._async_get_condition(config)
|
||||||
|
|
||||||
def _log(self, msg: str, *args: Any, level: int = logging.INFO) -> None:
|
def _log(
|
||||||
self._script._log(msg, *args, level=level) # pylint: disable=protected-access
|
self, msg: str, *args: Any, level: int = logging.INFO, **kwargs: Any
|
||||||
|
) -> None:
|
||||||
|
self._script._log( # pylint: disable=protected-access
|
||||||
|
msg, *args, level=level, **kwargs
|
||||||
|
)
|
||||||
|
|
||||||
async def async_run(self) -> None:
|
async def async_run(self) -> None:
|
||||||
"""Run script."""
|
"""Run script."""
|
||||||
|
@ -623,8 +627,8 @@ class _ScriptRun:
|
||||||
}
|
}
|
||||||
done.set()
|
done.set()
|
||||||
|
|
||||||
def log_cb(level, msg):
|
def log_cb(level, msg, **kwargs):
|
||||||
self._log(msg, level=level)
|
self._log(msg, level=level, **kwargs)
|
||||||
|
|
||||||
to_context = None
|
to_context = None
|
||||||
remove_triggers = await async_initialize_triggers(
|
remove_triggers = await async_initialize_triggers(
|
||||||
|
@ -1128,11 +1132,13 @@ class Script:
|
||||||
self._choose_data[step] = choose_data
|
self._choose_data[step] = choose_data
|
||||||
return choose_data
|
return choose_data
|
||||||
|
|
||||||
def _log(self, msg: str, *args: Any, level: int = logging.INFO) -> None:
|
def _log(
|
||||||
|
self, msg: str, *args: Any, level: int = logging.INFO, **kwargs: Any
|
||||||
|
) -> None:
|
||||||
msg = f"%s: {msg}"
|
msg = f"%s: {msg}"
|
||||||
args = (self.name, *args)
|
args = (self.name, *args)
|
||||||
|
|
||||||
if level == _LOG_EXCEPTION:
|
if level == _LOG_EXCEPTION:
|
||||||
self._logger.exception(msg, *args)
|
self._logger.exception(msg, *args, **kwargs)
|
||||||
else:
|
else:
|
||||||
self._logger.log(level, msg, *args)
|
self._logger.log(level, msg, *args, **kwargs)
|
||||||
|
|
|
@ -75,12 +75,19 @@ async def async_initialize_triggers(
|
||||||
platform = await _async_get_trigger_platform(hass, conf)
|
platform = await _async_get_trigger_platform(hass, conf)
|
||||||
triggers.append(platform.async_attach_trigger(hass, conf, action, info))
|
triggers.append(platform.async_attach_trigger(hass, conf, action, info))
|
||||||
|
|
||||||
removes = await asyncio.gather(*triggers)
|
attach_results = await asyncio.gather(*triggers, return_exceptions=True)
|
||||||
|
removes = []
|
||||||
|
|
||||||
if None in removes:
|
for result in attach_results:
|
||||||
log_cb(logging.ERROR, "Error setting up trigger")
|
if isinstance(result, Exception):
|
||||||
|
log_cb(logging.ERROR, "Error setting up trigger", exc_info=result)
|
||||||
|
elif result is None:
|
||||||
|
log_cb(
|
||||||
|
logging.ERROR, "Unknown error while setting up trigger (empty result)"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
removes.append(result)
|
||||||
|
|
||||||
removes = list(filter(None, removes))
|
|
||||||
if not removes:
|
if not removes:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -955,7 +955,33 @@ async def test_wait_for_trigger_bad(hass, caplog):
|
||||||
hass.async_create_task(script_obj.async_run())
|
hass.async_create_task(script_obj.async_run())
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert "Unknown error while setting up trigger" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
async def test_wait_for_trigger_generated_exception(hass, caplog):
|
||||||
|
"""Test bad wait_for_trigger."""
|
||||||
|
script_obj = script.Script(
|
||||||
|
hass,
|
||||||
|
cv.SCRIPT_SCHEMA(
|
||||||
|
{"wait_for_trigger": {"platform": "state", "entity_id": "sensor.abc"}}
|
||||||
|
),
|
||||||
|
"Test Name",
|
||||||
|
"test_domain",
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_attach_trigger_mock(*args, **kwargs):
|
||||||
|
raise ValueError("something bad")
|
||||||
|
|
||||||
|
with mock.patch(
|
||||||
|
"homeassistant.components.homeassistant.triggers.state.async_attach_trigger",
|
||||||
|
wraps=async_attach_trigger_mock,
|
||||||
|
):
|
||||||
|
hass.async_create_task(script_obj.async_run())
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert "Error setting up trigger" in caplog.text
|
assert "Error setting up trigger" in caplog.text
|
||||||
|
assert "ValueError" in caplog.text
|
||||||
|
assert "something bad" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
async def test_condition_basic(hass):
|
async def test_condition_basic(hass):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue