mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
Add update component action and scripts (#196)
* Add update component action * Add script component
This commit is contained in:
parent
629f2b128e
commit
34fc2147a4
3 changed files with 62 additions and 3 deletions
|
@ -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
|
CONF_OR, CONF_RANGE, CONF_THEN, CONF_TRIGGER_ID
|
||||||
from esphomeyaml.core import ESPHomeYAMLError
|
from esphomeyaml.core import ESPHomeYAMLError
|
||||||
from esphomeyaml.helpers import App, ArrayInitializer, Pvariable, TemplateArguments, add, add_job, \
|
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
|
from esphomeyaml.util import ServiceRegistry
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,6 +57,7 @@ ACTION_REGISTRY = ServiceRegistry()
|
||||||
DelayAction = esphomelib_ns.DelayAction
|
DelayAction = esphomelib_ns.DelayAction
|
||||||
LambdaAction = esphomelib_ns.LambdaAction
|
LambdaAction = esphomelib_ns.LambdaAction
|
||||||
IfAction = esphomelib_ns.IfAction
|
IfAction = esphomelib_ns.IfAction
|
||||||
|
UpdateComponentAction = esphomelib_ns.UpdateComponentAction
|
||||||
Automation = esphomelib_ns.Automation
|
Automation = esphomelib_ns.Automation
|
||||||
|
|
||||||
CONDITIONS_SCHEMA = vol.All(cv.ensure_list, [cv.templatable({
|
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)
|
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):
|
def build_action(full_config, arg_type):
|
||||||
action_id = full_config[CONF_ACTION_ID]
|
action_id = full_config[CONF_ACTION_ID]
|
||||||
key, config = next((k, v) for k, v in full_config.items() if k in ACTION_REGISTRY)
|
key, config = next((k, v) for k, v in full_config.items() if k in ACTION_REGISTRY)
|
||||||
|
|
36
esphomeyaml/components/script.py
Normal file
36
esphomeyaml/components/script.py
Normal file
|
@ -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)
|
|
@ -124,8 +124,9 @@ text_sensor:
|
||||||
icon: mdi:icon
|
icon: mdi:icon
|
||||||
id: version_sensor
|
id: version_sensor
|
||||||
on_value:
|
on_value:
|
||||||
lambda: |-
|
- lambda: |-
|
||||||
ESP_LOGD("main", "The value is %s=%s", x.c_str(), id(version_sensor).value.c_str());
|
ESP_LOGD("main", "The value is %s=%s", x.c_str(), id(version_sensor).value.c_str());
|
||||||
|
- script.execute: my_script
|
||||||
- platform: mqtt_subscribe
|
- platform: mqtt_subscribe
|
||||||
name: "MQTT Subscribe Text"
|
name: "MQTT Subscribe Text"
|
||||||
topic: "the/topic"
|
topic: "the/topic"
|
||||||
|
@ -134,3 +135,8 @@ text_sensor:
|
||||||
name: "Template Text Sensor"
|
name: "Template Text Sensor"
|
||||||
lambda: |-
|
lambda: |-
|
||||||
return {"Hello World"};
|
return {"Hello World"};
|
||||||
|
|
||||||
|
script:
|
||||||
|
- id: my_script
|
||||||
|
then:
|
||||||
|
- lambda: 'ESP_LOGD("main", "Hello World!");'
|
||||||
|
|
Loading…
Reference in a new issue