From cf9ada3b0ed4c7e322eae5850b3bae78fa95bacb Mon Sep 17 00:00:00 2001 From: Allen Porter <allen@thebends.org> Date: Mon, 10 Apr 2023 09:05:08 -0700 Subject: [PATCH] Fix all day event coercion logic (#91169) --- homeassistant/components/calendar/__init__.py | 31 ++++++------------- .../components/local_calendar/calendar.py | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/calendar/__init__.py b/homeassistant/components/calendar/__init__.py index 594964c129c..2445c054c6d 100644 --- a/homeassistant/components/calendar/__init__.py +++ b/homeassistant/components/calendar/__init__.py @@ -140,26 +140,6 @@ def _has_min_duration( return validate -def _has_all_day_event_duration( - start_key: str, - end_key: str, -) -> Callable[[dict[str, Any]], dict[str, Any]]: - """Modify all day events to have a duration of one day.""" - - def validate(obj: dict[str, Any]) -> dict[str, Any]: - if ( - (start := obj.get(start_key)) - and (end := obj.get(end_key)) - and not isinstance(start, datetime.datetime) - and not isinstance(end, datetime.datetime) - and start == end - ): - obj[end_key] = start + datetime.timedelta(days=1) - return obj - - return validate - - def _has_same_type(*keys: Any) -> Callable[[dict[str, Any]], dict[str, Any]]: """Verify that all values are of the same type.""" @@ -267,7 +247,6 @@ CALENDAR_EVENT_SCHEMA = vol.Schema( _has_consistent_timezone("start", "end"), _as_local_timezone("start", "end"), _has_min_duration("start", "end", MIN_EVENT_DURATION), - _has_all_day_event_duration("start", "end"), ), extra=vol.ALLOW_EXTRA, ) @@ -375,6 +354,16 @@ class CalendarEvent: f"Failed to validate CalendarEvent: {err}" ) from err + # It is common to set a start an end date to be the same thing for + # an all day event, but that is not a valid duration. Fix to have a + # duration of one day. + if ( + not isinstance(self.start, datetime.datetime) + and not isinstance(self.end, datetime.datetime) + and self.start == self.end + ): + self.end = self.start + datetime.timedelta(days=1) + def _event_dict_factory(obj: Iterable[tuple[str, Any]]) -> dict[str, str]: """Convert CalendarEvent dataclass items to dictionary of attributes.""" diff --git a/homeassistant/components/local_calendar/calendar.py b/homeassistant/components/local_calendar/calendar.py index 6cfcaec61d0..423be8143b8 100644 --- a/homeassistant/components/local_calendar/calendar.py +++ b/homeassistant/components/local_calendar/calendar.py @@ -196,7 +196,7 @@ def _get_calendar_event(event: Event) -> CalendarEvent: else: start = event.start end = event.end - if (end - start) <= timedelta(days=0): + if (end - start) < timedelta(days=0): end = start + timedelta(days=1) return CalendarEvent( -- GitLab