Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions pytile/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ async def async_get_tiles(self) -> dict[str, Tile]:
for tile_uuid in [tile["tile_id"] for tile in states["result"]]
}

results = await asyncio.gather(*details_tasks.values())

return {
tile_uuid: Tile(self._async_request, tile_data)
for tile_uuid, tile_data, in zip(details_tasks, results) # noqa: B905
}
results = await asyncio.gather(*details_tasks.values(), return_exceptions=True)

data = {}
for tile_uuid, result in zip(details_tasks, results):
if isinstance(result, RequestError):
if "412" in str(result):
# Tile Labels will return an HTTP 412 because they don't have
# additional details; we can safely ignore these errors and still
# track the Tile (without additional details):
continue
data[tile_uuid] = Tile(self._async_request, result)

return data

async def async_init(self) -> None:
"""Create a Tile session."""
Expand Down
42 changes: 42 additions & 0 deletions tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,48 @@ async def test_tile_as_dict(
aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_tile_label(
aresponses: ResponsesMockServer,
authenticated_tile_api_server: ResponsesMockServer,
tile_states_response: dict[str, Any],
) -> None:
"""Test a Tile label (which is ignored by this library).

This is a sanity check that a Tile Label, which returns an HTTP 412 when passed to
/api/v1/tiles/{TILE_UUID}, is handled correctly.

Args:
aresponses: An aresponses server.
authenticated_tile_api_server: A mock Tile API server connection.
tile_states_response: An API response payload.
"""
async with authenticated_tile_api_server:
authenticated_tile_api_server.add(
"production.tile-api.com",
"/api/v1/tiles/tile_states",
"get",
response=aiohttp.web_response.json_response(
tile_states_response, status=200
),
)
authenticated_tile_api_server.add(
"production.tile-api.com",
f"/api/v1/tiles/{TILE_TILE_UUID}",
"get",
response=aresponses.Response(text="", status=412),
)

async with aiohttp.ClientSession() as session:
api = await async_login(
TILE_EMAIL, TILE_PASSWORD, session, client_uuid=TILE_CLIENT_UUID
)
tiles = await api.async_get_tiles()
assert len(tiles) == 0

aresponses.assert_plan_strictly_followed()


@pytest.mark.asyncio
async def test_tile_update(
aresponses: ResponsesMockServer,
Expand Down