Fix handling of empty google_calendars.yaml file (#84909)

fixes undefined
This commit is contained in:
Allen Porter 2022-12-31 02:50:56 -08:00 committed by GitHub
parent fbb406842e
commit 6b6f115d7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View file

@ -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:

View file

@ -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

View file

@ -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,