ESPHome  2024.4.0
nextion_binarysensor.cpp
Go to the documentation of this file.
1 #include "nextion_binarysensor.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_binarysensor";
9 
10 void NextionBinarySensor::process_bool(const std::string &variable_name, bool state) {
11  if (!this->nextion_->is_setup())
12  return;
13 
14  if (this->variable_name_.empty()) // This is a touch component
15  return;
16 
17  if (this->variable_name_ == variable_name) {
18  this->publish_state(state);
19  ESP_LOGD(TAG, "Processed binarysensor \"%s\" state %s", variable_name.c_str(), state ? "ON" : "OFF");
20  }
21 }
22 
23 void NextionBinarySensor::process_touch(uint8_t page_id, uint8_t component_id, bool state) {
24  if (this->page_id_ == page_id && this->component_id_ == component_id) {
25  this->publish_state(state);
26  }
27 }
28 
30  if (!this->nextion_->is_setup())
31  return;
32 
33  if (this->variable_name_.empty()) // This is a touch component
34  return;
35 
36  this->nextion_->add_to_get_queue(this);
37 }
38 
39 void NextionBinarySensor::set_state(bool state, bool publish, bool send_to_nextion) {
40  if (!this->nextion_->is_setup())
41  return;
42 
43  if (this->component_id_ == 0) // This is a legacy touch component
44  return;
45 
46  if (send_to_nextion) {
47  if (this->nextion_->is_sleeping() || !this->visible_) {
48  this->needs_to_send_update_ = true;
49  } else {
50  this->needs_to_send_update_ = false;
51  this->nextion_->add_no_result_to_queue_with_set(this, (int) state);
52  }
53  }
54 
55  if (publish) {
56  this->publish_state(state);
57  } else {
58  this->state = state;
59  this->has_state_ = true;
60  }
61 
63 
64  ESP_LOGN(TAG, "Wrote state for sensor \"%s\" state %s", this->variable_name_.c_str(),
65  ONOFF(this->variable_name_.c_str()));
66 }
67 
68 } // namespace nextion
69 } // namespace esphome
void process_bool(const std::string &variable_name, bool state) override
virtual void add_to_get_queue(NextionComponentBase *component)=0
bool state
The current reported state of the binary sensor.
Definition: binary_sensor.h:61
void publish_state(bool state)
Publish a new state to the front-end.
void process_touch(uint8_t page_id, uint8_t component_id, bool state) override
virtual void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value)=0
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool state
Definition: fan.h:34