From 34fc2147a45e3499f1a92cb124389dfb87044a14 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Sat, 20 Oct 2018 12:58:02 +0200 Subject: [PATCH] Add update component action and scripts (#196) * Add update component action * Add script component --- esphomeyaml/automation.py | 19 ++++++++++++++++- esphomeyaml/components/script.py | 36 ++++++++++++++++++++++++++++++++ tests/test2.yaml | 10 +++++++-- 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 esphomeyaml/components/script.py diff --git a/esphomeyaml/automation.py b/esphomeyaml/automation.py index 95f432f380..08077aaf2b 100644 --- a/esphomeyaml/automation.py +++ b/esphomeyaml/automation.py @@ -8,7 +8,7 @@ from esphomeyaml.const import CONF_ABOVE, CONF_ACTION_ID, CONF_AND, CONF_AUTOMAT CONF_OR, CONF_RANGE, CONF_THEN, CONF_TRIGGER_ID from esphomeyaml.core import ESPHomeYAMLError from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, TemplateArguments, add, add_job, \ - esphomelib_ns, float_, process_lambda, templatable, uint32 + esphomelib_ns, float_, process_lambda, templatable, uint32, get_variable from esphomeyaml.util import ServiceRegistry @@ -57,6 +57,7 @@ ACTION_REGISTRY = ServiceRegistry() DelayAction = esphomelib_ns.DelayAction LambdaAction = esphomelib_ns.LambdaAction IfAction = esphomelib_ns.IfAction +UpdateComponentAction = esphomelib_ns.UpdateComponentAction Automation = esphomelib_ns.Automation CONDITIONS_SCHEMA = vol.All(cv.ensure_list, [cv.templatable({ @@ -200,6 +201,22 @@ def lambda_action_to_code(config, action_id, arg_type): yield Pvariable(action_id, rhs, type=type) +CONF_COMPONENT_UPDATE = 'component.update' +COMPONENT_UPDATE_ACTION_SCHEMA = maybe_simple_id({ + vol.Required(CONF_ID): cv.use_variable_id(None), +}) + + +@ACTION_REGISTRY.register(CONF_COMPONENT_UPDATE, COMPONENT_UPDATE_ACTION_SCHEMA) +def component_update_action_to_code(config, action_id, arg_type): + template_arg = TemplateArguments(arg_type) + for var in get_variable(config[CONF_ID]): + yield None + rhs = UpdateComponentAction.new(var) + type = UpdateComponentAction.template(template_arg) + yield Pvariable(action_id, rhs, type=type) + + def build_action(full_config, arg_type): action_id = full_config[CONF_ACTION_ID] key, config = next((k, v) for k, v in full_config.items() if k in ACTION_REGISTRY) diff --git a/esphomeyaml/components/script.py b/esphomeyaml/components/script.py new file mode 100644 index 0000000000..dc11820228 --- /dev/null +++ b/esphomeyaml/components/script.py @@ -0,0 +1,36 @@ +import voluptuous as vol + +from esphomeyaml import automation +from esphomeyaml.automation import ACTION_REGISTRY, maybe_simple_id +import esphomeyaml.config_validation as cv +from esphomeyaml.const import CONF_ID +from esphomeyaml.helpers import NoArg, Pvariable, TemplateArguments, esphomelib_ns, get_variable + +Script = esphomelib_ns.Script +ScriptExecuteAction = esphomelib_ns.ScriptExecuteAction + +CONFIG_SCHEMA = vol.All(cv.ensure_list, [automation.validate_automation({ + vol.Required(CONF_ID): cv.declare_variable_id(Script), +})]) + + +def to_code(config): + for conf in config: + trigger = Pvariable(conf[CONF_ID], Script.new()) + automation.build_automation(trigger, NoArg, conf) + + +CONF_SCRIPT_EXECUTE = 'script.execute' +SCRIPT_EXECUTE_ACTION_SCHEMA = maybe_simple_id({ + vol.Required(CONF_ID): cv.use_variable_id(Script), +}) + + +@ACTION_REGISTRY.register(CONF_SCRIPT_EXECUTE, SCRIPT_EXECUTE_ACTION_SCHEMA) +def script_execute_action_to_code(config, action_id, arg_type): + template_arg = TemplateArguments(arg_type) + for var in get_variable(config[CONF_ID]): + yield None + rhs = var.make_execute_action(template_arg) + type = ScriptExecuteAction.template(arg_type) + yield Pvariable(action_id, rhs, type=type) diff --git a/tests/test2.yaml b/tests/test2.yaml index 3e091f9b3e..05d9fc0501 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -124,8 +124,9 @@ text_sensor: icon: mdi:icon id: version_sensor on_value: - lambda: |- - ESP_LOGD("main", "The value is %s=%s", x.c_str(), id(version_sensor).value.c_str()); + - lambda: |- + ESP_LOGD("main", "The value is %s=%s", x.c_str(), id(version_sensor).value.c_str()); + - script.execute: my_script - platform: mqtt_subscribe name: "MQTT Subscribe Text" topic: "the/topic" @@ -134,3 +135,8 @@ text_sensor: name: "Template Text Sensor" lambda: |- return {"Hello World"}; + +script: + - id: my_script + then: + - lambda: 'ESP_LOGD("main", "Hello World!");'