From 9bf0cbd65937f9cf7abdd08a8e7c81972c52a0c8 Mon Sep 17 00:00:00 2001 From: Joel Hawksley Date: Sun, 27 Oct 2024 21:54:09 -0700 Subject: [PATCH] Omit declined Google Calendar events (#128900) * Omit decline Google Calendar events * move comment to top of function and update * Apply suggestions from code review * import ResponseStatus --- homeassistant/components/google/calendar.py | 18 ++++++- tests/components/google/test_calendar.py | 56 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/google/calendar.py b/homeassistant/components/google/calendar.py index dea286237d3..5ac5dae616c 100644 --- a/homeassistant/components/google/calendar.py +++ b/homeassistant/components/google/calendar.py @@ -10,7 +10,14 @@ from typing import Any, cast from gcal_sync.api import Range, SyncEventsRequest from gcal_sync.exceptions import ApiException -from gcal_sync.model import AccessRole, Calendar, DateOrDatetime, Event, EventTypeEnum +from gcal_sync.model import ( + AccessRole, + Calendar, + DateOrDatetime, + Event, + EventTypeEnum, + ResponseStatus, +) from gcal_sync.store import ScopedCalendarStore from gcal_sync.sync import CalendarEventSyncManager @@ -367,7 +374,14 @@ class GoogleCalendarEntity( return event def _event_filter(self, event: Event) -> bool: - """Return True if the event is visible.""" + """Return True if the event is visible and not declined.""" + + if any( + attendee.is_self and attendee.response_status == ResponseStatus.DECLINED + for attendee in event.attendees + ): + return False + if event.event_type == EventTypeEnum.WORKING_LOCATION: return self.entity_description.working_location if self._ignore_availability: diff --git a/tests/components/google/test_calendar.py b/tests/components/google/test_calendar.py index 03b171c5e19..6ce95a2bc17 100644 --- a/tests/components/google/test_calendar.py +++ b/tests/components/google/test_calendar.py @@ -572,6 +572,62 @@ async def test_opaque_event( assert state.state == (STATE_ON if expect_visible_event else STATE_OFF) +async def test_declined_event( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_calendars_yaml, + mock_events_list_items, + component_setup, +) -> None: + """Test querying the API and fetching events from the server.""" + event = { + **TEST_EVENT, + **upcoming(), + "attendees": [ + { + "self": "True", + "responseStatus": "declined", + } + ], + } + mock_events_list_items([event]) + assert await component_setup() + + client = await hass_client() + response = await client.get(upcoming_event_url(TEST_YAML_ENTITY)) + assert response.status == HTTPStatus.OK + events = await response.json() + assert len(events) == 0 + + +async def test_attending_event( + hass: HomeAssistant, + hass_client: ClientSessionGenerator, + mock_calendars_yaml, + mock_events_list_items, + component_setup, +) -> None: + """Test querying the API and fetching events from the server.""" + event = { + **TEST_EVENT, + **upcoming(), + "attendees": [ + { + "self": "True", + "responseStatus": "accepted", + } + ], + } + mock_events_list_items([event]) + assert await component_setup() + + client = await hass_client() + response = await client.get(upcoming_event_url(TEST_YAML_ENTITY)) + assert response.status == HTTPStatus.OK + events = await response.json() + assert len(events) == 1 + + @pytest.mark.parametrize("mock_test_setup", [None]) async def test_scan_calendar_error( hass: HomeAssistant,