diff --git a/homeassistant/components/sonos/__init__.py b/homeassistant/components/sonos/__init__.py index 48c4a614ca4..028c412cd75 100644 --- a/homeassistant/components/sonos/__init__.py +++ b/homeassistant/components/sonos/__init__.py @@ -35,6 +35,7 @@ from homeassistant.helpers import ( from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_call_later, async_track_time_interval from homeassistant.helpers.typing import ConfigType +from homeassistant.util.async_ import create_eager_task from .alarms import SonosAlarms from .const import ( @@ -320,11 +321,15 @@ class SonosDiscoveryManager: zgs.total_requests, ) await asyncio.gather( - *(speaker.async_offline() for speaker in self.data.discovered.values()) + *( + create_eager_task(speaker.async_offline()) + for speaker in self.data.discovered.values() + ) ) if events_asyncio.event_listener: await events_asyncio.event_listener.async_stop() + @callback def _stop_manual_heartbeat(self, event: Event | None = None) -> None: if self.data.hosts_heartbeat: self.data.hosts_heartbeat() @@ -569,14 +574,18 @@ class SonosDiscoveryManager: await self.hass.config_entries.async_forward_entry_setups(self.entry, PLATFORMS) self.entry.async_on_unload( self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._async_stop_event_listener + EVENT_HOMEASSISTANT_STOP, + self._async_stop_event_listener, + run_immediately=True, ) ) _LOGGER.debug("Adding discovery job") if self.hosts: self.entry.async_on_unload( self.hass.bus.async_listen_once( - EVENT_HOMEASSISTANT_STOP, self._stop_manual_heartbeat + EVENT_HOMEASSISTANT_STOP, + self._stop_manual_heartbeat, + run_immediately=True, ) ) await self.async_poll_manual_hosts()