mirror of
https://github.com/esphome/esphome.git
synced 2025-10-30 06:33:51 +00:00
Code cleanup fixes for the select component (#3457)
Co-authored-by: Maurice Makaay <mmakaay1@xs4all.net>
This commit is contained in:
@@ -20,9 +20,12 @@ void TemplateSelect::setup() {
|
||||
this->pref_ = global_preferences->make_preference<size_t>(this->get_object_id_hash());
|
||||
if (!this->pref_.load(&index)) {
|
||||
value = this->initial_option_;
|
||||
ESP_LOGD(TAG, "State from initial (could not load): %s", value.c_str());
|
||||
ESP_LOGD(TAG, "State from initial (could not load stored index): %s", value.c_str());
|
||||
} else if (!this->has_index(index)) {
|
||||
value = this->initial_option_;
|
||||
ESP_LOGD(TAG, "State from initial (restored index %d out of bounds): %s", index, value.c_str());
|
||||
} else {
|
||||
value = this->traits.get_options().at(index);
|
||||
value = this->at(index).value();
|
||||
ESP_LOGD(TAG, "State from restore: %s", value.c_str());
|
||||
}
|
||||
}
|
||||
@@ -38,9 +41,8 @@ void TemplateSelect::update() {
|
||||
if (!val.has_value())
|
||||
return;
|
||||
|
||||
auto options = this->traits.get_options();
|
||||
if (std::find(options.begin(), options.end(), *val) == options.end()) {
|
||||
ESP_LOGE(TAG, "lambda returned an invalid option %s", (*val).c_str());
|
||||
if (!this->has_option(*val)) {
|
||||
ESP_LOGE(TAG, "Lambda returned an invalid option: %s", (*val).c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -54,12 +56,11 @@ void TemplateSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
|
||||
if (this->restore_value_) {
|
||||
auto options = this->traits.get_options();
|
||||
size_t index = std::find(options.begin(), options.end(), value) - options.begin();
|
||||
|
||||
this->pref_.save(&index);
|
||||
auto index = this->index_of(value);
|
||||
this->pref_.save(&index.value());
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateSelect::dump_config() {
|
||||
LOG_SELECT("", "Template Select", this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
|
||||
Reference in New Issue
Block a user