mirror of
https://github.com/jdejaegh/irm-kmi-ha.git
synced 2025-06-27 03:35:56 +02:00
Keep the previous value for pollen and radar when the API fails only for those items but not for the main forecast
This commit is contained in:
parent
3c2bd51e85
commit
ac0fe07f4f
3 changed files with 19 additions and 9 deletions
|
@ -116,10 +116,8 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
|
|||
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:]
|
||||
|
@ -152,8 +150,9 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
|
|||
_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()
|
||||
|
||||
|
|
|
@ -8,6 +8,11 @@ from custom_components.irm_kmi.const import POLLEN_NAMES
|
|||
_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)
|
||||
|
@ -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
|
||||
|
@ -53,6 +59,11 @@ class PollenParser:
|
|||
"""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"""
|
||||
|
@ -128,4 +139,3 @@ class PollenParser:
|
|||
|
||||
_LOGGER.debug(f"Pollen data: {pollen_data}")
|
||||
return pollen_data
|
||||
|
||||
|
|
|
@ -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']
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue