hass-core/homeassistant/components/caldav/api.py
J. Nick Koston ea9c969d15
Fix executor being overloaded in caldav (#112084)
Migrate to using a single executor job instead of creating
one per calendar. If the user had a lot of calendars the
executor would get overloaded
2024-03-02 21:53:51 -05:00

28 lines
834 B
Python

"""Library for working with CalDAV api."""
import caldav
from homeassistant.core import HomeAssistant
async def async_get_calendars(
hass: HomeAssistant, client: caldav.DAVClient, component: str
) -> list[caldav.Calendar]:
"""Get all calendars that support the specified component."""
def _get_calendars() -> list[caldav.Calendar]:
return [
calendar
for calendar in client.principal().calendars()
if component in calendar.get_supported_components()
]
return await hass.async_add_executor_job(_get_calendars)
def get_attr_value(obj: caldav.CalendarObjectResource, attribute: str) -> str | None:
"""Return the value of the CalDav object attribute if defined."""
if hasattr(obj, attribute):
return getattr(obj, attribute).value
return None