Skip to content

Commit

Permalink
add homework event entity
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed Feb 20, 2025
1 parent 8ea7257 commit 4632521
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 35 deletions.
11 changes: 7 additions & 4 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,14 @@ def __init__(
self._stop_periodic_update: CALLBACK_TYPE | None = None

self.lesson_change_callback = None
self.homework_change_callback = None

def listen_on_lesson_change(self, callback) -> None:
def event_entity_listen(self, callback, id) -> None:
"""Listen for lesson change events."""
self.lesson_change_callback = callback
_LOGGER.debug("set lesson_change_callback")
if id == "lesson_change_event":
self.lesson_change_callback = callback
elif id == "homework_event":
self.homework_change_callback = callback

def start_periodic_update(self) -> None:
"""Start periodic execution of update method."""
Expand Down Expand Up @@ -495,7 +498,7 @@ async def _async_status_request(self) -> None:
if event["homework_id"] not in self.calendar_homework_ids:
self.calendar_homework_ids.append(event["homework_id"])

self.lesson_change_callback(
self.homework_change_callback(
"homework", {"homework_data": service}
)

Expand Down
6 changes: 4 additions & 2 deletions custom_components/webuntis/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
ICON_TODAY_END = "mdi:calendar-end"
ICON_CALENDER_HOMEWORK = "mdi:home-edit"
ICON_CALENDER_EXAM = "mdi:pen"
ICON_EVENT_ENTITY = "mdi:calendar-clock"
ICON_EVENT_LESSNON_CHANGE = "mdi:calendar-clock"
ICON_EVENT_HOMEWORK = "mdi:home-edit"

NAME_STATUS = "Class"
NAME_NEXT_CLASS = "Next Class"
Expand All @@ -43,7 +44,8 @@
NAME_TODAY_END = "Today school end"
NAME_CALENDER_HOMEWORK = "Webuntis Homework Calendar"
NAME_CALENDER_EXAM = "Webuntis Exam Calendar"
NAME_EVENT_ENTITY = "WebUntis Lesson Change"
NAME_EVENT_LESSNON_CHANGE = "WebUntis Lesson Change"
NAME_EVENT_HOMEWORK = "WebUntis Homework"


SCAN_INTERVAL = 10 * 60 # 10min
Expand Down
85 changes: 58 additions & 27 deletions custom_components/webuntis/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import WebUntisEntity
from .const import DOMAIN, ICON_EVENT_ENTITY, NAME_EVENT_ENTITY
from .const import (
DOMAIN,
ICON_EVENT_LESSNON_CHANGE,
NAME_EVENT_LESSNON_CHANGE,
ICON_EVENT_HOMEWORK,
NAME_EVENT_HOMEWORK,
)


async def async_setup_entry(
Expand All @@ -16,45 +22,70 @@ async def async_setup_entry(
) -> None:
"""Set up Example sensor based on a config entry."""
server = hass.data[DOMAIN][config_entry.unique_id]
async_add_entities([LessonChangeEventEntity(server)], True)
entities = [LessonChangeEventEntity(server)]
if server.timetable_source != "teacher":
entities.append(HomeworkEventEntity(server))
async_add_entities(
entities,
True,
)


class LessonChangeEventEntity(WebUntisEntity, EventEntity):
"""Representation of a Web Untis Event entity."""

_attr_event_types = [
"homework",
"code",
"rooms",
"teachers",
"cancelled",
"lesson_change",
]
_attr_translation_key = "lesson_change_event"

def __init__(self, server) -> None:
"""Set up the instance."""

super().__init__(
server=server,
type_name=NAME_EVENT_ENTITY,
icon=ICON_EVENT_ENTITY,
device_class=None,
)
class BaseUntisEventEntity(WebUntisEntity, EventEntity):
"""Base class for WebUntis event entities."""

def __init__(
self,
server,
name: str,
sensor_id: str,
icon: str,
event_types: list[str],
) -> None:
"""Initialize the base event entity."""
super().__init__(server=server, type_name=name, icon=icon, device_class=None)
self.sensor_id = sensor_id
self._server = server
self._attr_event_types = event_types
self._attr_translation_key = sensor_id

@callback
def _async_handle_event(self, event: str, data: dict) -> None:
"""Handle the demo button event."""
"""Handle incoming event and update state."""
self._trigger_event(event, data)
self.async_write_ha_state()

async def async_added_to_hass(self) -> None:
"""Register callbacks with your device API/library."""
self._server.listen_on_lesson_change(self._async_handle_event)
"""Register event listener with the server."""
self._server.event_entity_listen(self._async_handle_event, self.sensor_id)

@property
def available(self) -> bool:
"""Return sensor availability."""
return True


class LessonChangeEventEntity(BaseUntisEventEntity):
"""Event entity for lesson changes."""

def __init__(self, server) -> None:
super().__init__(
server=server,
name=NAME_EVENT_LESSNON_CHANGE,
sensor_id="lesson_change_event",
icon=ICON_EVENT_LESSNON_CHANGE,
event_types=["lesson_change", "rooms", "teachers", "cancelled", "code"],
)


class HomeworkEventEntity(BaseUntisEventEntity):
"""Event entity for homework changes."""

def __init__(self, server) -> None:
super().__init__(
server=server,
name=NAME_EVENT_HOMEWORK,
sensor_id="homework_event",
icon=ICON_EVENT_HOMEWORK,
event_types=["homework"],
)
10 changes: 9 additions & 1 deletion custom_components/webuntis/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@
"state_attributes": {
"event_type": {
"state": {
"homework": "Hausaufgabe hinzugefügt",
"code": "Stunden-Status geändert",
"rooms": "Raum Änderung",
"teachers": "Lehrer Änderung",
Expand All @@ -284,6 +283,15 @@
}
}
}
},
"homework_event": {
"state_attributes": {
"event_type": {
"state": {
"homework": "Hausaufgabe hinzugefügt"
}
}
}
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion custom_components/webuntis/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@
"state_attributes": {
"event_type": {
"state": {
"homework": "Homework added",
"code": "Lesson status changed",
"rooms": "Room changed",
"teachers": "Teacher changed",
Expand All @@ -285,6 +284,15 @@
}
}
}
},
"homework_event": {
"state_attributes": {
"event_type": {
"state": {
"homework": "Homework added"
}
}
}
}
}
}
Expand Down

0 comments on commit 4632521

Please sign in to comment.