Fix for Google Calendar API returning invalid RRULE:DATE rules (#103870)

pull/103873/head
Allen Porter 2023-11-12 10:44:26 -08:00 committed by GitHub
parent 80042aa108
commit abb1328a67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 2 deletions

View File

@ -521,8 +521,13 @@ class GoogleCalendarEntity(
def _get_calendar_event(event: Event) -> CalendarEvent:
"""Return a CalendarEvent from an API event."""
rrule: str | None = None
if len(event.recurrence) == 1:
rrule = event.recurrence[0].lstrip(RRULE_PREFIX)
# Home Assistant expects a single RRULE: and all other rule types are unsupported or ignored
if (
len(event.recurrence) == 1
and (raw_rule := event.recurrence[0])
and raw_rule.startswith(RRULE_PREFIX)
):
rrule = raw_rule.removeprefix(RRULE_PREFIX)
return CalendarEvent(
uid=event.ical_uuid,
recurrence_id=event.id if event.recurring_event_id else None,

View File

@ -1299,3 +1299,51 @@ async def test_event_differs_timezone(
"description": event["description"],
"supported_features": 3,
}
async def test_invalid_rrule_fix(
hass: HomeAssistant,
hass_client: ClientSessionGenerator,
mock_events_list_items,
component_setup,
) -> None:
"""Test that an invalid RRULE returned from Google Calendar API is handled correctly end to end."""
week_from_today = dt_util.now().date() + datetime.timedelta(days=7)
end_event = week_from_today + datetime.timedelta(days=1)
event = {
**TEST_EVENT,
"start": {"date": week_from_today.isoformat()},
"end": {"date": end_event.isoformat()},
"recurrence": [
"RRULE:DATE;TZID=Europe/Warsaw:20230818T020000,20230915T020000,20231013T020000,20231110T010000,20231208T010000",
],
}
mock_events_list_items([event])
assert await component_setup()
state = hass.states.get(TEST_ENTITY)
assert state.name == TEST_ENTITY_NAME
assert state.state == STATE_OFF
# Pick a date range that contains two instances of the event
web_client = await hass_client()
response = await web_client.get(
get_events_url(TEST_ENTITY, "2023-08-10T00:00:00Z", "2023-09-20T00:00:00Z")
)
assert response.status == HTTPStatus.OK
events = await response.json()
# Both instances are returned, however the RDATE rule is ignored by Home
# Assistant so they are just treateded as flattened events.
assert len(events) == 2
event = events[0]
assert event["uid"] == "cydrevtfuybguinhomj@google.com"
assert event["recurrence_id"] == "_c8rinwq863h45qnucyoi43ny8_20230818"
assert event["rrule"] is None
event = events[1]
assert event["uid"] == "cydrevtfuybguinhomj@google.com"
assert event["recurrence_id"] == "_c8rinwq863h45qnucyoi43ny8_20230915"
assert event["rrule"] is None