Skip to content

Commit

Permalink
Update sensor.py
Browse files Browse the repository at this point in the history
  • Loading branch information
reiniertc authored Jun 12, 2024
1 parent b3fd163 commit 94b6dbb
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions custom_components/magister_rooster/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util.dt import now
from homeassistant.util.dt import now, get_time_zone

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,7 +26,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME)

add_entities([
VolgendeSchooldagSensor(name, url),
VolgendeSchooldagSensor(hass, name, url),
InpakkenVoorMorgenSensor(name, url),
BegintijdMorgenSensor(name, url),
EindtijdMorgenSensor(name, url),
Expand Down Expand Up @@ -69,15 +69,27 @@ def update(self):
event_start = component.get('dtstart').dt
event_end = component.get('dtend').dt
summary = component.get('summary')

if isinstance(event_start, datetime):
event_start = event_start.date()
if isinstance(event_end, datetime):
event_end = event_end.date()
if event_start.tzinfo is None:
event_start = event_start.replace(tzinfo=get_time_zone(self.hass))
event_start_date = event_start.date()
else:
event_start_date = event_start

if event_start == today:
self._events_today.append((event_start, event_end, summary))
if event_start == self._next_school_day:
self._events_tomorrow.append((event_start, event_end, summary))
if isinstance(event_end, datetime):
if event_end.tzinfo is None:
event_end = event_end.replace(tzinfo=get_time_zone(self.hass))
event_end_date = event_end.date()
else:
event_end_date = event_end

# Alleen gebeurtenissen die geen hele dag zijn
if 'allday' not in component:
if event_start_date == today:
self._events_today.append((event_start, event_end, summary))
if event_start_date == self._next_school_day:
self._events_tomorrow.append((event_start, event_end, summary))

self._events = self._events_today + self._events_tomorrow

Expand All @@ -95,6 +107,10 @@ def get_next_school_day(self, today):
return today + timedelta(days=1)

class VolgendeSchooldagSensor(MagisterRoosterBaseSensor):
def __init__(self, hass, name, url):
super().__init__(name, url)
self.hass = hass

@property
def name(self):
return f"{self._name} Volgende Schooldag"
Expand All @@ -112,7 +128,8 @@ def name(self):
def update(self):
super().update()
if self._events_tomorrow:
self._state = ", ".join([event[2].split(' ', 1)[1] for event in self._events_tomorrow if ' ' in event[2]])
unique_summaries = list(set(event[2].split(' ', 1)[1] for event in self._events_tomorrow if ' ' in event[2]))
self._state = ", ".join(unique_summaries)
else:
self._state = None

Expand Down

0 comments on commit 94b6dbb

Please sign in to comment.