ESPHome 2025.12.2
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 result = this->select_var_->current_option();
46 }
47 }
48
49 return result;
50}
51
53 bool changed = false;
54
55 if (this->select_var_ != nullptr) {
57 this->on_value_();
58 changed = true;
59 }
60
61 return changed;
62}
63
65 bool changed = false;
66
67 if (this->select_var_ != nullptr) {
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_.c_str(), get_number_value_());
86 result = data;
87 }
88
89 return result;
90}
91
93 bool changed = false;
94
95 if (this->number_var_ != nullptr) {
96 float last = this->number_var_->state;
98
99 if (this->number_var_->state != last) {
100 this->on_value_();
101 changed = true;
102 }
103 }
104
105 return changed;
106}
107
109 bool changed = false;
110
111 if (this->number_var_ != nullptr) {
112 float last = this->number_var_->state;
114
115 if (this->number_var_->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->number_var_ != nullptr) {
128 if (!this->number_var_->has_state() || this->number_var_->state < this->number_var_->traits.get_min_value()) {
129 result = this->number_var_->traits.get_min_value();
130 } else if (this->number_var_->state > this->number_var_->traits.get_max_value()) {
131 result = this->number_var_->traits.get_max_value();
132 } else {
133 result = this->number_var_->state;
134 }
135 }
136
137 return result;
138}
139#endif // USE_NUMBER
140
141#ifdef USE_SWITCH
143 std::string result;
144
145 if (this->value_getter_.has_value()) {
146 result = this->value_getter_.value()(this);
147 } else {
148 result = this->get_switch_state_() ? this->switch_on_text_ : this->switch_off_text_;
149 }
150
151 return result;
152}
153
155
157
158bool MenuItemSwitch::get_switch_state_() const { return (this->switch_var_ != nullptr && this->switch_var_->state); }
159
161 bool changed = false;
162
163 if (this->switch_var_ != nullptr) {
164 this->switch_var_->toggle();
165 this->on_value_();
166 changed = true;
167 }
168
169 return changed;
170}
171#endif // USE_SWITCH
172
174 return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
175}
176
178 this->on_value_();
179 return true;
180}
181
183 this->on_value_();
184 return true;
185}
186
188 this->on_next_();
189 this->on_value_();
190 return true;
191}
192
194 this->on_prev_();
195 this->on_value_();
196 return true;
197}
198
200
202
203} // namespace display_menu_base
204} // namespace esphome
bool has_state() const
Definition entity_base.h:93
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:78
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:52
const char * current_option() const
Return the currently selected option (as const char* from flash).
Definition select.cpp:42
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