Skip to content

Commit

Permalink
Add handler for login error for API. Retain last data for up to 10 ti…
Browse files Browse the repository at this point in the history
…meouts from Haywards API to prevent unavailable message in #15. Ready for release and testing.
  • Loading branch information
djtimca committed Jun 11, 2022
1 parent 4dd4843 commit d1c4758
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions custom_components/omnilogic/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from datetime import timedelta
import logging

from importlib_metadata import NullFinder
import async_timeout

from omnilogic import OmniLogic, OmniLogicException
from omnilogic import OmniLogic, OmniLogicException, LoginException

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -35,6 +37,8 @@ def __init__(
"""Initialize the global Omnilogic data updater."""
self.api = api
self.config_entry = config_entry
self._last_data = None
self._timeout_count = 0

super().__init__(
hass=hass,
Expand All @@ -49,13 +53,24 @@ async def _async_update_data(self):
async with async_timeout.timeout(20):
data = await self.api.get_telemetry_data()

self._timeout_count = 0

except OmniLogicException as error:
raise ConfigEntryNotReady(f"Error updating from OmniLogic: {error}") from error
raise UpdateFailed(f"Error updating from OmniLogic: {error}") from error

except LoginException as error:
raise UpdateFailed(f"Login failed for Omnilogic: {error}") from error

except TimeoutError as error:
raise ConfigEntryNotReady(f"Timeout updating OmniLogic from cloud: {error}") from error
self._timeout_count += 1

if self._timeout_count > 10 or not self._last_data:
raise UpdateFailed(f"Timeout updating OmniLogic from cloud: {error}") from error
else:
data = self._last_data

parsed_data = {}
self._last_data = data

def get_item_data(item, item_kind, current_id, data):
"""Get data per kind of Omnilogic API item."""
Expand Down

0 comments on commit d1c4758

Please sign in to comment.