From 53c3e27ed9cc7e753c3dd437c4bc3a3505b5bc4a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 11 Mar 2024 13:51:03 -1000 Subject: [PATCH] Add support for run_immediately to async_listen_once (#113020) --- homeassistant/core.py | 3 ++- tests/test_core.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index ae5c9119738..65f120ae8a2 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1465,6 +1465,7 @@ class EventBus: self, event_type: str, listener: Callable[[Event[Any]], Coroutine[Any, Any, None] | None], + run_immediately: bool = False, ) -> CALLBACK_TYPE: """Listen once for event of a specific type. @@ -1485,7 +1486,7 @@ class EventBus: job_type=HassJobType.Callback, ), None, - False, + run_immediately, ), ) one_time_listener.remove = remove diff --git a/tests/test_core.py b/tests/test_core.py index 99490fe4365..51805e46dba 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1169,6 +1169,21 @@ async def test_eventbus_run_immediately_coro(hass: HomeAssistant) -> None: unsub() +async def test_eventbus_listen_once_run_immediately_coro(hass: HomeAssistant) -> None: + """Test we can call events immediately with a coro.""" + calls = [] + + async def listener(event): + """Mock listener.""" + calls.append(event) + + hass.bus.async_listen_once("test", listener, run_immediately=True) + + hass.bus.async_fire("test", {"event": True}) + # No async_block_till_done here + assert len(calls) == 1 + + async def test_eventbus_unsubscribe_listener(hass: HomeAssistant) -> None: """Test unsubscribe listener from returned function.""" calls = []