Skip to content

Commit

Permalink
Handle errors raised by core CalendarEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerSelwyn committed Apr 18, 2023
1 parent 4d3d14b commit f194d4d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 54 deletions.
85 changes: 39 additions & 46 deletions custom_components/o365/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
is_offset_reached,
)
from homeassistant.const import CONF_NAME
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_platform, entity_registry
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.util import dt
Expand Down Expand Up @@ -75,7 +76,10 @@
check_file_location,
clean_html,
format_event_data,
get_end_date,
get_hass_date,
get_permissions,
get_start_date,
load_yaml_file,
update_calendar_file,
validate_minimum_permission,
Expand Down Expand Up @@ -533,6 +537,7 @@ def __init__(
self._exclude = exclude
self.event = None
self._entity_id = entity_id
self._error = False

async def _async_get_calendar(self, hass):
self.calendar = await hass.async_add_executor_job(
Expand Down Expand Up @@ -611,21 +616,26 @@ async def async_get_events(self, hass, start_date, end_date):
if not results:
return
vevent_list = list(results)
vevent_list.sort(key=attrgetter("start"))
event_list = []
for vevent in vevent_list:
event = CalendarEvent(
self.get_hass_date(vevent.start, vevent.is_all_day),
self.get_hass_date(self.get_end_date(vevent), vevent.is_all_day),
vevent.subject,
clean_html(vevent.body),
vevent.location["displayName"],
uid=vevent.object_id,
)
if vevent.series_master_id:
event.recurrence_id = vevent.series_master_id
event_list.append(event)
try:
event = CalendarEvent(
get_hass_date(vevent.start, vevent.is_all_day),
get_hass_date(get_end_date(vevent), vevent.is_all_day),
vevent.subject,
clean_html(vevent.body),
vevent.location["displayName"],
uid=vevent.object_id,
)
if vevent.series_master_id:
event.recurrence_id = vevent.series_master_id
event_list.append(event)
except HomeAssistantError as err:
_LOGGER.warning(
"Invalid event found - Error: %s, Event: %s", err, vevent
)

event_list.sort(key=attrgetter("start"))
return event_list

async def async_update(self, hass):
Expand Down Expand Up @@ -653,13 +663,21 @@ async def async_update(self, hass):
self.event = None
return

self.event = CalendarEvent(
self.get_hass_date(vevent.start, vevent.is_all_day),
self.get_hass_date(self.get_end_date(vevent), vevent.is_all_day),
vevent.subject,
clean_html(vevent.body),
vevent.location["displayName"],
)
try:
self.event = CalendarEvent(
get_hass_date(vevent.start, vevent.is_all_day),
get_hass_date(get_end_date(vevent), vevent.is_all_day),
vevent.subject,
clean_html(vevent.body),
vevent.location["displayName"],
)
self._error = False
except HomeAssistantError as err:
if not self._error:
_LOGGER.warning(
"Invalid event found - Error: %s, Event: %s", err, vevent
)
self._error = True

def _get_root_event(self, results):
started_event = None
Expand Down Expand Up @@ -699,21 +717,12 @@ def is_all_day(vevent):
@staticmethod
def is_started(vevent):
"""Is it over."""
return dt.utcnow() >= O365CalendarData.to_datetime(
O365CalendarData.get_start_date(vevent)
)
return dt.utcnow() >= O365CalendarData.to_datetime(get_start_date(vevent))

@staticmethod
def is_finished(vevent):
"""Is it over."""
return dt.utcnow() >= O365CalendarData.to_datetime(
O365CalendarData.get_end_date(vevent)
)

@staticmethod
def get_hass_date(obj, is_all_day):
"""Get the date."""
return obj if isinstance(obj, datetime) and not is_all_day else obj.date()
return dt.utcnow() >= O365CalendarData.to_datetime(get_end_date(vevent))

@staticmethod
def to_datetime(obj):
Expand All @@ -734,22 +743,6 @@ def to_datetime(obj):
date_obj = dt.as_local(dt.parse_datetime(obj["dateTime"]))
return dt.as_utc(date_obj)

@staticmethod
def get_end_date(obj):
"""Get the end date."""
if hasattr(obj, "end"):
return obj.end

if hasattr(obj, "duration"):
return obj.start + obj.duration.value

return obj.start + timedelta(days=1)

@staticmethod
def get_start_date(obj):
"""Get the start date."""
return obj.start


class CalendarServices:
"""Calendar Services."""
Expand Down
8 changes: 3 additions & 5 deletions custom_components/o365/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
)
from homeassistant.const import CONF_ENABLED, CONF_NAME

from O365.calendar import (
AttendeeType, # pylint: disable=no-name-in-module
EventSensitivity, # pylint: disable=no-name-in-module
EventShowAs, # pylint: disable=no-name-in-module
)
from O365.calendar import AttendeeType # pylint: disable=no-name-in-module
from O365.calendar import EventSensitivity # pylint: disable=no-name-in-module
from O365.calendar import EventShowAs # pylint: disable=no-name-in-module
from O365.mailbox import ExternalAudience # pylint: disable=no-name-in-module
from O365.utils import ImportanceLevel # pylint: disable=no-name-in-module

Expand Down
32 changes: 29 additions & 3 deletions custom_components/o365/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import os
import shutil
import zipfile
from datetime import datetime
from datetime import datetime, timedelta
from pathlib import Path

import yaml
from bs4 import BeautifulSoup
from homeassistant.const import CONF_ENABLED, CONF_NAME
from homeassistant.helpers.network import get_url
from homeassistant.util import dt
from voluptuous.error import Error as VoluptuousError

from O365.calendar import Attendee # pylint: disable=no-name-in-module)
Expand Down Expand Up @@ -273,8 +274,8 @@ def format_event_data(event):
"""Format the event data."""
return {
"summary": event.subject,
"start": event.start,
"end": event.end,
"start": get_hass_date(event.start, event.is_all_day),
"end": get_hass_date(get_end_date(event), event.is_all_day),
"all_day": event.is_all_day,
"description": clean_html(event.body),
"location": event.location["displayName"],
Expand All @@ -289,6 +290,31 @@ def format_event_data(event):
}


def get_hass_date(obj, is_all_day):
"""Get the date."""
if isinstance(obj, datetime) and not is_all_day:
return obj
else:
return dt.as_utc(dt.start_of_local_day(obj))
# return obj if isinstance(obj, datetime) and not is_all_day else obj.date()


def get_end_date(obj):
"""Get the end date."""
if hasattr(obj, "end"):
return obj.end

if hasattr(obj, "duration"):
return obj.start + obj.duration.value

return obj.start + timedelta(days=1)


def get_start_date(obj):
"""Get the start date."""
return obj.start


def add_call_data_to_event(
event,
subject,
Expand Down

0 comments on commit f194d4d

Please sign in to comment.