Skip to content

Commit

Permalink
Add HTTP error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlawren committed Jun 16, 2022
1 parent 11a24d2 commit c6c87e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions custom_components/sonos_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
9 changes: 9 additions & 0 deletions custom_components/sonos_cloud/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit c6c87e4

Please sign in to comment.