ESPHome  2022.9.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  if (this->wave_chan_id_ == UINT8_MAX) {
80  if (publish) {
81  this->publish_state(state);
82  } else {
83  this->raw_state = state;
84  this->state = state;
85  this->has_state_ = true;
86  }
87  }
89 
90  ESP_LOGN(TAG, "Wrote state for sensor \"%s\" state %lf", this->variable_name_.c_str(), state);
91 }
92 
94  if (this->nextion_->is_sleeping() || this->wave_buffer_.empty()) {
95  return;
96  }
97 
98 #ifdef NEXTION_PROTOCOL_LOG
99  size_t buffer_to_send =
100  this->wave_buffer_.size() < 255 ? this->wave_buffer_.size() : 255; // ADDT command can only send 255
101 
102  ESP_LOGN(TAG, "wave_update send %zu of %zu value(s) to wave nextion component id %d and wave channel id %d",
103  buffer_to_send, this->wave_buffer_.size(), this->component_id_, this->wave_chan_id_);
104 #endif
105 
106  this->nextion_->add_addt_command_to_queue(this);
107 }
108 
109 } // namespace nextion
110 } // 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:139
float state
This member variable stores the last state that has passed through all filters.
Definition: sensor.h:133
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:72
virtual void add_no_result_to_queue_with_set(NextionComponentBase *component, int state_value)=0
Definition: a4988.cpp:4
void set_state(float state) override
bool state
Definition: fan.h:34