Implement output button (#3109)

This commit is contained in:
Oxan van Leeuwen 2022-01-24 21:30:48 +01:00 committed by GitHub
parent 4e6bdb31ac
commit 6a2c58fcc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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