Skip to content

Commit

Permalink
add school start/ end sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasJoKuJonas committed May 9, 2023
1 parent ac68360 commit 571a1f1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
38 changes: 38 additions & 0 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def __init__(

self.subjects = []

self.today = [None, None]

# Dispatcher signal name
self.signal_name = f"{SIGNAL_NAME_PREFIX}_{self.unique_id}"

Expand Down Expand Up @@ -372,6 +374,18 @@ async def _async_status_request(self) -> None:
error,
)

try:
self.today = await self._hass.async_add_executor_job(self._today)
except OSError as error:
self.today = [None, None]

_LOGGER.warning(
"Updating the propertie today-sensor of '%s@%s' failed - OSError: %s",
self.school,
self.username,
error,
)

if not self.keep_loged_in:
await self._hass.async_add_executor_job(self.session.logout)
_LOGGER.debug("Logout successful")
Expand Down Expand Up @@ -561,6 +575,30 @@ def _get_events(self):

return event_list

def _today(self):
today = date.today()

# pylint: disable=maybe-no-member
table = self.get_timetable(start=today, end=today)

time_list_start = []
for lesson in table:
if self.check_lesson(lesson):
time_list_start.append(lesson.start)

time_list_end = []
for lesson in table:
if self.check_lesson(lesson):
time_list_end.append(lesson.end)

try:
return [
sorted(time_list_start)[0].astimezone(),
sorted(time_list_end)[-1].astimezone(),
]
except IndexError:
return [None, None]

def check_lesson(self, lesson, ignor_cancelled=False) -> bool:
"""Checks if a lesson is taking place"""
if lesson.code == "cancelled" and not ignor_cancelled:
Expand Down
6 changes: 5 additions & 1 deletion custom_components/webuntis/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
ICON_NEXT_CLASS = "mdi:table-clock"
ICON_NEXT_LESSON_TO_WAKE_UP = "mdi:clock-start"
ICON_CALENDER = "mdi:school-outline"
ICON_TODAY_START = "mdi:calendar-start"
ICON_TODAY_END = "mdi:calendar-end"

NAME_STATUS = "Class"
NAME_NEXT_CLASS = "Next Class"
NAME_NEXT_LESSON_TO_WAKE_UP = "Next lesson to wake up"
NAME_CALENDER = " WebUntis Calender"
NAME_CALENDER = "WebUntis Calender"
NAME_TODAY_START = "Today school start"
NAME_TODAY_END = "Today school end"

SCAN_INTERVAL = 60 * 5 # 5min

Expand Down
50 changes: 49 additions & 1 deletion custom_components/webuntis/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
from .const import (
DOMAIN,
ICON_NEXT_CLASS,
NAME_NEXT_CLASS,
ICON_NEXT_LESSON_TO_WAKE_UP,
ICON_TODAY_END,
ICON_TODAY_START,
NAME_NEXT_CLASS,
NAME_NEXT_LESSON_TO_WAKE_UP,
NAME_TODAY_END,
NAME_TODAY_START,
)


Expand All @@ -30,6 +34,8 @@ async def async_setup_entry(
entities = [
WebUntisNextClassSensor(server),
WebUntisNextLessonToWakeUpSensor(server),
WebUntisToayStart(server),
WebUntisToayEnd(server),
]

# Add sensor entities.
Expand Down Expand Up @@ -105,3 +111,45 @@ async def async_update(self) -> None:
"""Update next lesson to wake up."""
self._attr_native_value = self._server.next_lesson_to_wake_up
self._attr_extra_state_attributes = {"day": self._server.next_day_json}


class WebUntisToayStart(WebUntisSensorEntity):
"""Representation of a Web Untis Today start sensor."""

unit: Optional[str] = None
device_class: Optional[str] = "timestamp"

def __init__(self, server: WebUntis) -> None:
"""Initialize sensor."""
super().__init__(
server=server,
type_name=NAME_TODAY_START,
icon=ICON_TODAY_START,
device_class=self.device_class,
)
self._attr_extra_state_attributes = {}

async def async_update(self) -> None:
"""Update sensor data."""
self._attr_native_value = self._server.today[0]


class WebUntisToayEnd(WebUntisSensorEntity):
"""Representation of a Web Untis Today end sensor."""

unit: Optional[str] = None
device_class: Optional[str] = "timestamp"

def __init__(self, server: WebUntis) -> None:
"""Initialize sensor."""
super().__init__(
server=server,
type_name=NAME_TODAY_END,
icon=ICON_TODAY_END,
device_class=self.device_class,
)
self._attr_extra_state_attributes = {}

async def async_update(self) -> None:
"""Update sensor data."""
self._attr_native_value = self._server.today[-1]

0 comments on commit 571a1f1

Please sign in to comment.