ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
uart_switch.cpp
Go to the documentation of this file.
1#include "uart_switch.h"
2#include "esphome/core/log.h"
4
5namespace esphome {
6namespace uart {
7
8static const char *const TAG = "uart.switch";
9
11 if (this->send_every_) {
12 const uint32_t now = App.get_loop_component_start_time();
13 if (now - this->last_transmission_ > this->send_every_) {
14 this->write_command_(this->state);
15 this->last_transmission_ = now;
16 }
17 }
18}
19
21 if (state && !this->data_on_.empty()) {
22 ESP_LOGD(TAG, "'%s': Sending on data...", this->get_name().c_str());
23 this->write_array(this->data_on_.data(), this->data_on_.size());
24 }
25 if (!state && !this->data_off_.empty()) {
26 ESP_LOGD(TAG, "'%s': Sending off data...", this->get_name().c_str());
27 this->write_array(this->data_off_.data(), this->data_off_.size());
28 }
29}
30
32 if (!this->single_state_) {
33 this->publish_state(state);
34 this->write_command_(state);
35 this->last_transmission_ = millis();
36 return;
37 }
38
39 if (!state) {
40 this->publish_state(false);
41 return;
42 }
43
44 this->publish_state(true);
45 this->write_command_(true);
46
47 if (this->send_every_ == 0) {
48 this->publish_state(false);
49 } else {
50 this->last_transmission_ = millis();
51 }
52}
53
55 LOG_SWITCH("", "UART Switch", this);
56 if (this->send_every_) {
57 ESP_LOGCONFIG(TAG, " Send Every: %" PRIu32, this->send_every_);
58 }
59}
60
61} // namespace uart
62} // namespace esphome
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:53
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:47
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
std::vector< uint8_t > data_off_
Definition uart_switch.h:28
void write_state(bool state) override
void write_command_(bool state)
std::vector< uint8_t > data_on_
Definition uart_switch.h:27
void dump_config() override
bool state
Definition fan.h:0
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.