Return CalDAV events in order (#186718)

This commit is contained in:
jimjaeger
2026-10-21 12:01:05 +03:00
committed by GitHub
parent 7bbe85e4bc
commit 0360fca71a
2 changed files with 97 additions and 0 deletions
@@ -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
+94
View File
@@ -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"),