mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 01:07:10 +00:00
Add option to ignore availability in google calendar events (#13714)
This commit is contained in:
parent
3394916a68
commit
48fe2d18e8
@ -11,6 +11,7 @@ from datetime import timedelta
|
|||||||
from homeassistant.components.calendar import CalendarEventDevice
|
from homeassistant.components.calendar import CalendarEventDevice
|
||||||
from homeassistant.components.google import (
|
from homeassistant.components.google import (
|
||||||
CONF_CAL_ID, CONF_ENTITIES, CONF_TRACK, TOKEN_FILE,
|
CONF_CAL_ID, CONF_ENTITIES, CONF_TRACK, TOKEN_FILE,
|
||||||
|
CONF_IGNORE_AVAILABILITY, CONF_SEARCH,
|
||||||
GoogleCalendarService)
|
GoogleCalendarService)
|
||||||
from homeassistant.util import Throttle, dt
|
from homeassistant.util import Throttle, dt
|
||||||
|
|
||||||
@ -18,7 +19,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
||||||
'orderBy': 'startTime',
|
'orderBy': 'startTime',
|
||||||
'maxResults': 1,
|
'maxResults': 5,
|
||||||
'singleEvents': True,
|
'singleEvents': True,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,18 +46,22 @@ class GoogleCalendarEventDevice(CalendarEventDevice):
|
|||||||
def __init__(self, hass, calendar_service, calendar, data):
|
def __init__(self, hass, calendar_service, calendar, data):
|
||||||
"""Create the Calendar event device."""
|
"""Create the Calendar event device."""
|
||||||
self.data = GoogleCalendarData(calendar_service, calendar,
|
self.data = GoogleCalendarData(calendar_service, calendar,
|
||||||
data.get('search', None))
|
data.get(CONF_SEARCH),
|
||||||
|
data.get(CONF_IGNORE_AVAILABILITY))
|
||||||
|
|
||||||
super().__init__(hass, data)
|
super().__init__(hass, data)
|
||||||
|
|
||||||
|
|
||||||
class GoogleCalendarData(object):
|
class GoogleCalendarData(object):
|
||||||
"""Class to utilize calendar service object to get next event."""
|
"""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."""
|
"""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.event = None
|
self.event = None
|
||||||
|
|
||||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||||
@ -80,5 +85,17 @@ class GoogleCalendarData(object):
|
|||||||
result = events.list(**params).execute()
|
result = events.list(**params).execute()
|
||||||
|
|
||||||
items = result.get('items', [])
|
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
|
return True
|
||||||
|
@ -44,6 +44,7 @@ CONF_ENTITIES = 'entities'
|
|||||||
CONF_TRACK = 'track'
|
CONF_TRACK = 'track'
|
||||||
CONF_SEARCH = 'search'
|
CONF_SEARCH = 'search'
|
||||||
CONF_OFFSET = 'offset'
|
CONF_OFFSET = 'offset'
|
||||||
|
CONF_IGNORE_AVAILABILITY = 'ignore_availability'
|
||||||
|
|
||||||
DEFAULT_CONF_TRACK_NEW = True
|
DEFAULT_CONF_TRACK_NEW = True
|
||||||
DEFAULT_CONF_OFFSET = '!!'
|
DEFAULT_CONF_OFFSET = '!!'
|
||||||
@ -74,8 +75,9 @@ _SINGLE_CALSEARCH_CONFIG = vol.Schema({
|
|||||||
vol.Required(CONF_NAME): cv.string,
|
vol.Required(CONF_NAME): cv.string,
|
||||||
vol.Required(CONF_DEVICE_ID): cv.string,
|
vol.Required(CONF_DEVICE_ID): cv.string,
|
||||||
vol.Optional(CONF_TRACK): cv.boolean,
|
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_OFFSET): cv.string,
|
||||||
|
vol.Optional(CONF_IGNORE_AVAILABILITY, default=True): cv.boolean,
|
||||||
})
|
})
|
||||||
|
|
||||||
DEVICE_SCHEMA = vol.Schema({
|
DEVICE_SCHEMA = vol.Schema({
|
||||||
|
@ -58,6 +58,7 @@ class TestGoogle(unittest.TestCase):
|
|||||||
'device_id': 'we_are_we_are_a_test_calendar',
|
'device_id': 'we_are_we_are_a_test_calendar',
|
||||||
'name': 'We are, we are, a... Test Calendar',
|
'name': 'We are, we are, a... Test Calendar',
|
||||||
'track': True,
|
'track': True,
|
||||||
|
'ignore_availability': True,
|
||||||
}]
|
}]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user