ESPHome 2026.11.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:
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 void set_wave_max_length(int wave_max_length) { this->wave_max_length_ = wave_max_length; }
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
91 protected:
92 std::string variable_name_;
94
95 uint8_t component_id_ = 0;
96#ifdef USE_NEXTION_WAVEFORM
97 uint8_t wave_chan_id_ = UINT8_MAX;
98 std::vector<uint8_t> wave_buffer_;
100#endif // USE_NEXTION_WAVEFORM
101
103};
104} // 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)
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