Skip to content

Commit d1c4758

Browse files
committed
Add handler for login error for API. Retain last data for up to 10 timeouts from Haywards API to prevent unavailable message in #15. Ready for release and testing.
1 parent 4dd4843 commit d1c4758

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

custom_components/omnilogic/common.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from datetime import timedelta
44
import logging
5+
6+
from importlib_metadata import NullFinder
57
import async_timeout
68

7-
from omnilogic import OmniLogic, OmniLogicException
9+
from omnilogic import OmniLogic, OmniLogicException, LoginException
810

911
from homeassistant.config_entries import ConfigEntry
1012
from homeassistant.core import HomeAssistant
@@ -35,6 +37,8 @@ def __init__(
3537
"""Initialize the global Omnilogic data updater."""
3638
self.api = api
3739
self.config_entry = config_entry
40+
self._last_data = None
41+
self._timeout_count = 0
3842

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

56+
self._timeout_count = 0
57+
5258
except OmniLogicException as error:
53-
raise ConfigEntryNotReady(f"Error updating from OmniLogic: {error}") from error
59+
raise UpdateFailed(f"Error updating from OmniLogic: {error}") from error
60+
61+
except LoginException as error:
62+
raise UpdateFailed(f"Login failed for Omnilogic: {error}") from error
5463

5564
except TimeoutError as error:
56-
raise ConfigEntryNotReady(f"Timeout updating OmniLogic from cloud: {error}") from error
65+
self._timeout_count += 1
66+
67+
if self._timeout_count > 10 or not self._last_data:
68+
raise UpdateFailed(f"Timeout updating OmniLogic from cloud: {error}") from error
69+
else:
70+
data = self._last_data
5771

5872
parsed_data = {}
73+
self._last_data = data
5974

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

0 commit comments

Comments
 (0)