Add trigger for http actions to receive the status code (#1599)

This commit is contained in:
Jesse Hills 2021-03-22 16:26:10 +13:00 committed by GitHub
parent 8f1eb77e05
commit af3273d930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 3 deletions

View File

@ -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

View File

@ -24,7 +24,7 @@ void HttpRequestComponent::set_url(std::string url) {
this->client_.setReuse(true);
}
void HttpRequestComponent::send() {
void HttpRequestComponent::send(const std::vector<HttpRequestResponseTrigger *> &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());

View File

@ -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<Header> headers) { this->headers_ = headers; }
void send();
void send(const std::vector<HttpRequestResponseTrigger *> &response_triggers);
void close();
const char *get_string();
@ -69,6 +71,8 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
void set_json(std::function<void(Ts..., JsonObject &)> 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<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
}
this->parent_->set_headers(headers);
}
this->parent_->send();
this->parent_->send(this->response_triggers_);
this->parent_->close();
}
@ -116,6 +120,12 @@ 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_;
};
class HttpRequestResponseTrigger : public Trigger<int> {
public:
void process(int status_code) { this->trigger(status_code); }
};
} // namespace http_request

View File

@ -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: