1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00

Better paths

This commit is contained in:
Otto Winter 2018-12-05 09:31:52 +01:00
parent 2ec5c5e2f3
commit 7da4c47d44
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E

View File

@ -26,28 +26,30 @@ def maybe_simple_id(*validators):
def validate_recursive_condition(value): def validate_recursive_condition(value):
is_list = isinstance(value, list)
value = cv.ensure_list(value)[:] value = cv.ensure_list(value)[:]
for i, item in enumerate(value): for i, item in enumerate(value):
path = [i] if is_list else []
item = copy.deepcopy(item) item = copy.deepcopy(item)
if not isinstance(item, dict): if not isinstance(item, dict):
raise vol.Invalid(u"Condition must consist of key-value mapping! Got {}".format(item), [i]) raise vol.Invalid(u"Condition must consist of key-value mapping! Got {}".format(item), path)
key = next((x for x in item if x != CONF_CONDITION_ID), None) key = next((x for x in item if x != CONF_CONDITION_ID), None)
if key is None: if key is None:
raise vol.Invalid(u"Key missing from action! Got {}".format(item), [i]) raise vol.Invalid(u"Key missing from action! Got {}".format(item), path)
if key not in CONDITION_REGISTRY: if key not in CONDITION_REGISTRY:
raise vol.Invalid(u"Unable to find condition with the name '{}', is the " raise vol.Invalid(u"Unable to find condition with the name '{}', is the "
u"component loaded?".format(key), [i]) u"component loaded?".format(key), path + [key])
item.setdefault(CONF_CONDITION_ID, None) item.setdefault(CONF_CONDITION_ID, None)
key2 = next((x for x in item if x != CONF_CONDITION_ID and x != key), None) key2 = next((x for x in item if x != CONF_CONDITION_ID and x != key), None)
if key2 is not None: if key2 is not None:
raise vol.Invalid(u"Cannot have two conditions in one item. Key '{}' overrides '{}'! " raise vol.Invalid(u"Cannot have two conditions in one item. Key '{}' overrides '{}'! "
u"Did you forget to indent the block inside the condition?" u"Did you forget to indent the block inside the condition?"
u"".format(key, key2), [i]) u"".format(key, key2), path)
validator = CONDITION_REGISTRY[key][0] validator = CONDITION_REGISTRY[key][0]
try: try:
condition = validator(item[key]) condition = validator(item[key])
except vol.Invalid as e: except vol.Invalid as e:
e.prepend([i]) e.prepend(path)
raise e raise e
value[i] = { value[i] = {
CONF_CONDITION_ID: cv.declare_variable_id(Condition)(item[CONF_CONDITION_ID]), CONF_CONDITION_ID: cv.declare_variable_id(Condition)(item[CONF_CONDITION_ID]),
@ -57,28 +59,30 @@ def validate_recursive_condition(value):
def validate_recursive_action(value): def validate_recursive_action(value):
is_list = isinstance(value, list)
value = cv.ensure_list(value)[:] value = cv.ensure_list(value)[:]
for i, item in enumerate(value): for i, item in enumerate(value):
path = [i] if is_list else []
item = copy.deepcopy(item) item = copy.deepcopy(item)
if not isinstance(item, dict): if not isinstance(item, dict):
raise vol.Invalid(u"Action must consist of key-value mapping! Got {}".format(item), [i]) raise vol.Invalid(u"Action must consist of key-value mapping! Got {}".format(item), path)
key = next((x for x in item if x != CONF_ACTION_ID), None) key = next((x for x in item if x != CONF_ACTION_ID), None)
if key is None: if key is None:
raise vol.Invalid(u"Key missing from action! Got {}".format(item), [i]) raise vol.Invalid(u"Key missing from action! Got {}".format(item), path)
if key not in ACTION_REGISTRY: if key not in ACTION_REGISTRY:
raise vol.Invalid(u"Unable to find action with the name '{}', is the component loaded?" raise vol.Invalid(u"Unable to find action with the name '{}', is the component loaded?"
u"".format(key), [i, key]) u"".format(key), path + [key])
item.setdefault(CONF_ACTION_ID, None) item.setdefault(CONF_ACTION_ID, None)
key2 = next((x for x in item if x != CONF_ACTION_ID and x != key), None) key2 = next((x for x in item if x != CONF_ACTION_ID and x != key), None)
if key2 is not None: if key2 is not None:
raise vol.Invalid(u"Cannot have two actions in one item. Key '{}' overrides '{}'! " raise vol.Invalid(u"Cannot have two actions in one item. Key '{}' overrides '{}'! "
u"Did you forget to indent the block inside the action?" u"Did you forget to indent the block inside the action?"
u"".format(key, key2), [i]) u"".format(key, key2), path)
validator = ACTION_REGISTRY[key][0] validator = ACTION_REGISTRY[key][0]
try: try:
action = validator(item[key]) action = validator(item[key])
except vol.Invalid as e: except vol.Invalid as e:
e.prepend([i]) e.prepend(path)
raise e raise e
value[i] = { value[i] = {
CONF_ACTION_ID: cv.declare_variable_id(Action)(item[CONF_ACTION_ID]), CONF_ACTION_ID: cv.declare_variable_id(Action)(item[CONF_ACTION_ID]),