1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-21 04:32:23 +01:00

Code cleanup fixes for the select component (#3457)

Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
Maurice Makaay
2022-05-11 00:58:28 +02:00
committed by GitHub
parent 235a97ea10
commit 62f9e181e0
5 changed files with 33 additions and 18 deletions

View File

@@ -23,6 +23,10 @@ void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&ca
this->state_callback_.add(std::move(callback));
}
bool Select::has_option(const std::string &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 {
auto options = traits.get_options();
return options.size();
@@ -46,11 +50,12 @@ optional<size_t> Select::active_index() const {
}
optional<std::string> Select::at(size_t index) const {
auto options = traits.get_options();
if (index >= options.size()) {
if (this->has_index(index)) {
auto options = traits.get_options();
return options.at(index);
} else {
return {};
}
return options.at(index);
}
uint32_t Select::hash_base() { return 2812997003UL; }

View File

@@ -28,16 +28,28 @@ class Select : public EntityBase {
void publish_state(const std::string &state);
/// Return whether this select has gotten a full state yet.
/// Return whether this select component has gotten a full state yet.
bool has_state() const { return has_state_; }
/// Instantiate a SelectCall object to modify this select component's state.
SelectCall make_call() { return SelectCall(this); }
void set(const std::string &value) { make_call().set_option(value).perform(); }
// Methods that provide an API to index-based access.
/// Return whether this select component contains the provided option.
bool has_option(const std::string &option) const;
/// Return whether this select component contains the provided index offset.
bool has_index(size_t index) const;
/// Return the number of options in this select component.
size_t size() const;
/// Find the (optional) index offset of the provided option value.
optional<size_t> index_of(const std::string &option) const;
/// Return the (optional) index offset of the currently active option.
optional<size_t> active_index() const;
/// Return the (optional) option value at the provided index offset.
optional<std::string> at(size_t index) const;
void add_on_state_callback(std::function<void(std::string, size_t)> &&callback);

View File

@@ -13,8 +13,6 @@ SelectCall &SelectCall::set_option(const std::string &option) {
SelectCall &SelectCall::set_index(size_t index) { return with_operation(SELECT_OP_SET_INDEX).with_index(index); }
const optional<std::string> &SelectCall::get_option() const { return option_; }
SelectCall &SelectCall::select_next(bool cycle) { return with_operation(SELECT_OP_NEXT).with_cycle(cycle); }
SelectCall &SelectCall::select_previous(bool cycle) { return with_operation(SELECT_OP_PREVIOUS).with_cycle(cycle); }

View File

@@ -24,7 +24,6 @@ class SelectCall {
SelectCall &set_option(const std::string &option);
SelectCall &set_index(size_t index);
const optional<std::string> &get_option() const;
SelectCall &select_next(bool cycle);
SelectCall &select_previous(bool cycle);