Skip to content
Snippets Groups Projects
Unverified Commit cf9ada3b authored by Allen Porter's avatar Allen Porter Committed by GitHub
Browse files

Fix all day event coercion logic (#91169)

parent 49079691
No related branches found
No related tags found
No related merge requests found
...@@ -140,26 +140,6 @@ def _has_min_duration( ...@@ -140,26 +140,6 @@ def _has_min_duration(
return validate 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]]: def _has_same_type(*keys: Any) -> Callable[[dict[str, Any]], dict[str, Any]]:
"""Verify that all values are of the same type.""" """Verify that all values are of the same type."""
...@@ -267,7 +247,6 @@ CALENDAR_EVENT_SCHEMA = vol.Schema( ...@@ -267,7 +247,6 @@ CALENDAR_EVENT_SCHEMA = vol.Schema(
_has_consistent_timezone("start", "end"), _has_consistent_timezone("start", "end"),
_as_local_timezone("start", "end"), _as_local_timezone("start", "end"),
_has_min_duration("start", "end", MIN_EVENT_DURATION), _has_min_duration("start", "end", MIN_EVENT_DURATION),
_has_all_day_event_duration("start", "end"),
), ),
extra=vol.ALLOW_EXTRA, extra=vol.ALLOW_EXTRA,
) )
...@@ -375,6 +354,16 @@ class CalendarEvent: ...@@ -375,6 +354,16 @@ class CalendarEvent:
f"Failed to validate CalendarEvent: {err}" f"Failed to validate CalendarEvent: {err}"
) from 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]: def _event_dict_factory(obj: Iterable[tuple[str, Any]]) -> dict[str, str]:
"""Convert CalendarEvent dataclass items to dictionary of attributes.""" """Convert CalendarEvent dataclass items to dictionary of attributes."""
......
...@@ -196,7 +196,7 @@ def _get_calendar_event(event: Event) -> CalendarEvent: ...@@ -196,7 +196,7 @@ def _get_calendar_event(event: Event) -> CalendarEvent:
else: else:
start = event.start start = event.start
end = event.end end = event.end
if (end - start) <= timedelta(days=0): if (end - start) < timedelta(days=0):
end = start + timedelta(days=1) end = start + timedelta(days=1)
return CalendarEvent( return CalendarEvent(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment