From 0dab42061ca2ad33cc74202acdf4f3c292d6a947 Mon Sep 17 00:00:00 2001 From: Lin Junrong Date: Mon, 31 Aug 2026 00:56:12 +0800 Subject: [PATCH] Use async_create_background_task for the songpal notification listener (#178000) --- homeassistant/components/songpal/media_player.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/songpal/media_player.py b/homeassistant/components/songpal/media_player.py index 6eddb716dd42..35f50d4cb44f 100644 --- a/homeassistant/components/songpal/media_player.py +++ b/homeassistant/components/songpal/media_player.py @@ -161,6 +161,11 @@ class SongpalEntity(MediaPlayerEntity): """Activate websocket for listening if wanted.""" _LOGGER.debug("Activating websocket connection") + # Narrowed once here rather than at each call site: the entity is only + # ever added from async_setup_entry, so the platform always has an entry. + entry = self.platform.config_entry + assert entry is not None + async def _volume_changed(volume: VolumeChange): _LOGGER.debug("Volume changed: %s", volume) self._volume = volume.volume @@ -218,7 +223,11 @@ class SongpalEntity(MediaPlayerEntity): # back from a disconnected state. await self.async_update_ha_state(force_refresh=True) - self.hass.loop.create_task(self._dev.listen_notifications()) + entry.async_create_background_task( + self.hass, + self._dev.listen_notifications(), + "songpal-listen-notifications", + ) _LOGGER.warning( "[%s(%s)] Connection reestablished", self.name, self._dev.endpoint ) @@ -234,7 +243,9 @@ class SongpalEntity(MediaPlayerEntity): self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, handle_stop) - self.hass.loop.create_task(self._dev.listen_notifications()) + entry.async_create_background_task( + self.hass, self._dev.listen_notifications(), "songpal-listen-notifications" + ) @property @override