From fb98a6f02661b3397ddbdc18dd3468cc281825a5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 5 Apr 2024 16:14:20 -1000 Subject: [PATCH] Make run_immediately the default for core EventBus listeners (#113752) * DNM: Make run_immediately the default for listeners This is a test to see how much progress we have made twords this goal https://github.com/home-assistant/core/pull/113727#issuecomment-2004587947 * fix shutdown * Revert "fix shutdown" This reverts commit a8969d7db9fed10040cb8b7b25459dc9d812eb9c. * set false since it break utility meter tests * one more * fix rfxtrx test * test needs to be explict now * fix matrix * fail sooner --- homeassistant/components/matrix/__init__.py | 4 +++- homeassistant/components/rfxtrx/__init__.py | 4 +++- homeassistant/core.py | 4 ++-- homeassistant/helpers/event.py | 5 ++++- homeassistant/helpers/start.py | 4 +++- tests/common.py | 4 +++- tests/components/automation/test_init.py | 4 +++- tests/test_core.py | 6 ++++-- 8 files changed, 25 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index b8f1ec08fe0..a283ba20dcb 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -219,7 +219,9 @@ class MatrixBot: loop_sleep_time=1_000, ) # milliseconds. - self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup) + self.hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_START, handle_startup, run_immediately=False + ) def _load_commands(self, commands: list[ConfigCommand]) -> None: for command in commands: diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 6f0e5932adc..b40e8d921ed 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -280,7 +280,9 @@ async def async_setup_internal(hass: HomeAssistant, entry: ConfigEntry) -> None: hass.data[DOMAIN][DATA_RFXOBJECT] = rfx_object entry.async_on_unload( - hass.bus.async_listen(dr.EVENT_DEVICE_REGISTRY_UPDATED, _updated_device) + hass.bus.async_listen( + dr.EVENT_DEVICE_REGISTRY_UPDATED, _updated_device, run_immediately=False + ) ) def _shutdown_rfxtrx(event: Event) -> None: diff --git a/homeassistant/core.py b/homeassistant/core.py index f94a7d4c1bb..48036de519e 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1485,7 +1485,7 @@ class EventBus: event_type: str, listener: Callable[[Event[_DataT]], Coroutine[Any, Any, None] | None], event_filter: Callable[[_DataT], bool] | None = None, - run_immediately: bool = False, + run_immediately: bool = True, ) -> CALLBACK_TYPE: """Listen for all events or events of a specific type. @@ -1558,7 +1558,7 @@ class EventBus: self, event_type: str, listener: Callable[[Event[Any]], Coroutine[Any, Any, None] | None], - run_immediately: bool = False, + run_immediately: bool = True, ) -> CALLBACK_TYPE: """Listen once for event of a specific type. diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index 2529b49d263..67feb6c48a4 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -272,7 +272,10 @@ def async_track_state_change( return async_track_state_change_event(hass, entity_ids, state_change_listener) return hass.bus.async_listen( - EVENT_STATE_CHANGED, state_change_dispatcher, event_filter=state_change_filter + EVENT_STATE_CHANGED, + state_change_dispatcher, + event_filter=state_change_filter, + run_immediately=False, ) diff --git a/homeassistant/helpers/start.py b/homeassistant/helpers/start.py index 148b416e087..92ce8e8cdde 100644 --- a/homeassistant/helpers/start.py +++ b/homeassistant/helpers/start.py @@ -47,7 +47,9 @@ def _async_at_core_state( if unsub: unsub() - unsub = hass.bus.async_listen_once(event_type, _matched_event) + unsub = hass.bus.async_listen_once( + event_type, _matched_event, run_immediately=False + ) return cancel diff --git a/tests/common.py b/tests/common.py index db96e36f7ec..59b93fc7288 100644 --- a/tests/common.py +++ b/tests/common.py @@ -358,7 +358,9 @@ async def async_test_home_assistant( """Clear global instance.""" INSTANCES.remove(hass) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance) + hass.bus.async_listen_once( + EVENT_HOMEASSISTANT_CLOSE, clear_instance, run_immediately=False + ) yield hass diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 31e74529777..ba02e61f0a7 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -2512,7 +2512,9 @@ async def test_recursive_automation_starting_script( hass.services.async_register( "test", "automation_started", async_service_handler ) - hass.bus.async_listen("automation_triggered", async_automation_triggered) + hass.bus.async_listen( + "automation_triggered", async_automation_triggered, run_immediately=False + ) hass.bus.async_fire("trigger_automation") await asyncio.wait_for(script_done_event.wait(), 10) diff --git a/tests/test_core.py b/tests/test_core.py index 905d8efe6de..44da9695fdc 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -3361,9 +3361,11 @@ async def test_report_state_listener_restrictions(hass: HomeAssistant) -> None: """Mock filter.""" return False - # run_immediately not set + # run_immediately set to False with pytest.raises(HomeAssistantError): - hass.bus.async_listen(EVENT_STATE_REPORTED, listener, event_filter=filter) + hass.bus.async_listen( + EVENT_STATE_REPORTED, listener, event_filter=filter, run_immediately=False + ) # no filter with pytest.raises(HomeAssistantError):