From 2c9e6bd9275030c939ec1a68f53641bc8fa14e56 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Wed, 2 Jun 2021 23:10:27 -0500 Subject: [PATCH] Handle Sonos connection issues better when polling (#51376) --- homeassistant/components/sonos/entity.py | 6 +++++- homeassistant/components/sonos/media_player.py | 17 +++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/sonos/entity.py b/homeassistant/components/sonos/entity.py index 8c47c69b2d7..7d4e168c960 100644 --- a/homeassistant/components/sonos/entity.py +++ b/homeassistant/components/sonos/entity.py @@ -5,6 +5,7 @@ import datetime import logging from pysonos.core import SoCo +from pysonos.exceptions import SoCoException import homeassistant.helpers.device_registry as dr from homeassistant.helpers.dispatcher import ( @@ -70,7 +71,10 @@ class SonosEntity(Entity): self.speaker.subscription_address, ) self.speaker.is_first_poll = False - await self.async_update() # pylint: disable=no-member + try: + await self.async_update() # pylint: disable=no-member + except (OSError, SoCoException) as ex: + _LOGGER.debug("Error connecting to %s: %s", self.entity_id, ex) @property def soco(self) -> SoCo: diff --git a/homeassistant/components/sonos/media_player.py b/homeassistant/components/sonos/media_player.py index 133fe9efe8f..6d26363f83b 100644 --- a/homeassistant/components/sonos/media_player.py +++ b/homeassistant/components/sonos/media_player.py @@ -13,7 +13,7 @@ from pysonos.core import ( PLAY_MODE_BY_MEANING, PLAY_MODES, ) -from pysonos.exceptions import SoCoException, SoCoUPnPException +from pysonos.exceptions import SoCoUPnPException from pysonos.plugins.sharelink import ShareLinkPlugin import voluptuous as vol @@ -294,18 +294,15 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): return STATE_IDLE async def async_update(self) -> None: - """Retrieve latest state.""" + """Retrieve latest state by polling.""" await self.hass.async_add_executor_job(self._update) def _update(self) -> None: - """Retrieve latest state.""" - try: - self.speaker.update_groups() - self.speaker.update_volume() - if self.speaker.is_coordinator: - self.speaker.update_media() - except SoCoException: - pass + """Retrieve latest state by polling.""" + self.speaker.update_groups() + self.speaker.update_volume() + if self.speaker.is_coordinator: + self.speaker.update_media() @property def volume_level(self) -> float | None: