mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
[template] Store initial option as index in template select
This commit is contained in:
@@ -74,7 +74,8 @@ async def to_code(config):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
cg.add(var.set_optimistic(config[CONF_OPTIMISTIC]))
|
||||||
cg.add(var.set_initial_option(config[CONF_INITIAL_OPTION]))
|
initial_option_index = config[CONF_OPTIONS].index(config[CONF_INITIAL_OPTION])
|
||||||
|
cg.add(var.set_initial_option_index(initial_option_index))
|
||||||
|
|
||||||
if CONF_RESTORE_VALUE in config:
|
if CONF_RESTORE_VALUE in config:
|
||||||
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE]))
|
||||||
|
|||||||
@@ -10,26 +10,21 @@ void TemplateSelect::setup() {
|
|||||||
if (this->f_.has_value())
|
if (this->f_.has_value())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string value;
|
size_t index = this->initial_option_index_;
|
||||||
if (!this->restore_value_) {
|
if (this->restore_value_) {
|
||||||
value = this->initial_option_;
|
|
||||||
ESP_LOGD(TAG, "State from initial: %s", value.c_str());
|
|
||||||
} else {
|
|
||||||
size_t index;
|
|
||||||
this->pref_ = global_preferences->make_preference<size_t>(this->get_preference_hash());
|
this->pref_ = global_preferences->make_preference<size_t>(this->get_preference_hash());
|
||||||
if (!this->pref_.load(&index)) {
|
size_t restored_index;
|
||||||
value = this->initial_option_;
|
if (this->pref_.load(&restored_index) && this->has_index(restored_index)) {
|
||||||
ESP_LOGD(TAG, "State from initial (could not load stored index): %s", value.c_str());
|
index = restored_index;
|
||||||
} else if (!this->has_index(index)) {
|
ESP_LOGD(TAG, "State from restore: %s", this->at(index).value().c_str());
|
||||||
value = this->initial_option_;
|
|
||||||
ESP_LOGD(TAG, "State from initial (restored index %d out of bounds): %s", index, value.c_str());
|
|
||||||
} else {
|
} else {
|
||||||
value = this->at(index).value();
|
ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->at(index).value().c_str());
|
||||||
ESP_LOGD(TAG, "State from restore: %s", value.c_str());
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ESP_LOGD(TAG, "State from initial: %s", this->at(index).value().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
this->publish_state(value);
|
this->publish_state(this->at(index).value());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TemplateSelect::update() {
|
void TemplateSelect::update() {
|
||||||
@@ -65,11 +60,14 @@ void TemplateSelect::dump_config() {
|
|||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
if (this->f_.has_value())
|
if (this->f_.has_value())
|
||||||
return;
|
return;
|
||||||
|
auto initial_option = this->at(this->initial_option_index_);
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
" Optimistic: %s\n"
|
" Optimistic: %s\n"
|
||||||
" Initial Option: %s\n"
|
" Initial Option: %s\n"
|
||||||
" Restore Value: %s",
|
" Restore Value: %s",
|
||||||
YESNO(this->optimistic_), this->initial_option_.c_str(), YESNO(this->restore_value_));
|
YESNO(this->optimistic_),
|
||||||
|
initial_option.has_value() ? initial_option.value().c_str() : LOG_STR_LITERAL("unknown"),
|
||||||
|
YESNO(this->restore_value_));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace template_
|
} // namespace template_
|
||||||
|
|||||||
@@ -19,13 +19,13 @@ class TemplateSelect : public select::Select, public PollingComponent {
|
|||||||
|
|
||||||
Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
|
Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; }
|
||||||
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
void set_optimistic(bool optimistic) { this->optimistic_ = optimistic; }
|
||||||
void set_initial_option(const std::string &initial_option) { this->initial_option_ = initial_option; }
|
void set_initial_option_index(size_t initial_option_index) { this->initial_option_index_ = initial_option_index; }
|
||||||
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void control(const std::string &value) override;
|
void control(const std::string &value) override;
|
||||||
bool optimistic_ = false;
|
bool optimistic_ = false;
|
||||||
std::string initial_option_;
|
size_t initial_option_index_{0};
|
||||||
bool restore_value_ = false;
|
bool restore_value_ = false;
|
||||||
Trigger<std::string> *set_trigger_ = new Trigger<std::string>();
|
Trigger<std::string> *set_trigger_ = new Trigger<std::string>();
|
||||||
optional<std::function<optional<std::string>()>> f_;
|
optional<std::function<optional<std::string>()>> f_;
|
||||||
|
|||||||
Reference in New Issue
Block a user