Rewrite calendar component (#24950)
* Correct google calendar test name * Rewrite calendar component * Save component in hass.data. * Rename device_state_attributes to state_attributes. * Remove offset attribute from base state_attributes. * Extract offset helpers to calendar component. * Clean imports. * Remove stale constants. * Remove name and add async_get_events. * Add normalize_event helper function. Copied from #21495. * Add event property to base entity. * Use event property for calendar state. * Ensure event start and end. * Remove entity init. * Add comment about event data class. * Temporary keep old start and end datetime format. * Convert demo calendar * Convert google calendar * Convert google calendar. * Clean up google component. * Keep offset feature by using offset helpers. * Convert caldav calendar * Clean up caldav calendar. * Update caldav cal on addition. * Bring back offset to caldav calendar. * Copy caldav event on update. * Convert todoist calendar
This commit is contained in:
1 parent
c6af8811fb
commit
177f5a35ae
7 files changed
+272
-248
No files matched your search
@@ -8,7 +8,6 @@ import voluptuous as vol
|
||||
from voluptuous.error import Error as VoluptuousError
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.helpers.event import track_time_change
|
||||
@@ -312,9 +311,6 @@ def do_setup(hass, hass_config, config):
|
||||
setup_services(hass, hass_config, track_new_found_calendars,
|
||||
calendar_service)
|
||||
|
||||
# Ensure component is loaded
|
||||
setup_component(hass, 'calendar', config)
|
||||
|
||||
for calendar in hass.data[DATA_INDEX].values():
|
||||
discovery.load_platform(hass, 'calendar', DOMAIN, calendar,
|
||||
hass_config)
|
||||
|
||||
@@ -1,13 +1,17 @@
|
||||
"""Support for Google Calendar Search binary sensors."""
|
||||
import copy
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
from homeassistant.components.calendar import CalendarEventDevice
|
||||
from homeassistant.components.calendar import (
|
||||
ENTITY_ID_FORMAT, CalendarEventDevice, calculate_offset, is_offset_reached)
|
||||
from homeassistant.helpers.entity import generate_entity_id
|
||||
from homeassistant.util import Throttle, dt
|
||||
|
||||
from . import (
|
||||
CONF_CAL_ID, CONF_ENTITIES, CONF_IGNORE_AVAILABILITY, CONF_SEARCH,
|
||||
CONF_TRACK, TOKEN_FILE, CONF_MAX_RESULTS, GoogleCalendarService)
|
||||
CONF_CAL_ID, CONF_DEVICE_ID, CONF_ENTITIES, CONF_IGNORE_AVAILABILITY,
|
||||
CONF_MAX_RESULTS, CONF_NAME, CONF_OFFSET, CONF_SEARCH, CONF_TRACK,
|
||||
DEFAULT_CONF_OFFSET, TOKEN_FILE, GoogleCalendarService)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -29,27 +33,66 @@ def setup_platform(hass, config, add_entities, disc_info=None):
|
||||
return
|
||||
|
||||
calendar_service = GoogleCalendarService(hass.config.path(TOKEN_FILE))
|
||||
add_entities([GoogleCalendarEventDevice(hass, calendar_service,
|
||||
disc_info[CONF_CAL_ID], data)
|
||||
for data in disc_info[CONF_ENTITIES] if data[CONF_TRACK]])
|
||||
entities = []
|
||||
for data in disc_info[CONF_ENTITIES]:
|
||||
if not data[CONF_TRACK]:
|
||||
continue
|
||||
entity_id = generate_entity_id(
|
||||
ENTITY_ID_FORMAT, data[CONF_DEVICE_ID], hass=hass)
|
||||
entity = GoogleCalendarEventDevice(
|
||||
calendar_service, disc_info[CONF_CAL_ID], data, entity_id)
|
||||
entities.append(entity)
|
||||
|
||||
add_entities(entities, True)
|
||||
|
||||
|
||||
class GoogleCalendarEventDevice(CalendarEventDevice):
|
||||
"""A calendar event device."""
|
||||
|
||||
def __init__(self, hass, calendar_service, calendar, data):
|
||||
def __init__(self, calendar_service, calendar, data, entity_id):
|
||||
"""Create the Calendar event device."""
|
||||
self.data = GoogleCalendarData(calendar_service, calendar,
|
||||
data.get(CONF_SEARCH),
|
||||
data.get(CONF_IGNORE_AVAILABILITY),
|
||||
data.get(CONF_MAX_RESULTS))
|
||||
self.data = GoogleCalendarData(
|
||||
calendar_service, calendar,
|
||||
data.get(CONF_SEARCH), data.get(CONF_IGNORE_AVAILABILITY),
|
||||
data.get(CONF_MAX_RESULTS))
|
||||
self._event = None
|
||||
self._name = data[CONF_NAME]
|
||||
self._offset = data.get(CONF_OFFSET, DEFAULT_CONF_OFFSET)
|
||||
self._offset_reached = False
|
||||
self.entity_id = entity_id
|
||||
|
||||
super().__init__(hass, data)
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the device state attributes."""
|
||||
return {
|
||||
'offset_reached': self._offset_reached,
|
||||
}
|
||||
|
||||
@property
|
||||
def event(self):
|
||||
"""Return the next upcoming event."""
|
||||
return self._event
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the entity."""
|
||||
return self._name
|
||||
|
||||
async def async_get_events(self, hass, start_date, end_date):
|
||||
"""Get all events in a specific time frame."""
|
||||
return await self.data.async_get_events(hass, start_date, end_date)
|
||||
|
||||
def update(self):
|
||||
"""Update event data."""
|
||||
self.data.update()
|
||||
event = copy.deepcopy(self.data.event)
|
||||
if event is None:
|
||||
self._event = event
|
||||
return
|
||||
event = calculate_offset(event, self._offset)
|
||||
self._offset_reached = is_offset_reached(event)
|
||||
self._event = event
|
||||
|
||||
|
||||
class GoogleCalendarData:
|
||||
"""Class to utilize calendar service object to get next event."""
|
||||
@@ -71,7 +114,7 @@ class GoogleCalendarData:
|
||||
try:
|
||||
service = self.calendar_service.get()
|
||||
except ServerNotFoundError:
|
||||
_LOGGER.warning("Unable to connect to Google, using cached data")
|
||||
_LOGGER.error("Unable to connect to Google")
|
||||
return None, None
|
||||
params = dict(DEFAULT_GOOGLE_SEARCH_PARAMS)
|
||||
params['calendarId'] = self.calendar_id
|
||||
@@ -87,7 +130,7 @@ class GoogleCalendarData:
|
||||
service, params = await hass.async_add_executor_job(
|
||||
self._prepare_query)
|
||||
if service is None:
|
||||
return
|
||||
return []
|
||||
params['timeMin'] = start_date.isoformat('T')
|
||||
params['timeMax'] = end_date.isoformat('T')
|
||||
|
||||
@@ -111,7 +154,7 @@ class GoogleCalendarData:
|
||||
"""Get the latest data."""
|
||||
service, params = self._prepare_query()
|
||||
if service is None:
|
||||
return False
|
||||
return
|
||||
params['timeMin'] = dt.now().isoformat('T')
|
||||
|
||||
events = service.events()
|
||||
@@ -131,4 +174,3 @@ class GoogleCalendarData:
|
||||
break
|
||||
|
||||
self.event = new_event
|
||||
return True
|
||||
Reference in new issue
Block a user