mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +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: | ||||
|         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: | ||||
|             cg.add(var.set_restore_value(config[CONF_RESTORE_VALUE])) | ||||
|   | ||||
| @@ -10,26 +10,21 @@ void TemplateSelect::setup() { | ||||
|   if (this->f_.has_value()) | ||||
|     return; | ||||
|  | ||||
|   std::string value; | ||||
|   if (!this->restore_value_) { | ||||
|     value = this->initial_option_; | ||||
|     ESP_LOGD(TAG, "State from initial: %s", value.c_str()); | ||||
|   } else { | ||||
|     size_t index; | ||||
|   size_t index = this->initial_option_index_; | ||||
|   if (this->restore_value_) { | ||||
|     this->pref_ = global_preferences->make_preference<size_t>(this->get_preference_hash()); | ||||
|     if (!this->pref_.load(&index)) { | ||||
|       value = this->initial_option_; | ||||
|       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()); | ||||
|     size_t restored_index; | ||||
|     if (this->pref_.load(&restored_index) && this->has_index(restored_index)) { | ||||
|       index = restored_index; | ||||
|       ESP_LOGD(TAG, "State from restore: %s", this->at(index).value().c_str()); | ||||
|     } else { | ||||
|       value = this->at(index).value(); | ||||
|       ESP_LOGD(TAG, "State from restore: %s", value.c_str()); | ||||
|       ESP_LOGD(TAG, "State from initial (could not load or invalid stored index): %s", this->at(index).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() { | ||||
| @@ -65,11 +60,14 @@ void TemplateSelect::dump_config() { | ||||
|   LOG_UPDATE_INTERVAL(this); | ||||
|   if (this->f_.has_value()) | ||||
|     return; | ||||
|   auto initial_option = this->at(this->initial_option_index_); | ||||
|   ESP_LOGCONFIG(TAG, | ||||
|                 "  Optimistic: %s\n" | ||||
|                 "  Initial Option: %s\n" | ||||
|                 "  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_ | ||||
|   | ||||
| @@ -19,13 +19,13 @@ class TemplateSelect : public select::Select, public PollingComponent { | ||||
|  | ||||
|   Trigger<std::string> *get_set_trigger() const { return this->set_trigger_; } | ||||
|   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; } | ||||
|  | ||||
|  protected: | ||||
|   void control(const std::string &value) override; | ||||
|   bool optimistic_ = false; | ||||
|   std::string initial_option_; | ||||
|   size_t initial_option_index_{0}; | ||||
|   bool restore_value_ = false; | ||||
|   Trigger<std::string> *set_trigger_ = new Trigger<std::string>(); | ||||
|   optional<std::function<optional<std::string>()>> f_; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user