diff --git a/esphome/components/output/button/__init__.py b/esphome/components/output/button/__init__.py new file mode 100644 index 0000000000..4b81cedc0c --- /dev/null +++ b/esphome/components/output/button/__init__.py @@ -0,0 +1,26 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import button, output +from esphome.const import CONF_ID, CONF_OUTPUT, CONF_DURATION +from .. import output_ns + +OutputButton = output_ns.class_("OutputButton", button.Button, cg.Component) + +CONFIG_SCHEMA = button.BUTTON_SCHEMA.extend( + { + cv.GenerateID(): cv.declare_id(OutputButton), + cv.Required(CONF_OUTPUT): cv.use_id(output.BinaryOutput), + cv.Required(CONF_DURATION): cv.positive_time_period_milliseconds, + } +).extend(cv.COMPONENT_SCHEMA) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + cg.add(var.set_duration(config[CONF_DURATION])) + + output_ = await cg.get_variable(config[CONF_OUTPUT]) + cg.add(var.set_output(output_)) + + await cg.register_component(var, config) + await button.register_button(var, config) diff --git a/esphome/components/output/button/output_button.cpp b/esphome/components/output/button/output_button.cpp new file mode 100644 index 0000000000..4dd7ec249b --- /dev/null +++ b/esphome/components/output/button/output_button.cpp @@ -0,0 +1,21 @@ +#include "output_button.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace output { + +static const char *const TAG = "output.button"; + +void OutputButton::dump_config() { + LOG_BUTTON("", "Output Button", this); + ESP_LOGCONFIG(TAG, " Duration: %.1fs", this->duration_ / 1e3f); +} +void OutputButton::press_action() { + this->output_->turn_on(); + + // Use a named timeout so that it's automatically cancelled if button is pressed again before it's reset + this->set_timeout("reset", this->duration_, [this]() { this->output_->turn_off(); }); +} + +} // namespace output +} // namespace esphome diff --git a/esphome/components/output/button/output_button.h b/esphome/components/output/button/output_button.h new file mode 100644 index 0000000000..8802c9754d --- /dev/null +++ b/esphome/components/output/button/output_button.h @@ -0,0 +1,25 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/button/button.h" +#include "esphome/components/output/binary_output.h" + +namespace esphome { +namespace output { + +class OutputButton : public button::Button, public Component { + public: + void dump_config() override; + + void set_output(BinaryOutput *output) { output_ = output; } + void set_duration(uint32_t duration) { duration_ = duration; } + + protected: + void press_action() override; + + output::BinaryOutput *output_; + uint32_t duration_; +}; + +} // namespace output +} // namespace esphome diff --git a/tests/test3.yaml b/tests/test3.yaml index b22ec72226..2b00a3612e 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -1352,6 +1352,10 @@ daly_bms: uart_id: uart1 button: + - platform: output + id: output_button + output: out + duration: 100ms - platform: wake_on_lan target_mac_address: 12:34:56:78:90:ab name: wol_test_1