mirror of
https://github.com/home-assistant/core.git
synced 2025-12-27 06:28:31 +00:00
Improve validation of device trigger config (#26910)
* Improve validation of device trigger config * Remove action and condition checks * Move config validation to own file * Fix tests * Fixes * Fixes * Small tweak
This commit is contained in:
@@ -416,7 +416,7 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None:
|
||||
|
||||
@callback
|
||||
def async_log_exception(
|
||||
ex: vol.Invalid, domain: str, config: Dict, hass: HomeAssistant
|
||||
ex: Exception, domain: str, config: Dict, hass: HomeAssistant
|
||||
) -> None:
|
||||
"""Log an error for configuration validation.
|
||||
|
||||
@@ -428,23 +428,26 @@ def async_log_exception(
|
||||
|
||||
|
||||
@callback
|
||||
def _format_config_error(ex: vol.Invalid, domain: str, config: Dict) -> str:
|
||||
def _format_config_error(ex: Exception, domain: str, config: Dict) -> str:
|
||||
"""Generate log exception for configuration validation.
|
||||
|
||||
This method must be run in the event loop.
|
||||
"""
|
||||
message = f"Invalid config for [{domain}]: "
|
||||
if "extra keys not allowed" in ex.error_message:
|
||||
message += (
|
||||
"[{option}] is an invalid option for [{domain}]. "
|
||||
"Check: {domain}->{path}.".format(
|
||||
option=ex.path[-1],
|
||||
domain=domain,
|
||||
path="->".join(str(m) for m in ex.path),
|
||||
if isinstance(ex, vol.Invalid):
|
||||
if "extra keys not allowed" in ex.error_message:
|
||||
message += (
|
||||
"[{option}] is an invalid option for [{domain}]. "
|
||||
"Check: {domain}->{path}.".format(
|
||||
option=ex.path[-1],
|
||||
domain=domain,
|
||||
path="->".join(str(m) for m in ex.path),
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
message += "{}.".format(humanize_error(config, ex))
|
||||
else:
|
||||
message += "{}.".format(humanize_error(config, ex))
|
||||
message += str(ex)
|
||||
|
||||
try:
|
||||
domain_config = config.get(domain, config)
|
||||
@@ -717,6 +720,24 @@ async def async_process_component_config(
|
||||
_LOGGER.error("Unable to import %s: %s", domain, ex)
|
||||
return None
|
||||
|
||||
# Check if the integration has a custom config validator
|
||||
config_validator = None
|
||||
try:
|
||||
config_validator = integration.get_platform("config")
|
||||
except ImportError:
|
||||
pass
|
||||
if config_validator is not None and hasattr(
|
||||
config_validator, "async_validate_config"
|
||||
):
|
||||
try:
|
||||
return await config_validator.async_validate_config( # type: ignore
|
||||
hass, config
|
||||
)
|
||||
except (vol.Invalid, HomeAssistantError) as ex:
|
||||
async_log_exception(ex, domain, config, hass)
|
||||
return None
|
||||
|
||||
# No custom config validator, proceed with schema validation
|
||||
if hasattr(component, "CONFIG_SCHEMA"):
|
||||
try:
|
||||
return component.CONFIG_SCHEMA(config) # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user