First steps for better timeout handling

This commit is contained in:
Jules 2024-05-10 14:58:21 +02:00
parent 8aeb656360
commit 3c2bd51e85
Signed by: jdejaegh
GPG key ID: 99D6D184CA66933A
2 changed files with 10 additions and 7 deletions

View file

@ -71,7 +71,7 @@ class IrmKmiApiClient:
"""Get information from the API.""" """Get information from the API."""
try: try:
async with async_timeout.timeout(10): async with async_timeout.timeout(60):
response = await self._session.request( response = await self._session.request(
method=method, method=method,
url=f"{self._base_url if base_url is None else base_url}{path}", url=f"{self._base_url if base_url is None else base_url}{path}",

View file

@ -30,7 +30,8 @@ from .utils import disable_from_config, get_config_value
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
# TODO inherit from TimestampDataUpdateCoordinator and use the last update time to avoid having
# entities unavailable if API is down for a few minutes only (like 3x the update interval)
class IrmKmiCoordinator(DataUpdateCoordinator): class IrmKmiCoordinator(DataUpdateCoordinator):
"""Coordinator to update data from IRM KMI""" """Coordinator to update data from IRM KMI"""
@ -67,7 +68,7 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
try: try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already # Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator. # handled by the data update coordinator.
async with async_timeout.timeout(10): async with async_timeout.timeout(60):
api_data = await self._api_client.get_forecasts_coord( api_data = await self._api_client.get_forecasts_coord(
{'lat': zone.attributes[ATTR_LATITUDE], {'lat': zone.attributes[ATTR_LATITUDE],
'long': zone.attributes[ATTR_LONGITUDE]} 'long': zone.attributes[ATTR_LONGITUDE]}
@ -114,8 +115,10 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
try: try:
images_from_api = await self.download_images_from_api(animation_data, country, localisation_layer_url) images_from_api = await self.download_images_from_api(animation_data, country, localisation_layer_url)
except IrmKmiApiError: except IrmKmiApiError as err:
_LOGGER.warning(f"Could not get images for weather radar") _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() return RadarAnimationData()
localisation = images_from_api[0] localisation = images_from_api[0]
@ -148,8 +151,8 @@ class IrmKmiCoordinator(DataUpdateCoordinator):
try: try:
_LOGGER.debug(f"Requesting pollen SVG at url {svg_url}") _LOGGER.debug(f"Requesting pollen SVG at url {svg_url}")
pollen_svg: str = await self._api_client.get_svg(svg_url) pollen_svg: str = await self._api_client.get_svg(svg_url)
except IrmKmiApiError: except IrmKmiApiError as err:
_LOGGER.warning(f"Could not get pollen data from the API") _LOGGER.warning(f"Could not get pollen data from the API: {err}")
return PollenParser.get_default_data() return PollenParser.get_default_data()
return PollenParser(pollen_svg).get_pollen_data() return PollenParser(pollen_svg).get_pollen_data()