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 = []