From 4775c9cf26258b63233891e04ef09f1075a31421 Mon Sep 17 00:00:00 2001 From: jjlawren Date: Mon, 9 Jan 2023 21:59:59 -0600 Subject: [PATCH] Handle connection errors more gracefully Fixes #41 --- custom_components/sonos_cloud/__init__.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/custom_components/sonos_cloud/__init__.py b/custom_components/sonos_cloud/__init__.py index aa1b138..0761c90 100644 --- a/custom_components/sonos_cloud/__init__.py +++ b/custom_components/sonos_cloud/__init__.py @@ -72,7 +72,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.data[DOMAIN][SESSION] = session url = "https://api.ws.sonos.com/control/api/v1/households" - result = await session.async_request("get", url) + try: + result = await session.async_request("get", url) + except OSError as exc: + _LOGGER.error("Connection error requesting households: %s", exc) + raise ConfigEntryNotReady from exc + if result.status >= 400: body = await result.text() _LOGGER.error( @@ -87,7 +92,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_get_available_players(household: str) -> list[dict]: url = f"https://api.ws.sonos.com/control/api/v1/households/{household}/groups" - result = await session.async_request("get", url) + try: + result = await session.async_request("get", url) + except OSError as exc: + _LOGGER.error("Connection error requesting players: %s", exc) + raise ConfigEntryNotReady from exc if result.status >= 400: body = await result.text() _LOGGER.error(