Cleanup sonos shutdown process (#113654)

This commit is contained in:
J. Nick Koston 2024-03-16 16:10:57 -10:00 committed by GitHub
parent 26c1b7e72e
commit 885abe2fda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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()