add on/off options for uart switch (#5539)

Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
Samuel Sieb 2023-10-17 12:07:29 -07:00 committed by GitHub
parent b0ac729a8e
commit c19dbdb02d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 15 deletions

View file

@ -9,14 +9,24 @@ DEPENDENCIES = ["uart"]
UARTSwitch = uart_ns.class_("UARTSwitch", switch.Switch, uart.UARTDevice, cg.Component)
CONF_TURN_OFF = "turn_off"
CONF_TURN_ON = "turn_on"
CONFIG_SCHEMA = (
switch.switch_schema(UARTSwitch, block_inverted=True)
.extend(
{
cv.Required(CONF_DATA): validate_raw_data,
cv.Required(CONF_DATA): cv.Any(
validate_raw_data,
cv.Schema(
{
cv.Optional(CONF_TURN_OFF): validate_raw_data,
cv.Optional(CONF_TURN_ON): validate_raw_data,
}
),
),
cv.Optional(CONF_SEND_EVERY): cv.positive_time_period_milliseconds,
}
},
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
@ -29,9 +39,20 @@ async def to_code(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 isinstance(data, dict):
if data_on := data.get(CONF_TURN_ON):
if isinstance(data_on, bytes):
data_on = [HexInt(x) for x in data_on]
cg.add(var.set_data_on(data_on))
if data_off := data.get(CONF_TURN_OFF):
if isinstance(data_off, bytes):
data_off = [HexInt(x) for x in data_off]
cg.add(var.set_data_off(data_off))
else:
data = config[CONF_DATA]
if isinstance(data, bytes):
data = [HexInt(x) for x in data]
cg.add(var.set_data_on(data))
cg.add(var.set_single_state(True))
if CONF_SEND_EVERY in config:
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))

View file

@ -7,28 +7,41 @@ namespace uart {
static const char *const TAG = "uart.switch";
void UARTSwitch::loop() {
if (this->state && this->send_every_) {
if (this->send_every_) {
const uint32_t now = millis();
if (now - this->last_transmission_ > this->send_every_) {
this->write_command_();
this->write_command_(this->state);
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_command_(bool state) {
if (state && !this->data_on_.empty()) {
ESP_LOGD(TAG, "'%s': Sending on data...", this->get_name().c_str());
this->write_array(this->data_on_.data(), this->data_on_.size());
}
if (!state && !this->data_off_.empty()) {
ESP_LOGD(TAG, "'%s': Sending off data...", this->get_name().c_str());
this->write_array(this->data_off_.data(), this->data_off_.size());
}
}
void UARTSwitch::write_state(bool state) {
if (!this->single_state_) {
this->publish_state(state);
this->write_command_(state);
this->last_transmission_ = millis();
return;
}
if (!state) {
this->publish_state(false);
return;
}
this->publish_state(true);
this->write_command_();
this->write_command_(true);
if (this->send_every_ == 0) {
this->publish_state(false);
@ -36,6 +49,7 @@ void UARTSwitch::write_state(bool state) {
this->last_transmission_ = millis();
}
}
void UARTSwitch::dump_config() {
LOG_SWITCH("", "UART Switch", this);
if (this->send_every_) {

View file

@ -13,15 +13,19 @@ 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_data_on(const std::vector<uint8_t> &data) { this->data_on_ = data; }
void set_data_off(const std::vector<uint8_t> &data) { this->data_off_ = data; }
void set_send_every(uint32_t send_every) { this->send_every_ = send_every; }
void set_single_state(bool single) { this->single_state_ = single; }
void dump_config() override;
protected:
void write_command_();
void write_command_(bool state);
void write_state(bool state) override;
std::vector<uint8_t> data_;
std::vector<uint8_t> data_on_;
std::vector<uint8_t> data_off_;
bool single_state_{false};
uint32_t send_every_;
uint32_t last_transmission_;
};

View file

@ -2588,6 +2588,12 @@ switch:
name: UART Recurring Output
data: [0xDE, 0xAD, 0xBE, 0xEF]
send_every: 1s
- platform: uart
uart_id: uart_0
name: "UART On/Off"
data:
turn_on: "TurnOn\r\n"
turn_off: "TurnOff\r\n"
- platform: template
assumed_state: true
name: Stepper Switch