mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	[http_request] Implement on_error trigger for requests (#7696)
				
					
				
			This commit is contained in:
		| @@ -6,6 +6,7 @@ from esphome.const import ( | ||||
|     CONF_ESP8266_DISABLE_SSL_SUPPORT, | ||||
|     CONF_ID, | ||||
|     CONF_METHOD, | ||||
|     CONF_ON_ERROR, | ||||
|     CONF_TIMEOUT, | ||||
|     CONF_TRIGGER_ID, | ||||
|     CONF_URL, | ||||
| @@ -185,6 +186,13 @@ HTTP_REQUEST_ACTION_SCHEMA = cv.Schema( | ||||
|         cv.Optional(CONF_ON_RESPONSE): automation.validate_automation( | ||||
|             {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(HttpRequestResponseTrigger)} | ||||
|         ), | ||||
|         cv.Optional(CONF_ON_ERROR): automation.validate_automation( | ||||
|             { | ||||
|                 cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id( | ||||
|                     automation.Trigger.template() | ||||
|                 ) | ||||
|             } | ||||
|         ), | ||||
|         cv.Optional(CONF_MAX_RESPONSE_BUFFER_SIZE, default="1kB"): cv.validate_bytes, | ||||
|     } | ||||
| ) | ||||
| @@ -272,5 +280,9 @@ async def http_request_action_to_code(config, action_id, template_arg, args): | ||||
|             ], | ||||
|             conf, | ||||
|         ) | ||||
|     for conf in config.get(CONF_ON_ERROR, []): | ||||
|         trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) | ||||
|         cg.add(var.register_error_trigger(trigger)) | ||||
|         await automation.build_automation(trigger, [], conf) | ||||
|  | ||||
|     return var | ||||
|   | ||||
| @@ -135,8 +135,8 @@ class HttpRequestComponent : public Component { | ||||
|  | ||||
|  protected: | ||||
|   const char *useragent_{nullptr}; | ||||
|   bool follow_redirects_; | ||||
|   uint16_t redirect_limit_; | ||||
|   bool follow_redirects_{}; | ||||
|   uint16_t redirect_limit_{}; | ||||
|   uint16_t timeout_{4500}; | ||||
|   uint32_t watchdog_timeout_{0}; | ||||
| }; | ||||
| @@ -157,6 +157,8 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> { | ||||
|  | ||||
|   void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); } | ||||
|  | ||||
|   void register_error_trigger(Trigger<> *trigger) { this->error_triggers_.push_back(trigger); } | ||||
|  | ||||
|   void set_max_response_buffer_size(size_t max_response_buffer_size) { | ||||
|     this->max_response_buffer_size_ = max_response_buffer_size; | ||||
|   } | ||||
| @@ -186,6 +188,8 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> { | ||||
|     auto container = this->parent_->start(this->url_.value(x...), this->method_.value(x...), body, headers); | ||||
|  | ||||
|     if (container == nullptr) { | ||||
|       for (auto *trigger : this->error_triggers_) | ||||
|         trigger->trigger(x...); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
| @@ -237,7 +241,8 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> { | ||||
|   std::map<const char *, TemplatableValue<const char *, Ts...>> headers_{}; | ||||
|   std::map<const char *, TemplatableValue<std::string, Ts...>> json_{}; | ||||
|   std::function<void(Ts..., JsonObject)> json_func_{nullptr}; | ||||
|   std::vector<HttpRequestResponseTrigger *> response_triggers_; | ||||
|   std::vector<HttpRequestResponseTrigger *> response_triggers_{}; | ||||
|   std::vector<Trigger<> *> error_triggers_{}; | ||||
|  | ||||
|   size_t max_response_buffer_size_{SIZE_MAX}; | ||||
| }; | ||||
|   | ||||
| @@ -12,6 +12,8 @@ esphome: | ||||
|           url: https://esphome.io | ||||
|           headers: | ||||
|             Content-Type: application/json | ||||
|           on_error: | ||||
|             logger.log: "Request failed" | ||||
|           on_response: | ||||
|             then: | ||||
|               - logger.log: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user