Clean up Sonos resubscription failure logic and logging (#50831)

This commit is contained in:
jjlawren 2021-05-18 22:15:16 -05:00 committed by GitHub
parent 3d5b354def
commit ab86c7a135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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."""