ESPHome  2024.3.1
nextion_sensor.cpp
Go to the documentation of this file.
1 #include "nextion_sensor.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_sensor";
9 
10 void NextionSensor::process_sensor(const std::string &variable_name, int state) {
11  if (!this->nextion_->is_setup())
12  return;
13 
14  if (this->wave_chan_id_ == UINT8_MAX && this->variable_name_ == variable_name) {
15  this->publish_state(state);
16  ESP_LOGD(TAG, "Processed sensor \"%s\" state %d", variable_name.c_str(), state);
17  }
18 }
19 
21  this->needs_to_send_update_ = true;
22 
23  int wave_state = (int) ((state / (float) this->wave_maxvalue_) * 100);
24 
25  wave_buffer_.push_back(wave_state);
26 
27  if (this->wave_buffer_.size() > (size_t) this->wave_max_length_) {
28  this->wave_buffer_.erase(this->wave_buffer_.begin());
29  }
30 }
31 
33  if (!this->nextion_->is_setup())
34  return;
35 
36  if (this->wave_chan_id_ == UINT8_MAX) {
37  this->nextion_->add_to_get_queue(this);
38  } else {
39  if (this->send_last_value_) {
40  this->add_to_wave_buffer(this->last_value_);
41  }
42 
43  this->wave_update_();
44  }
45 }
46 
47 void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
48  if (!this->nextion_->is_setup())
49  return;
50 
51  if (std::isnan(state))
52  return;
53 
54  if (this->wave_chan_id_ == UINT8_MAX) {
55  if (send_to_nextion) {
56  if (this->nextion_->is_sleeping() || !this->visible_) {
57  this->needs_to_send_update_ = true;
58  } else {
59  this->needs_to_send_update_ = false;
60 
61  if (this->precision_ > 0) {
62  double to_multiply = pow(10, this->precision_);
63  int state_value = (int) (state * to_multiply);
64 
65  this->nextion_->add_no_result_to_queue_with_set(this, (int) state_value);
66  } else {
67  this->nextion_->add_no_result_to_queue_with_set(this, (int) state);
68  }
69  }
70  }
71  } else {
72  if (this->send_last_value_) {
73  this->last_value_ = state; // Update will handle setting the buffer
74  } else {
75  this->add_to_wave_buffer(state);
76  }
77  }
78 
79  float published_state = state;
80  if (this->wave_chan_id_ == UINT8_MAX) {
81  if (publish) {
82  if (this->precision_ > 0) {
83  double to_multiply = pow(10, -this->precision_);
84  published_state = (float) (state * to_multiply);
85  }
86 
87  this->publish_state(published_state);
88  } else {
89  this->raw_state = state;
90  this->state = state;
91  this->has_state_ = true;
92  }
93  }
95 
96  ESP_LOGN(TAG, "Wrote state for sensor \"%s\" state %lf", this->variable_name_.c_str(), published_state);
97 }
98 
100  if (this->nextion_->is_sleeping() || this->wave_buffer_.empty()) {
101  return;
102  }
103 
104 #ifdef NEXTION_PROTOCOL_LOG
105  size_t buffer_to_send =
106  this->wave_buffer_.size() < 255 ? this->wave_buffer_.size() : 255; // ADDT command can only send 255
107 
108  ESP_LOGN(TAG, "wave_update send %zu of %zu value(s) to wave nextion component id %d and wave channel id %d",
109  buffer_to_send, this->wave_buffer_.size(), this->component_id_, this->wave_chan_id_);
110 #endif
111 
112  this->nextion_->add_addt_command_to_queue(this);
113 }
114 
115 } // namespace nextion
116 } // namespace esphome
void add_to_wave_buffer(float state)
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied...
Definition: sensor.h:137
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:131
void process_sensor(const std::string &variable_name, int state) override
virtual void add_to_get_queue(NextionComponentBase *component)=0
virtual void add_addt_command_to_queue(NextionComponentBase *component)=0
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
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
void set_state(float state) override
bool state
Definition: fan.h:34