Skip to content

Commit

Permalink
load canteen calendar only if there was no exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrfilipmarek committed Dec 17, 2024
1 parent b9dad9b commit dcea12a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 8 additions & 1 deletion custom_components/homeassistantedupage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,15 @@ async def fetch_data():
timetable_data_canceled[current_date] = canceled_lessons

canteen_menu_data = {}
canteen_calendar_enabled = True
for offset in range(14):
current_date = today + timedelta(days=offset)
lunch = await edupage.get_lunches(current_date)
try:
lunch = await edupage.get_lunches(current_date)
except Exception as e:
_LOGGER.error(f"Failed to fetch lunch data for {current_date}: {e}")
lunch = None
canteen_calendar_enabled = False
meals_to_add = []
if lunch is not None and lunch.menus is not None and len(lunch.menus) > 0:
_LOGGER.debug(f"Lunch for {current_date}: {lunch}")
Expand All @@ -115,6 +121,7 @@ async def fetch_data():
"subjects": subjects,
"timetable": timetable_data,
"canteen_menu": canteen_menu_data,
"canteen_calendar_enabled": canteen_calendar_enabled,
"cancelled_lessons": timetable_data_canceled,
"notifications": notifications,
}
Expand Down
10 changes: 7 additions & 3 deletions custom_components/homeassistantedupage/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback

coordinator = hass.data[DOMAIN][entry.entry_id]

edupage_canteen_calendar = EdupageCanteenCalendar(coordinator, entry.data)
async_add_entities([edupage_canteen_calendar])

edupage_calendar = EdupageCalendar(coordinator, entry.data)
async_add_entities([edupage_calendar])

if coordinator.data.get("canteen_calendar_enabled", {}):
edupage_canteen_calendar = EdupageCanteenCalendar(coordinator, entry.data)
async_add_entities([edupage_canteen_calendar])
_LOGGER.debug("Canteen calendar added")
else:
_LOGGER.debug("Canteen calendar skipped, calendar disabled due to exceptions")

_LOGGER.debug("CALENDAR async_setup_entry finished.")

async def async_added_to_hass(self) -> None:
Expand Down

0 comments on commit dcea12a

Please sign in to comment.