Remove google found_calendar service (#72260)

* Remove google found_calendar service

* Partial revert of yaml changes

* Store calendar id in a local variable
This commit is contained in:
Allen Porter 2022-05-21 08:39:41 -07:00 committed by GitHub
parent f1ac9f8cca
commit b38289ee50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 20 deletions

View File

@ -10,7 +10,7 @@ from typing import Any
import aiohttp import aiohttp
from gcal_sync.api import GoogleCalendarService from gcal_sync.api import GoogleCalendarService
from gcal_sync.exceptions import ApiException 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 from oauth2client.file import Storage
import voluptuous as vol import voluptuous as vol
from voluptuous.error import Error as VoluptuousError from voluptuous.error import Error as VoluptuousError
@ -87,7 +87,6 @@ NOTIFICATION_TITLE = "Google Calendar Setup"
GROUP_NAME_ALL_CALENDARS = "Google Calendar Sensors" GROUP_NAME_ALL_CALENDARS = "Google Calendar Sensors"
SERVICE_SCAN_CALENDARS = "scan_for_calendars" SERVICE_SCAN_CALENDARS = "scan_for_calendars"
SERVICE_FOUND_CALENDARS = "found_calendar"
SERVICE_ADD_EVENT = "add_event" SERVICE_ADD_EVENT = "add_event"
YAML_DEVICES = f"{DOMAIN}_calendars.yaml" YAML_DEVICES = f"{DOMAIN}_calendars.yaml"
@ -250,19 +249,19 @@ async def async_setup_services(
) -> None: ) -> None:
"""Set up the service listeners.""" """Set up the service listeners."""
created_calendars = set()
calendars = await hass.async_add_executor_job( calendars = await hass.async_add_executor_job(
load_config, hass.config.path(YAML_DEVICES) load_config, hass.config.path(YAML_DEVICES)
) )
async def _found_calendar(call: ServiceCall) -> None: async def _found_calendar(calendar_item: Calendar) -> None:
calendar = get_calendar_info(hass, call.data) calendar = get_calendar_info(
calendar_id = calendar[CONF_CAL_ID] hass,
{
if calendar_id in created_calendars: **calendar_item.dict(exclude_unset=True),
return CONF_TRACK: track_new,
created_calendars.add(calendar_id) },
)
calendar_id = calendar_item.id
# Populate the yaml file with all discovered calendars # Populate the yaml file with all discovered calendars
if calendar_id not in calendars: if calendar_id not in calendars:
calendars[calendar_id] = calendar calendars[calendar_id] = calendar
@ -274,7 +273,7 @@ async def async_setup_services(
calendar = calendars[calendar_id] calendar = calendars[calendar_id]
async_dispatcher_send(hass, DISCOVER_CALENDAR, calendar) 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: async def _scan_for_calendars(call: ServiceCall) -> None:
"""Scan for new calendars.""" """Scan for new calendars."""
@ -284,11 +283,10 @@ async def async_setup_services(
raise HomeAssistantError(str(err)) from err raise HomeAssistantError(str(err)) from err
tasks = [] tasks = []
for calendar_item in result.items: for calendar_item in result.items:
calendar = calendar_item.dict(exclude_unset=True) if calendar_item.id in created_calendars:
calendar[CONF_TRACK] = track_new continue
tasks.append( created_calendars.add(calendar_item.id)
hass.services.async_call(DOMAIN, SERVICE_FOUND_CALENDARS, calendar) tasks.append(_found_calendar(calendar_item))
)
await asyncio.gather(*tasks) await asyncio.gather(*tasks)
hass.services.async_register(DOMAIN, SERVICE_SCAN_CALENDARS, _scan_for_calendars) hass.services.async_register(DOMAIN, SERVICE_SCAN_CALENDARS, _scan_for_calendars)

View File

@ -1,6 +1,3 @@
found_calendar:
name: Found Calendar
description: Add calendar if it has not been already discovered.
scan_for_calendars: scan_for_calendars:
name: Scan for calendars name: Scan for calendars
description: Scan for new calendars. description: Scan for new calendars.