ESPHome  2024.10.2
nextion_switch.cpp
Go to the documentation of this file.
1 #include "nextion_switch.h"
2 #include "esphome/core/util.h"
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace nextion {
7 
8 static const char *const TAG = "nextion_switch";
9 
10 void NextionSwitch::process_bool(const std::string &variable_name, bool on) {
11  if (!this->nextion_->is_setup())
12  return;
13  if (this->variable_name_ == variable_name) {
14  this->publish_state(on);
15 
16  ESP_LOGD(TAG, "Processed switch \"%s\" state %s", variable_name.c_str(), state ? "ON" : "OFF");
17  }
18 }
19 
21  if (!this->nextion_->is_setup() || this->nextion_->is_updating())
22  return;
23  this->nextion_->add_to_get_queue(this);
24 }
25 
26 void NextionSwitch::set_state(bool state, bool publish, bool send_to_nextion) {
27  if (!this->nextion_->is_setup() || this->nextion_->is_updating())
28  return;
29 
30  if (send_to_nextion) {
31  if (this->nextion_->is_sleeping() || !this->visible_) {
32  this->needs_to_send_update_ = true;
33  } else {
34  this->needs_to_send_update_ = false;
35  this->nextion_->add_no_result_to_queue_with_set(this, (int) state);
36  }
37  }
38  if (publish) {
39  this->publish_state(state);
40  } else {
41  this->state = state;
42  }
43 
45 
46  ESP_LOGN(TAG, "Updated switch \"%s\" state %s", this->variable_name_.c_str(), ONOFF(state));
47 }
48 
49 void NextionSwitch::write_state(bool state) { this->set_state(state); }
50 
51 } // namespace nextion
52 } // namespace esphome
void set_state(bool state) override
virtual void add_to_get_queue(NextionComponentBase *component)=0
void write_state(bool state) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value)=0
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition: switch.cpp:47
void process_bool(const std::string &variable_name, bool on) override
bool state
The current reported state of the binary sensor.
Definition: switch.h:53