From af3273d93016afe2cb0a7fe46fc69b481819c64a Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Mon, 22 Mar 2021 16:26:10 +1300 Subject: [PATCH] Add trigger for http actions to receive the status code (#1599) --- esphome/components/http_request/__init__.py | 13 +++++++++++++ esphome/components/http_request/http_request.cpp | 5 ++++- esphome/components/http_request/http_request.h | 14 ++++++++++++-- tests/test1.yaml | 6 ++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/esphome/components/http_request/__init__.py b/esphome/components/http_request/__init__.py index 0cac088f3d..dee3fe8f77 100644 --- a/esphome/components/http_request/__init__.py +++ b/esphome/components/http_request/__init__.py @@ -10,6 +10,7 @@ from esphome.const import ( CONF_METHOD, CONF_ARDUINO_VERSION, ARDUINO_VERSION_ESP8266, + CONF_TRIGGER_ID, CONF_URL, ) from esphome.core import CORE, Lambda @@ -23,12 +24,16 @@ HttpRequestComponent = http_request_ns.class_("HttpRequestComponent", cg.Compone HttpRequestSendAction = http_request_ns.class_( "HttpRequestSendAction", automation.Action ) +HttpRequestResponseTrigger = http_request_ns.class_( + "HttpRequestResponseTrigger", automation.Trigger +) CONF_HEADERS = "headers" CONF_USERAGENT = "useragent" CONF_BODY = "body" CONF_JSON = "json" CONF_VERIFY_SSL = "verify_ssl" +CONF_ON_RESPONSE = "on_response" def validate_framework(config): @@ -117,6 +122,9 @@ HTTP_REQUEST_ACTION_SCHEMA = cv.Schema( cv.Schema({cv.string: cv.templatable(cv.string)}) ), cv.Optional(CONF_VERIFY_SSL, default=True): cv.boolean, + cv.Optional(CONF_ON_RESPONSE): automation.validate_automation( + {cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(HttpRequestResponseTrigger)} + ), } ).add_extra(validate_secure_url) HTTP_REQUEST_GET_ACTION_SCHEMA = automation.maybe_conf( @@ -189,4 +197,9 @@ def http_request_action_to_code(config, action_id, template_arg, args): ) cg.add(var.add_header(key, template_)) + for conf in config.get(CONF_ON_RESPONSE, []): + trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID]) + cg.add(var.register_response_trigger(trigger)) + yield automation.build_automation(trigger, [(int, "status_code")], conf) + yield var diff --git a/esphome/components/http_request/http_request.cpp b/esphome/components/http_request/http_request.cpp index 0e73a7956a..79f698441e 100644 --- a/esphome/components/http_request/http_request.cpp +++ b/esphome/components/http_request/http_request.cpp @@ -24,7 +24,7 @@ void HttpRequestComponent::set_url(std::string url) { this->client_.setReuse(true); } -void HttpRequestComponent::send() { +void HttpRequestComponent::send(const std::vector &response_triggers) { bool begin_status = false; const String url = this->url_.c_str(); #ifdef ARDUINO_ARCH_ESP32 @@ -54,6 +54,9 @@ void HttpRequestComponent::send() { } int http_code = this->client_.sendRequest(this->method_, this->body_.c_str()); + for (auto *trigger : response_triggers) + trigger->process(http_code); + if (http_code < 0) { ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(), HTTPClient::errorToString(http_code).c_str()); diff --git a/esphome/components/http_request/http_request.h b/esphome/components/http_request/http_request.h index c69683db0e..f2c688d2f9 100644 --- a/esphome/components/http_request/http_request.h +++ b/esphome/components/http_request/http_request.h @@ -22,6 +22,8 @@ struct Header { const char *value; }; +class HttpRequestResponseTrigger; + class HttpRequestComponent : public Component { public: void dump_config() override; @@ -33,7 +35,7 @@ class HttpRequestComponent : public Component { void set_timeout(uint16_t timeout) { this->timeout_ = timeout; } void set_body(std::string body) { this->body_ = body; } void set_headers(std::list
headers) { this->headers_ = headers; } - void send(); + void send(const std::vector &response_triggers); void close(); const char *get_string(); @@ -69,6 +71,8 @@ template class HttpRequestSendAction : public Action { void set_json(std::function json_func) { this->json_func_ = json_func; } + void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); } + void play(Ts... x) override { this->parent_->set_url(this->url_.value(x...)); this->parent_->set_method(this->method_.value(x...)); @@ -100,7 +104,7 @@ template class HttpRequestSendAction : public Action { } this->parent_->set_headers(headers); } - this->parent_->send(); + this->parent_->send(this->response_triggers_); this->parent_->close(); } @@ -116,6 +120,12 @@ template class HttpRequestSendAction : public Action { std::map> headers_{}; std::map> json_{}; std::function json_func_{nullptr}; + std::vector response_triggers_; +}; + +class HttpRequestResponseTrigger : public Trigger { + public: + void process(int status_code) { this->trigger(status_code); } }; } // namespace http_request diff --git a/tests/test1.yaml b/tests/test1.yaml index 461d8e5da1..bcd4460d3c 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -49,6 +49,12 @@ esphome: Content-Type: application/json body: 'Some data' verify_ssl: false + on_response: + then: + - logger.log: + format: 'Response status: %d' + args: + - status_code build_path: build/test1 packages: