ESPHome  2023.8.3
uart_switch.cpp
Go to the documentation of this file.
1 #include "uart_switch.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace uart {
6 
7 static const char *const TAG = "uart.switch";
8 
10  if (this->state && this->send_every_) {
11  const uint32_t now = millis();
12  if (now - this->last_transmission_ > this->send_every_) {
13  this->write_command_();
14  this->last_transmission_ = now;
15  }
16  }
17 }
18 
20  ESP_LOGD(TAG, "'%s': Sending data...", this->get_name().c_str());
21  this->write_array(this->data_.data(), this->data_.size());
22 }
23 
25  if (!state) {
26  this->publish_state(false);
27  return;
28  }
29 
30  this->publish_state(true);
31  this->write_command_();
32 
33  if (this->send_every_ == 0) {
34  this->publish_state(false);
35  } else {
36  this->last_transmission_ = millis();
37  }
38 }
40  LOG_SWITCH("", "UART Switch", this);
41  if (this->send_every_) {
42  ESP_LOGCONFIG(TAG, " Send Every: %u", this->send_every_);
43  }
44 }
45 
46 } // namespace uart
47 } // namespace esphome
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:27
void dump_config() override
Definition: uart_switch.cpp:39
std::vector< uint8_t > data_
Definition: uart_switch.h:24
void loop() override
Definition: uart_switch.cpp:9
void write_state(bool state) override
Definition: uart_switch.cpp:24
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
bool state
The current reported state of the binary sensor.
Definition: switch.h:53
const StringRef & get_name() const
Definition: entity_base.cpp:10