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