Add legacy PLATFORM_SCHEMA config validation (#21072)
* Add legacy PLATFORM_SCHEMA config validation * Fix tests * Lint * Add persistent notification * Update bootstrap.py
This commit is contained in:
commit
f1b84962ed
3 changed files with 87 additions and 9 deletions
|
@ -34,6 +34,7 @@ OLD_ENTITY_ID_VALIDATION = r"^(\w+)\.(\w+)$"
|
|||
# persistent notification. Rare temporary exception to use a global.
|
||||
INVALID_SLUGS_FOUND = {}
|
||||
INVALID_ENTITY_IDS_FOUND = {}
|
||||
INVALID_EXTRA_KEYS_FOUND = []
|
||||
|
||||
|
||||
# Home Assistant types
|
||||
|
@ -633,8 +634,61 @@ def key_dependency(key, dependency):
|
|||
|
||||
|
||||
# Schemas
|
||||
class HASchema(vol.Schema):
|
||||
"""Schema class that allows us to mark PREVENT_EXTRA errors as warnings."""
|
||||
|
||||
PLATFORM_SCHEMA = vol.Schema({
|
||||
def __call__(self, data):
|
||||
"""Override __call__ to mark PREVENT_EXTRA as warning."""
|
||||
try:
|
||||
return super().__call__(data)
|
||||
except vol.Invalid as orig_err:
|
||||
if self.extra != vol.PREVENT_EXTRA:
|
||||
raise
|
||||
|
||||
# orig_error is of type vol.MultipleInvalid (see super __call__)
|
||||
assert isinstance(orig_err, vol.MultipleInvalid)
|
||||
# pylint: disable=no-member
|
||||
# If it fails with PREVENT_EXTRA, try with ALLOW_EXTRA
|
||||
self.extra = vol.ALLOW_EXTRA
|
||||
# In case it still fails the following will raise
|
||||
try:
|
||||
validated = super().__call__(data)
|
||||
finally:
|
||||
self.extra = vol.PREVENT_EXTRA
|
||||
|
||||
# This is a legacy config, print warning
|
||||
extra_key_errs = [err for err in orig_err.errors
|
||||
if err.error_message == 'extra keys not allowed']
|
||||
if extra_key_errs:
|
||||
msg = "Your configuration contains extra keys " \
|
||||
"that the platform does not support.\n" \
|
||||
"Please remove "
|
||||
submsg = ', '.join('[{}]'.format(err.path[-1]) for err in
|
||||
extra_key_errs)
|
||||
submsg += '. '
|
||||
if hasattr(data, '__config_file__'):
|
||||
submsg += " (See {}, line {}). ".format(
|
||||
data.__config_file__, data.__line__)
|
||||
msg += submsg
|
||||
logging.getLogger(__name__).warning(msg)
|
||||
INVALID_EXTRA_KEYS_FOUND.append(submsg)
|
||||
else:
|
||||
# This should not happen (all errors should be extra key
|
||||
# errors). Let's raise the original error anyway.
|
||||
raise orig_err
|
||||
|
||||
# Return legacy validated config
|
||||
return validated
|
||||
|
||||
def extend(self, schema, required=None, extra=None):
|
||||
"""Extend this schema and convert it to HASchema if necessary."""
|
||||
ret = super().extend(schema, required=required, extra=extra)
|
||||
if extra is not None:
|
||||
return ret
|
||||
return HASchema(ret.schema, required=required, extra=self.extra)
|
||||
|
||||
|
||||
PLATFORM_SCHEMA = HASchema({
|
||||
vol.Required(CONF_PLATFORM): string,
|
||||
vol.Optional(CONF_ENTITY_NAMESPACE): string,
|
||||
vol.Optional(CONF_SCAN_INTERVAL): time_period
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue