mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[script] Reduce RAM usage by storing names in flash (#10941)
This commit is contained in:
		| @@ -12,6 +12,7 @@ from esphome.cpp_generator import (  # noqa: F401 | ||||
|     ArrayInitializer, | ||||
|     Expression, | ||||
|     LineComment, | ||||
|     LogStringLiteral, | ||||
|     MockObj, | ||||
|     MockObjClass, | ||||
|     Pvariable, | ||||
|   | ||||
| @@ -124,7 +124,7 @@ async def to_code(config): | ||||
|         template, func_args = parameters_to_template(conf[CONF_PARAMETERS]) | ||||
|         trigger = cg.new_Pvariable(conf[CONF_ID], template) | ||||
|         # Add a human-readable name to the script | ||||
|         cg.add(trigger.set_name(conf[CONF_ID].id)) | ||||
|         cg.add(trigger.set_name(cg.LogStringLiteral(conf[CONF_ID].id))) | ||||
|  | ||||
|         if CONF_MAX_RUNS in conf: | ||||
|             cg.add(trigger.set_max_runs(conf[CONF_MAX_RUNS])) | ||||
|   | ||||
| @@ -48,14 +48,14 @@ template<typename... Ts> class Script : public ScriptLogger, public Trigger<Ts.. | ||||
|   } | ||||
|  | ||||
|   // Internal function to give scripts readable names. | ||||
|   void set_name(const std::string &name) { name_ = name; } | ||||
|   void set_name(const LogString *name) { name_ = name; } | ||||
|  | ||||
|  protected: | ||||
|   template<int... S> void execute_tuple_(const std::tuple<Ts...> &tuple, seq<S...> /*unused*/) { | ||||
|     this->execute(std::get<S>(tuple)...); | ||||
|   } | ||||
|  | ||||
|   std::string name_; | ||||
|   const LogString *name_{nullptr}; | ||||
| }; | ||||
|  | ||||
| /** A script type for which only a single instance at a time is allowed. | ||||
| @@ -68,7 +68,7 @@ template<typename... Ts> class SingleScript : public Script<Ts...> { | ||||
|   void execute(Ts... x) override { | ||||
|     if (this->is_action_running()) { | ||||
|       this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' is already running! (mode: single)"), | ||||
|                       this->name_.c_str()); | ||||
|                       LOG_STR_ARG(this->name_)); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -85,7 +85,7 @@ template<typename... Ts> class RestartScript : public Script<Ts...> { | ||||
|  public: | ||||
|   void execute(Ts... x) override { | ||||
|     if (this->is_action_running()) { | ||||
|       this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' restarting (mode: restart)"), this->name_.c_str()); | ||||
|       this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' restarting (mode: restart)"), LOG_STR_ARG(this->name_)); | ||||
|       this->stop_action(); | ||||
|     } | ||||
|  | ||||
| @@ -105,12 +105,12 @@ template<typename... Ts> class QueueingScript : public Script<Ts...>, public Com | ||||
|       // num_runs_ + 1 | ||||
|       if (this->max_runs_ != 0 && this->num_runs_ + 1 >= this->max_runs_) { | ||||
|         this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of queued runs exceeded!"), | ||||
|                         this->name_.c_str()); | ||||
|                         LOG_STR_ARG(this->name_)); | ||||
|         return; | ||||
|       } | ||||
|  | ||||
|       this->esp_logd_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' queueing new instance (mode: queued)"), | ||||
|                       this->name_.c_str()); | ||||
|                       LOG_STR_ARG(this->name_)); | ||||
|       this->num_runs_++; | ||||
|       this->var_queue_.push(std::make_tuple(x...)); | ||||
|       return; | ||||
| @@ -157,7 +157,7 @@ template<typename... Ts> class ParallelScript : public Script<Ts...> { | ||||
|   void execute(Ts... x) override { | ||||
|     if (this->max_runs_ != 0 && this->automation_parent_->num_running() >= this->max_runs_) { | ||||
|       this->esp_logw_(__LINE__, ESPHOME_LOG_FORMAT("Script '%s' maximum number of parallel runs exceeded!"), | ||||
|                       this->name_.c_str()); | ||||
|                       LOG_STR_ARG(this->name_)); | ||||
|       return; | ||||
|     } | ||||
|     this->trigger(x...); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user