From ab86c7a135a25dbd7a94b46490fa1e3ce7a19b08 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Tue, 18 May 2021 22:15:16 -0500 Subject: [PATCH] Clean up Sonos resubscription failure logic and logging (#50831) --- homeassistant/components/sonos/speaker.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sonos/speaker.py b/homeassistant/components/sonos/speaker.py index e647fc2fd68..eb4e194403e 100644 --- a/homeassistant/components/sonos/speaker.py +++ b/homeassistant/components/sonos/speaker.py @@ -136,6 +136,7 @@ class SonosSpeaker: self._is_ready: bool = False self._subscriptions: list[SubscriptionBase] = [] + self._resubscription_lock: asyncio.Lock | None = None self._poll_timer: Callable | None = None self._seen_timer: Callable | None = None self._platforms_ready: set[str] = set() @@ -198,6 +199,7 @@ class SonosSpeaker: """Listen to new entities to trigger first subscription.""" self._platforms_ready.add(entity_type) if self._platforms_ready == PLATFORMS: + self._resubscription_lock = asyncio.Lock() await self.async_subscribe() self._is_ready = True @@ -313,11 +315,27 @@ class SonosSpeaker: self.async_write_entity_states() + async def async_resubscribe(self, exception: Exception) -> None: + """Attempt to resubscribe when a renewal failure is detected.""" + async with self._resubscription_lock: + if self.available: + if getattr(exception, "status", None) == 412: + _LOGGER.warning( + "Subscriptions for %s failed, speaker may have lost power", + self.zone_name, + ) + else: + _LOGGER.error( + "Subscription renewals for %s failed", + self.zone_name, + exc_info=exception, + ) + await self.async_unseen() + @callback def async_renew_failed(self, exception: Exception) -> None: """Handle a failed subscription renewal.""" - if self.available: - self.hass.async_add_job(self.async_unseen) + self.hass.async_create_task(self.async_resubscribe(exception)) async def async_unseen(self, now: datetime.datetime | None = None) -> None: """Make this player unavailable when it was not seen recently."""