From 67eec208bd962c7ccb3c1163795362cf206c9fcb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Apr 2026 16:01:05 -1000 Subject: [PATCH 1/2] [yaml] Add IncludeFile representer to ESPHomeDumper The deferred IncludeFile objects introduced in #12213 could not be serialized by the YAML dumper, causing test_build_components to fail when merging configs that contain !include package references. Uses add_multi_representer to also match the dynamically-created ESPHomeDataBase subclass produced by add_class_to_obj. --- esphome/yaml_util.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esphome/yaml_util.py b/esphome/yaml_util.py index c621428196..19bbca61f8 100644 --- a/esphome/yaml_util.py +++ b/esphome/yaml_util.py @@ -754,6 +754,9 @@ class ESPHomeDumper(yaml.SafeDumper): def represent_remove(self, value): return self.represent_scalar(tag="!remove", value=value.value) + def represent_include_file(self, value): + return self.represent_scalar(tag="!include", value=value.file.as_posix()) + def represent_id(self, value): if is_secret(value.id): return self.represent_secret(value.id) @@ -785,3 +788,4 @@ ESPHomeDumper.add_multi_representer(Remove, ESPHomeDumper.represent_remove) ESPHomeDumper.add_multi_representer(core.ID, ESPHomeDumper.represent_id) ESPHomeDumper.add_multi_representer(uuid.UUID, ESPHomeDumper.represent_stringify) ESPHomeDumper.add_multi_representer(Path, ESPHomeDumper.represent_stringify) +ESPHomeDumper.add_multi_representer(IncludeFile, ESPHomeDumper.represent_include_file) From 1d982c47cc713a9f15218551cbd65a93f8e4b604 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Apr 2026 16:02:34 -1000 Subject: [PATCH 2/2] [yaml] Add tests for IncludeFile YAML dumper representer --- tests/unit_tests/test_yaml_util.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/unit_tests/test_yaml_util.py b/tests/unit_tests/test_yaml_util.py index 0bd7c9453b..a918e521d8 100644 --- a/tests/unit_tests/test_yaml_util.py +++ b/tests/unit_tests/test_yaml_util.py @@ -508,6 +508,28 @@ def test_represent_remove() -> None: assert yaml_util.dump({"key": Remove("my_id")}) == "key: !remove 'my_id'\n" +def test_represent_include_file() -> None: + """Test that IncludeFile objects are dumped as !include scalars.""" + include = yaml_util.IncludeFile( + Path("/fake/main.yaml"), "path/to/file.yaml", None, lambda _: {} + ) + assert yaml_util.dump({"key": include}) == "key: !include 'path/to/file.yaml'\n" + + +def test_represent_include_file_with_data_base_mixin() -> None: + """Test that IncludeFile wrapped with ESPHomeDataBase mixin is also dumped correctly. + + The YAML loader wraps IncludeFile via add_class_to_obj, creating a dynamic + subclass. add_multi_representer must match this subclass through the MRO. + """ + include = yaml_util.IncludeFile( + Path("/fake/main.yaml"), "common/spi.yaml", None, lambda _: {} + ) + wrapped = yaml_util.make_data_base(include) + assert isinstance(wrapped, yaml_util.ESPHomeDataBase) + assert yaml_util.dump({"pkg": wrapped}) == "pkg: !include 'common/spi.yaml'\n" + + # ── IncludeFile unit tests ──────────────────────────────────────────────────