mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	Add optional timeout for wait_until action (#2282)
This commit is contained in:
		| @@ -6,6 +6,7 @@ from esphome.const import ( | ||||
|     CONF_ELSE, | ||||
|     CONF_ID, | ||||
|     CONF_THEN, | ||||
|     CONF_TIMEOUT, | ||||
|     CONF_TRIGGER_ID, | ||||
|     CONF_TYPE_ID, | ||||
|     CONF_TIME, | ||||
| @@ -244,6 +245,9 @@ def validate_wait_until(value): | ||||
|     schema = cv.Schema( | ||||
|         { | ||||
|             cv.Required(CONF_CONDITION): validate_potentially_and_condition, | ||||
|             cv.Optional(CONF_TIMEOUT): cv.templatable( | ||||
|                 cv.positive_time_period_milliseconds | ||||
|             ), | ||||
|         } | ||||
|     ) | ||||
|     if isinstance(value, dict) and CONF_CONDITION in value: | ||||
| @@ -255,6 +259,9 @@ def validate_wait_until(value): | ||||
| async def wait_until_action_to_code(config, action_id, template_arg, args): | ||||
|     conditions = await build_condition(config[CONF_CONDITION], template_arg, args) | ||||
|     var = cg.new_Pvariable(action_id, template_arg, conditions) | ||||
|     if CONF_TIMEOUT in config: | ||||
|         template_ = await cg.templatable(config[CONF_TIMEOUT], args, cg.uint32) | ||||
|         cg.add(var.set_timeout_value(template_)) | ||||
|     await cg.register_component(var, {}) | ||||
|     return var | ||||
|  | ||||
|   | ||||
| @@ -228,6 +228,8 @@ template<typename... Ts> class WaitUntilAction : public Action<Ts...>, public Co | ||||
|  public: | ||||
|   WaitUntilAction(Condition<Ts...> *condition) : condition_(condition) {} | ||||
|  | ||||
|   TEMPLATABLE_VALUE(uint32_t, timeout_value) | ||||
|  | ||||
|   void play_complex(Ts... x) override { | ||||
|     this->num_running_++; | ||||
|     // Check if we can continue immediately. | ||||
| @@ -238,6 +240,12 @@ template<typename... Ts> class WaitUntilAction : public Action<Ts...>, public Co | ||||
|       return; | ||||
|     } | ||||
|     this->var_ = std::make_tuple(x...); | ||||
|  | ||||
|     if (this->timeout_value_.has_value()) { | ||||
|       auto f = std::bind(&WaitUntilAction<Ts...>::play_next_, this, x...); | ||||
|       this->set_timeout("timeout", this->timeout_value_.value(x...), f); | ||||
|     } | ||||
|  | ||||
|     this->loop(); | ||||
|   } | ||||
|  | ||||
| @@ -249,6 +257,8 @@ template<typename... Ts> class WaitUntilAction : public Action<Ts...>, public Co | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     this->cancel_timeout("timeout"); | ||||
|  | ||||
|     this->play_next_tuple_(this->var_); | ||||
|   } | ||||
|  | ||||
| @@ -257,6 +267,8 @@ template<typename... Ts> class WaitUntilAction : public Action<Ts...>, public Co | ||||
|   void play(Ts... x) override { /* ignore - see play_complex */ | ||||
|   } | ||||
|  | ||||
|   void stop() override { this->cancel_timeout("timeout"); } | ||||
|  | ||||
|  protected: | ||||
|   Condition<Ts...> *condition_; | ||||
|   std::tuple<Ts...> var_{}; | ||||
|   | ||||
| @@ -364,8 +364,11 @@ sensor: | ||||
|       then: | ||||
|         - lambda: >- | ||||
|             ESP_LOGD("main", "Got value range %f", x); | ||||
|         - wait_until: wifi.connected | ||||
|         - wait_until: | ||||
|             binary_sensor.is_on: binary_sensor1 | ||||
|             condition: | ||||
|               binary_sensor.is_on: binary_sensor1 | ||||
|             timeout: 1s | ||||
|     on_raw_value: | ||||
|       - lambda: >- | ||||
|           ESP_LOGD("main", "Got raw value %f", x); | ||||
|   | ||||
| @@ -5,10 +5,13 @@ esphome: | ||||
|   board: d1_mini | ||||
|   build_path: build/test3 | ||||
|   on_boot: | ||||
|     - wait_until: | ||||
|         - api.connected | ||||
|         - wifi.connected | ||||
|         - time.has_time | ||||
|     - if: | ||||
|         condition: | ||||
|           - api.connected | ||||
|           - wifi.connected | ||||
|           - time.has_time | ||||
|         then: | ||||
|           - logger.log: "Have time" | ||||
|   includes: | ||||
|     - custom.h | ||||
|  | ||||
| @@ -1291,4 +1294,3 @@ dsmr: | ||||
| daly_bms: | ||||
|   update_interval: 20s | ||||
|   uart_id: uart1 | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user