Make google calendar fail louder on invalid google_calendars.yaml (#83668)

* Make google calendar fail louder on invalid google_calendars.yaml

* Update homeassistant/components/google/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/google/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Allen Porter 2022-12-10 09:26:28 -08:00 committed by GitHub
parent af1c326cff
commit 1ac7f5f013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 22 deletions

View file

@ -11,7 +11,6 @@ from gcal_sync.api import GoogleCalendarService
from gcal_sync.exceptions import ApiException, AuthException
from gcal_sync.model import DateOrDatetime, Event
import voluptuous as vol
from voluptuous.error import Error as VoluptuousError
import yaml
from homeassistant.config_entries import ConfigEntry
@ -143,6 +142,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {}
# Validate google_calendars.yaml (if present) as soon as possible to return
# helpful error messages.
try:
await hass.async_add_executor_job(load_config, hass.config.path(YAML_DEVICES))
except vol.Invalid as err:
_LOGGER.error("Configuration error in %s: %s", YAML_DEVICES, str(err))
return False
implementation = (
await config_entry_oauth2_flow.async_get_config_entry_implementation(
hass, entry
@ -322,11 +329,7 @@ def load_config(path: str) -> dict[str, Any]:
with open(path, encoding="utf8") as file:
data = yaml.safe_load(file)
for calendar in data:
try:
calendars.update({calendar[CONF_CAL_ID]: DEVICE_SCHEMA(calendar)})
except VoluptuousError as exception:
# keep going
_LOGGER.warning("Calendar Invalid Data: %s", exception)
calendars[calendar[CONF_CAL_ID]] = DEVICE_SCHEMA(calendar)
except FileNotFoundError as err:
_LOGGER.debug("Error reading calendar configuration: %s", err)
# When YAML file could not be loaded/did not contain a dict