From 0360fca71a7ba285a6f6faa139eaf258654e5989 Mon Sep 17 00:00:00 2001 From: jimjaeger Date: Wed, 21 Oct 2026 11:01:05 +0200 Subject: [PATCH] Return CalDAV events in order (#186718) --- .../components/caldav/coordinator.py | 3 + tests/components/caldav/test_calendar.py | 94 +++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/homeassistant/components/caldav/coordinator.py b/homeassistant/components/caldav/coordinator.py index a1de80ec3e25..0b8ccd740007 100644 --- a/homeassistant/components/caldav/coordinator.py +++ b/homeassistant/components/caldav/coordinator.py @@ -129,6 +129,9 @@ class CalDavUpdateCoordinator(DataUpdateCoordinator[CalendarEvent | None]): ) ) + # Sort by timestamp, as datetimes in the same time zone are compared by + # wall time, which puts events in the wrong order when clocks fall back + event_list.sort(key=lambda event: event.start_datetime_local.timestamp()) return event_list @override diff --git a/tests/components/caldav/test_calendar.py b/tests/components/caldav/test_calendar.py index 594c6debf796..6ee16e6eb222 100644 --- a/tests/components/caldav/test_calendar.py +++ b/tests/components/caldav/test_calendar.py @@ -1353,6 +1353,100 @@ async def test_config_entry_supported_components( assert not state +def _ics_event(uid: str, dates: str) -> str: + """Return a VCALENDAR holding a single VEVENT with the given dates.""" + return ( + "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//Test//CalDAV Client//EN\n" + f"BEGIN:VEVENT\nUID:{uid}\nDTSTAMP:20240801T000000Z\n{dates}\n" + f"SUMMARY:{uid}\nEND:VEVENT\nEND:VCALENDAR\n" + ) + + +@pytest.mark.parametrize("tz", [AMERICA_NEW_YORK]) +@pytest.mark.parametrize( + "calendars", + [ + [ + _mock_calendar_holding( + CALENDAR_NAME, + [ + _ics_event( + "late", "DTSTART:20240829T150000Z\nDTEND:20240829T160000Z" + ), + _ics_event( + "all-day", + "DTSTART;VALUE=DATE:20240829\nDTEND;VALUE=DATE:20240830", + ), + _ics_event( + "early", "DTSTART:20240829T120000Z\nDTEND:20240829T130000Z" + ), + ], + ) + ] + ], +) +async def test_get_events_sorted( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + setup_platform_cb: Callable[[], Awaitable[None]], +) -> None: + """Test events are returned in order, including all-day events.""" + await setup_platform_cb() + + client = await hass_client() + response = await client.get( + f"/api/calendars/{TEST_ENTITY}" + "?start=2024-08-29T00:00:00-04:00&end=2024-08-30T00:00:00-04:00" + ) + assert response.status == HTTPStatus.OK + assert [event["summary"] for event in await response.json()] == [ + "all-day", + "early", + "late", + ] + + +@pytest.mark.parametrize("tz", [AMERICA_NEW_YORK]) +@pytest.mark.parametrize( + "calendars", + [ + [ + _mock_calendar_holding( + CALENDAR_NAME, + [ + # 01:15 EST, after the clocks fell back + _ics_event( + "second", "DTSTART:20241103T061500Z\nDTEND:20241103T063000Z" + ), + # 01:30 EDT, before the clocks fell back + _ics_event( + "first", "DTSTART:20241103T053000Z\nDTEND:20241103T054500Z" + ), + ], + ) + ] + ], +) +async def test_get_events_sorted_dst_fallback( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + setup_platform_cb: Callable[[], Awaitable[None]], +) -> None: + """Test events in the repeated hour are returned in chronological order.""" + await setup_platform_cb() + + client = await hass_client() + response = await client.get( + f"/api/calendars/{TEST_ENTITY}" + "?start=2024-11-03T00:00:00-04:00&end=2024-11-04T00:00:00-05:00" + ) + assert response.status == HTTPStatus.OK + assert [event["summary"] for event in await response.json()] == [ + "first", + "second", + ] + + @pytest.mark.parametrize("tz", [UTC]) @pytest.mark.parametrize( ("service_data", "expected_ics_fields"),