mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 00:18:11 +01:00
[http_request] Implement on_error
trigger for requests (#7696)
Some checks failed
CI / Create common environment (push) Has been cancelled
YAML lint / yamllint (push) Has been cancelled
CI / Check black (push) Has been cancelled
CI / Check flake8 (push) Has been cancelled
CI / Check pylint (push) Has been cancelled
CI / Check pyupgrade (push) Has been cancelled
CI / Run script/ci-custom (push) Has been cancelled
CI / Run pytest (push) Has been cancelled
CI / Check clang-format (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 IDF (push) Has been cancelled
CI / Run script/clang-tidy for ESP8266 (push) Has been cancelled
CI / list-components (push) Has been cancelled
CI / Component test (push) Has been cancelled
CI / Split components for testing into 20 groups maximum (push) Has been cancelled
CI / Test split components (push) Has been cancelled
CI / CI Status (push) Has been cancelled
Some checks failed
CI / Create common environment (push) Has been cancelled
YAML lint / yamllint (push) Has been cancelled
CI / Check black (push) Has been cancelled
CI / Check flake8 (push) Has been cancelled
CI / Check pylint (push) Has been cancelled
CI / Check pyupgrade (push) Has been cancelled
CI / Run script/ci-custom (push) Has been cancelled
CI / Run pytest (push) Has been cancelled
CI / Check clang-format (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 IDF (push) Has been cancelled
CI / Run script/clang-tidy for ESP8266 (push) Has been cancelled
CI / list-components (push) Has been cancelled
CI / Component test (push) Has been cancelled
CI / Split components for testing into 20 groups maximum (push) Has been cancelled
CI / Test split components (push) Has been cancelled
CI / CI Status (push) Has been cancelled
This commit is contained in:
parent
aae2ee2ecb
commit
ee3ee3a63b
3 changed files with 22 additions and 3 deletions
|
@ -6,6 +6,7 @@ from esphome.const import (
|
||||||
CONF_ESP8266_DISABLE_SSL_SUPPORT,
|
CONF_ESP8266_DISABLE_SSL_SUPPORT,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_METHOD,
|
CONF_METHOD,
|
||||||
|
CONF_ON_ERROR,
|
||||||
CONF_TIMEOUT,
|
CONF_TIMEOUT,
|
||||||
CONF_TRIGGER_ID,
|
CONF_TRIGGER_ID,
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
|
@ -185,6 +186,13 @@ HTTP_REQUEST_ACTION_SCHEMA = cv.Schema(
|
||||||
cv.Optional(CONF_ON_RESPONSE): automation.validate_automation(
|
cv.Optional(CONF_ON_RESPONSE): automation.validate_automation(
|
||||||
{cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(HttpRequestResponseTrigger)}
|
{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,
|
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,
|
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
|
return var
|
||||||
|
|
|
@ -135,8 +135,8 @@ class HttpRequestComponent : public Component {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const char *useragent_{nullptr};
|
const char *useragent_{nullptr};
|
||||||
bool follow_redirects_;
|
bool follow_redirects_{};
|
||||||
uint16_t redirect_limit_;
|
uint16_t redirect_limit_{};
|
||||||
uint16_t timeout_{4500};
|
uint16_t timeout_{4500};
|
||||||
uint32_t watchdog_timeout_{0};
|
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_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) {
|
void set_max_response_buffer_size(size_t max_response_buffer_size) {
|
||||||
this->max_response_buffer_size_ = 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);
|
auto container = this->parent_->start(this->url_.value(x...), this->method_.value(x...), body, headers);
|
||||||
|
|
||||||
if (container == nullptr) {
|
if (container == nullptr) {
|
||||||
|
for (auto *trigger : this->error_triggers_)
|
||||||
|
trigger->trigger(x...);
|
||||||
return;
|
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<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_;
|
std::vector<HttpRequestResponseTrigger *> response_triggers_{};
|
||||||
|
std::vector<Trigger<> *> error_triggers_{};
|
||||||
|
|
||||||
size_t max_response_buffer_size_{SIZE_MAX};
|
size_t max_response_buffer_size_{SIZE_MAX};
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,6 +12,8 @@ esphome:
|
||||||
url: https://esphome.io
|
url: https://esphome.io
|
||||||
headers:
|
headers:
|
||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
on_error:
|
||||||
|
logger.log: "Request failed"
|
||||||
on_response:
|
on_response:
|
||||||
then:
|
then:
|
||||||
- logger.log:
|
- logger.log:
|
||||||
|
|
Loading…
Reference in a new issue