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
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
def validate_dumpers(value): if isinstance(value, str) and value.lower() == "all": return validate_dumpers(list(DUMPER_REGISTRY.keys())) return cv.validate_registry("dumper", DUMPER_REGISTRY)(value)
MultiClickTriggerEvent = binary_sensor_ns.struct('MultiClickTriggerEvent') StateTrigger = binary_sensor_ns.class_('StateTrigger', automation.Trigger.template(bool)) BinarySensorPublishAction = binary_sensor_ns.class_('BinarySensorPublishAction', automation.Action) # Condition BinarySensorCondition = binary_sensor_ns.class_('BinarySensorCondition', Condition) # Filters Filter = binary_sensor_ns.class_('Filter') DelayedOnFilter = binary_sensor_ns.class_('DelayedOnFilter', Filter, cg.Component) DelayedOffFilter = binary_sensor_ns.class_('DelayedOffFilter', Filter, cg.Component) InvertFilter = binary_sensor_ns.class_('InvertFilter', Filter) LambdaFilter = binary_sensor_ns.class_('LambdaFilter', Filter) FILTER_REGISTRY = Registry() validate_filters = cv.validate_registry('filter', FILTER_REGISTRY) @FILTER_REGISTRY.register('invert', InvertFilter, {}) def invert_filter_to_code(config, filter_id): yield cg.new_Pvariable(filter_id) @FILTER_REGISTRY.register('delayed_on', DelayedOnFilter, cv.positive_time_period_milliseconds) def delayed_on_filter_to_code(config, filter_id): var = cg.new_Pvariable(filter_id, config) yield cg.register_component(var, {}) yield var
IS_PLATFORM_COMPONENT = True def validate_send_first_at(value): send_first_at = value.get(CONF_SEND_FIRST_AT) send_every = value[CONF_SEND_EVERY] if send_first_at is not None and send_first_at > send_every: raise cv.Invalid( f"send_first_at must be smaller than or equal to send_every! {send_first_at} <= {send_every}" ) return value FILTER_REGISTRY = Registry() validate_filters = cv.validate_registry("filter", FILTER_REGISTRY) def validate_datapoint(value): if isinstance(value, dict): return cv.Schema({ cv.Required(CONF_FROM): cv.float_, cv.Required(CONF_TO): cv.float_, })(value) value = cv.string(value) if "->" not in value: raise cv.Invalid("Datapoint mapping must contain '->'") a, b = value.split("->", 1) a, b = a.strip(), b.strip() return validate_datapoint({CONF_FROM: cv.float_(a), CONF_TO: cv.float_(b)})
def validate_dumpers(value): if isinstance(value, string_types) and value.lower() == 'all': return validate_dumpers(list(DUMPER_REGISTRY.keys())) return cv.validate_registry('dumper', DUMPER_REGISTRY)(value)
import esphome.config_validation as cv import esphome.const as ehc from esphome.core import coroutine from esphome.util import Registry from . import const as c from . import cpp_types as t PAYLOAD_SETTER_REGISTRY = Registry(None, ehc.CONF_ID) def register_payload_setter(name, payloadtype_type, schema): return PAYLOAD_SETTER_REGISTRY.register(name, payloadtype_type, schema) validate_payload_setter_list = cv.validate_registry('payload', PAYLOAD_SETTER_REGISTRY) @coroutine def build_payload_setter(full_config, template_arg, args): registry_entry, config = cg.extract_registry_entry_config( PAYLOAD_SETTER_REGISTRY, full_config) builder = registry_entry.coroutine_fun yield builder(config, config[ehc.CONF_ID], template_arg, args) @coroutine def build_payload_setter_list(config, templ, arg_type): payloads = [] for conf in config: payload = yield build_payload_setter(conf, templ, arg_type)
def register_action(name, action_type, schema): return ACTION_REGISTRY.register(name, action_type, schema) def register_condition(name, condition_type, schema): return CONDITION_REGISTRY.register(name, condition_type, schema) Action = cg.esphome_ns.class_("Action") Trigger = cg.esphome_ns.class_("Trigger") ACTION_REGISTRY = Registry() Condition = cg.esphome_ns.class_("Condition") CONDITION_REGISTRY = Registry() validate_action = cv.validate_registry_entry("action", ACTION_REGISTRY) validate_action_list = cv.validate_registry("action", ACTION_REGISTRY) validate_condition = cv.validate_registry_entry("condition", CONDITION_REGISTRY) validate_condition_list = cv.validate_registry("condition", CONDITION_REGISTRY) def validate_potentially_and_condition(value): if isinstance(value, list): with cv.remove_prepend_path(["and"]): return validate_condition({"and": value}) return validate_condition(value) DelayAction = cg.esphome_ns.class_("DelayAction", Action, cg.Component) LambdaAction = cg.esphome_ns.class_("LambdaAction", Action) IfAction = cg.esphome_ns.class_("IfAction", Action)
def register_action(name, action_type, schema): return ACTION_REGISTRY.register(name, action_type, schema) def register_condition(name, condition_type, schema): return CONDITION_REGISTRY.register(name, condition_type, schema) Action = cg.esphome_ns.class_('Action') Trigger = cg.esphome_ns.class_('Trigger') ACTION_REGISTRY = Registry() Condition = cg.esphome_ns.class_('Condition') CONDITION_REGISTRY = Registry() validate_action = cv.validate_registry_entry('action', ACTION_REGISTRY) validate_action_list = cv.validate_registry('action', ACTION_REGISTRY) validate_condition = cv.validate_registry_entry('condition', CONDITION_REGISTRY) validate_condition_list = cv.validate_registry('condition', CONDITION_REGISTRY) def validate_potentially_and_condition(value): if isinstance(value, list): with cv.remove_prepend_path(['and']): return validate_condition({ 'and': value }) return validate_condition(value) DelayAction = cg.esphome_ns.class_('DelayAction', Action, cg.Component) LambdaAction = cg.esphome_ns.class_('LambdaAction', Action)