Skip to content

Commit

Permalink
Merge pull request #53 from rine77/52-respect-lesson-is_cancelled-att…
Browse files Browse the repository at this point in the history
…ribute

respect canceled lessons and don't create calender events
  • Loading branch information
rine77 authored Dec 9, 2024
2 parents ea5901d + 1d3cc33 commit 87b6d83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
19 changes: 15 additions & 4 deletions custom_components/homeassistantedupage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import asyncio
from datetime import datetime, timedelta
from edupage_api.exceptions import BadCredentialsException, CaptchaException, SecondFactorFailedException
from edupage_api.exceptions import BadCredentialsException, CaptchaException
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
Expand Down Expand Up @@ -72,21 +72,32 @@ async def fetch_data():
notifications = await edupage.get_notifications()

timetable_data = {}
timetable_data_canceled = {}
today = datetime.now().date()
for offset in range(14):
current_date = today + timedelta(days=offset)
timetable = await edupage.get_timetable(student, current_date)
if timetable:
_LOGGER.debug(f"Timetable for {current_date}: {timetable}")
timetable_data[current_date] = timetable
lessons_to_add = []
canceled_lessons = []
for lesson in timetable:
if not lesson.is_cancelled:
lessons_to_add.append(lesson)
else:
canceled_lessons.append(lesson)
if lessons_to_add:
_LOGGER.debug(f"Timetable for {current_date}: {lessons_to_add}")
timetable_data[current_date] = lessons_to_add
else:
_LOGGER.warning(f"INIT No timetable found for {current_date}")
if canceled_lessons:
timetable_data_canceled[current_date] = canceled_lessons

return_data = {
"student": {"id": student.person_id, "name": student.name},
"grades": grades,
"subjects": subjects,
"timetable": timetable_data,
"cancelled_lessons": timetable_data_canceled,
"notifications": notifications,
}
_LOGGER.debug(f"INIT Coordinator fetch_data returning: {return_data}")
Expand Down
30 changes: 20 additions & 10 deletions custom_components/homeassistantedupage/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ async def async_get_events(self, hass, start_date: datetime, end_date: datetime)

_LOGGER.debug(f"CALENDAR Fetching events between {start_date} and {end_date}")
timetable = self.coordinator.data.get("timetable", {})
timetable_canceled = self.coordinator.data.get("cancelled_lessons", {})
_LOGGER.debug(f"CALENDAR Coordinator data: {self.coordinator.data}")
_LOGGER.debug(f"CALENDAR Fetched timetable data: {timetable}")

Expand All @@ -92,20 +93,27 @@ async def async_get_events(self, hass, start_date: datetime, end_date: datetime)

current_date = start_date.date()
while current_date <= end_date.date():
day_timetable = timetable.get(current_date)
if day_timetable:
for lesson in day_timetable:

_LOGGER.debug(f"CALENDAR Lesson attributes: {vars(lesson)}")

events.append(
self.map_lesson_to_calender_event(lesson, current_date)
)
events.extend(self.get_events(timetable, current_date))
events.extend(self.get_events(timetable_canceled, current_date))
current_date += timedelta(days=1)

_LOGGER.debug(f"CALENDAR Fetched {len(events)} events from {start_date} to {end_date}")
return events

def get_events(self, timetable, current_date):
events = []
day_timetable = timetable.get(current_date)
if day_timetable:
for lesson in day_timetable:

_LOGGER.debug(f"CALENDAR Lesson attributes: {vars(lesson)}")

events.append(
self.map_lesson_to_calender_event(lesson, current_date)
)
return events


def map_lesson_to_calender_event(self, lesson: Lesson, day: date) -> CalendarEvent:
room = "Unknown"
if lesson.classrooms:
Expand All @@ -117,10 +125,12 @@ def map_lesson_to_calender_event(self, lesson: Lesson, day: date) -> CalendarEve
local_tz = ZoneInfo(self.hass.config.time_zone)
start_time = datetime.combine(day, lesson.start_time).astimezone(local_tz)
end_time = datetime.combine(day, lesson.end_time).astimezone(local_tz)
lesson_subject = lesson.subject.name if lesson.subject else "Unknown Subject"
lesson_subject_prefix = "[Canceled] " if lesson.is_cancelled else ""
cal_event = CalendarEvent(
start=start_time,
end=end_time,
summary=lesson.subject.name if lesson.subject else "Unknown Subject",
summary= lesson_subject_prefix + lesson_subject,
description=f"Room: {room}\nTeacher(s): {teachers}",
location=room
)
Expand Down

0 comments on commit 87b6d83

Please sign in to comment.