ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
5namespace esphome {
6namespace nextion {
7
8static const char *const TAG = "nextion_sensor";
9
10void 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() || this->nextion_->is_updating())
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_) {
41 }
42
43 this->wave_update_();
44 }
45}
46
47void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
48 if (!this->nextion_->is_setup() || this->nextion_->is_updating())
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
113}
114
115} // namespace nextion
116} // namespace esphome
virtual void add_no_result_to_queue_with_set(NextionComponentBase *component, int32_t state_value)=0
virtual void add_addt_command_to_queue(NextionComponentBase *component)=0
virtual void add_to_get_queue(NextionComponentBase *component)=0
void set_state(float state) override
void process_sensor(const std::string &variable_name, int state) override
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
float state
This member variable stores the last state that has passed through all filters.
Definition sensor.h:131
float raw_state
This member variable stores the current raw state of the sensor, without any filters applied.
Definition sensor.h:137
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7