2019-04-22 21:56:30 +02:00
|
|
|
import esphome.codegen as cg
|
2019-02-13 16:54:02 +01:00
|
|
|
import esphome.config_validation as cv
|
2019-04-22 21:56:30 +02:00
|
|
|
from esphome.const import CONF_AUTOMATION_ID, CONF_CONDITION, CONF_ELSE, CONF_ID, CONF_THEN, \
|
2019-04-24 23:49:02 +02:00
|
|
|
CONF_TRIGGER_ID, CONF_TYPE_ID, CONF_TIME
|
2019-04-17 12:06:00 +02:00
|
|
|
from esphome.core import coroutine
|
2019-04-22 21:56:30 +02:00
|
|
|
from esphome.util import Registry
|
2018-08-13 19:11:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def maybe_simple_id(*validators):
|
2019-11-09 18:37:52 +01:00
|
|
|
return maybe_conf(CONF_ID, *validators)
|
|
|
|
|
|
|
|
|
|
|
|
def maybe_conf(conf, *validators):
|
2019-04-17 12:06:00 +02:00
|
|
|
validator = cv.All(*validators)
|
2018-08-13 19:11:33 +02:00
|
|
|
|
|
|
|
def validate(value):
|
|
|
|
if isinstance(value, dict):
|
|
|
|
return validator(value)
|
2019-11-09 18:37:52 +01:00
|
|
|
with cv.remove_prepend_path([conf]):
|
|
|
|
return validator({conf: value})
|
2018-08-13 19:11:33 +02:00
|
|
|
|
|
|
|
return validate
|
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
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)
|
|
|
|
WhileAction = cg.esphome_ns.class_('WhileAction', Action)
|
|
|
|
WaitUntilAction = cg.esphome_ns.class_('WaitUntilAction', Action, cg.Component)
|
|
|
|
UpdateComponentAction = cg.esphome_ns.class_('UpdateComponentAction', Action)
|
|
|
|
Automation = cg.esphome_ns.class_('Automation')
|
|
|
|
|
|
|
|
LambdaCondition = cg.esphome_ns.class_('LambdaCondition', Condition)
|
2019-06-06 13:15:49 +02:00
|
|
|
ForCondition = cg.esphome_ns.class_('ForCondition', Condition, cg.Component)
|
2018-05-20 12:41:52 +02:00
|
|
|
|
2018-06-13 21:27:58 +02:00
|
|
|
|
2018-10-20 13:15:09 +02:00
|
|
|
def validate_automation(extra_schema=None, extra_validators=None, single=False):
|
2019-01-09 15:18:54 +01:00
|
|
|
if extra_schema is None:
|
|
|
|
extra_schema = {}
|
2019-04-17 12:06:00 +02:00
|
|
|
if isinstance(extra_schema, cv.Schema):
|
2019-01-09 15:18:54 +01:00
|
|
|
extra_schema = extra_schema.schema
|
|
|
|
schema = AUTOMATION_SCHEMA.extend(extra_schema)
|
2018-06-13 21:27:58 +02:00
|
|
|
|
2018-10-20 13:15:09 +02:00
|
|
|
def validator_(value):
|
2018-06-13 21:27:58 +02:00
|
|
|
if isinstance(value, list):
|
2019-04-22 21:56:30 +02:00
|
|
|
# List of items, there are two possible options here, either a sequence of
|
|
|
|
# actions (no then:) or a list of automations.
|
2018-10-20 13:15:09 +02:00
|
|
|
try:
|
|
|
|
# First try as a sequence of actions
|
2019-04-22 21:56:30 +02:00
|
|
|
# If that succeeds, return immediately
|
|
|
|
with cv.remove_prepend_path([CONF_THEN]):
|
|
|
|
return [schema({CONF_THEN: value})]
|
2019-04-17 12:06:00 +02:00
|
|
|
except cv.Invalid as err:
|
2018-10-20 13:15:09 +02:00
|
|
|
# Next try as a sequence of automations
|
2018-10-20 15:19:59 +02:00
|
|
|
try:
|
2019-02-26 19:22:33 +01:00
|
|
|
return cv.Schema([schema])(value)
|
2019-04-17 12:06:00 +02:00
|
|
|
except cv.Invalid as err2:
|
2019-12-07 18:28:55 +01:00
|
|
|
if 'extra keys not allowed' in str(err2) and len(err2.path) == 2:
|
2019-04-24 23:49:02 +02:00
|
|
|
raise err
|
2019-12-07 18:28:55 +01:00
|
|
|
if 'Unable to find action' in str(err):
|
2018-11-12 21:00:17 +01:00
|
|
|
raise err2
|
2019-04-22 22:14:30 +02:00
|
|
|
raise cv.MultipleInvalid([err, err2])
|
2018-06-13 21:27:58 +02:00
|
|
|
elif isinstance(value, dict):
|
|
|
|
if CONF_THEN in value:
|
2018-10-20 13:15:09 +02:00
|
|
|
return [schema(value)]
|
2019-04-22 21:56:30 +02:00
|
|
|
with cv.remove_prepend_path([CONF_THEN]):
|
|
|
|
return [schema({CONF_THEN: value})]
|
2018-10-20 13:15:09 +02:00
|
|
|
# This should only happen with invalid configs, but let's have a nice error message.
|
|
|
|
return [schema(value)]
|
|
|
|
|
|
|
|
def validator(value):
|
|
|
|
value = validator_(value)
|
|
|
|
if extra_validators is not None:
|
2019-02-26 19:22:33 +01:00
|
|
|
value = cv.Schema([extra_validators])(value)
|
2018-10-20 13:15:09 +02:00
|
|
|
if single:
|
|
|
|
if len(value) != 1:
|
2019-04-17 12:06:00 +02:00
|
|
|
raise cv.Invalid("Cannot have more than 1 automation for templates")
|
2018-10-20 13:15:09 +02:00
|
|
|
return value[0]
|
|
|
|
return value
|
2018-06-13 21:27:58 +02:00
|
|
|
|
|
|
|
return validator
|
|
|
|
|
|
|
|
|
2019-02-26 19:22:33 +01:00
|
|
|
AUTOMATION_SCHEMA = cv.Schema({
|
2019-04-22 21:56:30 +02:00
|
|
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(Trigger),
|
|
|
|
cv.GenerateID(CONF_AUTOMATION_ID): cv.declare_id(Automation),
|
|
|
|
cv.Required(CONF_THEN): validate_action_list,
|
2018-05-20 12:41:52 +02:00
|
|
|
})
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
AndCondition = cg.esphome_ns.class_('AndCondition', Condition)
|
|
|
|
OrCondition = cg.esphome_ns.class_('OrCondition', Condition)
|
|
|
|
NotCondition = cg.esphome_ns.class_('NotCondition', Condition)
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_condition('and', AndCondition, validate_condition_list)
|
2019-02-26 19:28:11 +01:00
|
|
|
def and_condition_to_code(config, condition_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
conditions = yield build_condition_list(config, template_arg, args)
|
|
|
|
yield cg.new_Pvariable(condition_id, template_arg, conditions)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_condition('or', OrCondition, validate_condition_list)
|
2019-02-26 19:28:11 +01:00
|
|
|
def or_condition_to_code(config, condition_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
conditions = yield build_condition_list(config, template_arg, args)
|
|
|
|
yield cg.new_Pvariable(condition_id, template_arg, conditions)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-05-15 14:41:11 +02:00
|
|
|
@register_condition('not', NotCondition, validate_potentially_and_condition)
|
2019-04-22 21:56:30 +02:00
|
|
|
def not_condition_to_code(config, condition_id, template_arg, args):
|
|
|
|
condition = yield build_condition(config, template_arg, args)
|
|
|
|
yield cg.new_Pvariable(condition_id, template_arg, condition)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_condition('lambda', LambdaCondition, cv.lambda_)
|
|
|
|
def lambda_condition_to_code(config, condition_id, template_arg, args):
|
|
|
|
lambda_ = yield cg.process_lambda(config, args, return_type=bool)
|
|
|
|
yield cg.new_Pvariable(condition_id, template_arg, lambda_)
|
2018-10-16 23:16:06 +02:00
|
|
|
|
2018-05-20 12:41:52 +02:00
|
|
|
|
2019-04-24 23:49:02 +02:00
|
|
|
@register_condition('for', ForCondition, cv.Schema({
|
|
|
|
cv.Required(CONF_TIME): cv.templatable(cv.positive_time_period_milliseconds),
|
|
|
|
cv.Required(CONF_CONDITION): validate_potentially_and_condition,
|
|
|
|
}).extend(cv.COMPONENT_SCHEMA))
|
|
|
|
def for_condition_to_code(config, condition_id, template_arg, args):
|
|
|
|
condition = yield build_condition(config[CONF_CONDITION], cg.TemplateArguments(), [])
|
|
|
|
var = cg.new_Pvariable(condition_id, template_arg, condition)
|
|
|
|
yield cg.register_component(var, config)
|
|
|
|
templ = yield cg.templatable(config[CONF_TIME], args, cg.uint32)
|
|
|
|
cg.add(var.set_time(templ))
|
|
|
|
yield var
|
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_action('delay', DelayAction, cv.templatable(cv.positive_time_period_milliseconds))
|
2019-02-26 19:28:11 +01:00
|
|
|
def delay_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
var = cg.new_Pvariable(action_id, template_arg)
|
|
|
|
yield cg.register_component(var, {})
|
|
|
|
template_ = yield cg.templatable(config, args, cg.uint32)
|
|
|
|
cg.add(var.set_delay(template_))
|
|
|
|
yield var
|
|
|
|
|
|
|
|
|
|
|
|
@register_action('if', IfAction, cv.All({
|
|
|
|
cv.Required(CONF_CONDITION): validate_potentially_and_condition,
|
|
|
|
cv.Optional(CONF_THEN): validate_action_list,
|
|
|
|
cv.Optional(CONF_ELSE): validate_action_list,
|
|
|
|
}, cv.has_at_least_one_key(CONF_THEN, CONF_ELSE)))
|
2019-02-26 19:28:11 +01:00
|
|
|
def if_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
conditions = yield build_condition(config[CONF_CONDITION], template_arg, args)
|
|
|
|
var = cg.new_Pvariable(action_id, template_arg, conditions)
|
2018-10-16 23:16:06 +02:00
|
|
|
if CONF_THEN in config:
|
2019-04-22 21:56:30 +02:00
|
|
|
actions = yield build_action_list(config[CONF_THEN], template_arg, args)
|
|
|
|
cg.add(var.add_then(actions))
|
2018-10-16 23:16:06 +02:00
|
|
|
if CONF_ELSE in config:
|
2019-04-22 21:56:30 +02:00
|
|
|
actions = yield build_action_list(config[CONF_ELSE], template_arg, args)
|
|
|
|
cg.add(var.add_else(actions))
|
|
|
|
yield var
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_action('while', WhileAction, cv.Schema({
|
|
|
|
cv.Required(CONF_CONDITION): validate_potentially_and_condition,
|
|
|
|
cv.Required(CONF_THEN): validate_action_list,
|
|
|
|
}))
|
2019-02-26 19:28:11 +01:00
|
|
|
def while_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
conditions = yield build_condition(config[CONF_CONDITION], template_arg, args)
|
|
|
|
var = cg.new_Pvariable(action_id, template_arg, conditions)
|
|
|
|
actions = yield build_action_list(config[CONF_THEN], template_arg, args)
|
|
|
|
cg.add(var.add_then(actions))
|
|
|
|
yield var
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-02-17 19:27:33 +01:00
|
|
|
def validate_wait_until(value):
|
2019-02-26 19:22:33 +01:00
|
|
|
schema = cv.Schema({
|
2019-04-22 21:56:30 +02:00
|
|
|
cv.Required(CONF_CONDITION): validate_potentially_and_condition,
|
2019-02-17 19:27:33 +01:00
|
|
|
})
|
|
|
|
if isinstance(value, dict) and CONF_CONDITION in value:
|
|
|
|
return schema(value)
|
|
|
|
return validate_wait_until({CONF_CONDITION: value})
|
|
|
|
|
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_action('wait_until', WaitUntilAction, validate_wait_until)
|
2019-02-26 19:28:11 +01:00
|
|
|
def wait_until_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
conditions = yield build_condition(config[CONF_CONDITION], template_arg, args)
|
|
|
|
var = cg.new_Pvariable(action_id, template_arg, conditions)
|
|
|
|
yield cg.register_component(var, {})
|
|
|
|
yield var
|
2019-02-17 19:27:33 +01:00
|
|
|
|
2018-10-16 23:16:06 +02:00
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_action('lambda', LambdaAction, cv.lambda_)
|
2019-02-26 19:28:11 +01:00
|
|
|
def lambda_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
lambda_ = yield cg.process_lambda(config, args, return_type=cg.void)
|
|
|
|
yield cg.new_Pvariable(action_id, template_arg, lambda_)
|
2018-10-16 23:16:06 +02:00
|
|
|
|
2018-12-05 21:22:06 +01:00
|
|
|
|
2019-04-22 21:56:30 +02:00
|
|
|
@register_action('component.update', UpdateComponentAction, maybe_simple_id({
|
|
|
|
cv.Required(CONF_ID): cv.use_id(cg.PollingComponent),
|
|
|
|
}))
|
2019-02-26 19:28:11 +01:00
|
|
|
def component_update_action_to_code(config, action_id, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
comp = yield cg.get_variable(config[CONF_ID])
|
|
|
|
yield cg.new_Pvariable(action_id, template_arg, comp)
|
2018-10-20 12:58:02 +02:00
|
|
|
|
|
|
|
|
2019-04-09 14:30:12 +02:00
|
|
|
@coroutine
|
2019-02-26 19:28:11 +01:00
|
|
|
def build_action(full_config, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
registry_entry, config = cg.extract_registry_entry_config(ACTION_REGISTRY, full_config)
|
|
|
|
action_id = full_config[CONF_TYPE_ID]
|
|
|
|
builder = registry_entry.coroutine_fun
|
2019-04-09 14:30:12 +02:00
|
|
|
yield builder(config, action_id, template_arg, args)
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
|
2019-04-09 14:30:12 +02:00
|
|
|
@coroutine
|
2019-04-22 21:56:30 +02:00
|
|
|
def build_action_list(config, templ, arg_type):
|
2018-06-02 22:22:20 +02:00
|
|
|
actions = []
|
|
|
|
for conf in config:
|
2019-04-09 14:30:12 +02:00
|
|
|
action = yield build_action(conf, templ, arg_type)
|
2018-06-02 22:22:20 +02:00
|
|
|
actions.append(action)
|
2019-02-03 20:46:18 +01:00
|
|
|
yield actions
|
2018-05-20 12:41:52 +02:00
|
|
|
|
|
|
|
|
2019-04-09 14:30:12 +02:00
|
|
|
@coroutine
|
2019-02-26 19:28:11 +01:00
|
|
|
def build_condition(full_config, template_arg, args):
|
2019-04-22 21:56:30 +02:00
|
|
|
registry_entry, config = cg.extract_registry_entry_config(CONDITION_REGISTRY, full_config)
|
|
|
|
action_id = full_config[CONF_TYPE_ID]
|
|
|
|
builder = registry_entry.coroutine_fun
|
2019-04-09 14:30:12 +02:00
|
|
|
yield builder(config, action_id, template_arg, args)
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-09 14:30:12 +02:00
|
|
|
@coroutine
|
2019-04-22 21:56:30 +02:00
|
|
|
def build_condition_list(config, templ, args):
|
2018-12-05 21:22:06 +01:00
|
|
|
conditions = []
|
|
|
|
for conf in config:
|
2019-04-09 14:30:12 +02:00
|
|
|
condition = yield build_condition(conf, templ, args)
|
2018-12-05 21:22:06 +01:00
|
|
|
conditions.append(condition)
|
2019-02-03 20:46:18 +01:00
|
|
|
yield conditions
|
2018-12-05 21:22:06 +01:00
|
|
|
|
|
|
|
|
2019-04-09 14:30:12 +02:00
|
|
|
@coroutine
|
2019-04-17 12:06:00 +02:00
|
|
|
def build_automation(trigger, args, config):
|
2019-02-26 19:28:11 +01:00
|
|
|
arg_types = [arg[0] for arg in args]
|
2019-04-22 21:56:30 +02:00
|
|
|
templ = cg.TemplateArguments(*arg_types)
|
|
|
|
obj = cg.new_Pvariable(config[CONF_AUTOMATION_ID], templ, trigger)
|
|
|
|
actions = yield build_action_list(config[CONF_THEN], templ, args)
|
|
|
|
cg.add(obj.add_actions(actions))
|
2018-06-13 21:27:58 +02:00
|
|
|
yield obj
|