From a8d02bf63e727f27e32ac88f3295e4ba2df3643c Mon Sep 17 00:00:00 2001 From: RogerSelwyn Date: Sun, 26 Sep 2021 11:20:51 +0100 Subject: [PATCH] Fix incorrect handling of all daya events https://github.com/RogerSelwyn/O365-HomeAssistant/issues/6 --- custom_components/o365/__init__.py | 2 +- custom_components/o365/calendar.py | 17 +++++++++-------- custom_components/o365/utils.py | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/custom_components/o365/__init__.py b/custom_components/o365/__init__.py index 742e622..71fe610 100644 --- a/custom_components/o365/__init__.py +++ b/custom_components/o365/__init__.py @@ -51,7 +51,7 @@ async def async_setup(hass, config): except NameError: callback_url = f"{hass.config.api.base_url}{AUTH_CALLBACK_PATH}" - account = Account(credentials, token_backend=TOKEN_BACKEND) + account = Account(credentials, token_backend=TOKEN_BACKEND, timezone="UTC") is_authenticated = account.is_authenticated permissions = validate_permissions() if is_authenticated and permissions: diff --git a/custom_components/o365/calendar.py b/custom_components/o365/calendar.py index a40cb69..c0a2b12 100644 --- a/custom_components/o365/calendar.py +++ b/custom_components/o365/calendar.py @@ -97,7 +97,7 @@ def __init__(self, hass, account, calendar_id, entity, entity_id): def device_state_attributes(self): """Device state property.""" return { - "all_day": self._event.get("is_all_day", False) if self.data.event is not None else False, + "all_day": self._event.get("all_day", False) if self.data.event is not None else False, "offset_reached": self._offset_reached, "data": self._data_attribute, } @@ -175,8 +175,8 @@ async def async_get_events(self, hass, start_date, end_date): event_list = [] for event in vevent_list: data = format_event_data(event, self.calendar.calendar_id) - data["start"] = self.get_hass_date(data["start"]) - data["end"] = self.get_hass_date(data["end"]) + data["start"] = self.get_hass_date(data["start"], event.is_all_day) + data["end"] = self.get_hass_date(data["end"], event.is_all_day) event_list.append(data) return event_list @@ -208,10 +208,11 @@ async def async_update(self, hass): self.event = { "summary": vevent.subject, - "start": self.get_hass_date(vevent.start), - "end": self.get_hass_date(self.get_end_date(vevent)), + "start": self.get_hass_date(vevent.start, vevent.is_all_day), + "end": self.get_hass_date(self.get_end_date(vevent), vevent.is_all_day), "location": vevent.location, "description": clean_html(vevent.body), + "all_day": vevent.is_all_day, } @staticmethod @@ -225,12 +226,12 @@ def is_over(vevent): return dt.now() >= O365CalendarData.to_datetime(O365CalendarData.get_end_date(vevent)) @staticmethod - def get_hass_date(obj): + def get_hass_date(obj, is_all_day): """Get the date.""" - if isinstance(obj, datetime): + if isinstance(obj, datetime) and not is_all_day: return {"dateTime": obj.isoformat()} - return {"date": obj.isoformat()} + return {"date": obj.date().isoformat()} @staticmethod def to_datetime(obj): diff --git a/custom_components/o365/utils.py b/custom_components/o365/utils.py index 9ef5250..d82cb2c 100644 --- a/custom_components/o365/utils.py +++ b/custom_components/o365/utils.py @@ -102,7 +102,7 @@ def format_event_data(event, calendar_id): "categories": event.categories, "sensitivity": event.sensitivity.name, "show_as": event.show_as.name, - "is_all_day": event.is_all_day, + "all_day": event.is_all_day, "attendees": [ {"email": x.address, "type": x.attendee_type.value} for x in event.attendees._Attendees__attendees ],