ESPHome 2026.9.0
Loading...
Searching...
No Matches
nextion_component_base.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <utility>
5#include <vector>
7
8namespace esphome::nextion {
9
18
19static const char *const NEXTION_QUEUE_TYPE_STRINGS[] = {"NO_RESULT", "SENSOR", "BINARY_SENSOR",
20 "SWITCH", "TEXT_SENSOR", "WAVEFORM_SENSOR"};
21
22class NextionComponentBase;
23
25 public:
28
29 // Store command for retry if spacing blocked it
30 std::string pending_command; // Empty if command was sent successfully
31};
32
34 public:
35 virtual ~NextionComponentBase() = default;
36
37 void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send = "") {
38 this->variable_name_ = variable_name;
39 this->variable_name_to_send_ = variable_name_to_send.empty() ? variable_name : variable_name_to_send;
40 }
41
42 virtual void update_component_settings(){};
43 virtual void update_component_settings(bool force_update){};
44
45 virtual void update_component(){};
46 virtual void process_sensor(const std::string &variable_name, int state){};
47 virtual void process_touch(uint8_t page_id, uint8_t component_id, bool on){};
48 virtual void process_text(const std::string &variable_name, const std::string &text_value){};
49 virtual void process_bool(const std::string &variable_name, bool on){};
50
51 virtual void set_state(float state){};
52 virtual void set_state(float state, bool publish){};
53 virtual void set_state(float state, bool publish, bool send_to_nextion){};
54
55 virtual void set_state(bool state){};
56 virtual void set_state(bool state, bool publish){};
57 virtual void set_state(bool state, bool publish, bool send_to_nextion){};
58
59 virtual void set_state(const std::string &state) {}
60 virtual void set_state(const std::string &state, bool publish) {}
61 virtual void set_state(const std::string &state, bool publish, bool send_to_nextion){};
62
63 uint8_t get_component_id() const { return this->component_id_; }
64 void set_component_id(uint8_t component_id) { this->component_id_ = component_id; }
65
66#ifdef USE_NEXTION_WAVEFORM
67 uint8_t get_wave_channel_id() const { return this->wave_chan_id_; }
68 void set_wave_channel_id(uint8_t wave_chan_id) { this->wave_chan_id_ = wave_chan_id; }
69
70 const std::vector<uint8_t> &get_wave_buffer() const { return this->wave_buffer_; }
71 size_t get_wave_buffer_size() const { return this->wave_buffer_.size(); }
72 void clear_wave_buffer(size_t buffer_sent) {
73 if (this->wave_buffer_.size() <= buffer_sent) {
74 this->wave_buffer_.clear();
75 } else {
76 this->wave_buffer_.erase(this->wave_buffer_.begin(), this->wave_buffer_.begin() + buffer_sent);
77 }
78 }
79#endif // USE_NEXTION_WAVEFORM
80
81 const std::string &get_variable_name() const { return this->variable_name_; }
82 const std::string &get_variable_name_to_send() const { return this->variable_name_to_send_; }
84 virtual const char *get_queue_type_string() const { return NEXTION_QUEUE_TYPE_STRINGS[this->get_queue_type()]; }
85 virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion){};
86 virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion){};
87 virtual void send_state_to_nextion(){};
88 bool get_needs_to_send_update() const { return this->needs_to_send_update_; }
89#ifdef USE_NEXTION_WAVEFORM
90 // Remove before 2026.10.0
91 ESPDEPRECATED("Use get_wave_channel_id() instead. Will be removed in 2026.10.0", "2026.4.0")
92 uint8_t get_wave_chan_id() const { return this->get_wave_channel_id(); }
93 void set_wave_max_length(int wave_max_length) { this->wave_max_length_ = wave_max_length; }
94#endif // USE_NEXTION_WAVEFORM
95
96 protected:
97 std::string variable_name_;
99
100 uint8_t component_id_ = 0;
101#ifdef USE_NEXTION_WAVEFORM
102 uint8_t wave_chan_id_ = UINT8_MAX;
103 std::vector<uint8_t> wave_buffer_;
105#endif // USE_NEXTION_WAVEFORM
106
108};
109} // namespace esphome::nextion
virtual void set_state(bool state, bool publish, bool send_to_nextion)
const std::string & get_variable_name() const
virtual void set_state(const std::string &state, bool publish)
virtual void process_sensor(const std::string &variable_name, int state)
virtual NextionQueueType get_queue_type() const
virtual void process_touch(uint8_t page_id, uint8_t component_id, bool on)
virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion)
virtual void set_state(bool state, bool publish)
virtual void set_state(const std::string &state, bool publish, bool send_to_nextion)
ESPDEPRECATED("Use get_wave_channel_id() instead. Will be removed in 2026.10.0", "2026.4.0") uint8_t get_wave_chan_id() const
virtual const char * get_queue_type_string() const
const std::vector< uint8_t > & get_wave_buffer() const
virtual void set_state(float state, bool publish)
virtual void process_bool(const std::string &variable_name, bool on)
virtual void set_state(float state, bool publish, bool send_to_nextion)
virtual void process_text(const std::string &variable_name, const std::string &text_value)
virtual void set_state(const std::string &state)
const std::string & get_variable_name_to_send() const
void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send="")
virtual void update_component_settings(bool force_update)
virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion)
bool state
Definition fan.h:2
static void uint32_t