mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 00:07:10 +00:00
Fix Google Calendar event loading (#54231)
This commit is contained in:
parent
1661de5c19
commit
476f3b5cb5
@ -108,16 +108,19 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
_SINGLE_CALSEARCH_CONFIG = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
|
||||
vol.Optional(CONF_OFFSET): cv.string,
|
||||
vol.Optional(CONF_SEARCH): cv.string,
|
||||
vol.Optional(CONF_TRACK): cv.boolean,
|
||||
vol.Optional(CONF_MAX_RESULTS): cv.positive_int,
|
||||
}
|
||||
_SINGLE_CALSEARCH_CONFIG = vol.All(
|
||||
cv.deprecated(CONF_MAX_RESULTS),
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_NAME): cv.string,
|
||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
|
||||
vol.Optional(CONF_OFFSET): cv.string,
|
||||
vol.Optional(CONF_SEARCH): cv.string,
|
||||
vol.Optional(CONF_TRACK): cv.boolean,
|
||||
vol.Optional(CONF_MAX_RESULTS): cv.positive_int, # Now unused
|
||||
}
|
||||
),
|
||||
)
|
||||
|
||||
DEVICE_SCHEMA = vol.Schema(
|
||||
|
@ -18,7 +18,6 @@ from homeassistant.util import Throttle, dt
|
||||
from . import (
|
||||
CONF_CAL_ID,
|
||||
CONF_IGNORE_AVAILABILITY,
|
||||
CONF_MAX_RESULTS,
|
||||
CONF_SEARCH,
|
||||
CONF_TRACK,
|
||||
DEFAULT_CONF_OFFSET,
|
||||
@ -30,7 +29,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
||||
"orderBy": "startTime",
|
||||
"maxResults": 5,
|
||||
"singleEvents": True,
|
||||
}
|
||||
|
||||
@ -71,7 +69,6 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
|
||||
calendar,
|
||||
data.get(CONF_SEARCH),
|
||||
data.get(CONF_IGNORE_AVAILABILITY),
|
||||
data.get(CONF_MAX_RESULTS),
|
||||
)
|
||||
self._event = None
|
||||
self._name = data[CONF_NAME]
|
||||
@ -113,15 +110,12 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
|
||||
class GoogleCalendarData:
|
||||
"""Class to utilize calendar service object to get next event."""
|
||||
|
||||
def __init__(
|
||||
self, calendar_service, calendar_id, search, ignore_availability, max_results
|
||||
):
|
||||
def __init__(self, calendar_service, calendar_id, search, ignore_availability):
|
||||
"""Set up how we are going to search the google calendar."""
|
||||
self.calendar_service = calendar_service
|
||||
self.calendar_id = calendar_id
|
||||
self.search = search
|
||||
self.ignore_availability = ignore_availability
|
||||
self.max_results = max_results
|
||||
self.event = None
|
||||
|
||||
def _prepare_query(self):
|
||||
@ -132,8 +126,8 @@ class GoogleCalendarData:
|
||||
return None, None
|
||||
params = dict(DEFAULT_GOOGLE_SEARCH_PARAMS)
|
||||
params["calendarId"] = self.calendar_id
|
||||
if self.max_results:
|
||||
params["maxResults"] = self.max_results
|
||||
params["maxResults"] = 100 # Page size
|
||||
|
||||
if self.search:
|
||||
params["q"] = self.search
|
||||
|
||||
@ -147,18 +141,30 @@ class GoogleCalendarData:
|
||||
params["timeMin"] = start_date.isoformat("T")
|
||||
params["timeMax"] = end_date.isoformat("T")
|
||||
|
||||
event_list = []
|
||||
events = await hass.async_add_executor_job(service.events)
|
||||
page_token = None
|
||||
while True:
|
||||
page_token = await self.async_get_events_page(
|
||||
hass, events, params, page_token, event_list
|
||||
)
|
||||
if not page_token:
|
||||
break
|
||||
return event_list
|
||||
|
||||
async def async_get_events_page(self, hass, events, params, page_token, event_list):
|
||||
"""Get a page of events in a specific time frame."""
|
||||
params["pageToken"] = page_token
|
||||
result = await hass.async_add_executor_job(events.list(**params).execute)
|
||||
|
||||
items = result.get("items", [])
|
||||
event_list = []
|
||||
for item in items:
|
||||
if not self.ignore_availability and "transparency" in item:
|
||||
if item["transparency"] == "opaque":
|
||||
event_list.append(item)
|
||||
else:
|
||||
event_list.append(item)
|
||||
return event_list
|
||||
return result.get("nextPageToken")
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
|
Loading…
x
Reference in New Issue
Block a user