From 6b6f115d7ea4158c0abe78a5ed51975b48de2c45 Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sat, 31 Dec 2022 02:50:56 -0800 Subject: [PATCH] Fix handling of empty google_calendars.yaml file (#84909) fixes undefined --- homeassistant/components/google/__init__.py | 2 +- tests/components/google/conftest.py | 4 +++- tests/components/google/test_init.py | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/google/__init__.py b/homeassistant/components/google/__init__.py index bc5e814b24d..fa39d3bb31b 100644 --- a/homeassistant/components/google/__init__.py +++ b/homeassistant/components/google/__init__.py @@ -327,7 +327,7 @@ def load_config(path: str) -> dict[str, Any]: calendars = {} try: with open(path, encoding="utf8") as file: - data = yaml.safe_load(file) + data = yaml.safe_load(file) or [] for calendar in data: calendars[calendar[CONF_CAL_ID]] = DEVICE_SCHEMA(calendar) except FileNotFoundError as err: diff --git a/tests/components/google/conftest.py b/tests/components/google/conftest.py index 88e75499678..6e466bdcd4f 100644 --- a/tests/components/google/conftest.py +++ b/tests/components/google/conftest.py @@ -121,7 +121,9 @@ def mock_calendars_yaml( calendars_config: list[dict[str, Any]], ) -> Generator[Mock, None, None]: """Fixture that prepares the google_calendars.yaml mocks.""" - mocked_open_function = mock_open(read_data=yaml.dump(calendars_config)) + mocked_open_function = mock_open( + read_data=yaml.dump(calendars_config) if calendars_config else None + ) with patch("homeassistant.components.google.open", mocked_open_function): yield mocked_open_function diff --git a/tests/components/google/test_init.py b/tests/components/google/test_init.py index 452b2300e5b..861f3d9cbdf 100644 --- a/tests/components/google/test_init.py +++ b/tests/components/google/test_init.py @@ -192,6 +192,26 @@ async def test_calendar_yaml_error( assert hass.states.get(TEST_API_ENTITY) +@pytest.mark.parametrize("calendars_config", [None]) +async def test_empty_calendar_yaml( + hass: HomeAssistant, + component_setup: ComponentSetup, + calendars_config: list[dict[str, Any]], + mock_calendars_yaml: None, + mock_calendars_list: ApiResult, + test_api_calendar: dict[str, Any], + mock_events_list: ApiResult, +) -> None: + """Test an empty yaml file is equivalent to a missing yaml file.""" + mock_calendars_list({"items": [test_api_calendar]}) + mock_events_list({}) + + assert await component_setup() + + assert not hass.states.get(TEST_YAML_ENTITY) + assert hass.states.get(TEST_API_ENTITY) + + async def test_init_calendar( hass: HomeAssistant, component_setup: ComponentSetup,