ESPHome 2026.2.1
Loading...
Searching...
No Matches
menu_item.cpp
Go to the documentation of this file.
1#include "menu_item.h"
2
3#include <cstdio>
4
5namespace esphome {
6namespace display_menu_base {
7
9 switch (type) {
11 return LOG_STR("MENU_ITEM_LABEL");
13 return LOG_STR("MENU_ITEM_MENU");
15 return LOG_STR("MENU_ITEM_BACK");
17 return LOG_STR("MENU_ITEM_SELECT");
19 return LOG_STR("MENU_ITEM_NUMBER");
21 return LOG_STR("MENU_ITEM_SWITCH");
23 return LOG_STR("MENU_ITEM_COMMAND");
25 return LOG_STR("MENU_ITEM_CUSTOM");
26 default:
27 return LOG_STR("UNKNOWN");
28 }
29}
30
32
34
36
37#ifdef USE_SELECT
38std::string MenuItemSelect::get_value_text() const {
39 std::string result;
40
41 if (this->value_getter_.has_value()) {
42 result = this->value_getter_.value()(this);
43 } else {
44 if (this->select_var_ != nullptr) {
45 auto option = this->select_var_->current_option();
46 result.assign(option.c_str(), option.size());
47 }
48 }
49
50 return result;
51}
52
54 bool changed = false;
55
56 if (this->select_var_ != nullptr) {
58 this->on_value_();
59 changed = true;
60 }
61
62 return changed;
63}
64
66 bool changed = false;
67
68 if (this->select_var_ != nullptr) {
70 this->on_value_();
71 changed = true;
72 }
73
74 return changed;
75}
76#endif // USE_SELECT
77
78#ifdef USE_NUMBER
79std::string MenuItemNumber::get_value_text() const {
80 std::string result;
81
82 if (this->value_getter_.has_value()) {
83 result = this->value_getter_.value()(this);
84 } else {
85 char data[32];
86 snprintf(data, sizeof(data), this->format_.c_str(), get_number_value_());
87 result = data;
88 }
89
90 return result;
91}
92
94 bool changed = false;
95
96 if (this->number_var_ != nullptr) {
97 float last = this->number_var_->state;
99
100 if (this->number_var_->state != last) {
101 this->on_value_();
102 changed = true;
103 }
104 }
105
106 return changed;
107}
108
110 bool changed = false;
111
112 if (this->number_var_ != nullptr) {
113 float last = this->number_var_->state;
115
116 if (this->number_var_->state != last) {
117 this->on_value_();
118 changed = true;
119 }
120 }
121
122 return changed;
123}
124
126 float result = 0.0;
127
128 if (this->number_var_ != nullptr) {
129 if (!this->number_var_->has_state() || this->number_var_->state < this->number_var_->traits.get_min_value()) {
130 result = this->number_var_->traits.get_min_value();
131 } else if (this->number_var_->state > this->number_var_->traits.get_max_value()) {
132 result = this->number_var_->traits.get_max_value();
133 } else {
134 result = this->number_var_->state;
135 }
136 }
137
138 return result;
139}
140#endif // USE_NUMBER
141
142#ifdef USE_SWITCH
144 std::string result;
145
146 if (this->value_getter_.has_value()) {
147 result = this->value_getter_.value()(this);
148 } else {
149 result = this->get_switch_state_() ? this->switch_on_text_ : this->switch_off_text_;
150 }
151
152 return result;
153}
154
156
158
159bool MenuItemSwitch::get_switch_state_() const { return (this->switch_var_ != nullptr && this->switch_var_->state); }
160
162 bool changed = false;
163
164 if (this->switch_var_ != nullptr) {
165 this->switch_var_->toggle();
166 this->on_value_();
167 changed = true;
168 }
169
170 return changed;
171}
172#endif // USE_SWITCH
173
175 return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
176}
177
179 this->on_value_();
180 return true;
181}
182
184 this->on_value_();
185 return true;
186}
187
189 this->on_next_();
190 this->on_value_();
191 return true;
192}
193
195 this->on_prev_();
196 this->on_value_();
197 return true;
198}
199
201
203
204} // namespace display_menu_base
205} // namespace esphome
bool has_state() const
CallbackManager< void()> on_next_callbacks_
Definition menu_item.h:186
std::string get_value_text() const override
CallbackManager< void()> on_prev_callbacks_
Definition menu_item.h:187
optional< value_getter_t > value_getter_
Definition menu_item.h:97
CallbackManager< void()> on_leave_callbacks_
Definition menu_item.h:70
CallbackManager< void()> on_value_callbacks_
Definition menu_item.h:71
CallbackManager< void()> on_enter_callbacks_
Definition menu_item.h:69
std::string get_value_text() const override
Definition menu_item.cpp:79
std::string get_value_text() const override
Definition menu_item.cpp:38
std::string get_value_text() const override
NumberCall & number_decrement(bool cycle)
NumberCall & number_increment(bool cycle)
NumberCall make_call()
Definition number.h:35
NumberTraits traits
Definition number.h:39
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
SelectCall & select_next(bool cycle)
SelectCall & select_previous(bool cycle)
SelectCall make_call()
Instantiate a SelectCall object to modify this select component's state.
Definition select.h:53
StringRef current_option() const
Return the currently selected option, or empty StringRef if no state.
Definition select.cpp:41
void toggle()
Toggle this switch.
Definition switch.cpp:29
bool state
The current reported state of the binary sensor.
Definition switch.h:56
uint16_t type
const LogString * menu_item_type_to_string(MenuItemType type)
Returns a string representation of a menu item type suitable for logging.
Definition menu_item.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7