[select] Inline the trivial Select accessors

This commit is contained in:
J. Nick Koston
2026-08-22 00:40:44 -05:00
parent ef1d77885d
commit 75988f0747
4 changed files with 8 additions and 20 deletions
-12
View File
@@ -8,8 +8,6 @@ namespace esphome::select {
static const char *const TAG = "select";
void Select::publish_state(const std::string &state) { this->publish_state(state.c_str()); }
void Select::publish_state(const char *state) {
auto index = this->index_of(state);
if (index.has_value()) {
@@ -34,16 +32,6 @@ void Select::publish_state(size_t index) {
#endif
}
StringRef Select::current_option() const {
return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
}
bool Select::has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
bool Select::has_option(const char *option) const { return this->index_of(option).has_value(); }
bool Select::has_index(size_t index) const { return index < this->size(); }
size_t Select::size() const {
const auto &options = traits.get_options();
return options.size();
+7 -5
View File
@@ -33,24 +33,26 @@ class Select : public EntityBase {
Select() = default;
~Select() = default;
void publish_state(const std::string &state);
void publish_state(const std::string &state) { this->publish_state(state.c_str()); }
void publish_state(const char *state);
void publish_state(size_t index);
/// Return the currently selected option, or empty StringRef if no state.
/// The returned StringRef points to string literals from codegen (static storage).
/// Traits are set once at startup and valid for the lifetime of the program.
StringRef current_option() const;
StringRef current_option() const {
return this->has_state() ? StringRef(this->option_at(this->active_index_)) : StringRef();
}
/// Instantiate a SelectCall object to modify this select component's state.
SelectCall make_call() { return SelectCall(this); }
/// Return whether this select component contains the provided option.
bool has_option(const std::string &option) const;
bool has_option(const char *option) const;
bool has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
bool has_option(const char *option) const { return this->index_of(option).has_value(); }
/// Return whether this select component contains the provided index offset.
bool has_index(size_t index) const;
bool has_index(size_t index) const { return index < this->size(); }
/// Return the number of options in this select component.
size_t size() const;
@@ -11,6 +11,4 @@ void SelectTraits::set_options(const FixedVector<const char *> &options) {
}
}
const FixedVector<const char *> &SelectTraits::get_options() const { return this->options_; }
} // namespace esphome::select
+1 -1
View File
@@ -9,7 +9,7 @@ class SelectTraits {
public:
void set_options(const std::initializer_list<const char *> &options);
void set_options(const FixedVector<const char *> &options);
const FixedVector<const char *> &get_options() const;
const FixedVector<const char *> &get_options() const { return this->options_; }
protected:
FixedVector<const char *> options_;