Skip to content

Commit

Permalink
Keep old data in case of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
Giga77 committed Mar 27, 2024
1 parent 3b2fca0 commit 135f3fe
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions custom_components/ecole_directe/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
# get_messages, session, None, year_data
# )
# except Exception as ex:
# self.data["messages"] = None
# _LOGGER.warning(
# "Error getting messages for family from ecole directe: %s", ex
# )
Expand Down Expand Up @@ -108,25 +107,23 @@ async def _async_update_data(self) -> dict[Platform, dict[str, Any]]:
_LOGGER.warning(
"Error getting homeworks from ecole directe: %s", ex
)
self.data[f"{eleve.get_fullname_lower()}_homework"] = {}
if "NOTES" in eleve.modules:
try:
self.data[
f"{eleve.get_fullname_lower()}_grades"
] = await self.hass.async_add_executor_job(
get_grades, session, eleve, year_data
)
self.compare_data(
previous_data,
f"{eleve.get_fullname_lower()}_grades",
["date", "subject", "grade_out_of"],
"new_grade",
eleve,
format_grade,
)
except Exception as ex:
_LOGGER.warning("Error getting grades from ecole directe: %s", ex)
self.data[f"{eleve.get_fullname_lower()}_grades"] = {}
self.compare_data(
previous_data,
f"{eleve.get_fullname_lower()}_grades",
["date", "subject", "grade_out_of"],
"new_grade",
eleve,
format_grade,
)
# if "MESSAGERIE" in eleve.modules:
# try:
# self.data[
Expand All @@ -150,24 +147,33 @@ def compare_data(
):
"""Compare data from previous session"""

if (
previous_data is not None
and data_key in previous_data
and data_key in self.data
):
not_found_items = []
for item in self.data[data_key]:
found = False
for previous_item in previous_data[data_key]:
if {
key: format_func(previous_item)[key] for key in compare_keys
} == {key: format_func(item)[key] for key in compare_keys}:
found = True
break
if found is False:
not_found_items.append(item)
for not_found_item in not_found_items:
self.trigger_event(event_type, eleve, format_func(not_found_item))
try:
if (
previous_data is not None
and data_key in previous_data
and data_key in self.data
):
not_found_items = []
for item in self.data[data_key]:
found = False
for previous_item in previous_data[data_key]:
if {
key: format_func(previous_item)[key] for key in compare_keys
} == {key: format_func(item)[key] for key in compare_keys}:
found = True
break
if found is False:
not_found_items.append(item)
for not_found_item in not_found_items:
self.trigger_event(event_type, eleve, format_func(not_found_item))
except Exception as ex:
_LOGGER.warning(
"Error comparing data: self[%s] previous_data[%s] data_key[%s] ex[%s]",
self,
previous_data,
data_key,
ex,
)

def trigger_event(self, event_type, eleve: EDEleve, event_data):
"""Trigger an event if there is new data"""
Expand Down

0 comments on commit 135f3fe

Please sign in to comment.