Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions homeassistant/components/calendar/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from homeassistant.components.calendar import CalendarEventDevice
from homeassistant.components.google import (
CONF_CAL_ID, CONF_ENTITIES, CONF_TRACK, TOKEN_FILE,
CONF_IGNORE_AVAILABILITY, CONF_SEARCH,
GoogleCalendarService)
from homeassistant.util import Throttle, dt

_LOGGER = logging.getLogger(__name__)

DEFAULT_GOOGLE_SEARCH_PARAMS = {
'orderBy': 'startTime',
'maxResults': 1,
'maxResults': 5,
'singleEvents': True,
}

Expand All @@ -45,18 +46,22 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
def __init__(self, hass, calendar_service, calendar, data):
"""Create the Calendar event device."""
self.data = GoogleCalendarData(calendar_service, calendar,
data.get('search', None))
data.get(CONF_SEARCH),
data.get(CONF_IGNORE_AVAILABILITY))

super().__init__(hass, data)


class GoogleCalendarData(object):
"""Class to utilize calendar service object to get next event."""

def __init__(self, calendar_service, calendar_id, search=None):
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.event = None

@Throttle(MIN_TIME_BETWEEN_UPDATES)
Expand All @@ -80,5 +85,17 @@ def update(self):
result = events.list(**params).execute()

items = result.get('items', [])
self.event = items[0] if len(items) == 1 else None

new_event = None
for item in items:
if (not self.ignore_availability
and 'transparency' in item.keys()):
if item['transparency'] == 'opaque':
new_event = item
break
else:
new_event = item
break

self.event = new_event
return True
4 changes: 3 additions & 1 deletion homeassistant/components/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
CONF_TRACK = 'track'
CONF_SEARCH = 'search'
CONF_OFFSET = 'offset'
CONF_IGNORE_AVAILABILITY = 'ignore_availability'

DEFAULT_CONF_TRACK_NEW = True
DEFAULT_CONF_OFFSET = '!!'
Expand Down Expand Up @@ -74,8 +75,9 @@
vol.Required(CONF_NAME): cv.string,
vol.Required(CONF_DEVICE_ID): cv.string,
vol.Optional(CONF_TRACK): cv.boolean,
vol.Optional(CONF_SEARCH): vol.Any(cv.string, None),
vol.Optional(CONF_SEARCH): cv.string,
vol.Optional(CONF_OFFSET): cv.string,
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
})

DEVICE_SCHEMA = vol.Schema({
Expand Down
1 change: 1 addition & 0 deletions tests/components/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_get_calendar_info(self):
'device_id': 'we_are_we_are_a_test_calendar',
'name': 'We are, we are, a... Test Calendar',
'track': True,
'ignore_availability': True,
}]
})

Expand Down