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
4 changed files with 60 additions and 15 deletions
+20 -6
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_) {