Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Handle no entries
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly committed Feb 9, 2024
1 parent 01c2a08 commit e2f8a55
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions custom_components/bedste_lectio/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sensor platforms for BedsteLectio."""
from __future__ import annotations
from datetime import datetime
import re
from zoneinfo import ZoneInfo
from dateutil import parser

Expand Down Expand Up @@ -44,7 +45,8 @@ def get_next_room(entries: list[dict[str, str]]) -> dict[str, str]:

rooms = []
for entry in entries:
date = parser.parse(entry["tidspunkt"].split(" til")[0], fuzzy=True).replace(tzinfo=ZoneInfo("Europe/Copenhagen"))
dt_str =re.sub(r"(?<!\d)(\d)(?!\d)", "0\\1", entry["tidspunkt"].split(" til")[0])
date = datetime.strptime(dt_str, "%d/%m-%Y %H:%M").replace(tzinfo=ZoneInfo("Europe/Copenhagen"))
if date < datetime.now().astimezone(ZoneInfo("Europe/Copenhagen")):
continue

Expand All @@ -56,7 +58,16 @@ def get_next_room(entries: list[dict[str, str]]) -> dict[str, str]:
"start": date,
})

return rooms[0] # The first entry is closest to now that hasn't passed
if rooms:
return rooms[0] # The first entry is closest to now that hasn't passed
else:
return {
"room": "N/A",
"activity": "Ingen moduler de næste par dage.",
"class": "N/A",
"teacher": "N/A",
"start": "N/A",
}


class BedsteLectioSensor(BedsteLectioEntity, SensorEntity):
Expand Down

0 comments on commit e2f8a55

Please sign in to comment.