Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions homeassistant/components/remote_calendar/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

from ical.event import Event
from ical.timeline import Timeline

from homeassistant.components.calendar import CalendarEntity, CalendarEvent
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -49,18 +48,12 @@ def __init__(
super().__init__(coordinator)
self._attr_name = entry.data[CONF_CALENDAR_NAME]
self._attr_unique_id = entry.entry_id
self._timeline: Timeline | None = None
self._event: CalendarEvent | None = None

@property
def event(self) -> CalendarEvent | None:
"""Return the next upcoming event."""
if self._timeline is None:
return None
now = dt_util.now()
events = self._timeline.active_after(now)
if event := next(events, None):
return _get_calendar_event(event)
return None
return self._event

async def async_get_events(
self, hass: HomeAssistant, start_date: datetime, end_date: datetime
Expand All @@ -86,12 +79,14 @@ async def async_update(self) -> None:
"""
await super().async_update()

def _get_timeline() -> Timeline | None:
"""Return the next active event."""
def next_event() -> CalendarEvent | None:
now = dt_util.now()
return self.coordinator.data.timeline_tz(now.tzinfo)
events = self.coordinator.data.timeline_tz(now.tzinfo).active_after(now)
if event := next(events, None):
return _get_calendar_event(event)
return None

self._timeline = await self.hass.async_add_executor_job(_get_timeline)
self._event = await self.hass.async_add_executor_job(next_event)


def _get_calendar_event(event: Event) -> CalendarEvent:
Expand Down
Loading