Attach relevant config to check_config errors (#104048)

This commit is contained in:
Erik Montnemery 2023-11-16 07:25:52 +01:00 committed by GitHub
parent 613afe322f
commit 1c817cc18c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -102,7 +102,10 @@ async def async_check_ha_config_file( # noqa: C901
result.add_warning(message, domain, pack_config) result.add_warning(message, domain, pack_config)
def _comp_error( def _comp_error(
ex: vol.Invalid | HomeAssistantError, domain: str, component_config: ConfigType ex: vol.Invalid | HomeAssistantError,
domain: str,
component_config: ConfigType,
config_to_attach: ConfigType,
) -> None: ) -> None:
"""Handle errors from components.""" """Handle errors from components."""
if isinstance(ex, vol.Invalid): if isinstance(ex, vol.Invalid):
@ -110,9 +113,9 @@ async def async_check_ha_config_file( # noqa: C901
else: else:
message = format_homeassistant_error(ex, domain, component_config) message = format_homeassistant_error(ex, domain, component_config)
if domain in frontend_dependencies: if domain in frontend_dependencies:
result.add_error(message, domain, component_config) result.add_error(message, domain, config_to_attach)
else: else:
result.add_warning(message, domain, component_config) result.add_warning(message, domain, config_to_attach)
async def _get_integration( async def _get_integration(
hass: HomeAssistant, domain: str hass: HomeAssistant, domain: str
@ -207,7 +210,7 @@ async def async_check_ha_config_file( # noqa: C901
)[domain] )[domain]
continue continue
except (vol.Invalid, HomeAssistantError) as ex: except (vol.Invalid, HomeAssistantError) as ex:
_comp_error(ex, domain, config) _comp_error(ex, domain, config, config[domain])
continue continue
except Exception as err: # pylint: disable=broad-except except Exception as err: # pylint: disable=broad-except
logging.getLogger(__name__).exception( logging.getLogger(__name__).exception(
@ -228,7 +231,7 @@ async def async_check_ha_config_file( # noqa: C901
if domain in config: if domain in config:
result[domain] = config[domain] result[domain] = config[domain]
except vol.Invalid as ex: except vol.Invalid as ex:
_comp_error(ex, domain, config) _comp_error(ex, domain, config, config[domain])
continue continue
component_platform_schema = getattr( component_platform_schema = getattr(
@ -246,7 +249,7 @@ async def async_check_ha_config_file( # noqa: C901
try: try:
p_validated = component_platform_schema(p_config) p_validated = component_platform_schema(p_config)
except vol.Invalid as ex: except vol.Invalid as ex:
_comp_error(ex, domain, p_config) _comp_error(ex, domain, p_config, p_config)
continue continue
# Not all platform components follow same pattern for platforms # Not all platform components follow same pattern for platforms
@ -282,7 +285,7 @@ async def async_check_ha_config_file( # noqa: C901
try: try:
p_validated = platform_schema(p_validated) p_validated = platform_schema(p_validated)
except vol.Invalid as ex: except vol.Invalid as ex:
_comp_error(ex, f"{domain}.{p_name}", p_config) _comp_error(ex, f"{domain}.{p_name}", p_config, p_config)
continue continue
platforms.append(p_validated) platforms.append(p_validated)

View File

@ -442,21 +442,19 @@ action:
@pytest.mark.parametrize( @pytest.mark.parametrize(
("exception", "errors", "warnings", "message", "config"), ("exception", "errors", "warnings", "message"),
[ [
( (
Exception("Broken"), Exception("Broken"),
1, 1,
0, 0,
"Unexpected error calling config validator: Broken", "Unexpected error calling config validator: Broken",
{"value": 1},
), ),
( (
HomeAssistantError("Broken"), HomeAssistantError("Broken"),
0, 0,
1, 1,
"Invalid config for [bla]: Broken", "Invalid config for [bla]: Broken",
{"bla": {"value": 1}},
), ),
], ],
) )
@ -466,7 +464,6 @@ async def test_config_platform_raise(
errors: int, errors: int,
warnings: int, warnings: int,
message: str, message: str,
config: dict,
) -> None: ) -> None:
"""Test bad config validation platform.""" """Test bad config validation platform."""
mock_platform( mock_platform(
@ -486,7 +483,7 @@ bla:
error = CheckConfigError( error = CheckConfigError(
message, message,
"bla", "bla",
config, {"value": 1},
) )
_assert_warnings_errors(res, [error] * warnings, [error] * errors) _assert_warnings_errors(res, [error] * warnings, [error] * errors)