From 3b00cfd6c4b3d55b18882961eaf5bbeebacb0532 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 26 Feb 2019 19:28:11 +0100 Subject: [PATCH] Convert automation engine to use variadic templates (#452) --- esphome/automation.py | 90 ++++++++++---------- esphome/components/api.py | 4 +- esphome/components/binary_sensor/__init__.py | 32 +++---- esphome/components/binary_sensor/template.py | 6 +- esphome/components/cover/__init__.py | 12 +-- esphome/components/cover/template.py | 20 ++--- esphome/components/deep_sleep.py | 8 +- esphome/components/display/__init__.py | 14 +-- esphome/components/fan/__init__.py | 16 ++-- esphome/components/interval.py | 6 +- esphome/components/light/__init__.py | 28 +++--- esphome/components/logger.py | 8 +- esphome/components/mqtt.py | 22 ++--- esphome/components/output/__init__.py | 14 +-- esphome/components/pn532.py | 2 +- esphome/components/script.py | 14 +-- esphome/components/sensor/__init__.py | 10 +-- esphome/components/sensor/template.py | 6 +- esphome/components/stepper/__init__.py | 12 +-- esphome/components/switch/__init__.py | 30 +++---- esphome/components/switch/template.py | 16 ++-- esphome/components/text_sensor/__init__.py | 2 +- esphome/components/text_sensor/template.py | 6 +- esphome/components/time/__init__.py | 6 +- esphome/core_config.py | 16 ++-- esphome/cpp_generator.py | 24 ++++-- esphome/cpp_types.py | 1 - 27 files changed, 213 insertions(+), 212 deletions(-) diff --git a/esphome/automation.py b/esphome/automation.py index 349628d3c4..538c8c5c46 100644 --- a/esphome/automation.py +++ b/esphome/automation.py @@ -3,9 +3,9 @@ import copy import voluptuous as vol import esphome.config_validation as cv -from esphome.const import CONF_ABOVE, CONF_ACTION_ID, CONF_AND, CONF_AUTOMATION_ID, \ - CONF_BELOW, CONF_CONDITION, CONF_CONDITION_ID, CONF_DELAY, CONF_ELSE, CONF_ID, CONF_IF, \ - CONF_LAMBDA, CONF_OR, CONF_RANGE, CONF_THEN, CONF_TRIGGER_ID, CONF_WHILE, CONF_WAIT_UNTIL +from esphome.const import CONF_ABOVE, CONF_ACTION_ID, CONF_AND, CONF_AUTOMATION_ID, CONF_BELOW, \ + CONF_CONDITION, CONF_CONDITION_ID, CONF_DELAY, CONF_ELSE, CONF_ID, CONF_IF, CONF_LAMBDA, \ + CONF_OR, CONF_RANGE, CONF_THEN, CONF_TRIGGER_ID, CONF_WAIT_UNTIL, CONF_WHILE from esphome.core import CORE from esphome.cpp_generator import Pvariable, TemplateArguments, add, get_variable, \ process_lambda, templatable @@ -167,8 +167,8 @@ AND_CONDITION_SCHEMA = validate_recursive_condition @CONDITION_REGISTRY.register(CONF_AND, AND_CONDITION_SCHEMA) -def and_condition_to_code(config, condition_id, arg_type, template_arg): - for conditions in build_conditions(config, arg_type): +def and_condition_to_code(config, condition_id, template_arg, args): + for conditions in build_conditions(config, template_arg, args): yield rhs = AndCondition.new(template_arg, conditions) type = AndCondition.template(template_arg) @@ -179,8 +179,8 @@ OR_CONDITION_SCHEMA = validate_recursive_condition @CONDITION_REGISTRY.register(CONF_OR, OR_CONDITION_SCHEMA) -def or_condition_to_code(config, condition_id, arg_type, template_arg): - for conditions in build_conditions(config, arg_type): +def or_condition_to_code(config, condition_id, template_arg, args): + for conditions in build_conditions(config, template_arg, args): yield rhs = OrCondition.new(template_arg, conditions) type = OrCondition.template(template_arg) @@ -194,18 +194,18 @@ RANGE_CONDITION_SCHEMA = vol.All(cv.Schema({ @CONDITION_REGISTRY.register(CONF_RANGE, RANGE_CONDITION_SCHEMA) -def range_condition_to_code(config, condition_id, arg_type, template_arg): - for conditions in build_conditions(config, arg_type): +def range_condition_to_code(config, condition_id, template_arg, args): + for conditions in build_conditions(config, template_arg, args): yield rhs = RangeCondition.new(template_arg, conditions) type = RangeCondition.template(template_arg) condition = Pvariable(condition_id, rhs, type=type) if CONF_ABOVE in config: - for template_ in templatable(config[CONF_ABOVE], arg_type, float_): + for template_ in templatable(config[CONF_ABOVE], args, float_): yield condition.set_min(template_) if CONF_BELOW in config: - for template_ in templatable(config[CONF_BELOW], arg_type, float_): + for template_ in templatable(config[CONF_BELOW], args, float_): yield condition.set_max(template_) yield condition @@ -215,11 +215,11 @@ DELAY_ACTION_SCHEMA = cv.templatable(cv.positive_time_period_milliseconds) @ACTION_REGISTRY.register(CONF_DELAY, DELAY_ACTION_SCHEMA) -def delay_action_to_code(config, action_id, arg_type, template_arg): +def delay_action_to_code(config, action_id, template_arg, args): rhs = App.register_component(DelayAction.new(template_arg)) type = DelayAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config, arg_type, uint32): + for template_ in templatable(config, args, uint32): yield add(action.set_delay(template_)) yield action @@ -233,18 +233,18 @@ IF_ACTION_SCHEMA = vol.All({ @ACTION_REGISTRY.register(CONF_IF, IF_ACTION_SCHEMA) -def if_action_to_code(config, action_id, arg_type, template_arg): - for conditions in build_conditions(config[CONF_CONDITION], arg_type): +def if_action_to_code(config, action_id, template_arg, args): + for conditions in build_conditions(config[CONF_CONDITION], template_arg, args): yield None rhs = IfAction.new(template_arg, conditions) type = IfAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_THEN in config: - for actions in build_actions(config[CONF_THEN], arg_type): + for actions in build_actions(config[CONF_THEN], template_arg, args): yield None add(action.add_then(actions)) if CONF_ELSE in config: - for actions in build_actions(config[CONF_ELSE], arg_type): + for actions in build_actions(config[CONF_ELSE], template_arg, args): yield None add(action.add_else(actions)) yield action @@ -257,13 +257,13 @@ WHILE_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_WHILE, WHILE_ACTION_SCHEMA) -def while_action_to_code(config, action_id, arg_type, template_arg): - for conditions in build_conditions(config[CONF_CONDITION], arg_type): +def while_action_to_code(config, action_id, template_arg, args): + for conditions in build_conditions(config[CONF_CONDITION], template_arg, args): yield None rhs = WhileAction.new(template_arg, conditions) type = WhileAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for actions in build_actions(config[CONF_THEN], arg_type): + for actions in build_actions(config[CONF_THEN], template_arg, args): yield None add(action.add_then(actions)) yield action @@ -282,8 +282,8 @@ WAIT_UNTIL_ACTION_SCHEMA = validate_wait_until @ACTION_REGISTRY.register(CONF_WAIT_UNTIL, WAIT_UNTIL_ACTION_SCHEMA) -def wait_until_action_to_code(config, action_id, arg_type, template_arg): - for conditions in build_conditions(config[CONF_CONDITION], arg_type): +def wait_until_action_to_code(config, action_id, template_arg, args): + for conditions in build_conditions(config[CONF_CONDITION], template_arg, args): yield None rhs = WaitUntilAction.new(template_arg, conditions) type = WaitUntilAction.template(template_arg) @@ -296,8 +296,8 @@ LAMBDA_ACTION_SCHEMA = cv.lambda_ @ACTION_REGISTRY.register(CONF_LAMBDA, LAMBDA_ACTION_SCHEMA) -def lambda_action_to_code(config, action_id, arg_type, template_arg): - for lambda_ in process_lambda(config, [(arg_type, 'x')], return_type=void): +def lambda_action_to_code(config, action_id, template_arg, args): + for lambda_ in process_lambda(config, args, return_type=void): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) @@ -308,8 +308,8 @@ LAMBDA_CONDITION_SCHEMA = cv.lambda_ @CONDITION_REGISTRY.register(CONF_LAMBDA, LAMBDA_CONDITION_SCHEMA) -def lambda_condition_to_code(config, condition_id, arg_type, template_arg): - for lambda_ in process_lambda(config, [(arg_type, 'x')], return_type=bool_): +def lambda_condition_to_code(config, condition_id, template_arg, args): + for lambda_ in process_lambda(config, args, return_type=bool_): yield rhs = LambdaCondition.new(template_arg, lambda_) type = LambdaCondition.template(template_arg) @@ -323,7 +323,7 @@ COMPONENT_UPDATE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_COMPONENT_UPDATE, COMPONENT_UPDATE_ACTION_SCHEMA) -def component_update_action_to_code(config, action_id, arg_type, template_arg): +def component_update_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = UpdateComponentAction.new(template_arg, var) @@ -331,61 +331,59 @@ def component_update_action_to_code(config, action_id, arg_type, template_arg): yield Pvariable(action_id, rhs, type=type) -def build_action(full_config, arg_type): +def build_action(full_config, template_arg, args): action_id = full_config[CONF_ACTION_ID] key, config = next((k, v) for k, v in full_config.items() if k in ACTION_REGISTRY) builder = ACTION_REGISTRY[key][1] - template_arg = TemplateArguments(arg_type) - for result in builder(config, action_id, arg_type, template_arg): + for result in builder(config, action_id, template_arg, args): yield None yield result -def build_actions(config, arg_type): +def build_actions(config, templ, arg_type): actions = [] for conf in config: - for action in build_action(conf, arg_type): + for action in build_action(conf, templ, arg_type): yield None actions.append(action) yield actions -def build_condition(full_config, arg_type): +def build_condition(full_config, template_arg, args): action_id = full_config[CONF_CONDITION_ID] key, config = next((k, v) for k, v in full_config.items() if k in CONDITION_REGISTRY) builder = CONDITION_REGISTRY[key][1] - template_arg = TemplateArguments(arg_type) - for result in builder(config, action_id, arg_type, template_arg): + for result in builder(config, action_id, template_arg, args): yield None yield result -def build_conditions(config, arg_type): +def build_conditions(config, templ, args): conditions = [] for conf in config: - for condition in build_condition(conf, arg_type): + for condition in build_condition(conf, templ, args): yield None conditions.append(condition) yield conditions -def build_automation_(trigger, arg_type, config): - rhs = App.make_automation(TemplateArguments(arg_type), trigger) - type = Automation.template(arg_type) +def build_automation_(trigger, args, config): + arg_types = [arg[0] for arg in args] + templ = TemplateArguments(*arg_types) + rhs = App.make_automation(templ, trigger) + type = Automation.template(templ) obj = Pvariable(config[CONF_AUTOMATION_ID], rhs, type=type) if CONF_IF in config: - conditions = None - for conditions in build_conditions(config[CONF_IF], arg_type): + for conditions in build_conditions(config[CONF_IF], templ, args): yield None add(obj.add_conditions(conditions)) - actions = None - for actions in build_actions(config[CONF_THEN], arg_type): + for actions in build_actions(config[CONF_THEN], templ, args): yield None add(obj.add_actions(actions)) yield obj -def build_automation(trigger, arg_type, config): - CORE.add_job(build_automation_, trigger, arg_type, config) +def build_automations(trigger, args, config): + CORE.add_job(build_automation_, trigger, args, config) diff --git a/esphome/components/api.py b/esphome/components/api.py index 00a578be9d..e51e97f599 100644 --- a/esphome/components/api.py +++ b/esphome/components/api.py @@ -65,11 +65,11 @@ HOMEASSISTANT_SERVIC_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_HOMEASSISTANT_SERVICE, HOMEASSISTANT_SERVIC_ACTION_SCHEMA) -def homeassistant_service_to_code(config, action_id, arg_type, template_arg): +def homeassistant_service_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_home_assistant_service_call_action(template_arg) - type = HomeAssistantServiceCallAction.template(arg_type) + type = HomeAssistantServiceCallAction.template(template_arg) act = Pvariable(action_id, rhs, type=type) add(act.set_service(config[CONF_SERVICE])) if CONF_DATA in config: diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 45e129853f..048e9503df 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -12,7 +12,7 @@ from esphome.const import CONF_DELAYED_OFF, CONF_DELAYED_ON, CONF_DEVICE_CLASS, CONF_STATE, CONF_TIMING, CONF_TRIGGER_ID from esphome.core import CORE from esphome.cpp_generator import Pvariable, StructInitializer, add, get_variable, process_lambda -from esphome.cpp_types import App, Component, Nameable, NoArg, Trigger, bool_, esphome_ns, optional +from esphome.cpp_types import App, Component, Nameable, Trigger, bool_, esphome_ns, optional from esphome.py_compat import string_types DEVICE_CLASSES = [ @@ -32,11 +32,11 @@ BinarySensorPtr = BinarySensor.operator('ptr') MQTTBinarySensorComponent = binary_sensor_ns.class_('MQTTBinarySensorComponent', mqtt.MQTTComponent) # Triggers -PressTrigger = binary_sensor_ns.class_('PressTrigger', Trigger.template(NoArg)) -ReleaseTrigger = binary_sensor_ns.class_('ReleaseTrigger', Trigger.template(NoArg)) -ClickTrigger = binary_sensor_ns.class_('ClickTrigger', Trigger.template(NoArg)) -DoubleClickTrigger = binary_sensor_ns.class_('DoubleClickTrigger', Trigger.template(NoArg)) -MultiClickTrigger = binary_sensor_ns.class_('MultiClickTrigger', Trigger.template(NoArg), Component) +PressTrigger = binary_sensor_ns.class_('PressTrigger', Trigger.template()) +ReleaseTrigger = binary_sensor_ns.class_('ReleaseTrigger', Trigger.template()) +ClickTrigger = binary_sensor_ns.class_('ClickTrigger', Trigger.template()) +DoubleClickTrigger = binary_sensor_ns.class_('DoubleClickTrigger', Trigger.template()) +MultiClickTrigger = binary_sensor_ns.class_('MultiClickTrigger', Trigger.template(), Component) MultiClickTriggerEvent = binary_sensor_ns.struct('MultiClickTriggerEvent') StateTrigger = binary_sensor_ns.class_('StateTrigger', Trigger.template(bool_)) @@ -234,23 +234,23 @@ def setup_binary_sensor_core_(binary_sensor_var, config): for conf in config.get(CONF_ON_PRESS, []): rhs = binary_sensor_var.make_press_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_RELEASE, []): rhs = binary_sensor_var.make_release_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_CLICK, []): rhs = binary_sensor_var.make_click_trigger(conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_DOUBLE_CLICK, []): rhs = binary_sensor_var.make_double_click_trigger(conf[CONF_MIN_LENGTH], conf[CONF_MAX_LENGTH]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_MULTI_CLICK, []): timings = [] @@ -265,12 +265,12 @@ def setup_binary_sensor_core_(binary_sensor_var, config): trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) if CONF_INVALID_COOLDOWN in conf: add(trigger.set_invalid_cooldown(conf[CONF_INVALID_COOLDOWN])) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_STATE, []): rhs = binary_sensor_var.make_state_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, bool_, conf) + automation.build_automations(trigger, [(bool_, 'x')], conf) setup_mqtt_component(binary_sensor_var.Pget_mqtt(), config) @@ -306,11 +306,11 @@ BINARY_SENSOR_IS_ON_CONDITION_SCHEMA = maybe_simple_id({ @CONDITION_REGISTRY.register(CONF_BINARY_SENSOR_IS_ON, BINARY_SENSOR_IS_ON_CONDITION_SCHEMA) -def binary_sensor_is_on_to_code(config, condition_id, arg_type, template_arg): +def binary_sensor_is_on_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_binary_sensor_is_on_condition(template_arg) - type = BinarySensorCondition.template(arg_type) + type = BinarySensorCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) @@ -321,9 +321,9 @@ BINARY_SENSOR_IS_OFF_CONDITION_SCHEMA = maybe_simple_id({ @CONDITION_REGISTRY.register(CONF_BINARY_SENSOR_IS_OFF, BINARY_SENSOR_IS_OFF_CONDITION_SCHEMA) -def binary_sensor_is_off_to_code(config, condition_id, arg_type, template_arg): +def binary_sensor_is_off_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_binary_sensor_is_off_condition(template_arg) - type = BinarySensorCondition.template(arg_type) + type = BinarySensorCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) diff --git a/esphome/components/binary_sensor/template.py b/esphome/components/binary_sensor/template.py index 1cd7c59a07..67b036a2d3 100644 --- a/esphome/components/binary_sensor/template.py +++ b/esphome/components/binary_sensor/template.py @@ -44,13 +44,13 @@ BINARY_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_BINARY_SENSOR_TEMPLATE_PUBLISH, BINARY_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA) -def binary_sensor_template_publish_to_code(config, action_id, arg_type, template_arg): +def binary_sensor_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_binary_sensor_publish_action(template_arg) - type = BinarySensorPublishAction.template(arg_type) + type = BinarySensorPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_STATE], arg_type, bool_): + for template_ in templatable(config[CONF_STATE], args, bool_): yield None add(action.set_state(template_)) yield action diff --git a/esphome/components/cover/__init__.py b/esphome/components/cover/__init__.py index 48c9dc90fa..06289ed3d9 100644 --- a/esphome/components/cover/__init__.py +++ b/esphome/components/cover/__init__.py @@ -60,11 +60,11 @@ COVER_OPEN_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_COVER_OPEN, COVER_OPEN_ACTION_SCHEMA) -def cover_open_to_code(config, action_id, arg_type, template_arg): +def cover_open_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_open_action(template_arg) - type = OpenAction.template(arg_type) + type = OpenAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -75,11 +75,11 @@ COVER_CLOSE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_COVER_CLOSE, COVER_CLOSE_ACTION_SCHEMA) -def cover_close_to_code(config, action_id, arg_type, template_arg): +def cover_close_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_close_action(template_arg) - type = CloseAction.template(arg_type) + type = CloseAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -90,11 +90,11 @@ COVER_STOP_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_COVER_STOP, COVER_STOP_ACTION_SCHEMA) -def cover_stop_to_code(config, action_id, arg_type, template_arg): +def cover_stop_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_stop_action(template_arg) - type = StopAction.template(arg_type) + type = StopAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) diff --git a/esphome/components/cover/template.py b/esphome/components/cover/template.py index 11c2b85e5f..2cf02ab805 100644 --- a/esphome/components/cover/template.py +++ b/esphome/components/cover/template.py @@ -8,7 +8,7 @@ from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_ID, CONF_L CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_STATE, CONF_STOP_ACTION from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable from esphome.cpp_helpers import setup_component -from esphome.cpp_types import Action, App, NoArg, optional +from esphome.cpp_types import Action, App, optional from esphome.py_compat import string_types TemplateCover = cover.cover_ns.class_('TemplateCover', cover.Cover) @@ -38,14 +38,14 @@ def to_code(config): yield add(var.set_state_lambda(template_)) if CONF_OPEN_ACTION in config: - automation.build_automation(var.get_open_trigger(), NoArg, - config[CONF_OPEN_ACTION]) + automation.build_automations(var.get_open_trigger(), [], + config[CONF_OPEN_ACTION]) if CONF_CLOSE_ACTION in config: - automation.build_automation(var.get_close_trigger(), NoArg, - config[CONF_CLOSE_ACTION]) + automation.build_automations(var.get_close_trigger(), [], + config[CONF_CLOSE_ACTION]) if CONF_STOP_ACTION in config: - automation.build_automation(var.get_stop_trigger(), NoArg, - config[CONF_STOP_ACTION]) + automation.build_automations(var.get_stop_trigger(), [], + config[CONF_STOP_ACTION]) if CONF_OPTIMISTIC in config: add(var.set_optimistic(config[CONF_OPTIMISTIC])) if CONF_ASSUMED_STATE in config: @@ -63,17 +63,17 @@ COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_COVER_TEMPLATE_PUBLISH, COVER_TEMPLATE_PUBLISH_ACTION_SCHEMA) -def cover_template_publish_to_code(config, action_id, arg_type, template_arg): +def cover_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_cover_publish_action(template_arg) - type = CoverPublishAction.template(arg_type) + type = CoverPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) state = config[CONF_STATE] if isinstance(state, string_types): template_ = cover.COVER_STATES[state] else: - for template_ in templatable(state, arg_type, cover.CoverState): + for template_ in templatable(state, args, cover.CoverState): yield None add(action.set_state(template_)) yield action diff --git a/esphome/components/deep_sleep.py b/esphome/components/deep_sleep.py index 34c69fd29e..1674278e99 100644 --- a/esphome/components/deep_sleep.py +++ b/esphome/components/deep_sleep.py @@ -95,11 +95,11 @@ DEEP_SLEEP_ENTER_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_DEEP_SLEEP_ENTER, DEEP_SLEEP_ENTER_ACTION_SCHEMA) -def deep_sleep_enter_to_code(config, action_id, arg_type, template_arg): +def deep_sleep_enter_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_enter_deep_sleep_action(template_arg) - type = EnterDeepSleepAction.template(arg_type) + type = EnterDeepSleepAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -110,9 +110,9 @@ DEEP_SLEEP_PREVENT_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_DEEP_SLEEP_PREVENT, DEEP_SLEEP_PREVENT_ACTION_SCHEMA) -def deep_sleep_prevent_to_code(config, action_id, arg_type, template_arg): +def deep_sleep_prevent_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_prevent_deep_sleep_action(template_arg) - type = PreventDeepSleepAction.template(arg_type) + type = PreventDeepSleepAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) diff --git a/esphome/components/display/__init__.py b/esphome/components/display/__init__.py index aa568059c9..88687c1e70 100644 --- a/esphome/components/display/__init__.py +++ b/esphome/components/display/__init__.py @@ -78,11 +78,11 @@ DISPLAY_PAGE_SHOW_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW, DISPLAY_PAGE_SHOW_ACTION_SCHEMA) -def display_page_show_to_code(config, action_id, arg_type, template_arg): - type = DisplayPageShowAction.template(arg_type) +def display_page_show_to_code(config, action_id, template_arg, args): + type = DisplayPageShowAction.template(template_arg) action = Pvariable(action_id, type.new(), type=type) if isinstance(config[CONF_ID], core.Lambda): - for template_ in templatable(config[CONF_ID], arg_type, DisplayPagePtr): + for template_ in templatable(config[CONF_ID], args, DisplayPagePtr): yield None add(action.set_page(template_)) else: @@ -99,10 +99,10 @@ DISPLAY_PAGE_SHOW_NEXT_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW_NEXT, DISPLAY_PAGE_SHOW_NEXT_ACTION_SCHEMA) -def display_page_show_next_to_code(config, action_id, arg_type, template_arg): +def display_page_show_next_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None - type = DisplayPageShowNextAction.template(arg_type) + type = DisplayPageShowNextAction.template(template_arg) yield Pvariable(action_id, type.new(var), type=type) @@ -113,10 +113,10 @@ DISPLAY_PAGE_SHOW_PREVIOUS_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_DISPLAY_PAGE_SHOW_PREVIOUS, DISPLAY_PAGE_SHOW_PREVIOUS_ACTION_SCHEMA) -def display_page_show_previous_to_code(config, action_id, arg_type, template_arg): +def display_page_show_previous_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None - type = DisplayPageShowPrevAction.template(arg_type) + type = DisplayPageShowPrevAction.template(template_arg) yield Pvariable(action_id, type.new(var), type=type) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index 35ad3f389b..32190f00d1 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -81,11 +81,11 @@ FAN_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_FAN_TOGGLE, FAN_TOGGLE_ACTION_SCHEMA) -def fan_toggle_to_code(config, action_id, arg_type, template_arg): +def fan_toggle_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) - type = ToggleAction.template(arg_type) + type = ToggleAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -96,11 +96,11 @@ FAN_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_FAN_TURN_OFF, FAN_TURN_OFF_ACTION_SCHEMA) -def fan_turn_off_to_code(config, action_id, arg_type, template_arg): +def fan_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) - type = TurnOffAction.template(arg_type) + type = TurnOffAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -113,18 +113,18 @@ FAN_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_FAN_TURN_ON, FAN_TURN_ON_ACTION_SCHEMA) -def fan_turn_on_to_code(config, action_id, arg_type, template_arg): +def fan_turn_on_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) - type = TurnOnAction.template(arg_type) + type = TurnOnAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_OSCILLATING in config: - for template_ in templatable(config[CONF_OSCILLATING], arg_type, bool_): + for template_ in templatable(config[CONF_OSCILLATING], args, bool_): yield None add(action.set_oscillating(template_)) if CONF_SPEED in config: - for template_ in templatable(config[CONF_SPEED], arg_type, FanSpeed): + for template_ in templatable(config[CONF_SPEED], args, FanSpeed): yield None if isinstance(template_, string_types): template_ = FAN_SPEEDS[template_] diff --git a/esphome/components/interval.py b/esphome/components/interval.py index 49aa9b9163..597dbf92aa 100644 --- a/esphome/components/interval.py +++ b/esphome/components/interval.py @@ -5,9 +5,9 @@ import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_INTERVAL from esphome.cpp_generator import Pvariable from esphome.cpp_helpers import setup_component -from esphome.cpp_types import App, NoArg, PollingComponent, Trigger, esphome_ns +from esphome.cpp_types import App, PollingComponent, Trigger, esphome_ns -IntervalTrigger = esphome_ns.class_('IntervalTrigger', Trigger.template(NoArg), PollingComponent) +IntervalTrigger = esphome_ns.class_('IntervalTrigger', Trigger.template(), PollingComponent) CONFIG_SCHEMA = automation.validate_automation(cv.Schema({ cv.GenerateID(): cv.declare_variable_id(IntervalTrigger), @@ -21,4 +21,4 @@ def to_code(config): trigger = Pvariable(conf[CONF_ID], rhs) setup_component(trigger, conf) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) diff --git a/esphome/components/light/__init__.py b/esphome/components/light/__init__.py index 13b40e19d4..92f9766113 100644 --- a/esphome/components/light/__init__.py +++ b/esphome/components/light/__init__.py @@ -404,14 +404,14 @@ LIGHT_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_LIGHT_TOGGLE, LIGHT_TOGGLE_ACTION_SCHEMA) -def light_toggle_to_code(config, action_id, arg_type, template_arg): +def light_toggle_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) type = ToggleAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_TRANSITION_LENGTH in config: - for template_ in templatable(config[CONF_TRANSITION_LENGTH], arg_type, uint32): + for template_ in templatable(config[CONF_TRANSITION_LENGTH], args, uint32): yield None add(action.set_transition_length(template_)) yield action @@ -425,14 +425,14 @@ LIGHT_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_LIGHT_TURN_OFF, LIGHT_TURN_OFF_ACTION_SCHEMA) -def light_turn_off_to_code(config, action_id, arg_type, template_arg): +def light_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) type = TurnOffAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_TRANSITION_LENGTH in config: - for template_ in templatable(config[CONF_TRANSITION_LENGTH], arg_type, uint32): + for template_ in templatable(config[CONF_TRANSITION_LENGTH], args, uint32): yield None add(action.set_transition_length(template_)) yield action @@ -456,46 +456,46 @@ LIGHT_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_LIGHT_TURN_ON, LIGHT_TURN_ON_ACTION_SCHEMA) -def light_turn_on_to_code(config, action_id, arg_type, template_arg): +def light_turn_on_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) type = TurnOnAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) if CONF_TRANSITION_LENGTH in config: - for template_ in templatable(config[CONF_TRANSITION_LENGTH], arg_type, uint32): + for template_ in templatable(config[CONF_TRANSITION_LENGTH], args, uint32): yield None add(action.set_transition_length(template_)) if CONF_FLASH_LENGTH in config: - for template_ in templatable(config[CONF_FLASH_LENGTH], arg_type, uint32): + for template_ in templatable(config[CONF_FLASH_LENGTH], args, uint32): yield None add(action.set_flash_length(template_)) if CONF_BRIGHTNESS in config: - for template_ in templatable(config[CONF_BRIGHTNESS], arg_type, float_): + for template_ in templatable(config[CONF_BRIGHTNESS], args, float_): yield None add(action.set_brightness(template_)) if CONF_RED in config: - for template_ in templatable(config[CONF_RED], arg_type, float_): + for template_ in templatable(config[CONF_RED], args, float_): yield None add(action.set_red(template_)) if CONF_GREEN in config: - for template_ in templatable(config[CONF_GREEN], arg_type, float_): + for template_ in templatable(config[CONF_GREEN], args, float_): yield None add(action.set_green(template_)) if CONF_BLUE in config: - for template_ in templatable(config[CONF_BLUE], arg_type, float_): + for template_ in templatable(config[CONF_BLUE], args, float_): yield None add(action.set_blue(template_)) if CONF_WHITE in config: - for template_ in templatable(config[CONF_WHITE], arg_type, float_): + for template_ in templatable(config[CONF_WHITE], args, float_): yield None add(action.set_white(template_)) if CONF_COLOR_TEMPERATURE in config: - for template_ in templatable(config[CONF_COLOR_TEMPERATURE], arg_type, float_): + for template_ in templatable(config[CONF_COLOR_TEMPERATURE], args, float_): yield None add(action.set_color_temperature(template_)) if CONF_EFFECT in config: - for template_ in templatable(config[CONF_EFFECT], arg_type, std_string): + for template_ in templatable(config[CONF_EFFECT], args, std_string): yield None add(action.set_effect(template_)) yield action diff --git a/esphome/components/logger.py b/esphome/components/logger.py index 8cf6b6dbf7..9211d41b22 100644 --- a/esphome/components/logger.py +++ b/esphome/components/logger.py @@ -167,13 +167,13 @@ LOGGER_LOG_ACTION_SCHEMA = vol.All(maybe_simple_message({ @ACTION_REGISTRY.register(CONF_LOGGER_LOG, LOGGER_LOG_ACTION_SCHEMA) -def logger_log_action_to_code(config, action_id, arg_type, template_arg): +def logger_log_action_to_code(config, action_id, template_arg, args): esp_log = LOG_LEVEL_TO_ESP_LOG[config[CONF_LEVEL]] - args = [RawExpression(text_type(x)) for x in config[CONF_ARGS]] + args_ = [RawExpression(text_type(x)) for x in config[CONF_ARGS]] - text = text_type(statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args))) + text = text_type(statement(esp_log(config[CONF_TAG], config[CONF_FORMAT], *args_))) - for lambda_ in process_lambda(Lambda(text), [(arg_type, 'x')], return_type=void): + for lambda_ in process_lambda(Lambda(text), args, return_type=void): yield None rhs = LambdaAction.new(template_arg, lambda_) type = LambdaAction.template(template_arg) diff --git a/esphome/components/mqtt.py b/esphome/components/mqtt.py index 1cd01583ab..d2c51a38d4 100644 --- a/esphome/components/mqtt.py +++ b/esphome/components/mqtt.py @@ -184,12 +184,12 @@ def to_code(config): add(trigger.set_qos(conf[CONF_QOS])) if CONF_PAYLOAD in conf: add(trigger.set_payload(conf[CONF_PAYLOAD])) - automation.build_automation(trigger, std_string, conf) + automation.build_automations(trigger, [(std_string, 'x')], conf) for conf in config.get(CONF_ON_JSON_MESSAGE, []): rhs = mqtt.make_json_message_trigger(conf[CONF_TOPIC], conf[CONF_QOS]) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, JsonObjectConstRef, conf) + automation.build_automations(trigger, [(JsonObjectConstRef, 'x')], conf) CONF_MQTT_PUBLISH = 'mqtt.publish' @@ -203,25 +203,25 @@ MQTT_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_MQTT_PUBLISH, MQTT_PUBLISH_ACTION_SCHEMA) -def mqtt_publish_action_to_code(config, action_id, arg_type, template_arg): +def mqtt_publish_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_publish_action(template_arg) type = MQTTPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): + for template_ in templatable(config[CONF_TOPIC], args, std_string): yield None add(action.set_topic(template_)) - for template_ in templatable(config[CONF_PAYLOAD], arg_type, std_string): + for template_ in templatable(config[CONF_PAYLOAD], args, std_string): yield None add(action.set_payload(template_)) if CONF_QOS in config: - for template_ in templatable(config[CONF_QOS], arg_type, uint8): + for template_ in templatable(config[CONF_QOS], args, uint8): yield add(action.set_qos(template_)) if CONF_RETAIN in config: - for template_ in templatable(config[CONF_RETAIN], arg_type, bool_): + for template_ in templatable(config[CONF_RETAIN], args, bool_): yield None add(action.set_retain(template_)) yield action @@ -238,18 +238,18 @@ MQTT_PUBLISH_JSON_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_MQTT_PUBLISH_JSON, MQTT_PUBLISH_JSON_ACTION_SCHEMA) -def mqtt_publish_json_action_to_code(config, action_id, arg_type, template_arg): +def mqtt_publish_json_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_publish_json_action(template_arg) type = MQTTPublishJsonAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_TOPIC], arg_type, std_string): + for template_ in templatable(config[CONF_TOPIC], args, std_string): yield None add(action.set_topic(template_)) - for lambda_ in process_lambda(config[CONF_PAYLOAD], [(arg_type, 'x'), (JsonObjectRef, 'root')], - return_type=void): + args_ = args + [(JsonObjectRef, 'root')] + for lambda_ in process_lambda(config[CONF_PAYLOAD], args_, return_type=void): yield None add(action.set_payload(lambda_)) if CONF_QOS in config: diff --git a/esphome/components/output/__init__.py b/esphome/components/output/__init__.py index aa5609b6af..e6621ffc78 100644 --- a/esphome/components/output/__init__.py +++ b/esphome/components/output/__init__.py @@ -71,11 +71,11 @@ OUTPUT_TURN_ON_ACTION = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_OUTPUT_TURN_ON, OUTPUT_TURN_ON_ACTION) -def output_turn_on_to_code(config, action_id, arg_type, template_arg): +def output_turn_on_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) - type = TurnOnAction.template(arg_type) + type = TurnOnAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -86,11 +86,11 @@ OUTPUT_TURN_OFF_ACTION = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_OUTPUT_TURN_OFF, OUTPUT_TURN_OFF_ACTION) -def output_turn_off_to_code(config, action_id, arg_type, template_arg): +def output_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) - type = TurnOffAction.template(arg_type) + type = TurnOffAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -102,13 +102,13 @@ OUTPUT_SET_LEVEL_ACTION = cv.Schema({ @ACTION_REGISTRY.register(CONF_OUTPUT_SET_LEVEL, OUTPUT_SET_LEVEL_ACTION) -def output_set_level_to_code(config, action_id, arg_type, template_arg): +def output_set_level_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_set_level_action(template_arg) - type = SetLevelAction.template(arg_type) + type = SetLevelAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_LEVEL], arg_type, float_): + for template_ in templatable(config[CONF_LEVEL], args, float_): yield None add(action.set_level(template_)) yield action diff --git a/esphome/components/pn532.py b/esphome/components/pn532.py index 58f44520a9..9955bc9d34 100644 --- a/esphome/components/pn532.py +++ b/esphome/components/pn532.py @@ -38,7 +38,7 @@ def to_code(config): for conf_ in config.get(CONF_ON_TAG, []): trigger = Pvariable(conf_[CONF_TRIGGER_ID], pn532.make_trigger()) - automation.build_automation(trigger, std_string, conf_) + automation.build_automations(trigger, [(std_string, 'x')], conf_) setup_component(pn532, config) diff --git a/esphome/components/script.py b/esphome/components/script.py index b2641b2d6b..59c3171346 100644 --- a/esphome/components/script.py +++ b/esphome/components/script.py @@ -5,9 +5,9 @@ from esphome.automation import ACTION_REGISTRY, maybe_simple_id import esphome.config_validation as cv from esphome.const import CONF_ID from esphome.cpp_generator import Pvariable, get_variable -from esphome.cpp_types import Action, NoArg, Trigger, esphome_ns +from esphome.cpp_types import Action, Trigger, esphome_ns -Script = esphome_ns.class_('Script', Trigger.template(NoArg)) +Script = esphome_ns.class_('Script', Trigger.template()) ScriptExecuteAction = esphome_ns.class_('ScriptExecuteAction', Action) ScriptStopAction = esphome_ns.class_('ScriptStopAction', Action) @@ -19,7 +19,7 @@ CONFIG_SCHEMA = automation.validate_automation({ def to_code(config): for conf in config: trigger = Pvariable(conf[CONF_ID], Script.new()) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) CONF_SCRIPT_EXECUTE = 'script.execute' @@ -29,11 +29,11 @@ SCRIPT_EXECUTE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_SCRIPT_EXECUTE, SCRIPT_EXECUTE_ACTION_SCHEMA) -def script_execute_action_to_code(config, action_id, arg_type, template_arg): +def script_execute_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_execute_action(template_arg) - type = ScriptExecuteAction.template(arg_type) + type = ScriptExecuteAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -44,9 +44,9 @@ SCRIPT_STOP_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_SCRIPT_STOP, SCRIPT_STOP_ACTION_SCHEMA) -def script_stop_action_to_code(config, action_id, arg_type, template_arg): +def script_stop_action_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_stop_action(template_arg) - type = ScriptStopAction.template(arg_type) + type = ScriptStopAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 4fae480ef9..6844c1fc35 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -181,11 +181,11 @@ def setup_sensor_core_(sensor_var, config): for conf in config.get(CONF_ON_VALUE, []): rhs = sensor_var.make_state_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, float_, conf) + automation.build_automations(trigger, [(float_, 'x')], conf) for conf in config.get(CONF_ON_RAW_VALUE, []): rhs = sensor_var.make_raw_state_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, float_, conf) + automation.build_automations(trigger, [(float_, 'x')], conf) for conf in config.get(CONF_ON_VALUE_RANGE, []): rhs = sensor_var.make_value_range_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) @@ -198,7 +198,7 @@ def setup_sensor_core_(sensor_var, config): for template_ in templatable(conf[CONF_BELOW], float_, float_): yield add(trigger.set_max(template_)) - automation.build_automation(trigger, float_, conf) + automation.build_automations(trigger, [(float_, 'x')], conf) mqtt_ = sensor_var.Pget_mqtt() if CONF_EXPIRE_AFTER in config: @@ -232,11 +232,11 @@ SENSOR_IN_RANGE_CONDITION_SCHEMA = vol.All({ @CONDITION_REGISTRY.register(CONF_SENSOR_IN_RANGE, SENSOR_IN_RANGE_CONDITION_SCHEMA) -def sensor_in_range_to_code(config, condition_id, arg_type, template_arg): +def sensor_in_range_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_sensor_in_range_condition(template_arg) - type = SensorInRangeCondition.template(arg_type) + type = SensorInRangeCondition.template(template_arg) cond = Pvariable(condition_id, rhs, type=type) if CONF_ABOVE in config: diff --git a/esphome/components/sensor/template.py b/esphome/components/sensor/template.py index c238aed080..9a85b07f1e 100644 --- a/esphome/components/sensor/template.py +++ b/esphome/components/sensor/template.py @@ -42,13 +42,13 @@ SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_SENSOR_TEMPLATE_PUBLISH, SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA) -def sensor_template_publish_to_code(config, action_id, arg_type, template_arg): +def sensor_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_sensor_publish_action(template_arg) - type = SensorPublishAction.template(arg_type) + type = SensorPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_STATE], arg_type, float_): + for template_ in templatable(config[CONF_STATE], args, float_): yield None add(action.set_state(template_)) yield action diff --git a/esphome/components/stepper/__init__.py b/esphome/components/stepper/__init__.py index ff9cb0252c..e09605fad8 100644 --- a/esphome/components/stepper/__init__.py +++ b/esphome/components/stepper/__init__.py @@ -92,13 +92,13 @@ STEPPER_SET_TARGET_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_STEPPER_SET_TARGET, STEPPER_SET_TARGET_ACTION_SCHEMA) -def stepper_set_target_to_code(config, action_id, arg_type, template_arg): +def stepper_set_target_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_set_target_action(template_arg) - type = SetTargetAction.template(arg_type) + type = SetTargetAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_TARGET], arg_type, int32): + for template_ in templatable(config[CONF_TARGET], args, int32): yield None add(action.set_target(template_)) yield action @@ -112,13 +112,13 @@ STEPPER_REPORT_POSITION_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_STEPPER_REPORT_POSITION, STEPPER_REPORT_POSITION_ACTION_SCHEMA) -def stepper_report_position_to_code(config, action_id, arg_type, template_arg): +def stepper_report_position_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_report_position_action(template_arg) - type = ReportPositionAction.template(arg_type) + type = ReportPositionAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_POSITION], arg_type, int32): + for template_ in templatable(config[CONF_POSITION], args, int32): yield None add(action.set_position(template_)) yield action diff --git a/esphome/components/switch/__init__.py b/esphome/components/switch/__init__.py index cd6fe3e3c7..27527633af 100644 --- a/esphome/components/switch/__init__.py +++ b/esphome/components/switch/__init__.py @@ -9,7 +9,7 @@ from esphome.const import CONF_ICON, CONF_ID, CONF_INTERNAL, CONF_INVERTED, CONF CONF_ON_TURN_OFF, CONF_ON_TURN_ON, CONF_OPTIMISTIC, CONF_TRIGGER_ID from esphome.core import CORE from esphome.cpp_generator import Pvariable, add, get_variable -from esphome.cpp_types import Action, App, Nameable, NoArg, Trigger, esphome_ns +from esphome.cpp_types import Action, App, Nameable, Trigger, esphome_ns PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ @@ -25,8 +25,8 @@ TurnOffAction = switch_ns.class_('TurnOffAction', Action) TurnOnAction = switch_ns.class_('TurnOnAction', Action) SwitchCondition = switch_ns.class_('SwitchCondition', Condition) -SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', Trigger.template(NoArg)) -SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', Trigger.template(NoArg)) +SwitchTurnOnTrigger = switch_ns.class_('SwitchTurnOnTrigger', Trigger.template()) +SwitchTurnOffTrigger = switch_ns.class_('SwitchTurnOffTrigger', Trigger.template()) SWITCH_SCHEMA = cv.MQTT_COMMAND_COMPONENT_SCHEMA.extend({ cv.GenerateID(CONF_MQTT_ID): cv.declare_variable_id(MQTTSwitchComponent), @@ -53,11 +53,11 @@ def setup_switch_core_(switch_var, config): for conf in config.get(CONF_ON_TURN_ON, []): rhs = switch_var.make_switch_turn_on_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_TURN_OFF, []): rhs = switch_var.make_switch_turn_off_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) setup_mqtt_component(switch_var.Pget_mqtt(), config) @@ -83,11 +83,11 @@ SWITCH_TOGGLE_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_SWITCH_TOGGLE, SWITCH_TOGGLE_ACTION_SCHEMA) -def switch_toggle_to_code(config, action_id, arg_type, template_arg): +def switch_toggle_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_toggle_action(template_arg) - type = ToggleAction.template(arg_type) + type = ToggleAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -98,11 +98,11 @@ SWITCH_TURN_OFF_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_SWITCH_TURN_OFF, SWITCH_TURN_OFF_ACTION_SCHEMA) -def switch_turn_off_to_code(config, action_id, arg_type, template_arg): +def switch_turn_off_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_off_action(template_arg) - type = TurnOffAction.template(arg_type) + type = TurnOffAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -113,11 +113,11 @@ SWITCH_TURN_ON_ACTION_SCHEMA = maybe_simple_id({ @ACTION_REGISTRY.register(CONF_SWITCH_TURN_ON, SWITCH_TURN_ON_ACTION_SCHEMA) -def switch_turn_on_to_code(config, action_id, arg_type, template_arg): +def switch_turn_on_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_turn_on_action(template_arg) - type = TurnOnAction.template(arg_type) + type = TurnOnAction.template(template_arg) yield Pvariable(action_id, rhs, type=type) @@ -128,11 +128,11 @@ SWITCH_IS_ON_CONDITION_SCHEMA = maybe_simple_id({ @CONDITION_REGISTRY.register(CONF_SWITCH_IS_ON, SWITCH_IS_ON_CONDITION_SCHEMA) -def switch_is_on_to_code(config, condition_id, arg_type, template_arg): +def switch_is_on_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_switch_is_on_condition(template_arg) - type = SwitchCondition.template(arg_type) + type = SwitchCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) @@ -143,11 +143,11 @@ SWITCH_IS_OFF_CONDITION_SCHEMA = maybe_simple_id({ @CONDITION_REGISTRY.register(CONF_SWITCH_IS_OFF, SWITCH_IS_OFF_CONDITION_SCHEMA) -def switch_is_off_to_code(config, condition_id, arg_type, template_arg): +def switch_is_off_to_code(config, condition_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_switch_is_off_condition(template_arg) - type = SwitchCondition.template(arg_type) + type = SwitchCondition.template(template_arg) yield Pvariable(condition_id, rhs, type=type) diff --git a/esphome/components/switch/template.py b/esphome/components/switch/template.py index 4e4868acc2..d8cf325a17 100644 --- a/esphome/components/switch/template.py +++ b/esphome/components/switch/template.py @@ -8,7 +8,7 @@ from esphome.const import CONF_ASSUMED_STATE, CONF_ID, CONF_LAMBDA, CONF_NAME, C CONF_RESTORE_STATE, CONF_STATE, CONF_TURN_OFF_ACTION, CONF_TURN_ON_ACTION from esphome.cpp_generator import Pvariable, add, get_variable, process_lambda, templatable from esphome.cpp_helpers import setup_component -from esphome.cpp_types import Action, App, Component, NoArg, bool_, optional +from esphome.cpp_types import Action, App, Component, bool_, optional TemplateSwitch = switch.switch_ns.class_('TemplateSwitch', switch.Switch, Component) SwitchPublishAction = switch.switch_ns.class_('SwitchPublishAction', Action) @@ -36,11 +36,11 @@ def to_code(config): yield add(template.set_state_lambda(template_)) if CONF_TURN_OFF_ACTION in config: - automation.build_automation(template.get_turn_off_trigger(), NoArg, - config[CONF_TURN_OFF_ACTION]) + automation.build_automations(template.get_turn_off_trigger(), [], + config[CONF_TURN_OFF_ACTION]) if CONF_TURN_ON_ACTION in config: - automation.build_automation(template.get_turn_on_trigger(), NoArg, - config[CONF_TURN_ON_ACTION]) + automation.build_automations(template.get_turn_on_trigger(), [], + config[CONF_TURN_ON_ACTION]) if CONF_OPTIMISTIC in config: add(template.set_optimistic(config[CONF_OPTIMISTIC])) if CONF_ASSUMED_STATE in config: @@ -62,13 +62,13 @@ SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_SWITCH_TEMPLATE_PUBLISH, SWITCH_TEMPLATE_PUBLISH_ACTION_SCHEMA) -def switch_template_publish_to_code(config, action_id, arg_type, template_arg): +def switch_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_switch_publish_action(template_arg) - type = SwitchPublishAction.template(arg_type) + type = SwitchPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_STATE], arg_type, bool_): + for template_ in templatable(config[CONF_STATE], args, bool_): yield None add(action.set_state(template_)) yield action diff --git a/esphome/components/text_sensor/__init__.py b/esphome/components/text_sensor/__init__.py index e98df74fce..c250ccdc66 100644 --- a/esphome/components/text_sensor/__init__.py +++ b/esphome/components/text_sensor/__init__.py @@ -44,7 +44,7 @@ def setup_text_sensor_core_(text_sensor_var, config): for conf in config.get(CONF_ON_VALUE, []): rhs = text_sensor_var.make_state_trigger() trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, std_string, conf) + automation.build_automations(trigger, [(std_string, 'x')], conf) setup_mqtt_component(text_sensor_var.get_mqtt(), config) diff --git a/esphome/components/text_sensor/template.py b/esphome/components/text_sensor/template.py index 1d6932eef6..a3e0986274 100644 --- a/esphome/components/text_sensor/template.py +++ b/esphome/components/text_sensor/template.py @@ -43,13 +43,13 @@ TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA = cv.Schema({ @ACTION_REGISTRY.register(CONF_TEXT_SENSOR_TEMPLATE_PUBLISH, TEXT_SENSOR_TEMPLATE_PUBLISH_ACTION_SCHEMA) -def text_sensor_template_publish_to_code(config, action_id, arg_type, template_arg): +def text_sensor_template_publish_to_code(config, action_id, template_arg, args): for var in get_variable(config[CONF_ID]): yield None rhs = var.make_text_sensor_publish_action(template_arg) - type = TextSensorPublishAction.template(arg_type) + type = TextSensorPublishAction.template(template_arg) action = Pvariable(action_id, rhs, type=type) - for template_ in templatable(config[CONF_STATE], arg_type, std_string): + for template_ in templatable(config[CONF_STATE], args, std_string): yield None add(action.set_state(template_)) yield action diff --git a/esphome/components/time/__init__.py b/esphome/components/time/__init__.py index f73c7c8375..4c5f807003 100644 --- a/esphome/components/time/__init__.py +++ b/esphome/components/time/__init__.py @@ -10,7 +10,7 @@ from esphome.const import CONF_CRON, CONF_DAYS_OF_MONTH, CONF_DAYS_OF_WEEK, CONF CONF_MINUTES, CONF_MONTHS, CONF_ON_TIME, CONF_SECONDS, CONF_TIMEZONE, CONF_TRIGGER_ID from esphome.core import CORE from esphome.cpp_generator import Pvariable, add -from esphome.cpp_types import App, Component, NoArg, Trigger, esphome_ns +from esphome.cpp_types import App, Component, Trigger, esphome_ns from esphome.py_compat import string_types _LOGGER = logging.getLogger(__name__) @@ -21,7 +21,7 @@ PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({ time_ns = esphome_ns.namespace('time') RealTimeClockComponent = time_ns.class_('RealTimeClockComponent', Component) -CronTrigger = time_ns.class_('CronTrigger', Trigger.template(NoArg), Component) +CronTrigger = time_ns.class_('CronTrigger', Trigger.template(), Component) ESPTime = time_ns.struct('ESPTime') @@ -295,7 +295,7 @@ def setup_time_core_(time_var, config): days_of_week = conf.get(CONF_DAYS_OF_WEEK, [x for x in range(1, 8)]) add(trigger.add_days_of_week(days_of_week)) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) def setup_time(time_var, config): diff --git a/esphome/core_config.py b/esphome/core_config.py index c003158b83..f7a2bfdf70 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -9,14 +9,12 @@ import esphome.config_validation as cv from esphome.const import ARDUINO_VERSION_ESP32_DEV, ARDUINO_VERSION_ESP8266_DEV, \ CONF_ARDUINO_VERSION, CONF_BOARD, CONF_BOARD_FLASH_MODE, CONF_BRANCH, CONF_BUILD_PATH, \ CONF_COMMIT, CONF_ESPHOME, CONF_ESPHOME_CORE_VERSION, CONF_INCLUDES, CONF_LIBRARIES, \ - CONF_LOCAL, \ - CONF_NAME, CONF_ON_BOOT, CONF_ON_LOOP, CONF_ON_SHUTDOWN, CONF_PLATFORM, \ - CONF_PLATFORMIO_OPTIONS, \ - CONF_PRIORITY, CONF_REPOSITORY, CONF_TAG, CONF_TRIGGER_ID, CONF_USE_CUSTOM_CODE, \ - ESPHOME_CORE_VERSION, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_ESP8266_RESTORE_FROM_FLASH + CONF_LOCAL, CONF_NAME, CONF_ON_BOOT, CONF_ON_LOOP, CONF_ON_SHUTDOWN, CONF_PLATFORM, \ + CONF_PLATFORMIO_OPTIONS, CONF_PRIORITY, CONF_REPOSITORY, CONF_TAG, CONF_TRIGGER_ID, \ + CONF_USE_CUSTOM_CODE, ESPHOME_CORE_VERSION, ESP_PLATFORM_ESP32, ESP_PLATFORM_ESP8266, CONF_ESP8266_RESTORE_FROM_FLASH from esphome.core import CORE, EsphomeError from esphome.cpp_generator import Pvariable, RawExpression, add -from esphome.cpp_types import App, NoArg, const_char_ptr, esphome_ns +from esphome.cpp_types import App, const_char_ptr, esphome_ns from esphome.py_compat import text_type _LOGGER = logging.getLogger(__name__) @@ -223,16 +221,16 @@ def to_code(config): for conf in config.get(CONF_ON_BOOT, []): rhs = App.register_component(StartupTrigger.new(conf.get(CONF_PRIORITY))) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) for conf in config.get(CONF_ON_SHUTDOWN, []): trigger = Pvariable(conf[CONF_TRIGGER_ID], ShutdownTrigger.new()) - automation.build_automation(trigger, const_char_ptr, conf) + automation.build_automations(trigger, [(const_char_ptr, 'x')], conf) for conf in config.get(CONF_ON_LOOP, []): rhs = App.register_component(LoopTrigger.new()) trigger = Pvariable(conf[CONF_TRIGGER_ID], rhs) - automation.build_automation(trigger, NoArg, conf) + automation.build_automations(trigger, [], conf) add(App.set_compilation_datetime(RawExpression('__DATE__ ", " __TIME__'))) diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 3bbd2bc2b1..f084be376a 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -420,12 +420,11 @@ def process_lambda(value, # type: Lambda def templatable(value, # type: Any - input_type, # type: Expression + args, # type: List[Tuple[Expression, str]] output_type # type: Optional[Expression] ): if isinstance(value, Lambda): - lambda_ = None - for lambda_ in process_lambda(value, [(input_type, 'x')], return_type=output_type): + for lambda_ in process_lambda(value, args, return_type=output_type): yield None yield lambda_ else: @@ -475,9 +474,11 @@ class MockObj(Expression): continue require.require() - def template(self, args): # type: (Union[TemplateArguments, Expression]) -> MockObj - if not isinstance(args, TemplateArguments): - args = TemplateArguments(args) + def template(self, *args): # type: (Tuple[Union[TemplateArguments, Expression]]) -> MockObj + if len(args) != 1 or not isinstance(args[0], TemplateArguments): + args = TemplateArguments(*args) + else: + args = args[0] obj = MockObj(u'{}{}'.format(self.base, args)) obj.requires.append(self) obj.requires.append(args) @@ -553,9 +554,14 @@ class MockObjClass(MockObj): return True return False - def template(self, args): # type: (Union[TemplateArguments, Expression]) -> MockObjClass - if not isinstance(args, TemplateArguments): - args = TemplateArguments(args) + def template(self, + *args # type: Tuple[Union[TemplateArguments, Expression]] + ): + # type: (...) -> MockObjClass + if len(args) != 1 or not isinstance(args[0], TemplateArguments): + args = TemplateArguments(*args) + else: + args = args[0] new_parents = self._parents[:] new_parents.append(self) obj = MockObjClass(u'{}{}'.format(self.base, args), parents=new_parents) diff --git a/esphome/cpp_types.py b/esphome/cpp_types.py index 8f9a2e2948..01518fe806 100644 --- a/esphome/cpp_types.py +++ b/esphome/cpp_types.py @@ -14,7 +14,6 @@ int32 = global_ns.namespace('int32_t') const_char_ptr = global_ns.namespace('const char *') NAN = global_ns.namespace('NAN') esphome_ns = global_ns # using namespace esphome; -NoArg = esphome_ns.class_('NoArg') App = esphome_ns.App io_ns = esphome_ns.namespace('io') Nameable = esphome_ns.class_('Nameable')