mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Support unloading/reloading Sonos (#54418)
This commit is contained in:
parent
c0a7fca628
commit
1eeb12ba1c
@ -138,6 +138,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
"""Unload a Sonos config entry."""
|
||||||
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
await hass.data[DATA_SONOS_DISCOVERY_MANAGER].async_shutdown()
|
||||||
|
hass.data.pop(DATA_SONOS)
|
||||||
|
hass.data.pop(DATA_SONOS_DISCOVERY_MANAGER)
|
||||||
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
class SonosDiscoveryManager:
|
class SonosDiscoveryManager:
|
||||||
"""Manage sonos discovery."""
|
"""Manage sonos discovery."""
|
||||||
|
|
||||||
@ -151,6 +160,11 @@ class SonosDiscoveryManager:
|
|||||||
self.hosts = hosts
|
self.hosts = hosts
|
||||||
self.discovery_lock = asyncio.Lock()
|
self.discovery_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
async def async_shutdown(self):
|
||||||
|
"""Stop all running tasks."""
|
||||||
|
await self._async_stop_event_listener()
|
||||||
|
self._stop_manual_heartbeat()
|
||||||
|
|
||||||
def _create_soco(self, ip_address: str, source: SoCoCreationSource) -> SoCo | None:
|
def _create_soco(self, ip_address: str, source: SoCoCreationSource) -> SoCo | None:
|
||||||
"""Create a soco instance and return if successful."""
|
"""Create a soco instance and return if successful."""
|
||||||
if ip_address in self.data.discovery_ignored:
|
if ip_address in self.data.discovery_ignored:
|
||||||
@ -171,7 +185,7 @@ class SonosDiscoveryManager:
|
|||||||
)
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def _async_stop_event_listener(self, event: Event) -> None:
|
async def _async_stop_event_listener(self, event: Event | None = None) -> None:
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
*(speaker.async_unsubscribe() for speaker in self.data.discovered.values()),
|
*(speaker.async_unsubscribe() for speaker in self.data.discovered.values()),
|
||||||
return_exceptions=True,
|
return_exceptions=True,
|
||||||
@ -179,7 +193,7 @@ class SonosDiscoveryManager:
|
|||||||
if events_asyncio.event_listener:
|
if events_asyncio.event_listener:
|
||||||
await events_asyncio.event_listener.async_stop()
|
await events_asyncio.event_listener.async_stop()
|
||||||
|
|
||||||
def _stop_manual_heartbeat(self, event: Event) -> None:
|
def _stop_manual_heartbeat(self, event: Event | None = None) -> None:
|
||||||
if self.data.hosts_heartbeat:
|
if self.data.hosts_heartbeat:
|
||||||
self.data.hosts_heartbeat()
|
self.data.hosts_heartbeat()
|
||||||
self.data.hosts_heartbeat = None
|
self.data.hosts_heartbeat = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user