Skip to content

Commit

Permalink
Keep the previous value for pollen and radar when the API fails only …
Browse files Browse the repository at this point in the history
…for those items but not for the main forecast
  • Loading branch information
jdejaegh committed May 10, 2024
1 parent 3c2bd51 commit ac0fe07
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 5 additions & 6 deletions custom_components/irm_kmi/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ async def _async_animation_data(self, api_data: dict) -> RadarAnimationData:
try:
images_from_api = await self.download_images_from_api(animation_data, country, localisation_layer_url)
except IrmKmiApiError as err:
_LOGGER.warning(f"Could not get images for weather radar: {err}")
# TODO idea return self.data.get('animation', RadarAnimationData())
# this way, when we cannot update, we keep the old data
return RadarAnimationData()
_LOGGER.warning(f"Could not get images for weather radar: {err}. Keep the existing radar data.")
return self.data.get('animation', RadarAnimationData()) if self.data is not None else RadarAnimationData()

localisation = images_from_api[0]
images_from_api = images_from_api[1:]
Expand Down Expand Up @@ -152,8 +150,9 @@ async def _async_pollen_data(self, api_data: dict) -> dict:
_LOGGER.debug(f"Requesting pollen SVG at url {svg_url}")
pollen_svg: str = await self._api_client.get_svg(svg_url)
except IrmKmiApiError as err:
_LOGGER.warning(f"Could not get pollen data from the API: {err}")
return PollenParser.get_default_data()
_LOGGER.warning(f"Could not get pollen data from the API: {err}. Keeping the same data.")
return self.data.get('pollen', PollenParser.get_unavailable_data()) \
if self.data is not None else PollenParser.get_unavailable_data()

return PollenParser(pollen_svg).get_pollen_data()

Expand Down
12 changes: 11 additions & 1 deletion custom_components/irm_kmi/pollen.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
_LOGGER = logging.getLogger(__name__)


def get_unavailable_data() -> dict:
"""Return all the known pollen with 'none' value"""
return {k.lower(): 'none' for k in POLLEN_NAMES}


class PollenParser:
"""
The SVG looks as follows (see test fixture for a real example)
Expand All @@ -25,6 +30,7 @@ class PollenParser:
For the color scale, look for a white dot (nearly) at the same level as the pollen name. From the white dot
horizontal position, determine the level
"""

def __init__(
self,
xml_string: str
Expand Down Expand Up @@ -53,6 +59,11 @@ def get_default_data() -> dict:
"""Return all the known pollen with 'none' value"""
return {k.lower(): 'none' for k in POLLEN_NAMES}

@staticmethod
def get_unavailable_data() -> dict:
"""Return all the known pollen with 'none' value"""
return {k.lower(): None for k in POLLEN_NAMES}

@staticmethod
def get_option_values() -> List[str]:
"""List all the values that the pollen can have"""
Expand Down Expand Up @@ -128,4 +139,3 @@ def get_pollen_data(self) -> dict:

_LOGGER.debug(f"Pollen data: {pollen_data}")
return pollen_data

5 changes: 3 additions & 2 deletions tests/test_pollen.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_svg_pollen_parsing():
assert data == {'birch': 'none', 'oak': 'active', 'hazel': 'none', 'mugwort': 'none', 'alder': 'active',
'grasses': 'none', 'ash': 'active'}


def test_pollen_options():
assert PollenParser.get_option_values() == ['active', 'green', 'yellow', 'orange', 'red', 'purple', 'none']

Expand All @@ -50,7 +51,7 @@ async def test_pollen_data_from_api(
assert result == expected


async def test_pollen_error_leads_to_default_values(
async def test_pollen_error_leads_to_unavailable_on_first_call(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_exception_irm_kmi_api_svg_pollen: AsyncMock
Expand All @@ -59,5 +60,5 @@ async def test_pollen_error_leads_to_default_values(
api_data = get_api_data("be_forecast_warning.json")

result = await coordinator._async_pollen_data(api_data)
expected = PollenParser.get_default_data()
expected = PollenParser.get_unavailable_data()
assert result == expected

0 comments on commit ac0fe07

Please sign in to comment.