diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 66d1560ac70..57ee6b99029 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -147,11 +147,15 @@ async def _register_trigger_platform( ) return - tasks: list[asyncio.Task[None]] = [ - create_eager_task(listener(new_triggers)) - for listener in hass.data[TRIGGER_PLATFORM_SUBSCRIPTIONS] - ] - await asyncio.gather(*tasks) + # We don't use gather here because gather adds additional overhead + # when wrapping each coroutine in a task, and we expect our listeners + # to call trigger.async_get_all_descriptions which will only yield + # the first time it's called, after that it returns cached data. + for listener in hass.data[TRIGGER_PLATFORM_SUBSCRIPTIONS]: + try: + await listener(new_triggers) + except Exception: + _LOGGER.exception("Error while notifying trigger platform listener") class Trigger(abc.ABC): diff --git a/tests/helpers/test_trigger.py b/tests/helpers/test_trigger.py index 27cde92d14f..ba9db9cb053 100644 --- a/tests/helpers/test_trigger.py +++ b/tests/helpers/test_trigger.py @@ -738,3 +738,45 @@ async def test_invalid_trigger_platform( await async_setup_component(hass, "test", {}) assert "Integration test does not provide trigger support, skipping" in caplog.text + + +@patch("annotatedyaml.loader.load_yaml") +@patch.object(Integration, "has_triggers", return_value=True) +async def test_subscribe_triggers( + mock_has_triggers: Mock, + mock_load_yaml: Mock, + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test trigger.async_subscribe_platform_events.""" + sun_trigger_descriptions = """ + sun: {} + """ + + def _load_yaml(fname, secrets=None): + if fname.endswith("sun/triggers.yaml"): + trigger_descriptions = sun_trigger_descriptions + else: + raise FileNotFoundError + with io.StringIO(trigger_descriptions) as file: + return parse_yaml(file) + + mock_load_yaml.side_effect = _load_yaml + + async def broken_subscriber(_): + """Simulate a broken subscriber.""" + raise Exception("Boom!") # noqa: TRY002 + + trigger_events = [] + + async def good_subscriber(new_triggers: set[str]): + """Simulate a working subscriber.""" + trigger_events.append(new_triggers) + + trigger.async_subscribe_platform_events(hass, broken_subscriber) + trigger.async_subscribe_platform_events(hass, good_subscriber) + + assert await async_setup_component(hass, "sun", {}) + + assert trigger_events == [{"sun"}] + assert "Error while notifying trigger platform listener" in caplog.text