Skip to content

Commit

Permalink
Merge pull request #6 from JonasJoKuJonas/master
Browse files Browse the repository at this point in the history
FirstClassSensor to NextLessonToWakeUpSensor
  • Loading branch information
JonasJoKuJonas committed Oct 16, 2022
2 parents 879c13c + 590c40f commit 142f903
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
39 changes: 33 additions & 6 deletions custom_components/webuntis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(
# Data provided by 3rd party library
self.is_class = None
self.next_class = None
self.first_class = None
self.next_lesson_to_wake_up = None

# Dispatcher signal name
self.signal_name = f"{SIGNAL_NAME_PREFIX}_{self.unique_id}"
Expand Down Expand Up @@ -137,7 +137,7 @@ async def _async_status_request(self) -> None:
# Login error, set all properties to unknown.
self.is_class = None
self.next_class = None
self.first_class = None
self.next_lesson_to_wake_up = None

self.session = webuntis.Session(
username=self.username,
Expand Down Expand Up @@ -183,14 +183,14 @@ async def _async_status_request(self) -> None:
)

try:
self.first_class = await self._hass.async_add_executor_job(
self._first_class
self.next_lesson_to_wake_up = await self._hass.async_add_executor_job(
self._next_lesson_to_wake_up
)
except OSError as error:
self.first_class = None
self.next_lesson_to_wake_up = None

_LOGGER.warning(
"Updating the propertie first_class of '%s@%s' failed - OSError: %s",
"Updating the propertie next_lesson_to_wake_up of '%s@%s' failed - OSError: %s",
self.school,
self.username,
error,
Expand Down Expand Up @@ -268,6 +268,33 @@ def _first_class(self):
else:
return None

def _next_lesson_to_wake_up(self):
"""returns time of the next lesson to weak up."""
today = date.today()
now = datetime.now()
in_x_days = today + timedelta(days=14)
timetable_object = self.get_timetable_object()

# pylint: disable=maybe-no-member
table = self.session.timetable(start=today, end=in_x_days, **timetable_object)

time_list = []
for lesson in table:
if lesson.code != "cancelled":
time_list.append(lesson.start)

time_list_new = []
for time in sorted(time_list):
if time < now:
now = now.replace(
hour=0, minute=0, second=0, microsecond=0
) + timedelta(days=1)
continue
else:
time_list_new.append(time)

return sorted(time_list_new)[0].astimezone()


class WebUntisEntity(Entity):
"""Representation of a Web Untis base entity."""
Expand Down
4 changes: 2 additions & 2 deletions custom_components/webuntis/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

ICON_STATUS = "mdi:school-outline"
ICON_NEXT_CLASS = "mdi:table-clock"
ICON_FIRST_CLASS = "mdi:clock-start"
ICON_NEXT_LESSON_TO_WAKE_UP = "mdi:clock-start"

NAME_STATUS = "Class"
NAME_NEXT_CLASS = "Next Class"
NAME_FIRST_CLASS = "First Class"
NAME_NEXT_LESSON_TO_WAKE_UP = "Next lesson to wake up"

SCAN_INTERVAL = 60 * 5 # 5min

Expand Down
23 changes: 13 additions & 10 deletions custom_components/webuntis/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
DOMAIN,
ICON_NEXT_CLASS,
NAME_NEXT_CLASS,
ICON_FIRST_CLASS,
NAME_FIRST_CLASS,
ICON_NEXT_LESSON_TO_WAKE_UP,
NAME_NEXT_LESSON_TO_WAKE_UP,
)


Expand All @@ -25,7 +25,10 @@ async def async_setup_entry(
server = hass.data[DOMAIN][config_entry.unique_id]

# Create entities list.
entities = [WebUntisNextClassSensor(server), WebUntisFirstClassSensor(server)]
entities = [
WebUntisNextClassSensor(server),
WebUntisNextLessonToWakeUpSensor(server),
]

# Add sensor entities.
async_add_entities(entities, True)
Expand Down Expand Up @@ -70,19 +73,19 @@ async def async_update(self) -> None:
self._attr_native_value = self._server.next_class


class WebUntisFirstClassSensor(WebUntisSensorEntity):
"""Representation of a Web Untis first class sensor."""
class WebUntisNextLessonToWakeUpSensor(WebUntisSensorEntity):
"""Representation of a Web Untis next lesson to wake up sensor."""

def __init__(self, server: WebUntis) -> None:
"""Initialize first class sensor."""
"""Initialize next lesson to wake up sensor."""
super().__init__(
server=server,
type_name=NAME_FIRST_CLASS,
icon=ICON_FIRST_CLASS,
type_name=NAME_NEXT_LESSON_TO_WAKE_UP,
icon=ICON_NEXT_LESSON_TO_WAKE_UP,
unit=None,
device_class="timestamp",
)

async def async_update(self) -> None:
"""Update first class."""
self._attr_native_value = self._server.first_class
"""Update next lesson to wake up."""
self._attr_native_value = self._server.next_lesson_to_wake_up

0 comments on commit 142f903

Please sign in to comment.