mirror of
https://github.com/home-assistant/core.git
synced 2025-07-07 21:37:07 +00:00
Don't gather TRIGGER_PLATFORM_SUBSCRIPTIONS (#147954)
Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
This commit is contained in:
parent
419e4f3b1d
commit
d2825e1c80
@ -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):
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user