async def async_update(self): """Do the update.""" await self.data.async_update(self.hass) event = deepcopy(self.data.event) if event: event.summary, offset = extract_offset(event.summary, DEFAULT_OFFSET) start = O365CalendarData.to_datetime(event.start) self._offset_reached = is_offset_reached(start, offset) results = await self.data.async_o365_get_events( self.hass, dt.utcnow() + timedelta(hours=self._start_offset), dt.utcnow() + timedelta(hours=self._end_offset), ) if results: events = list(results) elif self._event: return else: events = [] self._data_attribute = [ format_event_data(x, self.data.calendar.calendar_id) for x in events ] self._data_attribute.sort(key=itemgetter("start")) self._event = event
def _apply_coordinator_update(self) -> None: """Copy state from the coordinator to this entity.""" events = self.coordinator.data self._event = _get_calendar_event(next( iter(events))) if events else None if self._event: (self._event.summary, self._offset_value) = extract_offset(self._event.summary, self._offset)
def update(self): """Update event data.""" self.data.update() event = copy.deepcopy(self.data.event) if event is None: self._event = event return (summary, offset) = extract_offset(event["summary"], OFFSET) event["summary"] = summary self._event = event self._attr_extra_state_attributes = { "offset_reached": is_offset_reached(get_date(event["start"]), offset) }
def update(self): """Get the current or next event.""" self.data.update() event = copy.deepcopy(self.data.event) if event is None: self._event = event return [summary, offset] = extract_offset(event.summary, OFFSET) event.summary = summary self._event = event self._attr_extra_state_attributes = { "offset_reached": is_offset_reached( event.start_datetime_local, offset ) }
async def async_update(self) -> None: """Get the latest data.""" request = ListEventsRequest(calendar_id=self._calendar_id, search=self._search) try: result = await self._calendar_service.async_list_events(request) except ApiException as err: _LOGGER.error("Unable to connect to Google: %s", err) return # Pick the first visible event and apply offset calculations. valid_items = filter(self._event_filter, result.items) event = copy.deepcopy(next(valid_items, None)) if event: (event.summary, offset) = extract_offset(event.summary, self._offset) self._event = _get_calendar_event(event) self._offset_value = offset else: self._event = None
async def async_update(self) -> None: """Get the latest data.""" try: items, _ = await self._calendar_service.async_list_events( self._calendar_id, search=self._search) except ServerNotFoundError as err: _LOGGER.error("Unable to connect to Google: %s", err) return # Pick the first visible event and apply offset calculations. valid_items = filter(self._event_filter, items) self._event = copy.deepcopy(next(valid_items, None)) if self._event: (summary, offset) = extract_offset(self._event["summary"], self._offset) self._event["summary"] = summary self._offset_reached = is_offset_reached( get_date(self._event["start"]), offset)
def update(self): """Get the latest data.""" start_of_today = dt.start_of_local_day() start_of_tomorrow = dt.start_of_local_day() + timedelta(days=self.days) # We have to retrieve the results for the whole day as the server # won't return events that have already started results = self.calendar.date_search(start_of_today, start_of_tomorrow) # Create new events for each recurrence of an event that happens today. # For recurring events, some servers return the original event with recurrence rules # and they would not be properly parsed using their original start/end dates. new_events = [] for event in results: if not hasattr(event.instance, "vevent"): _LOGGER.warning("Skipped event with missing 'vevent' property") continue vevent = event.instance.vevent for start_dt in vevent.getrruleset() or []: _start_of_today = start_of_today _start_of_tomorrow = start_of_tomorrow if self.is_all_day(vevent): start_dt = start_dt.date() _start_of_today = _start_of_today.date() _start_of_tomorrow = _start_of_tomorrow.date() if _start_of_today <= start_dt < _start_of_tomorrow: new_event = event.copy() new_vevent = new_event.instance.vevent if hasattr(new_vevent, "dtend"): dur = new_vevent.dtend.value - new_vevent.dtstart.value new_vevent.dtend.value = start_dt + dur new_vevent.dtstart.value = start_dt new_events.append(new_event) elif _start_of_tomorrow <= start_dt: break vevents = [ event.instance.vevent for event in results + new_events if hasattr(event.instance, "vevent") ] # dtstart can be a date or datetime depending if the event lasts a # whole day. Convert everything to datetime to be able to sort it vevents.sort(key=lambda x: self.to_datetime(x.dtstart.value)) vevent = next( (vevent for vevent in vevents if (self.is_matching(vevent, self.search) and ( not self.is_all_day(vevent) or self.include_all_day) and not self.is_over(vevent))), None, ) # If no matching event could be found if vevent is None: _LOGGER.debug( "No matching event found in the %d results for %s", len(vevents), self.calendar.name, ) self.event = None return # Populate the entity attributes with the event values (summary, offset) = extract_offset(vevent.summary.value, OFFSET) self.event = CalendarEvent( summary=summary, start=vevent.dtstart.value, end=self.get_end_date(vevent), location=self.get_attr_value(vevent, "location"), description=self.get_attr_value(vevent, "description"), ) self.offset = offset