Fix Google Calendar event loading (#54231)
This commit is contained in:
parent
1661de5c19
commit
476f3b5cb5
2 changed files with 30 additions and 21 deletions
|
@ -108,16 +108,19 @@ CONFIG_SCHEMA = vol.Schema(
|
||||||
extra=vol.ALLOW_EXTRA,
|
extra=vol.ALLOW_EXTRA,
|
||||||
)
|
)
|
||||||
|
|
||||||
_SINGLE_CALSEARCH_CONFIG = vol.Schema(
|
_SINGLE_CALSEARCH_CONFIG = vol.All(
|
||||||
{
|
cv.deprecated(CONF_MAX_RESULTS),
|
||||||
vol.Required(CONF_NAME): cv.string,
|
vol.Schema(
|
||||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
{
|
||||||
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
|
vol.Required(CONF_NAME): cv.string,
|
||||||
vol.Optional(CONF_OFFSET): cv.string,
|
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||||
vol.Optional(CONF_SEARCH): cv.string,
|
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
|
||||||
vol.Optional(CONF_TRACK): cv.boolean,
|
vol.Optional(CONF_OFFSET): cv.string,
|
||||||
vol.Optional(CONF_MAX_RESULTS): cv.positive_int,
|
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(
|
DEVICE_SCHEMA = vol.Schema(
|
||||||
|
|
|
@ -18,7 +18,6 @@ from homeassistant.util import Throttle, dt
|
||||||
from . import (
|
from . import (
|
||||||
CONF_CAL_ID,
|
CONF_CAL_ID,
|
||||||
CONF_IGNORE_AVAILABILITY,
|
CONF_IGNORE_AVAILABILITY,
|
||||||
CONF_MAX_RESULTS,
|
|
||||||
CONF_SEARCH,
|
CONF_SEARCH,
|
||||||
CONF_TRACK,
|
CONF_TRACK,
|
||||||
DEFAULT_CONF_OFFSET,
|
DEFAULT_CONF_OFFSET,
|
||||||
|
@ -30,7 +29,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
||||||
"orderBy": "startTime",
|
"orderBy": "startTime",
|
||||||
"maxResults": 5,
|
|
||||||
"singleEvents": True,
|
"singleEvents": True,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +69,6 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
|
||||||
calendar,
|
calendar,
|
||||||
data.get(CONF_SEARCH),
|
data.get(CONF_SEARCH),
|
||||||
data.get(CONF_IGNORE_AVAILABILITY),
|
data.get(CONF_IGNORE_AVAILABILITY),
|
||||||
data.get(CONF_MAX_RESULTS),
|
|
||||||
)
|
)
|
||||||
self._event = None
|
self._event = None
|
||||||
self._name = data[CONF_NAME]
|
self._name = data[CONF_NAME]
|
||||||
|
@ -113,15 +110,12 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
|
||||||
class GoogleCalendarData:
|
class GoogleCalendarData:
|
||||||
"""Class to utilize calendar service object to get next event."""
|
"""Class to utilize calendar service object to get next event."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, calendar_service, calendar_id, search, ignore_availability):
|
||||||
self, calendar_service, calendar_id, search, ignore_availability, max_results
|
|
||||||
):
|
|
||||||
"""Set up how we are going to search the google calendar."""
|
"""Set up how we are going to search the google calendar."""
|
||||||
self.calendar_service = calendar_service
|
self.calendar_service = calendar_service
|
||||||
self.calendar_id = calendar_id
|
self.calendar_id = calendar_id
|
||||||
self.search = search
|
self.search = search
|
||||||
self.ignore_availability = ignore_availability
|
self.ignore_availability = ignore_availability
|
||||||
self.max_results = max_results
|
|
||||||
self.event = None
|
self.event = None
|
||||||
|
|
||||||
def _prepare_query(self):
|
def _prepare_query(self):
|
||||||
|
@ -132,8 +126,8 @@ class GoogleCalendarData:
|
||||||
return None, None
|
return None, None
|
||||||
params = dict(DEFAULT_GOOGLE_SEARCH_PARAMS)
|
params = dict(DEFAULT_GOOGLE_SEARCH_PARAMS)
|
||||||
params["calendarId"] = self.calendar_id
|
params["calendarId"] = self.calendar_id
|
||||||
if self.max_results:
|
params["maxResults"] = 100 # Page size
|
||||||
params["maxResults"] = self.max_results
|
|
||||||
if self.search:
|
if self.search:
|
||||||
params["q"] = self.search
|
params["q"] = self.search
|
||||||
|
|
||||||
|
@ -147,18 +141,30 @@ class GoogleCalendarData:
|
||||||
params["timeMin"] = start_date.isoformat("T")
|
params["timeMin"] = start_date.isoformat("T")
|
||||||
params["timeMax"] = end_date.isoformat("T")
|
params["timeMax"] = end_date.isoformat("T")
|
||||||
|
|
||||||
|
event_list = []
|
||||||
events = await hass.async_add_executor_job(service.events)
|
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)
|
result = await hass.async_add_executor_job(events.list(**params).execute)
|
||||||
|
|
||||||
items = result.get("items", [])
|
items = result.get("items", [])
|
||||||
event_list = []
|
|
||||||
for item in items:
|
for item in items:
|
||||||
if not self.ignore_availability and "transparency" in item:
|
if not self.ignore_availability and "transparency" in item:
|
||||||
if item["transparency"] == "opaque":
|
if item["transparency"] == "opaque":
|
||||||
event_list.append(item)
|
event_list.append(item)
|
||||||
else:
|
else:
|
||||||
event_list.append(item)
|
event_list.append(item)
|
||||||
return event_list
|
return result.get("nextPageToken")
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
def update(self):
|
def update(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue