Skip to content

Commit

Permalink
🔎 Improve context when cached/fetched data is invalid
Browse files Browse the repository at this point in the history
This could help #82 #79 #77
  • Loading branch information
kamaradclimber committed Jan 27, 2024
1 parent 76f70e0 commit e63b5fd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion custom_components/rte_ecowatt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ async def update_method(self):
so entities can quickly look up their data.
"""
body = None
loaded_from_cache = False
try:
previous_data = await self._custom_store.async_load()
if previous_data is not None:
Expand All @@ -172,6 +173,7 @@ async def update_method(self):
"Loading RTE ecowatt data from storage instead of querying api"
)
body = previous_data["body"]
loaded_from_cache = True
except Exception as e:
_LOGGER.warn(f"Impossible to load previous data: {e}")
if body is None:
Expand All @@ -186,7 +188,13 @@ async def update_method(self):
except Exception as e:
_LOGGER.exception("Error received while caching API data")
raise e
signals = json.loads(body)["signals"]
json_body = json.loads(body)
if "signals" not in json_body:
if loaded_from_cache:
raise Exception(f"Cached data is invalid. Removing `{self._custom_store.path}` and restart HA should help")
else:
raise Exception(f"Fetched data is invalid, it does not have 'signals' key. {body}")
signals = json_body["signals"]
# additional parsing
for day_data in signals:
parsed_time = datetime.strptime(day_data["jour"], "%Y-%m-%dT%H:%M:%S%z")
Expand Down

0 comments on commit e63b5fd

Please sign in to comment.