Skip to content

Commit

Permalink
Attempt to better handle multiple households
Browse files Browse the repository at this point in the history
Fixes #35
  • Loading branch information
jjlawren committed Jan 10, 2023
1 parent 438d6de commit 2455c8c
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions custom_components/sonos_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
json = await result.json()
households = json.get("households")

async def async_get_available_players(household):
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)
if result.status >= 400:
Expand All @@ -95,7 +95,7 @@ async def async_get_available_players(household):
result.status,
body,
)
raise ConfigEntryNotReady
return []

json = await result.json()
_LOGGER.debug("Result: %s", json)
Expand All @@ -114,13 +114,17 @@ async def async_get_available_players(household):

for household in households:
household_id = household["id"]
players = await async_get_available_players(household_id)
_LOGGER.debug(
"Adding players for household %s: %s",
household_id,
[player["name"] for player in players],
)
hass.data[DOMAIN][PLAYERS].extend(players)
if players := await async_get_available_players(household_id):
_LOGGER.debug(
"Adding players for household %s: %s",
household_id,
[player["name"] for player in players],
)
hass.data[DOMAIN][PLAYERS].extend(players)

if not hass.data[DOMAIN][PLAYERS]:
_LOGGER.error("No players returned from household(s): %s", households)
raise ConfigEntryNotReady

hass.config_entries.async_setup_platforms(entry, PLATFORMS)

Expand Down

0 comments on commit 2455c8c

Please sign in to comment.