Don't gather TRIGGER_PLATFORM_SUBSCRIPTIONS (#147954)

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
This commit is contained in:
Erik Montnemery 2025-07-03 19:33:28 +02:00 committed by GitHub
parent 419e4f3b1d
commit d2825e1c80
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 5 deletions

View File

@ -147,11 +147,15 @@ async def _register_trigger_platform(
) )
return return
tasks: list[asyncio.Task[None]] = [ # We don't use gather here because gather adds additional overhead
create_eager_task(listener(new_triggers)) # when wrapping each coroutine in a task, and we expect our listeners
for listener in hass.data[TRIGGER_PLATFORM_SUBSCRIPTIONS] # to call trigger.async_get_all_descriptions which will only yield
] # the first time it's called, after that it returns cached data.
await asyncio.gather(*tasks) 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): class Trigger(abc.ABC):

View File

@ -738,3 +738,45 @@ async def test_invalid_trigger_platform(
await async_setup_component(hass, "test", {}) await async_setup_component(hass, "test", {})
assert "Integration test does not provide trigger support, skipping" in caplog.text 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