Update local calendar to process calendar events in the executor (#144233)

This commit is contained in:
Allen Porter 2025-05-04 20:06:27 -07:00 committed by GitHub
parent c6b9a40234
commit 68d62ab58e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -94,20 +94,27 @@ class LocalCalendarEntity(CalendarEntity):
self, hass: HomeAssistant, start_date: datetime, end_date: datetime
) -> list[CalendarEvent]:
"""Get all events in a specific time frame."""
def events_in_range() -> list[CalendarEvent]:
events = self._calendar.timeline_tz(start_date.tzinfo).overlapping(
start_date,
end_date,
)
return [_get_calendar_event(event) for event in events]
return await self.hass.async_add_executor_job(events_in_range)
async def async_update(self) -> None:
"""Update entity state with the next upcoming event."""
def next_event() -> CalendarEvent | None:
now = dt_util.now()
events = self._calendar.timeline_tz(now.tzinfo).active_after(now)
if event := next(events, None):
self._event = _get_calendar_event(event)
else:
self._event = None
return _get_calendar_event(event)
return None
self._event = await self.hass.async_add_executor_job(next_event)
async def _async_store(self) -> None:
"""Persist the calendar to disk."""