Fix handling of empty google_calendars.yaml file (#84909)
fixes undefined
This commit is contained in:
parent
fbb406842e
commit
6b6f115d7e
3 changed files with 24 additions and 2 deletions
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue