mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +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
|
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):
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user