From 9d7e99eeb76457257e08b24a945d610deaac7b74 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Sun, 8 Jan 2023 17:39:26 -0600 Subject: [PATCH] Handle timeouts in Sonos, reduce logging noise (#85461) --- homeassistant/components/sonos/__init__.py | 15 +++++++++------ homeassistant/components/sonos/helpers.py | 3 ++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/sonos/__init__.py b/homeassistant/components/sonos/__init__.py index 1c9ffc02647..095267072ac 100644 --- a/homeassistant/components/sonos/__init__.py +++ b/homeassistant/components/sonos/__init__.py @@ -11,6 +11,7 @@ import socket from typing import TYPE_CHECKING, Any, Optional, cast from urllib.parse import urlparse +from requests.exceptions import Timeout from soco import events_asyncio, zonegroupstate import soco.config as soco_config from soco.core import SoCo @@ -223,13 +224,13 @@ class SonosDiscoveryManager: async def async_subscription_failed(now: datetime.datetime) -> None: """Fallback logic if the subscription callback never arrives.""" - await sub.unsubscribe() _LOGGER.warning( "Subscription to %s failed, attempting to poll directly", ip_address ) try: + await sub.unsubscribe() await self.hass.async_add_executor_job(soco.zone_group_state.poll, soco) - except (OSError, SoCoException) as ex: + except (OSError, SoCoException, Timeout) as ex: _LOGGER.warning( "Fallback pollling to %s failed, setup cannot continue: %s", ip_address, @@ -322,8 +323,8 @@ class SonosDiscoveryManager: new_coordinator.setup(soco) coord_dict[soco.household_id] = new_coordinator speaker.setup(self.entry) - except (OSError, SoCoException): - _LOGGER.warning("Failed to add SonosSpeaker using %s", soco, exc_info=True) + except (OSError, SoCoException, Timeout) as ex: + _LOGGER.warning("Failed to add SonosSpeaker using %s: %s", soco, ex) async def async_poll_manual_hosts( self, now: datetime.datetime | None = None @@ -344,8 +345,10 @@ class SonosDiscoveryManager: get_sync_attributes, soco, ) - except OSError: - _LOGGER.warning("Could not get visible Sonos devices from %s", ip_addr) + except (OSError, SoCoException, Timeout) as ex: + _LOGGER.warning( + "Could not get visible Sonos devices from %s: %s", ip_addr, ex + ) else: if new_hosts := { x.ip_address diff --git a/homeassistant/components/sonos/helpers.py b/homeassistant/components/sonos/helpers.py index 552d104786e..f1805eda054 100644 --- a/homeassistant/components/sonos/helpers.py +++ b/homeassistant/components/sonos/helpers.py @@ -5,6 +5,7 @@ from collections.abc import Callable import logging from typing import TYPE_CHECKING, Any, TypeVar, overload +from requests.exceptions import Timeout from soco import SoCo from soco.exceptions import SoCoException, SoCoUPnPException from typing_extensions import Concatenate, ParamSpec @@ -65,7 +66,7 @@ def soco_error( args_soco = next((arg for arg in args if isinstance(arg, SoCo)), None) try: result = funct(self, *args, **kwargs) - except (OSError, SoCoException, SoCoUPnPException) as err: + except (OSError, SoCoException, SoCoUPnPException, Timeout) as err: error_code = getattr(err, "error_code", None) function = funct.__qualname__ if errorcodes and error_code in errorcodes: