ESPHome  2024.11.1
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->send_every_) {
11  const uint32_t now = millis();
12  if (now - this->last_transmission_ > this->send_every_) {
13  this->write_command_(this->state);
14  this->last_transmission_ = now;
15  }
16  }
17 }
18 
20  if (state && !this->data_on_.empty()) {
21  ESP_LOGD(TAG, "'%s': Sending on data...", this->get_name().c_str());
22  this->write_array(this->data_on_.data(), this->data_on_.size());
23  }
24  if (!state && !this->data_off_.empty()) {
25  ESP_LOGD(TAG, "'%s': Sending off data...", this->get_name().c_str());
26  this->write_array(this->data_off_.data(), this->data_off_.size());
27  }
28 }
29 
31  if (!this->single_state_) {
32  this->publish_state(state);
33  this->write_command_(state);
34  this->last_transmission_ = millis();
35  return;
36  }
37 
38  if (!state) {
39  this->publish_state(false);
40  return;
41  }
42 
43  this->publish_state(true);
44  this->write_command_(true);
45 
46  if (this->send_every_ == 0) {
47  this->publish_state(false);
48  } else {
49  this->last_transmission_ = millis();
50  }
51 }
52 
54  LOG_SWITCH("", "UART Switch", this);
55  if (this->send_every_) {
56  ESP_LOGCONFIG(TAG, " Send Every: %" PRIu32, this->send_every_);
57  }
58 }
59 
60 } // namespace uart
61 } // namespace esphome
void write_array(const uint8_t *data, size_t len)
Definition: uart.h:21
std::vector< uint8_t > data_on_
Definition: uart_switch.h:27
void write_command_(bool state)
Definition: uart_switch.cpp:19
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
std::vector< uint8_t > data_off_
Definition: uart_switch.h:28
const char *const TAG
Definition: spi.cpp:8
void dump_config() override
Definition: uart_switch.cpp:53
void loop() override
Definition: uart_switch.cpp:9
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void write_state(bool state) override
Definition: uart_switch.cpp:30
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