mirror of
https://github.com/esphome/esphome.git
synced 2025-01-31 10:10:56 +00:00
Add trigger for http actions to receive the status code (#1599)
This commit is contained in:
parent
8f1eb77e05
commit
af3273d930
@ -10,6 +10,7 @@ from esphome.const import (
|
|||||||
CONF_METHOD,
|
CONF_METHOD,
|
||||||
CONF_ARDUINO_VERSION,
|
CONF_ARDUINO_VERSION,
|
||||||
ARDUINO_VERSION_ESP8266,
|
ARDUINO_VERSION_ESP8266,
|
||||||
|
CONF_TRIGGER_ID,
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
)
|
)
|
||||||
from esphome.core import CORE, Lambda
|
from esphome.core import CORE, Lambda
|
||||||
@ -23,12 +24,16 @@ HttpRequestComponent = http_request_ns.class_("HttpRequestComponent", cg.Compone
|
|||||||
HttpRequestSendAction = http_request_ns.class_(
|
HttpRequestSendAction = http_request_ns.class_(
|
||||||
"HttpRequestSendAction", automation.Action
|
"HttpRequestSendAction", automation.Action
|
||||||
)
|
)
|
||||||
|
HttpRequestResponseTrigger = http_request_ns.class_(
|
||||||
|
"HttpRequestResponseTrigger", automation.Trigger
|
||||||
|
)
|
||||||
|
|
||||||
CONF_HEADERS = "headers"
|
CONF_HEADERS = "headers"
|
||||||
CONF_USERAGENT = "useragent"
|
CONF_USERAGENT = "useragent"
|
||||||
CONF_BODY = "body"
|
CONF_BODY = "body"
|
||||||
CONF_JSON = "json"
|
CONF_JSON = "json"
|
||||||
CONF_VERIFY_SSL = "verify_ssl"
|
CONF_VERIFY_SSL = "verify_ssl"
|
||||||
|
CONF_ON_RESPONSE = "on_response"
|
||||||
|
|
||||||
|
|
||||||
def validate_framework(config):
|
def validate_framework(config):
|
||||||
@ -117,6 +122,9 @@ HTTP_REQUEST_ACTION_SCHEMA = cv.Schema(
|
|||||||
cv.Schema({cv.string: cv.templatable(cv.string)})
|
cv.Schema({cv.string: cv.templatable(cv.string)})
|
||||||
),
|
),
|
||||||
cv.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
|
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)
|
).add_extra(validate_secure_url)
|
||||||
HTTP_REQUEST_GET_ACTION_SCHEMA = automation.maybe_conf(
|
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_))
|
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
|
yield var
|
||||||
|
@ -24,7 +24,7 @@ void HttpRequestComponent::set_url(std::string url) {
|
|||||||
this->client_.setReuse(true);
|
this->client_.setReuse(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpRequestComponent::send() {
|
void HttpRequestComponent::send(const std::vector<HttpRequestResponseTrigger *> &response_triggers) {
|
||||||
bool begin_status = false;
|
bool begin_status = false;
|
||||||
const String url = this->url_.c_str();
|
const String url = this->url_.c_str();
|
||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
@ -54,6 +54,9 @@ void HttpRequestComponent::send() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int http_code = this->client_.sendRequest(this->method_, this->body_.c_str());
|
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) {
|
if (http_code < 0) {
|
||||||
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(),
|
ESP_LOGW(TAG, "HTTP Request failed; URL: %s; Error: %s", this->url_.c_str(),
|
||||||
HTTPClient::errorToString(http_code).c_str());
|
HTTPClient::errorToString(http_code).c_str());
|
||||||
|
@ -22,6 +22,8 @@ struct Header {
|
|||||||
const char *value;
|
const char *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class HttpRequestResponseTrigger;
|
||||||
|
|
||||||
class HttpRequestComponent : public Component {
|
class HttpRequestComponent : public Component {
|
||||||
public:
|
public:
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
@ -33,7 +35,7 @@ class HttpRequestComponent : public Component {
|
|||||||
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
|
void set_timeout(uint16_t timeout) { this->timeout_ = timeout; }
|
||||||
void set_body(std::string body) { this->body_ = body; }
|
void set_body(std::string body) { this->body_ = body; }
|
||||||
void set_headers(std::list<Header> headers) { this->headers_ = headers; }
|
void set_headers(std::list<Header> headers) { this->headers_ = headers; }
|
||||||
void send();
|
void send(const std::vector<HttpRequestResponseTrigger *> &response_triggers);
|
||||||
void close();
|
void close();
|
||||||
const char *get_string();
|
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 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 {
|
void play(Ts... x) override {
|
||||||
this->parent_->set_url(this->url_.value(x...));
|
this->parent_->set_url(this->url_.value(x...));
|
||||||
this->parent_->set_method(this->method_.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_->set_headers(headers);
|
||||||
}
|
}
|
||||||
this->parent_->send();
|
this->parent_->send(this->response_triggers_);
|
||||||
this->parent_->close();
|
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<const char *, Ts...>> headers_{};
|
||||||
std::map<const char *, TemplatableValue<std::string, Ts...>> json_{};
|
std::map<const char *, TemplatableValue<std::string, Ts...>> json_{};
|
||||||
std::function<void(Ts..., JsonObject &)> json_func_{nullptr};
|
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
|
} // namespace http_request
|
||||||
|
@ -49,6 +49,12 @@ esphome:
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
body: 'Some data'
|
body: 'Some data'
|
||||||
verify_ssl: false
|
verify_ssl: false
|
||||||
|
on_response:
|
||||||
|
then:
|
||||||
|
- logger.log:
|
||||||
|
format: 'Response status: %d'
|
||||||
|
args:
|
||||||
|
- status_code
|
||||||
build_path: build/test1
|
build_path: build/test1
|
||||||
|
|
||||||
packages:
|
packages:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user