From b38289ee507d164e15dc3021b6aaae6134534eba Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sat, 21 May 2022 08:39:41 -0700 Subject: [PATCH] Remove google found_calendar service (#72260) * Remove google found_calendar service * Partial revert of yaml changes * Store calendar id in a local variable --- homeassistant/components/google/__init__.py | 32 +++++++++---------- homeassistant/components/google/services.yaml | 3 -- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/google/__init__.py b/homeassistant/components/google/__init__.py index 61b25a9c027..73b7f8d8ae6 100644 --- a/homeassistant/components/google/__init__.py +++ b/homeassistant/components/google/__init__.py @@ -10,7 +10,7 @@ from typing import Any import aiohttp from gcal_sync.api import GoogleCalendarService from gcal_sync.exceptions import ApiException -from gcal_sync.model import DateOrDatetime, Event +from gcal_sync.model import Calendar, DateOrDatetime, Event from oauth2client.file import Storage import voluptuous as vol from voluptuous.error import Error as VoluptuousError @@ -87,7 +87,6 @@ NOTIFICATION_TITLE = "Google Calendar Setup" GROUP_NAME_ALL_CALENDARS = "Google Calendar Sensors" SERVICE_SCAN_CALENDARS = "scan_for_calendars" -SERVICE_FOUND_CALENDARS = "found_calendar" SERVICE_ADD_EVENT = "add_event" YAML_DEVICES = f"{DOMAIN}_calendars.yaml" @@ -250,19 +249,19 @@ async def async_setup_services( ) -> None: """Set up the service listeners.""" - created_calendars = set() calendars = await hass.async_add_executor_job( load_config, hass.config.path(YAML_DEVICES) ) - async def _found_calendar(call: ServiceCall) -> None: - calendar = get_calendar_info(hass, call.data) - calendar_id = calendar[CONF_CAL_ID] - - if calendar_id in created_calendars: - return - created_calendars.add(calendar_id) - + async def _found_calendar(calendar_item: Calendar) -> None: + calendar = get_calendar_info( + hass, + { + **calendar_item.dict(exclude_unset=True), + CONF_TRACK: track_new, + }, + ) + calendar_id = calendar_item.id # Populate the yaml file with all discovered calendars if calendar_id not in calendars: calendars[calendar_id] = calendar @@ -274,7 +273,7 @@ async def async_setup_services( calendar = calendars[calendar_id] async_dispatcher_send(hass, DISCOVER_CALENDAR, calendar) - hass.services.async_register(DOMAIN, SERVICE_FOUND_CALENDARS, _found_calendar) + created_calendars = set() async def _scan_for_calendars(call: ServiceCall) -> None: """Scan for new calendars.""" @@ -284,11 +283,10 @@ async def async_setup_services( raise HomeAssistantError(str(err)) from err tasks = [] for calendar_item in result.items: - calendar = calendar_item.dict(exclude_unset=True) - calendar[CONF_TRACK] = track_new - tasks.append( - hass.services.async_call(DOMAIN, SERVICE_FOUND_CALENDARS, calendar) - ) + if calendar_item.id in created_calendars: + continue + created_calendars.add(calendar_item.id) + tasks.append(_found_calendar(calendar_item)) await asyncio.gather(*tasks) hass.services.async_register(DOMAIN, SERVICE_SCAN_CALENDARS, _scan_for_calendars) diff --git a/homeassistant/components/google/services.yaml b/homeassistant/components/google/services.yaml index ff053f1be33..21df763374f 100644 --- a/homeassistant/components/google/services.yaml +++ b/homeassistant/components/google/services.yaml @@ -1,6 +1,3 @@ -found_calendar: - name: Found Calendar - description: Add calendar if it has not been already discovered. scan_for_calendars: name: Scan for calendars description: Scan for new calendars.