Skip to content

Commit

Permalink
Gracefully handle timeout error in OTE request
Browse files Browse the repository at this point in the history
  • Loading branch information
rnovacek committed Oct 4, 2023
1 parent 29edeec commit 6f64669
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions custom_components/cz_energy_spot_prices/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,27 @@ async def fetch_data(self):
gas=DailySpotRateData(gas_rates, zoneinfo=zoneinfo),
)

def retry_maybe(self):
def retry_maybe(self, exc_info: Exception | None=None):
try:
delay = self._retry_attempt_delays[self._retry_attempt]
except IndexError:
delay = None

self._retry_attempt += 1
if delay is not None:
logger.exception('OTE request failed %d times, retrying in %d seconds', self._retry_attempt, delay)
logger.error('OTE request failed %d times, retrying in %d seconds', self._retry_attempt, delay, exc_info=exc_info)
event.async_call_later(self.hass, delay=delay, action=self.update_data)
else:
logger.exception('OTE request failed %d times, not retrying', self._retry_attempt)
logger.error('OTE request failed %d times, not retrying', self._retry_attempt, exc_info=exc_info)

async def update_data(self, dt):
logger.debug('SpotRateCoordinator.update_data %s', dt)
try:
self._spot_rate_data = await self.fetch_data()
self.async_set_updated_data(self._spot_rate_data)

except OTEFault:
self.retry_maybe()
except (OTEFault, asyncio.TimeoutError) as e:
self.retry_maybe(exc_info=e)

except Exception:
logger.exception('OTE request failed unexpectedly, not retrying')
Expand All @@ -261,8 +261,8 @@ async def _async_update_data(self):
try:
self._spot_rate_data = await self.fetch_data()
logger.debug('SpotRateCoordinator._async_update_data fetched data: %s', self._spot_rate_data)
except OTEFault:
self.retry_maybe()
except (OTEFault, asyncio.TimeoutError) as e:
self.retry_maybe(exc_info=e)
except Exception:
logger.exception('OTE request failed unexpectedly during intial load')

Expand Down

0 comments on commit 6f64669

Please sign in to comment.