ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
select_call.cpp
Go to the documentation of this file.
1#include "select_call.h"
2#include "select.h"
3#include "esphome/core/log.h"
4
5namespace esphome::select {
6
7static const char *const TAG = "select";
8
9SelectCall &SelectCall::set_option(const char *option, size_t len) { return this->with_option(option, len); }
10
11SelectCall &SelectCall::set_index(size_t index) { return this->with_index(index); }
12
14
18
20
22
24 this->operation_ = operation;
25 return *this;
26}
27
29 this->cycle_ = cycle;
30 return *this;
31}
32
33SelectCall &SelectCall::with_option(const char *option, size_t len) {
35 // Find the option index - this validates the option exists
36 this->index_ = this->parent_->index_of(option, len);
37 return *this;
38}
39
42 if (index >= this->parent_->size()) {
43 ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", this->parent_->get_name().c_str(), index);
44 this->index_ = {}; // Store nullopt for invalid index
45 } else {
46 this->index_ = index;
47 }
48 return *this;
49}
50
51optional<size_t> SelectCall::calculate_target_index_(const char *name) {
52 const auto &options = this->parent_->traits.get_options();
53 if (options.empty()) {
54 ESP_LOGW(TAG, "'%s' - Select has no options", name);
55 return {};
56 }
57
58 if (this->operation_ == SELECT_OP_FIRST) {
59 return 0;
60 }
61
62 if (this->operation_ == SELECT_OP_LAST) {
63 return options.size() - 1;
64 }
65
66 if (this->operation_ == SELECT_OP_SET) {
67 ESP_LOGD(TAG, "'%s' - Setting", name);
68 if (!this->index_.has_value()) {
69 ESP_LOGW(TAG, "'%s' - No option set", name);
70 return {};
71 }
72 return this->index_.value();
73 }
74
75 // SELECT_OP_NEXT or SELECT_OP_PREVIOUS
76 ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name,
77 this->operation_ == SELECT_OP_NEXT ? LOG_STR_LITERAL("next") : LOG_STR_LITERAL("previous"),
78 this->cycle_ ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("out"));
79
80 const auto size = options.size();
81 if (!this->parent_->has_state()) {
82 return this->operation_ == SELECT_OP_NEXT ? 0 : size - 1;
83 }
84
85 // Use cached active_index_ instead of index_of() lookup
86 const auto active_index = this->parent_->active_index_;
87 if (this->cycle_) {
88 return (size + active_index + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
89 }
90
91 if (this->operation_ == SELECT_OP_PREVIOUS && active_index > 0) {
92 return active_index - 1;
93 }
94
95 if (this->operation_ == SELECT_OP_NEXT && active_index < size - 1) {
96 return active_index + 1;
97 }
98
99 return {}; // Can't navigate further without cycling
100}
101
103 auto *parent = this->parent_;
104 const auto *name = parent->get_name().c_str();
105
106 if (this->operation_ == SELECT_OP_NONE) {
107 ESP_LOGW(TAG, "'%s' - SelectCall performed without selecting an operation", name);
108 return;
109 }
110
111 // Calculate target index (with_index() and with_option() already validate bounds/existence)
112 auto target_index = this->calculate_target_index_(name);
113 if (!target_index.has_value()) {
114 return;
115 }
116
117 auto idx = target_index.value();
118 // All operations use indices, call control() by index to avoid string conversion
119 ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, parent->option_at(idx));
120 parent->control(idx);
121}
122
123} // namespace esphome::select
const StringRef & get_name() const
bool has_state() const
constexpr const char * c_str() const
Definition string_ref.h:69
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
SelectCall & select_next(bool cycle)
SelectCall & with_index(size_t index)
optional< size_t > index_
Definition select_call.h:44
SelectCall & with_cycle(bool cycle)
SelectCall & select_previous(bool cycle)
SelectOperation operation_
Definition select_call.h:45
SelectCall & with_operation(SelectOperation operation)
SelectCall & set_index(size_t index)
SelectCall & set_option(const char *option, size_t len)
SelectCall & with_option(const char *option, size_t len)
size_t size() const
Return the number of options in this select component.
Definition select.cpp:53
optional< size_t > index_of(const char *option, size_t len) const
Find the (optional) index offset of the provided option value.
Definition select.cpp:58
SelectTraits traits
Definition select.h:32
const FixedVector< const char * > & get_options() const
uint8_t options
std::string size_t len
Definition helpers.h:533