From bd38fd9516fb970d1a9ef97f627340480274aaf3 Mon Sep 17 00:00:00 2001
From: Allen Porter <allen@thebends.org>
Date: Tue, 10 Oct 2023 20:48:46 -0700
Subject: [PATCH] Add google calendar required feature for create event service
 (#101741)

* Add google calendar required feature for create event service

* Update docstring
---
 homeassistant/components/google/calendar.py |  1 +
 tests/components/google/test_init.py        | 53 +++++++++++++++++++++
 2 files changed, 54 insertions(+)

diff --git a/homeassistant/components/google/calendar.py b/homeassistant/components/google/calendar.py
index 9559a06d49c..bd0fe18912e 100644
--- a/homeassistant/components/google/calendar.py
+++ b/homeassistant/components/google/calendar.py
@@ -240,6 +240,7 @@ async def async_setup_entry(
             SERVICE_CREATE_EVENT,
             CREATE_EVENT_SCHEMA,
             async_create_event,
+            required_features=CalendarEntityFeature.CREATE_EVENT,
         )
 
 
diff --git a/tests/components/google/test_init.py b/tests/components/google/test_init.py
index 233635510e0..9ede0573922 100644
--- a/tests/components/google/test_init.py
+++ b/tests/components/google/test_init.py
@@ -576,6 +576,59 @@ async def test_add_event_date_time(
     }
 
 
+@pytest.mark.parametrize(
+    "calendars_config",
+    [
+        [
+            {
+                "cal_id": CALENDAR_ID,
+                "entities": [
+                    {
+                        "device_id": "backyard_light",
+                        "name": "Backyard Light",
+                        "search": "#Backyard",
+                    },
+                ],
+            }
+        ],
+    ],
+)
+async def test_unsupported_create_event(
+    hass: HomeAssistant,
+    mock_calendars_yaml: Mock,
+    component_setup: ComponentSetup,
+    mock_calendars_list: ApiResult,
+    mock_insert_event: Callable[[str, dict[str, Any]], None],
+    test_api_calendar: dict[str, Any],
+    mock_events_list: ApiResult,
+    aioclient_mock: AiohttpClientMocker,
+) -> None:
+    """Test create event service call is unsupported for virtual calendars."""
+
+    mock_calendars_list({"items": [test_api_calendar]})
+    mock_events_list({})
+    assert await component_setup()
+
+    start_datetime = datetime.datetime.now(tz=zoneinfo.ZoneInfo("America/Regina"))
+    delta = datetime.timedelta(days=3, hours=3)
+    end_datetime = start_datetime + delta
+
+    with pytest.raises(HomeAssistantError, match="does not support this service"):
+        await hass.services.async_call(
+            DOMAIN,
+            "create_event",
+            {
+                # **data,
+                "start_date_time": start_datetime.isoformat(),
+                "end_date_time": end_datetime.isoformat(),
+                "summary": TEST_EVENT_SUMMARY,
+                "description": TEST_EVENT_DESCRIPTION,
+            },
+            target={"entity_id": "calendar.backyard_light"},
+            blocking=True,
+        )
+
+
 async def test_add_event_failure(
     hass: HomeAssistant,
     component_setup: ComponentSetup,