From 123f14dd6cf3d6fe811459bff48ab189c11129d3 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 9 Nov 2023 00:06:04 +0100 Subject: [PATCH] Attach correct platform config in check_config warnings and errors (#103633) --- homeassistant/helpers/check_config.py | 4 +- tests/helpers/test_check_config.py | 75 ++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index f65cd4e359e..c333bab782b 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -243,7 +243,7 @@ async def async_check_ha_config_file( # noqa: C901 try: p_validated = component_platform_schema(p_config) except vol.Invalid as ex: - _comp_error(ex, domain, config) + _comp_error(ex, domain, p_config) continue # Not all platform components follow same pattern for platforms @@ -279,7 +279,7 @@ async def async_check_ha_config_file( # noqa: C901 try: p_validated = platform_schema(p_validated) except vol.Invalid as ex: - _comp_error(ex, f"{domain}.{p_name}", p_validated) + _comp_error(ex, f"{domain}.{p_name}", p_config) continue platforms.append(p_validated) diff --git a/tests/helpers/test_check_config.py b/tests/helpers/test_check_config.py index 197fb88695f..38c1b4913cd 100644 --- a/tests/helpers/test_check_config.py +++ b/tests/helpers/test_check_config.py @@ -3,6 +3,7 @@ import logging from unittest.mock import Mock, patch import pytest +import voluptuous as vol from homeassistant.config import YAML_CONFIG_FILE from homeassistant.core import HomeAssistant @@ -14,7 +15,13 @@ from homeassistant.helpers.check_config import ( import homeassistant.helpers.config_validation as cv from homeassistant.requirements import RequirementsNotFound -from tests.common import MockModule, mock_integration, mock_platform, patch_yaml_files +from tests.common import ( + MockModule, + MockPlatform, + mock_integration, + mock_platform, + patch_yaml_files, +) _LOGGER = logging.getLogger(__name__) @@ -255,6 +262,72 @@ async def test_platform_not_found_safe_mode(hass: HomeAssistant) -> None: _assert_warnings_errors(res, [], []) +@pytest.mark.parametrize( + ("extra_config", "warnings", "message", "config"), + [ + ( + "blah:\n - platform: test\n option1: abc", + 0, + None, + None, + ), + ( + "blah:\n - platform: test\n option1: 123", + 1, + "Invalid config for [blah.test]: expected str for dictionary value", + {"option1": 123, "platform": "test"}, + ), + # Test the attached config is unvalidated (key old is removed by validator) + ( + "blah:\n - platform: test\n old: blah\n option1: 123", + 1, + "Invalid config for [blah.test]: expected str for dictionary value", + {"old": "blah", "option1": 123, "platform": "test"}, + ), + # Test base platform configuration error + ( + "blah:\n - paltfrom: test\n", + 1, + "Invalid config for [blah]: required key not provided", + {"paltfrom": "test"}, + ), + ], +) +async def test_component_platform_schema_error( + hass: HomeAssistant, + extra_config: str, + warnings: int, + message: str | None, + config: dict | None, +) -> None: + """Test schema error in component.""" + comp_platform_schema = cv.PLATFORM_SCHEMA.extend({vol.Remove("old"): str}) + comp_platform_schema_base = comp_platform_schema.extend({}, extra=vol.ALLOW_EXTRA) + mock_integration( + hass, + MockModule("blah", platform_schema_base=comp_platform_schema_base), + ) + test_platform_schema = comp_platform_schema.extend({"option1": str}) + mock_platform( + hass, + "test.blah", + MockPlatform(platform_schema=test_platform_schema), + ) + + files = {YAML_CONFIG_FILE: BASE_CONFIG + extra_config} + hass.config.safe_mode = True + with patch("os.path.isfile", return_value=True), patch_yaml_files(files): + res = await async_check_ha_config_file(hass) + log_ha_config(res) + + assert len(res.errors) == 0 + assert len(res.warnings) == warnings + + for warn in res.warnings: + assert message in warn.message + assert warn.config == config + + async def test_component_config_platform_import_error(hass: HomeAssistant) -> None: """Test errors if config platform fails to import.""" # Make sure they don't exist