initial commit.

This commit is contained in:
Your Name
2023-02-11 21:10:30 +01:00
parent cbb9131ec8
commit ced79a4af9
63 changed files with 6470 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import switch, uart
from esphome.const import CONF_DATA, CONF_SEND_EVERY
from esphome.core import HexInt
from .. import uart_ns, validate_raw_data
DEPENDENCIES = ["uart"]
UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component)
CONFIG_SCHEMA = (
switch.switch_schema(UARTSwitch, block_inverted=True)
.extend(
{
cv.Required(CONF_DATA): validate_raw_data,
cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await switch.new_switch(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))
if CONF_SEND_EVERY in config:
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))

View File

@@ -0,0 +1,47 @@
#include "uart_switch.h"
#include "esphome/core/log.h"
namespace esphome {
namespace uart {
static const char *const TAG = "uart.switch";
void UARTSwitch::loop() {
if (this->state && this->send_every_) {
const uint32_t now = millis();
if (now - this->last_transmission_ > this->send_every_) {
this->write_command_();
this->last_transmission_ = now;
}
}
}
void UARTSwitch::write_command_() {
ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
this->write_array(this->data_.data(), this->data_.size());
}
void UARTSwitch::write_state(bool state) {
if (!state) {
this->publish_state(false);
return;
}
this->publish_state(true);
this->write_command_();
if (this->send_every_ == 0) {
this->publish_state(false);
} else {
this->last_transmission_ = millis();
}
}
void UARTSwitch::dump_config() {
LOG_SWITCH("", "UART Switch", this);
if (this->send_every_) {
ESP_LOGCONFIG(TAG, " Send Every: %u", this->send_every_);
}
}
} // namespace uart
} // namespace esphome

View File

@@ -0,0 +1,30 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/switch/switch.h"
#include <vector>
namespace esphome {
namespace uart {
class UARTSwitch : public switch_::Switch, public UARTDevice, public Component {
public:
void loop() override;
void set_data(const std::vector<uint8_t> &data) { data_ = data; }
void set_send_every(uint32_t send_every) { this->send_every_ = send_every; }
void dump_config() override;
protected:
void write_command_();
void write_state(bool state) override;
std::vector<uint8_t> data_;
uint32_t send_every_;
uint32_t last_transmission_;
};
} // namespace uart
} // namespace esphome