Exemplo n.º 1
0
    def validator(value):
        if value == SCHEMA_EXTRACT:
            return (allowed_effects, EFFECTS_REGISTRY)

        value = cv.validate_registry("effect", EFFECTS_REGISTRY)(value)
        errors = []
        names = set()
        for i, x in enumerate(value):
            key = next(it for it in x.keys())
            if key not in allowed_effects:
                errors.append(
                    cv.Invalid(
                        f"The effect '{key}' is not allowed for this light type",
                        [i],
                    ))
                continue
            name = x[key][CONF_NAME]
            if name in names:
                errors.append(
                    cv.Invalid(
                        f"Found the effect name '{name}' twice. All effects must have unique names",
                        [i],
                    ))
                continue
            names.add(name)
        if errors:
            raise cv.MultipleInvalid(errors)
        return value
Exemplo n.º 2
0
 def validator(value):
     value = cv.validate_registry("effect", EFFECTS_REGISTRY)(value)
     errors = []
     names = set()
     for i, x in enumerate(value):
         key = next(it for it in x.keys())
         if key not in allowed_effects:
             errors.append(
                 cv.Invalid(
                     "The effect '{}' is not allowed for this "
                     "light type".format(key),
                     [i],
                 ))
             continue
         name = x[key][CONF_NAME]
         if name in names:
             errors.append(
                 cv.Invalid(
                     "Found the effect name '{}' twice. All effects must have "
                     "unique names".format(name),
                     [i],
                 ))
             continue
         names.add(name)
     if errors:
         raise cv.MultipleInvalid(errors)
     return value
Exemplo n.º 3
0
 def validator_(value):
     if isinstance(value, list):
         # List of items, there are two possible options here, either a sequence of
         # actions (no then:) or a list of automations.
         try:
             # First try as a sequence of actions
             # If that succeeds, return immediately
             with cv.remove_prepend_path([CONF_THEN]):
                 return [schema({CONF_THEN: value})]
         except cv.Invalid as err:
             # Next try as a sequence of automations
             try:
                 return cv.Schema([schema])(value)
             except cv.Invalid as err2:
                 if "extra keys not allowed" in str(err2) and len(
                         err2.path) == 2:
                     # pylint: disable=raise-missing-from
                     raise err
                 if "Unable to find action" in str(err):
                     raise err2
                 raise cv.MultipleInvalid([err, err2])
     elif isinstance(value, dict):
         if CONF_THEN in value:
             return [schema(value)]
         with cv.remove_prepend_path([CONF_THEN]):
             return [schema({CONF_THEN: value})]
     # This should only happen with invalid configs, but let's have a nice error message.
     return [schema(value)]