diff --git a/homeassistant/config.py b/homeassistant/config.py index a33acead87063..027839ca6561d 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -515,7 +515,7 @@ def async_log_config_validator_error( if hass is not None: async_notify_setup_error(hass, domain, link) - message = format_homeassistant_error(ex, domain, config, link) + message = format_homeassistant_error(hass, ex, domain, config, link) _LOGGER.error(message, exc_info=ex) @@ -677,11 +677,19 @@ def humanize_error( @callback def format_homeassistant_error( - ex: HomeAssistantError, domain: str, config: dict, link: str | None = None + hass: HomeAssistant, + ex: HomeAssistantError, + domain: str, + config: dict, + link: str | None = None, ) -> str: """Format HomeAssistantError thrown by a custom config validator.""" - message = f"Invalid config for [{domain}]: {str(ex) or repr(ex)}" - + message_prefix = f"Invalid config for [{domain}]" + # HomeAssistantError raised by custom config validator has no path to the + # offending configuration key, use the domain key as path instead. + if annotation := find_annotation(config, [domain]): + message_prefix += f" at {_relpath(hass, annotation[0])}, line {annotation[1]}" + message = f"{message_prefix}: {str(ex) or repr(ex)}" if domain != CONF_CORE and link: message += f" Please check the docs at {link}." diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index 7466ddc617979..09bdf10cf704a 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -115,7 +115,7 @@ def _comp_error( if isinstance(ex, vol.Invalid): message = format_schema_error(hass, ex, domain, component_config) else: - message = format_homeassistant_error(ex, domain, component_config) + message = format_homeassistant_error(hass, ex, domain, component_config) if domain in frontend_dependencies: result.add_error(message, domain, config_to_attach) else: diff --git a/tests/helpers/test_check_config.py b/tests/helpers/test_check_config.py index 82500cb0b30b9..56df99bb03258 100644 --- a/tests/helpers/test_check_config.py +++ b/tests/helpers/test_check_config.py @@ -453,7 +453,7 @@ async def test_automation_config_platform(hass: HomeAssistant) -> None: HomeAssistantError("Broken"), 0, 1, - "Invalid config for [bla]: Broken", + "Invalid config for [bla] at configuration.yaml, line 11: Broken", ), ], ) diff --git a/tests/snapshots/test_config.ambr b/tests/snapshots/test_config.ambr index 97faac40bf590..b65617cb649d4 100644 --- a/tests/snapshots/test_config.ambr +++ b/tests/snapshots/test_config.ambr @@ -47,7 +47,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 55: broken', }), dict({ 'has_exc_info': True, @@ -103,7 +103,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 9: broken', }), dict({ 'has_exc_info': True, @@ -135,7 +135,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 1: broken', }), dict({ 'has_exc_info': True, @@ -167,7 +167,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 1: broken', }), dict({ 'has_exc_info': True, @@ -223,7 +223,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 67: broken', }), dict({ 'has_exc_info': True, @@ -279,7 +279,7 @@ }), dict({ 'has_exc_info': True, - 'message': 'Invalid config for [custom_validator_bad_1]: broken', + 'message': 'Invalid config for [custom_validator_bad_1] at integrations/custom_validator_bad_1.yaml, line 2: broken', }), dict({ 'has_exc_info': True, @@ -306,7 +306,7 @@ Invalid config for [adr_0007_5] at configuration.yaml, line 45: expected int for dictionary value 'adr_0007_5->port', got 'foo'. Please check the docs at https://www.home-assistant.io/integrations/adr_0007_5. ''', "Invalid config for [custom_validator_ok_2] at configuration.yaml, line 52: required key 'host' not provided. Please check the docs at https://www.home-assistant.io/integrations/custom_validator_ok_2.", - 'Invalid config for [custom_validator_bad_1]: broken Please check the docs at https://www.home-assistant.io/integrations/custom_validator_bad_1.', + 'Invalid config for [custom_validator_bad_1] at configuration.yaml, line 55: broken Please check the docs at https://www.home-assistant.io/integrations/custom_validator_bad_1.', 'Unknown error calling custom_validator_bad_2 config validator', ]) # ---