diff --git a/homeassistant/components/alexa/state_report.py b/homeassistant/components/alexa/state_report.py index 978879b5d88..24d750e7cb7 100644 --- a/homeassistant/components/alexa/state_report.py +++ b/homeassistant/components/alexa/state_report.py @@ -342,7 +342,6 @@ async def async_enable_proactive_mode( EVENT_STATE_CHANGED, _async_entity_state_listener, event_filter=_async_entity_state_filter, - run_immediately=True, ) diff --git a/homeassistant/components/api/__init__.py b/homeassistant/components/api/__init__.py index 489475e9dd7..496b6fa5fb1 100644 --- a/homeassistant/components/api/__init__.py +++ b/homeassistant/components/api/__init__.py @@ -398,7 +398,6 @@ class APIDomainServicesView(HomeAssistantView): cancel_listen = hass.bus.async_listen( EVENT_STATE_CHANGED, _async_save_changed_entities, - run_immediately=True, ) try: diff --git a/homeassistant/components/apple_tv/__init__.py b/homeassistant/components/apple_tv/__init__.py index 9a72a89c876..cd1a1c59127 100644 --- a/homeassistant/components/apple_tv/__init__.py +++ b/homeassistant/components/apple_tv/__init__.py @@ -102,9 +102,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await manager.disconnect() entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, on_hass_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, on_hass_stop) ) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/homeassistant/components/august/subscriber.py b/homeassistant/components/august/subscriber.py index 9332080d9ad..7294f8bc90f 100644 --- a/homeassistant/components/august/subscriber.py +++ b/homeassistant/components/august/subscriber.py @@ -71,7 +71,6 @@ class AugustSubscriberMixin: self._stop_interval = self._hass.bus.async_listen( EVENT_HOMEASSISTANT_STOP, self._async_cancel_update_interval, - run_immediately=True, ) @callback diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index ff0a5fc5193..fa05791b9c9 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -782,7 +782,6 @@ class AutomationEntity(BaseAutomationEntity, RestoreEntity): self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self._async_enable_automation, - run_immediately=True, ) self.async_write_ha_state() diff --git a/homeassistant/components/azure_event_hub/__init__.py b/homeassistant/components/azure_event_hub/__init__.py index 0a84ca44141..668444f9990 100644 --- a/homeassistant/components/azure_event_hub/__init__.py +++ b/homeassistant/components/azure_event_hub/__init__.py @@ -158,7 +158,7 @@ class AzureEventHub: """ logging.getLogger("azure.eventhub").setLevel(logging.WARNING) self._listener_remover = self.hass.bus.async_listen( - MATCH_ALL, self.async_listen, run_immediately=True + MATCH_ALL, self.async_listen ) self._schedule_next_send() diff --git a/homeassistant/components/bluetooth/__init__.py b/homeassistant/components/bluetooth/__init__.py index 1e091ec32cc..3273080d88b 100644 --- a/homeassistant/components/bluetooth/__init__.py +++ b/homeassistant/components/bluetooth/__init__.py @@ -166,9 +166,7 @@ async def _async_start_adapter_discovery( """Shutdown debouncer.""" discovery_debouncer.async_shutdown() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_shutdown_debouncer, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown_debouncer) async def _async_call_debouncer(now: datetime.datetime) -> None: """Call the debouncer at a later time.""" @@ -201,7 +199,6 @@ async def _async_start_adapter_discovery( hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, hass_callback(lambda event: cancel()), - run_immediately=True, ) diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index 3a240e9f01e..2eb07c5133f 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -135,11 +135,9 @@ class HomeAssistantBluetoothManager(BluetoothManager): self._bluetooth_adapters, self.storage ) self._cancel_logging_listener = self.hass.bus.async_listen( - EVENT_LOGGING_CHANGED, self._async_logging_changed, run_immediately=True - ) - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self.async_stop, run_immediately=True + EVENT_LOGGING_CHANGED, self._async_logging_changed ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) seen: set[str] = set() for address, service_info in itertools.chain( self._connectable_history.items(), self._all_history.items() diff --git a/homeassistant/components/bluetooth/passive_update_processor.py b/homeassistant/components/bluetooth/passive_update_processor.py index b3bf3adf93c..1d1078633fe 100644 --- a/homeassistant/components/bluetooth/passive_update_processor.py +++ b/homeassistant/components/bluetooth/passive_update_processor.py @@ -274,7 +274,6 @@ async def async_setup(hass: HomeAssistant) -> None: hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, _async_save_processor_data_at_stop, - run_immediately=True, ) diff --git a/homeassistant/components/bond/__init__.py b/homeassistant/components/bond/__init__.py index 216a4b501f2..9ecfedee570 100644 --- a/homeassistant/components/bond/__init__.py +++ b/homeassistant/components/bond/__init__.py @@ -68,9 +68,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.async_on_unload(_async_stop_event) entry.async_on_unload( - hass.bus.async_listen( - EVENT_HOMEASSISTANT_STOP, _async_stop_event, run_immediately=True - ) + hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, _async_stop_event) ) hass.data.setdefault(DOMAIN, {})[entry.entry_id] = BondData(hub, bpup_subs) diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index a81711f2793..2430ccebb4f 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -412,9 +412,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: stream.add_provider("hls") await stream.start() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STARTED, preload_stream, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, preload_stream) @callback def update_tokens(t: datetime) -> None: @@ -432,9 +430,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Unsubscribe track time interval timer.""" unsub() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, unsub_track_time_interval, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, unsub_track_time_interval) component.async_register_entity_service( SERVICE_ENABLE_MOTION, {}, "async_enable_motion_detection" diff --git a/homeassistant/components/cloud/__init__.py b/homeassistant/components/cloud/__init__.py index d85415cf9eb..80f9d9f9368 100644 --- a/homeassistant/components/cloud/__init__.py +++ b/homeassistant/components/cloud/__init__.py @@ -262,9 +262,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Shutdown event.""" await cloud.stop() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _shutdown, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown) _remote_handle_prefs_updated(cloud) diff --git a/homeassistant/components/conversation/default_agent.py b/homeassistant/components/conversation/default_agent.py index 57ac5e2cc58..121702115b9 100644 --- a/homeassistant/components/conversation/default_agent.py +++ b/homeassistant/components/conversation/default_agent.py @@ -197,24 +197,20 @@ class DefaultAgent(ConversationEntity): self.hass.bus.async_listen( ar.EVENT_AREA_REGISTRY_UPDATED, self._async_clear_slot_list, - run_immediately=True, ), self.hass.bus.async_listen( fr.EVENT_FLOOR_REGISTRY_UPDATED, self._async_clear_slot_list, - run_immediately=True, ), self.hass.bus.async_listen( er.EVENT_ENTITY_REGISTRY_UPDATED, self._async_clear_slot_list, event_filter=self._filter_entity_registry_changes, - run_immediately=True, ), self.hass.bus.async_listen( EVENT_STATE_CHANGED, self._async_clear_slot_list, event_filter=self._filter_state_changes, - run_immediately=True, ), async_listen_entity_updates(self.hass, DOMAIN, self._async_clear_slot_list), ] diff --git a/homeassistant/components/device_tracker/config_entry.py b/homeassistant/components/device_tracker/config_entry.py index 33c753c41e1..0372dff3a86 100644 --- a/homeassistant/components/device_tracker/config_entry.py +++ b/homeassistant/components/device_tracker/config_entry.py @@ -159,9 +159,7 @@ def _async_register_mac( # Enable entity ent_reg.async_update_entity(entity_id, disabled_by=None) - hass.bus.async_listen( - dr.EVENT_DEVICE_REGISTRY_UPDATED, handle_device_event, run_immediately=True - ) + hass.bus.async_listen(dr.EVENT_DEVICE_REGISTRY_UPDATED, handle_device_event) class BaseTrackerEntity(Entity): diff --git a/homeassistant/components/device_tracker/legacy.py b/homeassistant/components/device_tracker/legacy.py index e292e97a8ec..dfeed98f320 100644 --- a/homeassistant/components/device_tracker/legacy.py +++ b/homeassistant/components/device_tracker/legacy.py @@ -281,9 +281,7 @@ async def _async_setup_integration( """ cancel_update_stale() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _on_hass_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_hass_stop) @attr.s diff --git a/homeassistant/components/dhcp/__init__.py b/homeassistant/components/dhcp/__init__.py index 40cc0c02c84..b4d06b6e276 100644 --- a/homeassistant/components/dhcp/__init__.py +++ b/homeassistant/components/dhcp/__init__.py @@ -162,13 +162,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: for watcher in watchers: watcher.async_stop() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STARTED, _async_initialize, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _async_initialize) return True diff --git a/homeassistant/components/esphome/dashboard.py b/homeassistant/components/esphome/dashboard.py index b8a72ac4398..54a593fe0cc 100644 --- a/homeassistant/components/esphome/dashboard.py +++ b/homeassistant/components/esphome/dashboard.py @@ -103,7 +103,7 @@ class ESPHomeDashboardManager: await dashboard.async_shutdown() self._cancel_shutdown = hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, on_hass_stop, run_immediately=True + EVENT_HOMEASSISTANT_STOP, on_hass_stop ) new_data = {"info": {"addon_slug": addon_slug, "host": host, "port": port}} diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index ad24a68103d..3813d22ce97 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -550,15 +550,12 @@ class ESPHomeManager: # when the CLOSE event is fired so anything using a Bluetooth # proxy has a chance to shut down properly. entry_data.cleanup_callbacks.append( - hass.bus.async_listen( - EVENT_HOMEASSISTANT_CLOSE, self.on_stop, run_immediately=True - ) + hass.bus.async_listen(EVENT_HOMEASSISTANT_CLOSE, self.on_stop) ) entry_data.cleanup_callbacks.append( hass.bus.async_listen( EVENT_LOGGING_CHANGED, self._async_handle_logging_changed, - run_immediately=True, ) ) diff --git a/homeassistant/components/google_assistant/report_state.py b/homeassistant/components/google_assistant/report_state.py index 1230b9a272e..7fbe4bab5a9 100644 --- a/homeassistant/components/google_assistant/report_state.py +++ b/homeassistant/components/google_assistant/report_state.py @@ -190,7 +190,6 @@ def async_enable_report_state( EVENT_STATE_CHANGED, _async_entity_state_listener, event_filter=_async_entity_state_filter, - run_immediately=True, ) unsub = async_call_later( diff --git a/homeassistant/components/hassio/__init__.py b/homeassistant/components/hassio/__init__.py index 8f648bd006b..ce3b5b05ffe 100644 --- a/homeassistant/components/hassio/__init__.py +++ b/homeassistant/components/hassio/__init__.py @@ -386,7 +386,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: last_timezone = new_timezone await hassio.update_hass_timezone(new_timezone) - hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, push_config, run_immediately=True) + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, push_config) push_config_task = hass.async_create_task(push_config(None), eager_start=True) # Start listening for problems with supervisor and making issues diff --git a/homeassistant/components/homeassistant/triggers/event.py b/homeassistant/components/homeassistant/triggers/event.py index 85bd2708d5e..d29baf342ab 100644 --- a/homeassistant/components/homeassistant/triggers/event.py +++ b/homeassistant/components/homeassistant/triggers/event.py @@ -171,9 +171,7 @@ async def async_attach_trigger( event_filter = filter_event if event_data_items or event_data_schema else None removes = [ - hass.bus.async_listen( - event_type, handle_event, event_filter=event_filter, run_immediately=True - ) + hass.bus.async_listen(event_type, handle_event, event_filter=event_filter) for event_type in event_types ] diff --git a/homeassistant/components/homeassistant_alerts/__init__.py b/homeassistant/components/homeassistant_alerts/__init__.py index b0eefad053e..7dcd9f8db97 100644 --- a/homeassistant/components/homeassistant_alerts/__init__.py +++ b/homeassistant/components/homeassistant_alerts/__init__.py @@ -104,9 +104,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: refresh_debouncer.async_schedule_call() await coordinator.async_refresh() - hass.bus.async_listen( - EVENT_COMPONENT_LOADED, _component_loaded, run_immediately=True - ) + hass.bus.async_listen(EVENT_COMPONENT_LOADED, _component_loaded) async_at_start(hass, initial_refresh) diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 1b9ccaca8bf..f9f91ec162b 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -351,9 +351,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry.async_on_unload(entry.add_update_listener(_async_update_listener)) entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, homekit.async_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, homekit.async_stop) ) entry_data = HomeKitEntryData( diff --git a/homeassistant/components/homekit_controller/__init__.py b/homeassistant/components/homekit_controller/__init__.py index 218094ddaf5..639cec6dcb5 100644 --- a/homeassistant/components/homekit_controller/__init__.py +++ b/homeassistant/components/homekit_controller/__init__.py @@ -86,9 +86,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) ) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller) return True diff --git a/homeassistant/components/homekit_controller/connection.py b/homeassistant/components/homekit_controller/connection.py index 7b3f8e45845..78beb7bfffa 100644 --- a/homeassistant/components/homekit_controller/connection.py +++ b/homeassistant/components/homekit_controller/connection.py @@ -286,7 +286,6 @@ class HKDevice: self.hass.bus.async_listen( EVENT_HOMEASSISTANT_STARTED, self._async_populate_ble_accessory_state, - run_immediately=True, ) ) else: diff --git a/homeassistant/components/homekit_controller/utils.py b/homeassistant/components/homekit_controller/utils.py index 40879a533f4..2f94f5bac92 100644 --- a/homeassistant/components/homekit_controller/utils.py +++ b/homeassistant/components/homekit_controller/utils.py @@ -77,9 +77,7 @@ async def async_get_controller(hass: HomeAssistant) -> Controller: # Right now _async_stop_homekit_controller is only called on HA exiting # So we don't have to worry about leaking a callback here. - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller) await controller.async_start() diff --git a/homeassistant/components/isy994/__init__.py b/homeassistant/components/isy994/__init__.py index db72cc45a30..0c238182849 100644 --- a/homeassistant/components/isy994/__init__.py +++ b/homeassistant/components/isy994/__init__.py @@ -166,9 +166,7 @@ async def async_setup_entry( entry.async_on_unload(entry.add_update_listener(_async_update_listener)) entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_stop_auto_update, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_auto_update) ) # Register Integration-wide Services: diff --git a/homeassistant/components/logbook/helpers.py b/homeassistant/components/logbook/helpers.py index 8ec953a0afd..4fa0da9033a 100644 --- a/homeassistant/components/logbook/helpers.py +++ b/homeassistant/components/logbook/helpers.py @@ -176,8 +176,7 @@ def async_subscribe_events( target, entities_filter, entity_ids, device_ids ) subscriptions.extend( - hass.bus.async_listen(event_type, event_forwarder, run_immediately=True) - for event_type in event_types + hass.bus.async_listen(event_type, event_forwarder) for event_type in event_types ) if device_ids and not entity_ids: @@ -211,7 +210,6 @@ def async_subscribe_events( hass.bus.async_listen( EVENT_STATE_CHANGED, _forward_state_events_filtered, - run_immediately=True, ) ) diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index 98653ba19ad..b8f1ec08fe0 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -219,9 +219,7 @@ class MatrixBot: loop_sleep_time=1_000, ) # milliseconds. - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_START, handle_startup, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup) def _load_commands(self, commands: list[ConfigCommand]) -> None: for command in commands: diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 87b158f80c3..4f86654a7d3 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -241,7 +241,6 @@ class PersonStorageCollection(collection.DictStorageCollection): er.EVENT_ENTITY_REGISTRY_UPDATED, self._entity_registry_updated, event_filter=self._entity_registry_filter, - run_immediately=True, ) @callback diff --git a/homeassistant/components/recorder/core.py b/homeassistant/components/recorder/core.py index 1780436168d..cc412c88612 100644 --- a/homeassistant/components/recorder/core.py +++ b/homeassistant/components/recorder/core.py @@ -333,7 +333,6 @@ class Recorder(threading.Thread): self._event_listener = self.hass.bus.async_listen( MATCH_ALL, _event_listener, - run_immediately=True, ) self._queue_watcher = async_track_time_interval( self.hass, @@ -478,12 +477,8 @@ class Recorder(threading.Thread): def async_register(self) -> None: """Post connection initialize.""" bus = self.hass.bus - bus.async_listen_once( - EVENT_HOMEASSISTANT_CLOSE, self._async_close, run_immediately=True - ) - bus.async_listen_once( - EVENT_HOMEASSISTANT_FINAL_WRITE, self._async_shutdown, run_immediately=True - ) + bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, self._async_close) + bus.async_listen_once(EVENT_HOMEASSISTANT_FINAL_WRITE, self._async_shutdown) async_at_started(self.hass, self._async_hass_started) @callback diff --git a/homeassistant/components/recorder/entity_registry.py b/homeassistant/components/recorder/entity_registry.py index 1c0299fc8da..07f8f2f88de 100644 --- a/homeassistant/components/recorder/entity_registry.py +++ b/homeassistant/components/recorder/entity_registry.py @@ -47,7 +47,6 @@ def async_setup(hass: HomeAssistant) -> None: er.EVENT_ENTITY_REGISTRY_UPDATED, _async_entity_id_changed, event_filter=entity_registry_changed_filter, - run_immediately=True, ) async_at_start(hass, _setup_entity_registry_event_handler) diff --git a/homeassistant/components/rfxtrx/__init__.py b/homeassistant/components/rfxtrx/__init__.py index 78b7daa8347..6f0e5932adc 100644 --- a/homeassistant/components/rfxtrx/__init__.py +++ b/homeassistant/components/rfxtrx/__init__.py @@ -280,9 +280,7 @@ 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, run_immediately=True - ) + hass.bus.async_listen(dr.EVENT_DEVICE_REGISTRY_UPDATED, _updated_device) ) def _shutdown_rfxtrx(event: Event) -> None: diff --git a/homeassistant/components/samsungtv/__init__.py b/homeassistant/components/samsungtv/__init__.py index 68a58710c19..9dcb2f9f57e 100644 --- a/homeassistant/components/samsungtv/__init__.py +++ b/homeassistant/components/samsungtv/__init__.py @@ -149,9 +149,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await bridge.async_close_remote() entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, stop_bridge, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_bridge) ) await _async_update_ssdp_locations(hass, entry) diff --git a/homeassistant/components/shelly/coordinator.py b/homeassistant/components/shelly/coordinator.py index c52585c3363..18f96dd9c2e 100644 --- a/homeassistant/components/shelly/coordinator.py +++ b/homeassistant/components/shelly/coordinator.py @@ -189,9 +189,7 @@ class ShellyBlockCoordinator(ShellyCoordinatorBase[BlockDevice]): self.async_add_listener(self._async_device_updates_handler) ) entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) ) @callback @@ -420,9 +418,7 @@ class ShellyRpcCoordinator(ShellyCoordinatorBase[RpcDevice]): self._input_event_listeners: list[Callable[[dict[str, Any]], None]] = [] entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._handle_ha_stop) ) entry.async_on_unload(entry.add_update_listener(self._async_update_listener)) diff --git a/homeassistant/components/sonos/__init__.py b/homeassistant/components/sonos/__init__.py index 0eab8dcc779..2049cb4c8c7 100644 --- a/homeassistant/components/sonos/__init__.py +++ b/homeassistant/components/sonos/__init__.py @@ -577,7 +577,6 @@ class SonosDiscoveryManager: self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, self._async_stop_event_listener, - run_immediately=True, ) ) _LOGGER.debug("Adding discovery job") @@ -586,7 +585,6 @@ class SonosDiscoveryManager: self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, self._stop_manual_heartbeat, - run_immediately=True, ) ) await self.async_poll_manual_hosts() diff --git a/homeassistant/components/ssdp/__init__.py b/homeassistant/components/ssdp/__init__.py index 08d1bbb858e..1678daf4059 100644 --- a/homeassistant/components/ssdp/__init__.py +++ b/homeassistant/components/ssdp/__init__.py @@ -392,9 +392,7 @@ class Scanner: await self._async_start_ssdp_listeners() - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self.async_stop, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) self._cancel_scan = async_track_time_interval( self.hass, self.async_scan, SCAN_INTERVAL, name="SSDP scanner" ) @@ -755,13 +753,10 @@ class Server: async def async_start(self) -> None: """Start the server.""" bus = self.hass.bus - bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self.async_stop, run_immediately=True - ) + bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self._async_start_upnp_servers, - run_immediately=True, ) async def _async_get_instance_udn(self) -> str: diff --git a/homeassistant/components/stream/__init__.py b/homeassistant/components/stream/__init__.py index 44cf9177993..64c520150c2 100644 --- a/homeassistant/components/stream/__init__.py +++ b/homeassistant/components/stream/__init__.py @@ -214,7 +214,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # Only pass through PyAV log messages if stream logging is above DEBUG cancel_logging_listener = hass.bus.async_listen( - EVENT_LOGGING_CHANGED, update_pyav_logging, run_immediately=True + EVENT_LOGGING_CHANGED, update_pyav_logging ) # libav.mp4 and libav.swscaler have a few unimportant messages that are logged # at logging.WARNING. Set those Logger levels to logging.ERROR @@ -266,7 +266,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: _LOGGER.debug("Stopped stream workers") cancel_logging_listener() - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown, run_immediately=True) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, shutdown) return True diff --git a/homeassistant/components/template/coordinator.py b/homeassistant/components/template/coordinator.py index 47de31d07c2..d2ce44a0ad1 100644 --- a/homeassistant/components/template/coordinator.py +++ b/homeassistant/components/template/coordinator.py @@ -47,7 +47,7 @@ class TriggerUpdateCoordinator(DataUpdateCoordinator): await self._attach_triggers() else: self._unsub_start = self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_START, self._attach_triggers, run_immediately=True + EVENT_HOMEASSISTANT_START, self._attach_triggers ) for platform_domain in PLATFORMS: diff --git a/homeassistant/components/trace/__init__.py b/homeassistant/components/trace/__init__.py index 17fdf20368a..03b1845d6a8 100644 --- a/homeassistant/components/trace/__init__.py +++ b/homeassistant/components/trace/__init__.py @@ -68,9 +68,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: _LOGGER.error("Error storing traces", exc_info=exc) # Store traces when stopping hass - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_store_traces_at_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_store_traces_at_stop) return True diff --git a/homeassistant/components/unifiprotect/__init__.py b/homeassistant/components/unifiprotect/__init__.py index 5943cc4f85e..d85f91be860 100644 --- a/homeassistant/components/unifiprotect/__init__.py +++ b/homeassistant/components/unifiprotect/__init__.py @@ -110,9 +110,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data.setdefault(DOMAIN, {})[entry.entry_id] = data_service entry.async_on_unload(entry.add_update_listener(_async_options_updated)) entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, data_service.async_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, data_service.async_stop) ) if not entry.options.get(CONF_ALLOW_EA, False) and ( diff --git a/homeassistant/components/usb/__init__.py b/homeassistant/components/usb/__init__.py index 48697c98ae7..959a8f5894c 100644 --- a/homeassistant/components/usb/__init__.py +++ b/homeassistant/components/usb/__init__.py @@ -207,12 +207,8 @@ class USBDiscovery: async def async_setup(self) -> None: """Set up USB Discovery.""" await self._async_start_monitor() - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STARTED, self.async_start, run_immediately=True - ) - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self.async_stop, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, self.async_start) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self.async_stop) async def async_start(self, event: Event) -> None: """Start USB Discovery and run a manual scan.""" @@ -242,9 +238,7 @@ class USBDiscovery: def _stop_observer(event: Event) -> None: observer.stop() - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _stop_observer, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop_observer) self.observer_active = True def _get_monitor_observer(self) -> MonitorObserver | None: diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 7f30c08c076..54539158148 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -165,7 +165,7 @@ def handle_subscribe_events( ) connection.subscriptions[msg["id"]] = hass.bus.async_listen( - event_type, forward_events, run_immediately=True + event_type, forward_events ) connection.send_result(msg["id"]) @@ -410,7 +410,6 @@ def handle_subscribe_entities( connection.user, msg["id"], ), - run_immediately=True, ) connection.send_result(msg["id"]) diff --git a/homeassistant/components/websocket_api/http.py b/homeassistant/components/websocket_api/http.py index 83d68ee21ea..fc75b46ddbd 100644 --- a/homeassistant/components/websocket_api/http.py +++ b/homeassistant/components/websocket_api/http.py @@ -292,7 +292,7 @@ class WebSocketHandler: self._handle_task = asyncio.current_task() unsub_stop = hass.bus.async_listen( - EVENT_HOMEASSISTANT_STOP, self._async_handle_hass_stop, run_immediately=True + EVENT_HOMEASSISTANT_STOP, self._async_handle_hass_stop ) writer = wsock._writer # pylint: disable=protected-access diff --git a/homeassistant/components/wemo/__init__.py b/homeassistant/components/wemo/__init__.py index 4eb33a09553..7d068cbd5bf 100644 --- a/homeassistant/components/wemo/__init__.py +++ b/homeassistant/components/wemo/__init__.py @@ -96,9 +96,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: discovery_responder.stop() registry.stop() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _on_hass_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_hass_stop) yaml_config = config.get(DOMAIN, {}) hass.data[DOMAIN] = WemoData( diff --git a/homeassistant/components/yalexs_ble/__init__.py b/homeassistant/components/yalexs_ble/__init__.py index f608da6cd60..8c9c5176003 100644 --- a/homeassistant/components/yalexs_ble/__init__.py +++ b/homeassistant/components/yalexs_ble/__init__.py @@ -127,9 +127,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) entry.async_on_unload(entry.add_update_listener(_async_update_listener)) entry.async_on_unload( - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_shutdown, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_shutdown) ) return True diff --git a/homeassistant/components/zeroconf/__init__.py b/homeassistant/components/zeroconf/__init__.py index 5ebe1bb769e..7b4c06ffb62 100644 --- a/homeassistant/components/zeroconf/__init__.py +++ b/homeassistant/components/zeroconf/__init__.py @@ -169,9 +169,7 @@ async def _async_get_instance(hass: HomeAssistant, **zcargs: Any) -> HaAsyncZero # Wait to the close event to shutdown zeroconf to give # integrations time to send a good bye message - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_CLOSE, _async_stop_zeroconf, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, _async_stop_zeroconf) hass.data[DOMAIN] = aio_zc return aio_zc @@ -248,9 +246,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def _async_zeroconf_hass_stop(_event: Event) -> None: await discovery.async_stop() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_zeroconf_hass_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_zeroconf_hass_stop) async_when_setup_or_start(hass, "frontend", _async_zeroconf_hass_start) return True diff --git a/homeassistant/components/zone/__init__.py b/homeassistant/components/zone/__init__.py index eaee24376c7..2473200102d 100644 --- a/homeassistant/components/zone/__init__.py +++ b/homeassistant/components/zone/__init__.py @@ -288,9 +288,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Handle core config updated.""" await home_zone.async_update_config(_home_conf(hass)) - hass.bus.async_listen( - EVENT_CORE_CONFIG_UPDATE, core_config_updated, run_immediately=True - ) + hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, core_config_updated) hass.data[DOMAIN] = storage_collection diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index d7ffd697a2f..dd48c53160e 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -636,7 +636,6 @@ class ConfigEntry: self._async_cancel_retry_setup = hass.bus.async_listen( EVENT_HOMEASSISTANT_STARTED, functools.partial(self._async_setup_again, hass), - run_immediately=True, ) await self._async_process_on_unload(hass) @@ -1646,9 +1645,7 @@ class ConfigEntries: old_conf_migrate_func=_old_conf_migrator, ) - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._async_shutdown, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._async_shutdown) if config is None: self._entries = ConfigEntryItems(self.hass) diff --git a/homeassistant/core.py b/homeassistant/core.py index 113bbf7bf77..0cd7c29cf52 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -136,6 +136,7 @@ _P = ParamSpec("_P") _Ts = TypeVarTuple("_Ts") # Internal; not helpers.typing.UNDEFINED due to circular dependency _UNDEF: dict[Any, Any] = {} +_SENTINEL = object() _CallableT = TypeVar("_CallableT", bound=Callable[..., Any]) _DataT = TypeVar("_DataT", bound=Mapping[str, Any], default=Mapping[str, Any]) CALLBACK_TYPE = Callable[[], None] @@ -1355,7 +1356,6 @@ def _event_repr( _FilterableJobType = tuple[ HassJob[[Event[_DataT]], Coroutine[Any, Any, None] | None], # job Callable[[_DataT], bool] | None, # event_filter - bool, # run_immediately ] @@ -1399,9 +1399,7 @@ class EventBus: self._listeners[MATCH_ALL] = self._match_all_listeners self._hass = hass self._async_logging_changed() - self.async_listen( - EVENT_LOGGING_CHANGED, self._async_logging_changed, run_immediately=True - ) + self.async_listen(EVENT_LOGGING_CHANGED, self._async_logging_changed) @callback def _async_logging_changed(self, event: Event | None = None) -> None: @@ -1486,7 +1484,7 @@ class EventBus: event: Event[_DataT] | None = None - for job, event_filter, run_immediately in listeners: + for job, event_filter in listeners: if event_filter is not None: try: if event_data is None or not event_filter(event_data): @@ -1504,14 +1502,10 @@ class EventBus: context, ) - if run_immediately: - try: - self._hass.async_run_hass_job(job, event) - except Exception: # pylint: disable=broad-except - _LOGGER.exception("Error running job: %s", job) - else: - # pylint: disable-next=protected-access - self._hass._async_add_hass_job(job, event) + try: + self._hass.async_run_hass_job(job, event) + except Exception: # pylint: disable=broad-except + _LOGGER.exception("Error running job: %s", job) def listen( self, @@ -1539,7 +1533,7 @@ class EventBus: event_type: EventType[_DataT] | str, listener: Callable[[Event[_DataT]], Coroutine[Any, Any, None] | None], event_filter: Callable[[_DataT], bool] | None = None, - run_immediately: bool = True, + run_immediately: bool | object = _SENTINEL, ) -> CALLBACK_TYPE: """Listen for all events or events of a specific type. @@ -1556,6 +1550,16 @@ class EventBus: This method must be run in the event loop. """ + if run_immediately in (True, False): + # late import to avoid circular imports + from .helpers import frame # pylint: disable=import-outside-toplevel + + frame.report( + "calls `async_listen` with run_immediately, which is" + " deprecated and will be removed in Assistant 2025.5", + error_if_core=False, + ) + if event_filter is not None and not is_callback_check_partial(event_filter): raise HomeAssistantError(f"Event filter {event_filter} is not a callback") if event_type == EVENT_STATE_REPORTED: @@ -1563,16 +1567,11 @@ class EventBus: raise HomeAssistantError( f"Event filter is required for event {event_type}" ) - if not run_immediately: - raise HomeAssistantError( - f"Run immediately must be set to True for event {event_type}" - ) return self._async_listen_filterable_job( event_type, ( HassJob(listener, f"listen {event_type}"), event_filter, - run_immediately, ), ) @@ -1614,7 +1613,7 @@ class EventBus: self, event_type: EventType[_DataT] | str, listener: Callable[[Event[_DataT]], Coroutine[Any, Any, None] | None], - run_immediately: bool = True, + run_immediately: bool | object = _SENTINEL, ) -> CALLBACK_TYPE: """Listen once for event of a specific type. @@ -1625,6 +1624,16 @@ class EventBus: This method must be run in the event loop. """ + if run_immediately in (True, False): + # late import to avoid circular imports + from .helpers import frame # pylint: disable=import-outside-toplevel + + frame.report( + "calls `async_listen_once` with run_immediately, which is " + "deprecated and will be removed in Assistant 2025.5", + error_if_core=False, + ) + one_time_listener: _OneTimeListener[_DataT] = _OneTimeListener( self._hass, HassJob(listener) ) @@ -1637,7 +1646,6 @@ class EventBus: job_type=HassJobType.Callback, ), None, - run_immediately, ), ) one_time_listener.remove = remove diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 1384735e2fd..2734ab5e2e5 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -393,7 +393,6 @@ class AreaRegistry(BaseRegistry[AreasRegistryStoreData]): event_type=fr.EVENT_FLOOR_REGISTRY_UPDATED, event_filter=_removed_from_registry_filter, listener=_handle_floor_registry_update, - run_immediately=True, ) @callback @@ -410,7 +409,6 @@ class AreaRegistry(BaseRegistry[AreasRegistryStoreData]): event_type=lr.EVENT_LABEL_REGISTRY_UPDATED, event_filter=_removed_from_registry_filter, listener=_handle_label_registry_update, - run_immediately=True, ) diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index 2142cae854a..0270c8dc456 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -1207,7 +1207,6 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None: event_type=lr.EVENT_LABEL_REGISTRY_UPDATED, event_filter=_label_removed_from_registry_filter, listener=_handle_label_registry_update, - run_immediately=True, ) @callback @@ -1245,7 +1244,6 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None: entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, _async_entity_registry_changed, event_filter=entity_registry_changed_filter, - run_immediately=True, ) return @@ -1255,22 +1253,17 @@ def async_setup_cleanup(hass: HomeAssistant, dev_reg: DeviceRegistry) -> None: entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, _async_entity_registry_changed, event_filter=entity_registry_changed_filter, - run_immediately=True, ) await debounced_cleanup.async_call() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STARTED, startup_clean, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, startup_clean) @callback def _on_homeassistant_stop(event: Event) -> None: """Cancel debounced cleanup.""" debounced_cleanup.async_cancel() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _on_homeassistant_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_homeassistant_stop) def _normalize_connections(connections: set[tuple[str, str]]) -> set[tuple[str, str]]: diff --git a/homeassistant/helpers/discovery_flow.py b/homeassistant/helpers/discovery_flow.py index e24b405c685..b6633a3f718 100644 --- a/homeassistant/helpers/discovery_flow.py +++ b/homeassistant/helpers/discovery_flow.py @@ -82,9 +82,7 @@ class FlowDispatcher: @callback def async_setup(self) -> None: """Set up the flow disptcher.""" - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STARTED, self._async_start, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, self._async_start) async def _async_start(self, event: Event) -> None: """Start processing pending flows.""" diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index b764a29a686..f467b5683a9 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -120,9 +120,7 @@ class EntityComponent(Generic[_EntityT]): Note: this is only required if the integration never calls `setup` or `async_setup`. """ - self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._async_shutdown, run_immediately=True - ) + self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, self._async_shutdown) def setup(self, config: ConfigType) -> None: """Set up a full entity component. diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index 727dbda9c2d..d6e7395a2cb 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -683,7 +683,6 @@ class EntityRegistry(BaseRegistry): self.hass.bus.async_listen( EVENT_DEVICE_REGISTRY_UPDATED, self.async_device_modified, - run_immediately=True, ) @callback @@ -1492,7 +1491,6 @@ def _async_setup_cleanup(hass: HomeAssistant, registry: EntityRegistry) -> None: event_type=lr.EVENT_LABEL_REGISTRY_UPDATED, event_filter=_removed_from_registry_filter, listener=_handle_label_registry_update, - run_immediately=True, ) @callback @@ -1506,7 +1504,6 @@ def _async_setup_cleanup(hass: HomeAssistant, registry: EntityRegistry) -> None: event_type=cr.EVENT_CATEGORY_REGISTRY_UPDATED, event_filter=_removed_from_registry_filter, listener=_handle_category_registry_update, - run_immediately=True, ) @callback @@ -1525,9 +1522,7 @@ def _async_setup_cleanup(hass: HomeAssistant, registry: EntityRegistry) -> None: """Cancel cleanup.""" cancel() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _on_homeassistant_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _on_homeassistant_stop) @callback @@ -1553,7 +1548,6 @@ def _async_setup_entity_restore(hass: HomeAssistant, registry: EntityRegistry) - EVENT_ENTITY_REGISTRY_UPDATED, cleanup_restored_states, event_filter=cleanup_restored_states_filter, - run_immediately=True, ) if hass.is_running: @@ -1570,9 +1564,7 @@ def _async_setup_entity_restore(hass: HomeAssistant, registry: EntityRegistry) - entry.write_unavailable_state(hass) - hass.bus.async_listen( - EVENT_HOMEASSISTANT_START, _write_unavailable_states, run_immediately=True - ) + hass.bus.async_listen(EVENT_HOMEASSISTANT_START, _write_unavailable_states) async def async_migrate_entries( diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index b622534d571..3d51610010c 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -276,7 +276,6 @@ def async_track_state_change( EVENT_STATE_CHANGED, state_change_dispatcher, event_filter=state_change_filter, - run_immediately=True, ) @@ -419,7 +418,6 @@ def _async_track_event( tracker.event_type, ft.partial(tracker.dispatcher_callable, hass, callbacks), event_filter=ft.partial(tracker.filter_callable, hass, callbacks), - run_immediately=True, ) job = HassJob(action, f"track {tracker.event_type} event {keys}", job_type=job_type) diff --git a/homeassistant/helpers/integration_platform.py b/homeassistant/helpers/integration_platform.py index 6d474557748..70846156702 100644 --- a/homeassistant/helpers/integration_platform.py +++ b/homeassistant/helpers/integration_platform.py @@ -169,7 +169,6 @@ async def async_process_integration_platforms( hass, integration_platforms, ), - run_immediately=True, ) else: integration_platforms = hass.data[DATA_INTEGRATION_PLATFORMS] diff --git a/homeassistant/helpers/restore_state.py b/homeassistant/helpers/restore_state.py index 4970b89db1f..40c898fe1d2 100644 --- a/homeassistant/helpers/restore_state.py +++ b/homeassistant/helpers/restore_state.py @@ -252,7 +252,7 @@ class RestoreStateData: # Dump states when stopping hass self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_dump_states_at_stop, run_immediately=True + EVENT_HOMEASSISTANT_STOP, _async_dump_states_at_stop ) @callback diff --git a/homeassistant/helpers/start.py b/homeassistant/helpers/start.py index 839514cbf2d..70664430582 100644 --- a/homeassistant/helpers/start.py +++ b/homeassistant/helpers/start.py @@ -50,7 +50,7 @@ def _async_at_core_state( if unsub: unsub() - unsub = hass.bus.async_listen_once(event_type, _matched_event, run_immediately=True) + unsub = hass.bus.async_listen_once(event_type, _matched_event) return cancel diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 60e464d3985..20054274275 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -125,7 +125,6 @@ class _StoreManager: hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STARTED, self._async_schedule_cleanup, - run_immediately=True, ) @callback @@ -185,7 +184,6 @@ class _StoreManager: self._hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, self._async_cancel_and_cleanup, - run_immediately=True, ) @callback @@ -481,7 +479,6 @@ class Store(Generic[_T]): self._unsub_final_write_listener = self.hass.bus.async_listen_once( EVENT_HOMEASSISTANT_FINAL_WRITE, self._async_callback_final_write, - run_immediately=True, ) @callback diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 63a700e031b..501dd21d416 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -212,12 +212,8 @@ def async_setup(hass: HomeAssistant) -> bool: cancel = async_track_time_interval( hass, _async_adjust_lru_sizes, timedelta(minutes=10) ) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_START, _async_adjust_lru_sizes, run_immediately=True - ) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, callback(lambda _: cancel()), run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, _async_adjust_lru_sizes) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, callback(lambda _: cancel())) return True diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index 0d7365c25bd..76472327d97 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -136,7 +136,7 @@ class DataUpdateCoordinator(BaseDataUpdateCoordinatorProtocol, Generic[_DataT]): await self.async_shutdown() self._unsub_shutdown = self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _on_hass_stop, run_immediately=True + EVENT_HOMEASSISTANT_STOP, _on_hass_stop ) @callback diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 0516f78b198..9979ebeafd5 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -615,14 +615,11 @@ def _async_when_setup( EVENT_COMPONENT_LOADED, _matched_event, event_filter=_async_is_component_filter, - run_immediately=True, ) ) if start_event: listeners.append( - hass.bus.async_listen( - EVENT_HOMEASSISTANT_START, _matched_event, run_immediately=True - ) + hass.bus.async_listen(EVENT_HOMEASSISTANT_START, _matched_event) ) diff --git a/tests/common.py b/tests/common.py index 38af58642c9..ba106d70e00 100644 --- a/tests/common.py +++ b/tests/common.py @@ -299,7 +299,6 @@ async def async_test_home_assistant( hass.bus.async_listen_once( EVENT_HOMEASSISTANT_STOP, hass.config_entries._async_shutdown, - run_immediately=True, ) # Load the registries @@ -358,9 +357,7 @@ async def async_test_home_assistant( await asyncio.sleep(0) # Give aiohttp one loop iteration to close INSTANCES.remove(hass) - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_CLOSE, clear_instance, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_CLOSE, clear_instance) yield hass @@ -918,9 +915,7 @@ class MockEntityPlatform(entity_platform.EntityPlatform): def _async_on_stop(_: Event) -> None: self.async_shutdown() - hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, _async_on_stop, run_immediately=True - ) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_on_stop) class MockToggleEntity(entity.ToggleEntity): @@ -1493,7 +1488,7 @@ def async_capture_events(hass: HomeAssistant, event_name: str) -> list[Event]: def capture_events(event: Event) -> None: events.append(event) - hass.bus.async_listen(event_name, capture_events, run_immediately=True) + hass.bus.async_listen(event_name, capture_events) return events diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 439ca76d545..7805f3ea151 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -2513,9 +2513,7 @@ 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, run_immediately=True - ) + hass.bus.async_listen("automation_triggered", async_automation_triggered) 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 a3722b0646d..c78a455f089 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1174,7 +1174,7 @@ async def test_eventbus_run_immediately_callback(hass: HomeAssistant) -> None: """Mock listener.""" calls.append(event) - unsub = hass.bus.async_listen("test", listener, run_immediately=True) + unsub = hass.bus.async_listen("test", listener) hass.bus.async_fire("test", {"event": True}) # No async_block_till_done here @@ -1191,7 +1191,7 @@ async def test_eventbus_run_immediately_coro(hass: HomeAssistant) -> None: """Mock listener.""" calls.append(event) - unsub = hass.bus.async_listen("test", listener, run_immediately=True) + unsub = hass.bus.async_listen("test", listener) hass.bus.async_fire("test", {"event": True}) # No async_block_till_done here @@ -1208,7 +1208,7 @@ async def test_eventbus_listen_once_run_immediately_coro(hass: HomeAssistant) -> """Mock listener.""" calls.append(event) - hass.bus.async_listen_once("test", listener, run_immediately=True) + hass.bus.async_listen_once("test", listener) hass.bus.async_fire("test", {"event": True}) # No async_block_till_done here @@ -3343,9 +3343,7 @@ async def test_statemachine_report_state(hass: HomeAssistant) -> None: hass.states.async_set("light.bowl", "on", {}) state_changed_events = async_capture_events(hass, EVENT_STATE_CHANGED) state_reported_events = [] - hass.bus.async_listen( - EVENT_STATE_REPORTED, listener, event_filter=filter, run_immediately=True - ) + hass.bus.async_listen(EVENT_STATE_REPORTED, listener, event_filter=filter) hass.states.async_set("light.bowl", "on") await hass.async_block_till_done() @@ -3380,17 +3378,36 @@ async def test_report_state_listener_restrictions(hass: HomeAssistant) -> None: """Mock filter.""" return False - # run_immediately set to False - with pytest.raises(HomeAssistantError): - hass.bus.async_listen( - EVENT_STATE_REPORTED, listener, event_filter=filter, run_immediately=False - ) - # no filter with pytest.raises(HomeAssistantError): - hass.bus.async_listen(EVENT_STATE_REPORTED, listener, run_immediately=True) + hass.bus.async_listen(EVENT_STATE_REPORTED, listener) # Both filter and run_immediately - hass.bus.async_listen( - EVENT_STATE_REPORTED, listener, event_filter=filter, run_immediately=True - ) + hass.bus.async_listen(EVENT_STATE_REPORTED, listener, event_filter=filter) + + +@pytest.mark.parametrize( + "run_immediately", + [True, False], +) +@pytest.mark.parametrize( + "method", + ["async_listen", "async_listen_once"], +) +async def test_async_listen_with_run_immediately_deprecated( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + run_immediately: bool, + method: str, +) -> None: + """Test async_add_job warns about its deprecation.""" + + async def _test(event: ha.Event): + pass + + func = getattr(hass.bus, method) + func(EVENT_HOMEASSISTANT_START, _test, run_immediately=run_immediately) + assert ( + f"Detected code that calls `{method}` with run_immediately, which is " + "deprecated and will be removed in Assistant 2025.5." + ) in caplog.text diff --git a/tests/test_data_entry_flow.py b/tests/test_data_entry_flow.py index edba232eb69..ab82ef65269 100644 --- a/tests/test_data_entry_flow.py +++ b/tests/test_data_entry_flow.py @@ -399,7 +399,6 @@ async def test_show_progress(hass: HomeAssistant, manager) -> None: hass.bus.async_listen( data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESSED, capture_events, - run_immediately=True, ) result = await manager.async_init("test") @@ -479,7 +478,6 @@ async def test_show_progress_error(hass: HomeAssistant, manager) -> None: hass.bus.async_listen( data_entry_flow.EVENT_DATA_ENTRY_FLOW_PROGRESSED, capture_events, - run_immediately=True, ) result = await manager.async_init("test")