Add send_every to uart switch for recurring data (#1514)

This commit is contained in:
Gabe Cook authored and GitHub committed 2021-02-27 19:53:53 -03:00
1 parent 520c4331e3
commit 08ecca86bc
4 files changed
+43 -5

No files matched your search

+28 -4
View File
@@ -6,6 +6,21 @@ namespace uart {
static const char *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);
@@ -13,11 +28,20 @@ void UARTSwitch::write_state(bool state) {
}
this->publish_state(true);
ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
this->write_array(this->data_.data(), this->data_.size());
this->publish_state(false);
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_);
}
}
void UARTSwitch::dump_config() { LOG_SWITCH("", "UART Switch", this); }
} // namespace uart
} // namespace esphome