Skip to content

Commit

Permalink
Perform retries in coordinator
Browse files Browse the repository at this point in the history
  • Loading branch information
PimDoos committed Mar 3, 2025
1 parent 7eeef42 commit cd7cf25
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 2 additions & 0 deletions custom_components/sessy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
SESSY_CACHE_INTERVAL = "sessy_cache_interval"

ENTITY_ERROR_THRESHOLD = 5
COORDINATOR_RETRIES = 5
COORDINATOR_RETRY_DELAY = 1

SESSY_RELEASE_NOTES_URL = "https://www.sessy.nl/firmware-updates"
47 changes: 26 additions & 21 deletions custom_components/sessy/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""API Data Update Coordinator for Sessy"""
from __future__ import annotations

import asyncio
from datetime import timedelta
import logging

Expand All @@ -17,7 +18,7 @@
from sessypy.devices import SessyBattery, SessyCTMeter, SessyDevice, SessyMeter, SessyP1Meter
from sessypy.util import SessyConnectionException, SessyLoginException, SessyNotSupportedException

from .const import DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL_POWER, ENTITY_ERROR_THRESHOLD, SCAN_INTERVAL_OTA_CHECK, SCAN_INTERVAL_SCHEDULE
from .const import COORDINATOR_RETRIES, COORDINATOR_RETRY_DELAY, DEFAULT_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL_POWER, ENTITY_ERROR_THRESHOLD, SCAN_INTERVAL_OTA_CHECK, SCAN_INTERVAL_SCHEDULE
from .models import SessyConfigEntry
from .util import get_nested_key

Expand Down Expand Up @@ -138,26 +139,30 @@ async def _async_update_data(self):
This is the place to pre-process the data to lookup tables
so entities can quickly look up their data.
"""
try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(self.update_interval.seconds / 2):
data = await self._device_function()

contexts: list[SessyEntityContext] = set(self.async_contexts())
flattened_data = dict()
for context in contexts:
flattened_data[context] = context.apply(data)

self._raw_data = data
return flattened_data

except SessyLoginException as err:
# Raising ConfigEntryAuthFailed will cancel future updates
# and start a config flow with SOURCE_REAUTH (async_step_reauth)
raise ConfigEntryAuthFailed from err
except SessyConnectionException as err:
raise UpdateFailed(f"Error communicating with Sessy API: {err}")
for retry in range(COORDINATOR_RETRIES):
try:
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
async with async_timeout.timeout(self.update_interval.seconds / 2):
data = await self._device_function()

contexts: list[SessyEntityContext] = set(self.async_contexts())
flattened_data = dict()
for context in contexts:
flattened_data[context] = context.apply(data)

self._raw_data = data
return flattened_data

except SessyLoginException as err:
# Raising ConfigEntryAuthFailed will cancel future updates
# and start a config flow with SOURCE_REAUTH (async_step_reauth)
raise ConfigEntryAuthFailed from err
except SessyConnectionException as err:
await asyncio.sleep(COORDINATOR_RETRY_DELAY)
continue

raise UpdateFailed(f"Error communicating with Sessy API after {COORDINATOR_RETRIES} retries: {err}")

def get_data(self):
return self.data
Expand Down

0 comments on commit cd7cf25

Please sign in to comment.