From b03b728d75cd6d0ef115fb189e83d984c59368c7 Mon Sep 17 00:00:00 2001 From: Erik Date: Sat, 20 Jan 2024 13:16:00 +0100 Subject: [PATCH 1/2] Fix empty files included by !include_dir_named --- homeassistant/util/yaml/loader.py | 7 ++++++- tests/util/yaml/test_init.py | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/util/yaml/loader.py b/homeassistant/util/yaml/loader.py index aac3e1274eeec..da46cc0b0e352 100644 --- a/homeassistant/util/yaml/loader.py +++ b/homeassistant/util/yaml/loader.py @@ -359,7 +359,12 @@ def _include_dir_named_yaml(loader: LoaderType, node: yaml.nodes.Node) -> NodeDi filename = os.path.splitext(os.path.basename(fname))[0] if os.path.basename(fname) == SECRET_YAML: continue - mapping[filename] = load_yaml(fname, loader.secrets) + loaded_yaml = load_yaml(fname, loader.secrets) + if loaded_yaml is None: + # Special case, an empty file included by !include_dir_named is treated + # an an empty dictionary + loaded_yaml = NodeDictClass() + mapping[filename] = loaded_yaml return _add_reference(mapping, loader, node) diff --git a/tests/util/yaml/test_init.py b/tests/util/yaml/test_init.py index 30637fe2785f7..93c8ed5049872 100644 --- a/tests/util/yaml/test_init.py +++ b/tests/util/yaml/test_init.py @@ -193,7 +193,7 @@ def test_include_dir_list_recursive( ), ( {"/test/first.yaml": "1", "/test/second.yaml": None}, - {"first": 1, "second": None}, + {"first": 1, "second": {}}, ), ], ) From 83cc626f8e71a70000bd53da304b6692a047f277 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sat, 20 Jan 2024 13:24:10 +0100 Subject: [PATCH 2/2] Update homeassistant/util/yaml/loader.py --- homeassistant/util/yaml/loader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/util/yaml/loader.py b/homeassistant/util/yaml/loader.py index da46cc0b0e352..51564b6da88c1 100644 --- a/homeassistant/util/yaml/loader.py +++ b/homeassistant/util/yaml/loader.py @@ -362,7 +362,7 @@ def _include_dir_named_yaml(loader: LoaderType, node: yaml.nodes.Node) -> NodeDi loaded_yaml = load_yaml(fname, loader.secrets) if loaded_yaml is None: # Special case, an empty file included by !include_dir_named is treated - # an an empty dictionary + # as an empty dictionary loaded_yaml = NodeDictClass() mapping[filename] = loaded_yaml return _add_reference(mapping, loader, node)