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,