From 8d8d42128629dc074c633a58540b5808d9cbcb30 Mon Sep 17 00:00:00 2001 From: wifwucite <74489218+wifwucite@users.noreply.github.com> Date: Sun, 23 May 2021 22:24:24 +0200 Subject: [PATCH] allow default option for typed_schema (#1700) Co-authored-by: Otto Winter --- esphome/config_validation.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/esphome/config_validation.py b/esphome/config_validation.py index dcc0da8b2b..2cdb6b0b76 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -1363,15 +1363,17 @@ def extract_keys(schema): def typed_schema(schemas, **kwargs): """Create a schema that has a key to distinguish between schemas""" key = kwargs.pop("key", CONF_TYPE) + default_schema_option = kwargs.pop("default_type", None) key_validator = one_of(*schemas, **kwargs) def validator(value): if not isinstance(value, dict): raise Invalid("Value must be dict") - if key not in value: - raise Invalid("type not specified!") value = value.copy() - key_v = key_validator(value.pop(key)) + schema_option = value.pop(key, default_schema_option) + if schema_option is None: + raise Invalid(key + " not specified!") + key_v = key_validator(schema_option) value = schemas[key_v](value) value[key] = key_v return value