[core] Only snapshot the user config when esphome config --no-defaults asks for it (#18113)

This commit is contained in:
J. Nick Koston
2026-08-05 21:20:52 -05:00
committed by GitHub
parent 3b6f45894b
commit eb0fac9cbf
4 changed files with 83 additions and 13 deletions
+20
View File
@@ -6362,6 +6362,26 @@ def test_run_esphome_skip_external_update_per_command(
assert mock_read.call_args.kwargs["skip_external_update"] is expected_skip
@pytest.mark.parametrize(
("argv_extra", "expected"),
[(["--no-defaults"], True), ([], False)],
)
def test_run_esphome_snapshot_user_config_only_for_no_defaults(
tmp_path: Path, argv_extra: list[str], expected: bool
) -> None:
"""read_config is invoked with snapshot_user_config=True only when the
config command is run with --no-defaults; otherwise the expensive deep
copy is skipped."""
yaml_file = tmp_path / "device.yaml"
yaml_file.write_text("esphome:\n name: test\n")
with patch("esphome.config.read_config", return_value=None) as mock_read:
run_esphome(["esphome", "config", str(yaml_file), *argv_extra])
mock_read.assert_called_once()
assert mock_read.call_args.kwargs["snapshot_user_config"] is expected
def test_get_configured_xtal_freq_reads_sdkconfig(tmp_path: Path) -> None:
"""Test reading XTAL_FREQ from sdkconfig."""
CORE.name = "test-device"
+28 -2
View File
@@ -370,7 +370,7 @@ def test_validate_config_captures_user_config_snapshot(tmp_path: Path) -> None:
"""
test_config = _get_test_minimal_valid_config(tmp_path)
result = config_module.validate_config(test_config, None)
result = config_module.validate_config(test_config, None, snapshot_user_config=True)
# Snapshot is populated.
assert result.user_config is not None
@@ -393,7 +393,7 @@ def test_validate_config_user_config_snapshot_is_deep_copy(tmp_path: Path) -> No
"""
test_config = _get_test_minimal_valid_config(tmp_path)
result = config_module.validate_config(test_config, None)
result = config_module.validate_config(test_config, None, snapshot_user_config=True)
assert result.user_config is not None
# preload_core_config injected build_path onto the validated config.
@@ -404,6 +404,32 @@ def test_validate_config_user_config_snapshot_is_deep_copy(tmp_path: Path) -> No
assert result["esphome"] is not result.user_config["esphome"]
def test_validate_config_snapshot_without_substitutions(tmp_path: Path) -> None:
"""The snapshot works for configs that have no substitutions block."""
test_config = _get_test_minimal_valid_config(tmp_path)
del test_config[CONF_SUBSTITUTIONS]
result = config_module.validate_config(test_config, None, snapshot_user_config=True)
assert result.user_config is not None
assert CONF_SUBSTITUTIONS not in result.user_config
assert result.user_config["esphome"] == {"name": "test_device"}
def test_validate_config_skips_user_config_snapshot_by_default(
tmp_path: Path,
) -> None:
"""Without ``snapshot_user_config`` the deep copy is skipped entirely;
only ``esphome config --no-defaults`` needs the snapshot and the copy is
too expensive to take on every load.
"""
test_config = _get_test_minimal_valid_config(tmp_path)
result = config_module.validate_config(test_config, None)
assert result.user_config is None
def test_merge_config_preserves_ordered_dict() -> None:
"""Test that merge_config preserves OrderedDict type.