Skip to content

Commit 45183b5

Browse files
krzysdabrobieniu
andauthored
Check AqIndex status before retrieving value (#569)
* Check AqIndex status before retrieving value * Add test * Do not trust GIOS API ;) --------- Co-authored-by: Maciej Bieniek <bieniu@users.noreply.github.com>
1 parent 4bddcc3 commit 45183b5

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

gios/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ async def async_update(self) -> GiosSensors:
143143
):
144144
sensor_data[ATTR_INDEX] = STATE_MAP[index_value]
145145

146-
if index_value := indexes.get("AqIndex", {}).get("Nazwa kategorii indeksu"):
146+
if (aq_index := indexes.get("AqIndex", {})).get(
147+
"Status indeksu ogólnego dla stacji pomiarowej"
148+
) and (index_value := aq_index.get("Nazwa kategorii indeksu")):
147149
data[ATTR_AQI.lower()] = {
148150
ATTR_NAME: ATTR_AQI,
149151
ATTR_VALUE: STATE_MAP[index_value],

tests/test_init.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,70 @@ async def test_invalid_station_id(
455455

456456
with pytest.raises(NoStationError, match="0 is not a valid measuring station ID"):
457457
await Gios.create(session, INVALID_STATION_ID)
458+
459+
460+
@pytest.mark.asyncio
461+
async def test_no_common_index(
462+
session: aiohttp.ClientSession,
463+
session_mock: aioresponses,
464+
stations: list[dict[str, Any]],
465+
station: list[dict[str, Any]],
466+
indexes: dict[str, Any],
467+
sensor_3759: dict[str, Any],
468+
sensor_3760: dict[str, Any],
469+
sensor_3761: dict[str, Any],
470+
sensor_3762: dict[str, Any],
471+
sensor_3764: dict[str, Any],
472+
sensor_3765: dict[str, Any],
473+
sensor_14688: dict[str, Any],
474+
) -> None:
475+
"""Test with valid data and valid first sensor's value."""
476+
indexes["AqIndex"]["Nazwa kategorii indeksu"] = "Brak indeksu"
477+
indexes["AqIndex"]["Status indeksu ogólnego dla stacji pomiarowej"] = False
478+
479+
session_mock.get(
480+
"https://api.gios.gov.pl/pjp-api/v1/rest/station/findAll?page=0&size=1000",
481+
payload=stations,
482+
)
483+
session_mock.get(
484+
f"https://api.gios.gov.pl/pjp-api/v1/rest/station/sensors/{VALID_STATION_ID}",
485+
payload=station,
486+
)
487+
session_mock.get(
488+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3759",
489+
payload=sensor_3759,
490+
)
491+
session_mock.get(
492+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3760",
493+
payload=sensor_3760,
494+
)
495+
session_mock.get(
496+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3761",
497+
payload=sensor_3761,
498+
)
499+
session_mock.get(
500+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3762",
501+
payload=sensor_3762,
502+
)
503+
session_mock.get(
504+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3764",
505+
payload=sensor_3764,
506+
)
507+
session_mock.get(
508+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/3765",
509+
payload=sensor_3765,
510+
status=HTTPStatus.BAD_REQUEST.value,
511+
)
512+
session_mock.get(
513+
"https://api.gios.gov.pl/pjp-api/v1/rest/data/getData/14688",
514+
payload=sensor_14688,
515+
)
516+
session_mock.get(
517+
f"https://api.gios.gov.pl/pjp-api/v1/rest/aqindex/getIndex/{VALID_STATION_ID}",
518+
payload=indexes,
519+
)
520+
521+
gios = await Gios.create(session, VALID_STATION_ID)
522+
data = await gios.async_update()
523+
524+
assert data.aqi is None

0 commit comments

Comments
 (0)