ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
uart_switch.cpp
Go to the documentation of this file.
1#include "uart_switch.h"
2#include "esphome/core/log.h"
4
5namespace esphome::uart {
6
7static const char *const TAG = "uart.switch";
8
10 if (this->send_every_) {
11 const uint32_t now = App.get_loop_component_start_time();
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 esphome::uart
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
const StringRef & get_name() const
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:57
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
std::vector< uint8_t > data_off_
Definition uart_switch.h:29
void write_state(bool state) override
void write_command_(bool state)
std::vector< uint8_t > data_on_
Definition uart_switch.h:28
void dump_config() override
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.