ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
menu_item.cpp
Go to the documentation of this file.
1#include "menu_item.h"
2
3#include <cstdio>
4
6
8 switch (type) {
10 return LOG_STR("MENU_ITEM_LABEL");
12 return LOG_STR("MENU_ITEM_MENU");
14 return LOG_STR("MENU_ITEM_BACK");
16 return LOG_STR("MENU_ITEM_SELECT");
18 return LOG_STR("MENU_ITEM_NUMBER");
20 return LOG_STR("MENU_ITEM_SWITCH");
22 return LOG_STR("MENU_ITEM_COMMAND");
24 return LOG_STR("MENU_ITEM_CUSTOM");
25 default:
26 return LOG_STR("UNKNOWN");
27 }
28}
29
31
33
35
36#ifdef USE_SELECT
37std::string MenuItemSelect::get_value_text() const {
38 std::string result;
39
40 if (this->value_getter_.has_value()) {
41 result = this->value_getter_.value()(this);
42 } else {
43 if (this->variable_select_ != nullptr) {
44 auto option = this->variable_select_->current_option();
45 result.assign(option.c_str(), option.size());
46 }
47 }
48
49 return result;
50}
51
52bool MenuItemSelect::select_next() {
53 bool changed = false;
54
55 if (this->variable_select_ != nullptr) {
56 this->variable_select_->make_call().select_next(true).perform();
57 this->on_value_();
58 changed = true;
59 }
60
61 return changed;
62}
63
64bool MenuItemSelect::select_prev() {
65 bool changed = false;
66
67 if (this->variable_select_ != nullptr) {
68 this->variable_select_->make_call().select_previous(true).perform();
69 this->on_value_();
70 changed = true;
71 }
72
73 return changed;
74}
75#endif // USE_SELECT
76
77#ifdef USE_NUMBER
78std::string MenuItemNumber::get_value_text() const {
79 std::string result;
80
81 if (this->value_getter_.has_value()) {
82 result = this->value_getter_.value()(this);
83 } else {
84 char data[32];
85 snprintf(data, sizeof(data), this->format_, get_number_value_());
86 result = data;
87 }
88
89 return result;
90}
91
92bool MenuItemNumber::select_next() {
93 bool changed = false;
94
95 if (this->variable_number_ != nullptr) {
96 float last = this->variable_number_->state;
97 this->variable_number_->make_call().number_increment(false).perform();
98
99 if (this->variable_number_->state != last) {
100 this->on_value_();
101 changed = true;
102 }
103 }
104
105 return changed;
106}
107
108bool MenuItemNumber::select_prev() {
109 bool changed = false;
110
111 if (this->variable_number_ != nullptr) {
112 float last = this->variable_number_->state;
113 this->variable_number_->make_call().number_decrement(false).perform();
114
115 if (this->variable_number_->state != last) {
116 this->on_value_();
117 changed = true;
118 }
119 }
120
121 return changed;
122}
123
125 float result = 0.0;
126
127 if (this->variable_number_ != nullptr) {
128 if (!this->variable_number_->has_state() ||
129 this->variable_number_->state < this->variable_number_->traits.get_min_value()) {
130 result = this->variable_number_->traits.get_min_value();
131 } else if (this->variable_number_->state > this->variable_number_->traits.get_max_value()) {
132 result = this->variable_number_->traits.get_max_value();
133 } else {
134 result = this->variable_number_->state;
135 }
136 }
137
138 return result;
139}
140#endif // USE_NUMBER
141
142#ifdef USE_SWITCH
143std::string MenuItemSwitch::get_value_text() const {
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
155bool MenuItemSwitch::select_next() { return this->toggle_switch_(); }
156
157bool MenuItemSwitch::select_prev() { return this->toggle_switch_(); }
158
160 return (this->variable_switch_ != nullptr && this->variable_switch_->state);
161}
162
164 bool changed = false;
165
166 if (this->variable_switch_ != nullptr) {
167 this->variable_switch_->toggle();
168 this->on_value_();
169 changed = true;
170 }
171
172 return changed;
173}
174#endif // USE_SWITCH
175
177 return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
178}
179
181 this->on_value_();
182 return true;
183}
184
186 this->on_value_();
187 return true;
188}
189
191 this->on_next_();
192 this->on_value_();
193 return true;
194}
195
197 this->on_prev_();
198 this->on_value_();
199 return true;
200}
201
203
205
206} // namespace esphome::display_menu_base
CallbackManager< void()> on_next_callbacks_
Definition menu_item.h:185
std::string get_value_text() const override
CallbackManager< void()> on_prev_callbacks_
Definition menu_item.h:186
optional< value_getter_t > value_getter_
Definition menu_item.h:96
CallbackManager< void()> on_leave_callbacks_
Definition menu_item.h:69
CallbackManager< void()> on_value_callbacks_
Definition menu_item.h:70
CallbackManager< void()> on_enter_callbacks_
Definition menu_item.h:68
uint8_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:7
std::span< const uint8_t > data