1
0
mirror of https://github.com/esphome/esphome.git synced 2025-03-15 15:18:16 +00:00
This commit is contained in:
Otto Winter 2018-12-05 09:12:49 +01:00
parent fb1013e885
commit 2ec5c5e2f3
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E
3 changed files with 28 additions and 16 deletions

View File

@ -30,23 +30,28 @@ def validate_recursive_condition(value):
for i, item in enumerate(value):
item = copy.deepcopy(item)
if not isinstance(item, dict):
raise vol.Invalid(u"Condition must consist of key-value mapping! Got {}".format(item))
raise vol.Invalid(u"Condition must consist of key-value mapping! Got {}".format(item), [i])
key = next((x for x in item if x != CONF_CONDITION_ID), None)
if key is None:
raise vol.Invalid(u"Key missing from action! Got {}".format(item))
raise vol.Invalid(u"Key missing from action! Got {}".format(item), [i])
if key not in CONDITION_REGISTRY:
raise vol.Invalid(u"Unable to find condition with the name '{}', is the "
u"component loaded?".format(key))
u"component loaded?".format(key), [i])
item.setdefault(CONF_CONDITION_ID, None)
key2 = next((x for x in item if x != CONF_CONDITION_ID and x != key), None)
if key2 is not None:
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"".format(key, key2))
u"".format(key, key2), [i])
validator = CONDITION_REGISTRY[key][0]
try:
condition = validator(item[key])
except vol.Invalid as e:
e.prepend([i])
raise e
value[i] = {
CONF_CONDITION_ID: cv.declare_variable_id(Condition)(item[CONF_CONDITION_ID]),
key: validator(item[key])
key: condition,
}
return value
@ -56,23 +61,28 @@ def validate_recursive_action(value):
for i, item in enumerate(value):
item = copy.deepcopy(item)
if not isinstance(item, dict):
raise vol.Invalid(u"Action must consist of key-value mapping! Got {}".format(item))
raise vol.Invalid(u"Action must consist of key-value mapping! Got {}".format(item), [i])
key = next((x for x in item if x != CONF_ACTION_ID), None)
if key is None:
raise vol.Invalid(u"Key missing from action! Got {}".format(item))
raise vol.Invalid(u"Key missing from action! Got {}".format(item), [i])
if key not in ACTION_REGISTRY:
raise vol.Invalid(u"Unable to find action with the name '{}', is the component loaded?"
u"".format(key))
u"".format(key), [i, key])
item.setdefault(CONF_ACTION_ID, None)
key2 = next((x for x in item if x != CONF_ACTION_ID and x != key), None)
if key2 is not None:
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"".format(key, key2))
u"".format(key, key2), [i])
validator = ACTION_REGISTRY[key][0]
try:
action = validator(item[key])
except vol.Invalid as e:
e.prepend([i])
raise e
value[i] = {
CONF_ACTION_ID: cv.declare_variable_id(Action)(item[CONF_ACTION_ID]),
key: validator(item[key])
key: action,
}
return value

View File

@ -182,21 +182,21 @@ def validate_effects(allowed_effects):
ret = []
for i, effect in enumerate(value):
if not isinstance(effect, dict):
raise vol.Invalid("Each effect must be a dictionary, not {}".format(type(value)))
raise vol.Invalid("Each effect must be a dictionary, not {}".format(type(value)), [i])
if len(effect) > 1:
raise vol.Invalid("Each entry in the 'effects:' option must be a single effect.")
raise vol.Invalid("Each entry in the 'effects:' option must be a single effect.", [i])
if not effect:
raise vol.Invalid("Found no effect for the {}th entry in 'effects:'!".format(i))
raise vol.Invalid("Found no effect for the {}th entry in 'effects:'!".format(i), [i])
key = next(iter(effect.keys()))
if key not in allowed_effects:
raise vol.Invalid("The effect '{}' does not exist or is not allowed for this "
"light type".format(key))
"light type".format(key), [i])
effect[key] = effect[key] or {}
conf = EFFECTS_SCHEMA(effect)
name = conf[key][CONF_NAME]
if name in names:
raise vol.Invalid(u"Found the effect name '{}' twice. All effects must have "
u"unique names".format(name))
u"unique names".format(name), [i])
names.add(name)
ret.append(conf)
return ret

View File

@ -448,8 +448,10 @@ def load_config():
def line_info(obj, highlight=True):
"""Display line config source."""
if not highlight:
return None
if hasattr(obj, '__config_file__'):
return color('cyan' if highlight else 'white', "[source {}:{}]"
return color('cyan', "[source {}:{}]"
.format(obj.__config_file__, obj.__line__ or '?'))
return None