mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
allow default option for typed_schema (#1700)
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
0ce57e5a39
commit
8d8d421286
1 changed files with 5 additions and 3 deletions
|
@ -1363,15 +1363,17 @@ def extract_keys(schema):
|
||||||
def typed_schema(schemas, **kwargs):
|
def typed_schema(schemas, **kwargs):
|
||||||
"""Create a schema that has a key to distinguish between schemas"""
|
"""Create a schema that has a key to distinguish between schemas"""
|
||||||
key = kwargs.pop("key", CONF_TYPE)
|
key = kwargs.pop("key", CONF_TYPE)
|
||||||
|
default_schema_option = kwargs.pop("default_type", None)
|
||||||
key_validator = one_of(*schemas, **kwargs)
|
key_validator = one_of(*schemas, **kwargs)
|
||||||
|
|
||||||
def validator(value):
|
def validator(value):
|
||||||
if not isinstance(value, dict):
|
if not isinstance(value, dict):
|
||||||
raise Invalid("Value must be dict")
|
raise Invalid("Value must be dict")
|
||||||
if key not in value:
|
|
||||||
raise Invalid("type not specified!")
|
|
||||||
value = value.copy()
|
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 = schemas[key_v](value)
|
||||||
value[key] = key_v
|
value[key] = key_v
|
||||||
return value
|
return value
|
||||||
|
|
Loading…
Reference in a new issue