Improve config final validation (#1917)

This commit is contained in:
Otto Winter
2021-06-17 21:54:14 +02:00
committed by GitHub
parent c19b3ecd43
commit 2419bc3678
18 changed files with 303 additions and 162 deletions
+16 -8
View File
@@ -1,6 +1,7 @@
import logging
import esphome.codegen as cg
import esphome.config_validation as cv
import esphome.final_validate as fv
from esphome import automation
from esphome.components.output import FloatOutput
from esphome.const import CONF_ID, CONF_OUTPUT, CONF_PLATFORM, CONF_TRIGGER_ID
@@ -36,12 +37,8 @@ CONFIG_SCHEMA = cv.Schema(
).extend(cv.COMPONENT_SCHEMA)
def validate(config, item_config):
# Not adding this to FloatOutput as this is the only component which needs `update_frequency`
parent_config = config.get_config_by_id(item_config[CONF_OUTPUT])
platform = parent_config[CONF_PLATFORM]
def validate_parent_output_config(value):
platform = value.get(CONF_PLATFORM)
PWM_GOOD = ["esp8266_pwm", "ledc"]
PWM_BAD = [
"ac_dimmer ",
@@ -55,14 +52,25 @@ def validate(config, item_config):
]
if platform in PWM_BAD:
raise ValueError(f"Component rtttl cannot use {platform} as output component")
raise cv.Invalid(f"Component rtttl cannot use {platform} as output component")
if platform not in PWM_GOOD:
_LOGGER.warning(
"Component rtttl is not known to work with the selected output type. Make sure this output supports custom frequency output method."
"Component rtttl is not known to work with the selected output type. "
"Make sure this output supports custom frequency output method."
)
FINAL_VALIDATE_SCHEMA = cv.Schema(
{
cv.Required(CONF_OUTPUT): fv.id_declaration_match_schema(
validate_parent_output_config
)
},
extra=cv.ALLOW_EXTRA,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)