ESPHome 2026.5.0-dev
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:
26 virtual ~NextionQueue() = default;
29
30 // Store command for retry if spacing blocked it
31 std::string pending_command; // Empty if command was sent successfully
32};
33
35 public:
36 virtual ~NextionComponentBase() = default;
37
38 void set_variable_name(const std::string &variable_name, const std::string &variable_name_to_send = "") {
39 this->variable_name_ = variable_name;
40 this->variable_name_to_send_ = variable_name_to_send.empty() ? variable_name : variable_name_to_send;
41 }
42
43 virtual void update_component_settings(){};
44 virtual void update_component_settings(bool force_update){};
45
46 virtual void update_component(){};
47 virtual void process_sensor(const std::string &variable_name, int state){};
48 virtual void process_touch(uint8_t page_id, uint8_t component_id, bool on){};
49 virtual void process_text(const std::string &variable_name, const std::string &text_value){};
50 virtual void process_bool(const std::string &variable_name, bool on){};
51
52 virtual void set_state(float state){};
53 virtual void set_state(float state, bool publish){};
54 virtual void set_state(float state, bool publish, bool send_to_nextion){};
55
56 virtual void set_state(bool state){};
57 virtual void set_state(bool state, bool publish){};
58 virtual void set_state(bool state, bool publish, bool send_to_nextion){};
59
60 virtual void set_state(const std::string &state) {}
61 virtual void set_state(const std::string &state, bool publish) {}
62 virtual void set_state(const std::string &state, bool publish, bool send_to_nextion){};
63
64 uint8_t get_component_id() const { return this->component_id_; }
65 void set_component_id(uint8_t component_id) { this->component_id_ = component_id; }
66
67#ifdef USE_NEXTION_WAVEFORM
68 uint8_t get_wave_channel_id() const { return this->wave_chan_id_; }
69 void set_wave_channel_id(uint8_t wave_chan_id) { this->wave_chan_id_ = wave_chan_id; }
70
71 const std::vector<uint8_t> &get_wave_buffer() const { return this->wave_buffer_; }
72 size_t get_wave_buffer_size() const { return this->wave_buffer_.size(); }
73 void clear_wave_buffer(size_t buffer_sent) {
74 if (this->wave_buffer_.size() <= buffer_sent) {
75 this->wave_buffer_.clear();
76 } else {
77 this->wave_buffer_.erase(this->wave_buffer_.begin(), this->wave_buffer_.begin() + buffer_sent);
78 }
79 }
80#endif // USE_NEXTION_WAVEFORM
81
82 const std::string &get_variable_name() const { return this->variable_name_; }
83 const std::string &get_variable_name_to_send() const { return this->variable_name_to_send_; }
85 virtual const char *get_queue_type_string() const { return NEXTION_QUEUE_TYPE_STRINGS[this->get_queue_type()]; }
86 virtual void set_state_from_int(int state_value, bool publish, bool send_to_nextion){};
87 virtual void set_state_from_string(const std::string &state_value, bool publish, bool send_to_nextion){};
88 virtual void send_state_to_nextion(){};
89 bool get_needs_to_send_update() const { return this->needs_to_send_update_; }
90#ifdef USE_NEXTION_WAVEFORM
91 // Remove before 2026.10.0
92 ESPDEPRECATED("Use get_wave_channel_id() instead. Will be removed in 2026.10.0", "2026.4.0")
93 uint8_t get_wave_chan_id() const { return this->get_wave_channel_id(); }
94 void set_wave_max_length(int wave_max_length) { this->wave_max_length_ = wave_max_length; }
95#endif // USE_NEXTION_WAVEFORM
96
97 protected:
98 std::string variable_name_;
100
101 uint8_t component_id_ = 0;
102#ifdef USE_NEXTION_WAVEFORM
103 uint8_t wave_chan_id_ = UINT8_MAX;
104 std::vector<uint8_t> wave_buffer_;
106#endif // USE_NEXTION_WAVEFORM
107
109};
110} // 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)
virtual ~NextionQueue()=default
bool state
Definition fan.h:2
static void uint32_t