From 1bb4c9d213dd0451b9003f365ebb4fd3aaa59445 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Tue, 27 Jan 2026 21:00:40 +0100 Subject: [PATCH] Add support for initial color in Google Calendar (#161671) --- homeassistant/components/google/calendar.py | 3 ++ tests/components/google/test_calendar.py | 40 +++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/homeassistant/components/google/calendar.py b/homeassistant/components/google/calendar.py index d6d740bd0aa..e9d5b41fef3 100644 --- a/homeassistant/components/google/calendar.py +++ b/homeassistant/components/google/calendar.py @@ -101,6 +101,7 @@ class GoogleCalendarEntityDescription(CalendarEntityDescription): search: str | None local_sync: bool device_id: str + initial_color: str | None = None event_type: EventTypeEnum | None = None @@ -161,6 +162,7 @@ def _get_entity_descriptions( local_sync=local_sync, entity_registry_enabled_default=entity_enabled, device_id=data[CONF_DEVICE_ID], + initial_color=calendar_item.background_color, ) entity_descriptions.append(entity_description) _LOGGER.debug( @@ -359,6 +361,7 @@ class GoogleCalendarEntity( if entity_description.entity_id: self.entity_id = entity_description.entity_id self._attr_unique_id = unique_id + self._attr_initial_color = entity_description.initial_color if not entity_description.read_only: self._attr_supported_features = ( CalendarEntityFeature.CREATE_EVENT | CalendarEntityFeature.DELETE_EVENT diff --git a/tests/components/google/test_calendar.py b/tests/components/google/test_calendar.py index 720c0176850..d5431ac8189 100644 --- a/tests/components/google/test_calendar.py +++ b/tests/components/google/test_calendar.py @@ -1562,3 +1562,43 @@ async def test_birthday_entity( assert state assert state.name == "Birthdays" assert state.attributes.get("message") == expected_event_message + + +@pytest.mark.parametrize( + ("background_color", "expected_color"), + [ + ("#16a765", "#16a765"), # Valid color + ("not-a-color", None), # Invalid color + (None, None), # Missing color + ], +) +async def test_calendar_background_color( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + mock_calendars_list: ApiResult, + mock_events_list_items: Callable[[list[dict[str, Any]]], None], + component_setup: ComponentSetup, + entity_registry: er.EntityRegistry, + background_color: str | None, + expected_color: str | None, +) -> None: + """Test backgroundColor from API is stored in entity options only if valid.""" + aioclient_mock.clear_requests() + calendar_item: dict[str, Any] = { + "id": CALENDAR_ID, + "etag": '"3584134138943410"', + "timeZone": "UTC", + "accessRole": "owner", + "summary": "Test Calendar", + } + if background_color is not None: + calendar_item["backgroundColor"] = background_color + mock_calendars_list({"items": [calendar_item]}) + mock_events_list_items([]) + + assert await component_setup() + + # Verify the main calendar entity has the color set + entity = entity_registry.async_get("calendar.test_calendar") + assert entity is not None + assert entity.options.get("calendar", {}).get("color") == expected_color