diff --git a/custom_components/sonos_cloud/__init__.py b/custom_components/sonos_cloud/__init__.py index f29b8f1..febe0ac 100644 --- a/custom_components/sonos_cloud/__init__.py +++ b/custom_components/sonos_cloud/__init__.py @@ -13,6 +13,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_entry_oauth2_flow, config_validation as cv from .const import DOMAIN, PLAYERS, SESSION @@ -69,12 +70,30 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: url = "https://api.ws.sonos.com/control/api/v1/households" result = await session.async_request("get", url) + if result.status >= 400: + body = await result.text() + _LOGGER.error( + "Household request failed (%s): %s", + result.status, + body, + ) + raise ConfigEntryNotReady + json = await result.json() households = json.get("households") async def async_get_available_players(household): url = f"https://api.ws.sonos.com/control/api/v1/households/{household}/groups" result = await session.async_request("get", url) + if result.status >= 400: + body = await result.text() + _LOGGER.error( + "Requesting devices failed (%s): %s", + result.status, + body, + ) + raise ConfigEntryNotReady + json = await result.json() _LOGGER.debug("Result: %s", json) all_players = json["players"] diff --git a/custom_components/sonos_cloud/media_player.py b/custom_components/sonos_cloud/media_player.py index 97fb516..53d1512 100644 --- a/custom_components/sonos_cloud/media_player.py +++ b/custom_components/sonos_cloud/media_player.py @@ -142,6 +142,15 @@ async def async_play_media( requests.append(session.async_request("post", url, json=data)) results = await asyncio.gather(*requests, return_exceptions=True) for result in results: + if result.status >= 400: + body = await result.text() + _LOGGER.error( + "Play request failed (%s): %s", + result.status, + body, + ) + continue + json = await result.json() _LOGGER.debug("Response for %s: %s", result.url, json)