-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
155 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,65 @@ | ||
import voluptuous as vol | ||
import logging | ||
from homeassistant.components.calendar import CalendarEventDevice | ||
from homeassistant.helpers.entity import generate_entity_id | ||
from homeassistant.util import dt as dt_util | ||
from datetime import datetime, timedelta | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed | ||
from homeassistant.core import HomeAssistant | ||
from .homeassistant_edupage import Edupage | ||
|
||
from homeassistant.components.calendar import CalendarEntity | ||
# Importiere die EduPage-API-Klassen, falls nötig | ||
# from edupage_api.models import Timetable, Lesson | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): | ||
"""Set up the EduPage Calendar platform.""" | ||
if discovery_info is None: | ||
return | ||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback): | ||
# Füge eine Instanz des Kalenders hinzu | ||
async_add_entities([EduPageCalendar(hass, discovery_info["name"], discovery_info["timetable_data"])]) | ||
|
||
username = entry.data["username"] | ||
password = entry.data["password"] | ||
subdomain = entry.data["subdomain"] | ||
|
||
edupage = Edupage(hass) | ||
unique_id = f"edupage_{username}_calendar" | ||
await hass.async_add_executor_job(edupage.login, username, password, subdomain) | ||
class EduPageCalendar(CalendarEventDevice): | ||
"""Representation of the EduPage Calendar.""" | ||
|
||
async def async_update_data(): | ||
def __init__(self, hass, name, timetable_data): | ||
"""Initialize the calendar.""" | ||
self._hass = hass | ||
self._name = name | ||
self._timetable_data = timetable_data | ||
self._events = [] | ||
|
||
today = datetime.now().date() | ||
try: | ||
return await edupage.get_timetable(today) | ||
except Exception as e: | ||
_LOGGER.error(f"error updating data: {e}") | ||
raise UpdateFailed(F"error updating data: {e}") | ||
|
||
async_add_entities([TimetableCalendar(edupage, unique_id)], True) | ||
|
||
class TimetableCalendar(CalendarEntity): | ||
def __init__(self, edupage, unique_id): | ||
self.edupage = edupage | ||
self._attr_unique_id = unique_id | ||
|
||
@property | ||
def name(self): | ||
"""return name of calendar""" | ||
"""Return the name of the calendar.""" | ||
return self._name | ||
|
||
async def async_get_events(self, today: datetime): | ||
timetable = self.get_timetable(today) | ||
# Konvertieren Sie 'timetable' in eine Liste von Ereignissen, die von dieser Methode zurückgegeben werden | ||
return timetable #dict | ||
|
||
async def async_get_events(self, hass, start_date, end_date): | ||
"""Return all events in the specified date range.""" | ||
events = [] | ||
for lesson in self._timetable_data.lessons: | ||
start_time = datetime.combine(start_date, lesson.start_time) | ||
end_time = datetime.combine(start_date, lesson.end_time) | ||
|
||
# Füge das Event hinzu, wenn es im Zeitfenster liegt | ||
if start_time >= start_date and end_time <= end_date: | ||
event = { | ||
"title": lesson.subject.name, | ||
"start": start_time.isoformat(), | ||
"end": end_time.isoformat(), | ||
"description": f"Lehrer: {', '.join([t.name for t in lesson.teachers])}", | ||
} | ||
events.append(event) | ||
|
||
self._events = events | ||
return events | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Extra attributes of the calendar.""" | ||
return {"number_of_events": len(self._events)} | ||
|
||
@property | ||
def event(self): | ||
"""Return the next upcoming event.""" | ||
now = dt_util.now() | ||
for event in self._events: | ||
if datetime.fromisoformat(event["start"]) > now: | ||
return event | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters