From 2c980a4db9bbd7d88234e2253031abfda64983cd Mon Sep 17 00:00:00 2001 From: JelleV <3942301+jcjveraa@users.noreply.github.com> Date: Tue, 16 Apr 2024 11:25:02 +0200 Subject: [PATCH] fix errors due to missing data --- custom_components/knmi/coordinator.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/custom_components/knmi/coordinator.py b/custom_components/knmi/coordinator.py index 3ecf12e..4d4313b 100644 --- a/custom_components/knmi/coordinator.py +++ b/custom_components/knmi/coordinator.py @@ -55,14 +55,19 @@ async def _async_update_data(self) -> dict[str, Any]: ) # Do not throw an exception unless it's failed a few times to avoid excessive "unavailable" data # in HASS - if self.failed_update_count > FAILED_UPDATES_ALLOWANCE: - _LOGGER.error( - "Update failed %s times, above limit of %s! - %s", - self.failed_update_count, - FAILED_UPDATES_ALLOWANCE, - exception, - ) - raise UpdateFailed() from exception + if self.failed_update_count <= FAILED_UPDATES_ALLOWANCE: + # return the 'old' data on a failed update + _LOGGER.debug("Update failed, returning existing data") + return self.data + + # Data update failed too many times, raise an error + _LOGGER.error( + "Update failed %s times, above limit of %s! - %s", + self.failed_update_count, + FAILED_UPDATES_ALLOWANCE, + exception, + ) + raise UpdateFailed() from exception def get_value(self, path: list[int | str], default=None) -> Any: """