From 27453afa4e13189fdc98262db10b055171640e2d Mon Sep 17 00:00:00 2001 From: mtl010957 Date: Tue, 18 Jun 2019 12:42:36 -0500 Subject: [PATCH] Template tilt cover (#577) * Enabling tilt control in the template cover Added test Add tilt option to publish action * Added CONF_TILT to alidation schema per comment * Removed default for CONF_TILT --- esphome/components/mqtt/mqtt_cover.cpp | 3 +++ esphome/components/template/cover/__init__.py | 16 +++++++++++++++- esphome/const.py | 2 ++ tests/test3.yaml | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/esphome/components/mqtt/mqtt_cover.cpp b/esphome/components/mqtt/mqtt_cover.cpp index 56d18a3d22..a414c261f0 100644 --- a/esphome/components/mqtt/mqtt_cover.cpp +++ b/esphome/components/mqtt/mqtt_cover.cpp @@ -73,6 +73,9 @@ void MQTTCoverComponent::send_discovery(JsonObject &root, mqtt::SendDiscoveryCon root["tilt_status_topic"] = this->get_tilt_state_topic(); root["tilt_command_topic"] = this->get_tilt_command_topic(); } + if (traits.get_supports_tilt() && !traits.get_supports_position()) { + config.command_topic = false; + } } std::string MQTTCoverComponent::component_type() const { return "cover"; } diff --git a/esphome/components/template/cover/__init__.py b/esphome/components/template/cover/__init__.py index 808318ac81..13370d749c 100644 --- a/esphome/components/template/cover/__init__.py +++ b/esphome/components/template/cover/__init__.py @@ -4,7 +4,7 @@ from esphome import automation from esphome.components import cover from esphome.const import CONF_ASSUMED_STATE, CONF_CLOSE_ACTION, CONF_CURRENT_OPERATION, CONF_ID, \ CONF_LAMBDA, CONF_OPEN_ACTION, CONF_OPTIMISTIC, CONF_POSITION, CONF_RESTORE_MODE, \ - CONF_STATE, CONF_STOP_ACTION + CONF_STATE, CONF_STOP_ACTION, CONF_TILT, CONF_TILT_ACTION, CONF_TILT_LAMBDA from .. import template_ns TemplateCover = template_ns.class_('TemplateCover', cover.Cover, cg.Component) @@ -24,6 +24,8 @@ CONFIG_SCHEMA = cover.COVER_SCHEMA.extend({ cv.Optional(CONF_OPEN_ACTION): automation.validate_automation(single=True), cv.Optional(CONF_CLOSE_ACTION): automation.validate_automation(single=True), cv.Optional(CONF_STOP_ACTION): automation.validate_automation(single=True), + cv.Optional(CONF_TILT_ACTION): automation.validate_automation(single=True), + cv.Optional(CONF_TILT_LAMBDA): cv.returning_lambda, cv.Optional(CONF_RESTORE_MODE, default='RESTORE'): cv.enum(RESTORE_MODES, upper=True), }).extend(cv.COMPONENT_SCHEMA) @@ -42,6 +44,14 @@ def to_code(config): yield automation.build_automation(var.get_close_trigger(), [], config[CONF_CLOSE_ACTION]) if CONF_STOP_ACTION in config: yield automation.build_automation(var.get_stop_trigger(), [], config[CONF_STOP_ACTION]) + if CONF_TILT_ACTION in config: + yield automation.build_automation(var.get_tilt_trigger(), [(float, 'tilt')], + config[CONF_TILT_ACTION]) + cg.add(var.set_has_tilt(True)) + if CONF_TILT_LAMBDA in config: + tilt_template_ = yield cg.process_lambda(config[CONF_TILT_LAMBDA], [], + return_type=cg.optional.template(float)) + cg.add(var.set_tilt_lambda(tilt_template_)) cg.add(var.set_optimistic(config[CONF_OPTIMISTIC])) cg.add(var.set_assumed_state(config[CONF_ASSUMED_STATE])) @@ -53,6 +63,7 @@ def to_code(config): cv.Exclusive(CONF_STATE, 'pos'): cv.templatable(cover.validate_cover_state), cv.Exclusive(CONF_POSITION, 'pos'): cv.templatable(cv.zero_to_one_float), cv.Optional(CONF_CURRENT_OPERATION): cv.templatable(cover.validate_cover_operation), + cv.Optional(CONF_TILT): cv.templatable(cv.zero_to_one_float), })) def cover_template_publish_to_code(config, action_id, template_arg, args): paren = yield cg.get_variable(config[CONF_ID]) @@ -63,6 +74,9 @@ def cover_template_publish_to_code(config, action_id, template_arg, args): if CONF_POSITION in config: template_ = yield cg.templatable(config[CONF_POSITION], args, float) cg.add(var.set_position(template_)) + if CONF_TILT in config: + template_ = yield cg.templatable(config[CONF_TILT], args, float) + cg.add(var.set_tilt(template_)) if CONF_CURRENT_OPERATION in config: template_ = yield cg.templatable(config[CONF_CURRENT_OPERATION], args, cover.CoverOperation) cg.add(var.set_current_operation(template_)) diff --git a/esphome/const.py b/esphome/const.py index 0fd67c6161..27c4b68799 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -407,6 +407,8 @@ CONF_THEN = 'then' CONF_THRESHOLD = 'threshold' CONF_THROTTLE = 'throttle' CONF_TILT = 'tilt' +CONF_TILT_ACTION = 'tilt_action' +CONF_TILT_LAMBDA = 'tilt_lambda' CONF_TIME = 'time' CONF_TIMEOUT = 'timeout' CONF_TIMES = 'times' diff --git a/tests/test3.yaml b/tests/test3.yaml index 41bae760d8..05fec27c80 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -439,6 +439,14 @@ cover: close_action: - switch.turn_on: gpio_switch2 close_duration: 4.5min + - platform: template + name: Template Cover with Tilt + tilt_lambda: "return 0.5;" + tilt_action: + - output.set_level: + id: out + level: !lambda "return tilt;" + output: - platform: esp8266_pwm