ESPHome 2025.5.0
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_->state;
46 }
47 }
48
49 return result;
50}
51
53 bool changed = false;
54
55 if (this->select_var_ != nullptr) {
57 changed = true;
58 }
59
60 return changed;
61}
62
64 bool changed = false;
65
66 if (this->select_var_ != nullptr) {
68 changed = true;
69 }
70
71 return changed;
72}
73#endif // USE_SELECT
74
75#ifdef USE_NUMBER
76std::string MenuItemNumber::get_value_text() const {
77 std::string result;
78
79 if (this->value_getter_.has_value()) {
80 result = this->value_getter_.value()(this);
81 } else {
82 char data[32];
83 snprintf(data, sizeof(data), this->format_.c_str(), get_number_value_());
84 result = data;
85 }
86
87 return result;
88}
89
91 bool changed = false;
92
93 if (this->number_var_ != nullptr) {
94 float last = this->number_var_->state;
96
97 if (this->number_var_->state != last) {
98 this->on_value_();
99 changed = true;
100 }
101 }
102
103 return changed;
104}
105
107 bool changed = false;
108
109 if (this->number_var_ != nullptr) {
110 float last = this->number_var_->state;
112
113 if (this->number_var_->state != last) {
114 this->on_value_();
115 changed = true;
116 }
117 }
118
119 return changed;
120}
121
123 float result = 0.0;
124
125 if (this->number_var_ != nullptr) {
126 if (!this->number_var_->has_state() || this->number_var_->state < this->number_var_->traits.get_min_value()) {
127 result = this->number_var_->traits.get_min_value();
128 } else if (this->number_var_->state > this->number_var_->traits.get_max_value()) {
129 result = this->number_var_->traits.get_max_value();
130 } else {
131 result = this->number_var_->state;
132 }
133 }
134
135 return result;
136}
137#endif // USE_NUMBER
138
139#ifdef USE_SWITCH
141 std::string result;
142
143 if (this->value_getter_.has_value()) {
144 result = this->value_getter_.value()(this);
145 } else {
146 result = this->get_switch_state_() ? this->switch_on_text_ : this->switch_off_text_;
147 }
148
149 return result;
150}
151
153
155
156bool MenuItemSwitch::get_switch_state_() const { return (this->switch_var_ != nullptr && this->switch_var_->state); }
157
159 bool changed = false;
160
161 if (this->switch_var_ != nullptr) {
162 this->switch_var_->toggle();
163 this->on_value_();
164 changed = true;
165 }
166
167 return changed;
168}
169#endif // USE_SWITCH
170
172 return (this->value_getter_.has_value()) ? this->value_getter_.value()(this) : "";
173}
174
176 this->on_value_();
177 return true;
178}
179
181 this->on_value_();
182 return true;
183}
184
186 this->on_next_();
187 this->on_value_();
188 return true;
189}
190
192 this->on_prev_();
193 this->on_value_();
194 return true;
195}
196
198
200
201} // namespace display_menu_base
202} // namespace esphome
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:76
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:45
NumberTraits traits
Definition number.h:49
bool has_state() const
Return whether this number has gotten a full state yet.
Definition number.h:52
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
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:42
std::string state
Definition select.h:33
void toggle()
Toggle this switch.
Definition switch.cpp:19
bool state
The current reported state of the binary sensor.
Definition switch.h:53
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:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7