add uart button (#5540)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2023-10-17 11:30:32 -07:00 committed by GitHub
parent b12dc98150
commit 6839de69c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 0 deletions

View file

@ -323,6 +323,7 @@ esphome/components/tuya/sensor/* @jesserockz
esphome/components/tuya/switch/* @jesserockz esphome/components/tuya/switch/* @jesserockz
esphome/components/tuya/text_sensor/* @dentra esphome/components/tuya/text_sensor/* @dentra
esphome/components/uart/* @esphome/core esphome/components/uart/* @esphome/core
esphome/components/uart/button/* @ssieb
esphome/components/ufire_ec/* @pvizeli esphome/components/ufire_ec/* @pvizeli
esphome/components/ufire_ise/* @pvizeli esphome/components/ufire_ise/* @pvizeli
esphome/components/ultrasonic/* @OttoWinter esphome/components/ultrasonic/* @OttoWinter

View file

@ -0,0 +1,35 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button, uart
from esphome.const import CONF_DATA
from esphome.core import HexInt
from .. import uart_ns, validate_raw_data
CODEOWNERS = ["@ssieb"]
DEPENDENCIES = ["uart"]
UARTButton = uart_ns.class_("UARTButton", button.Button, uart.UARTDevice, cg.Component)
CONFIG_SCHEMA = (
button.button_schema(UARTButton)
.extend(
{
cv.Required(CONF_DATA): validate_raw_data,
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await button.new_button(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
data = config[CONF_DATA]
if isinstance(data, bytes):
data = [HexInt(x) for x in data]
cg.add(var.set_data(data))

View file

@ -0,0 +1,17 @@
#include "uart_button.h"
#include "esphome/core/log.h"
namespace esphome {
namespace uart {
static const char *const TAG = "uart.button";
void UARTButton::press_action() {
ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
this->write_array(this->data_.data(), this->data_.size());
}
void UARTButton::dump_config() { LOG_BUTTON("", "UART Button", this); }
} // namespace uart
} // namespace esphome

View file

@ -0,0 +1,24 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/button/button.h"
#include <vector>
namespace esphome {
namespace uart {
class UARTButton : public button::Button, public UARTDevice, public Component {
public:
void set_data(const std::vector<uint8_t> &data) { this->data_ = data; }
void dump_config() override;
protected:
void press_action() override;
std::vector<uint8_t> data_;
};
} // namespace uart
} // namespace esphome

View file

@ -3504,6 +3504,10 @@ button:
name: "restart" name: "restart"
query_params: query_params:
name: query params name: query params
- platform: uart
uart_id: uart_0
name: UART button
data: "Pressed\r\n"
ld2410: ld2410:
id: my_ld2410 id: my_ld2410