mirror of
https://github.com/esphome/esphome.git
synced 2024-12-23 05:54:56 +01:00
commit
038f24fcea
4 changed files with 65 additions and 21 deletions
|
@ -1,17 +1,17 @@
|
||||||
import esphome.codegen as cg
|
|
||||||
import esphome.config_validation as cv
|
|
||||||
from esphome import automation
|
from esphome import automation
|
||||||
|
import esphome.codegen as cg
|
||||||
|
from esphome.components import esp32
|
||||||
|
import esphome.config_validation as cv
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
__version__,
|
CONF_ESP8266_DISABLE_SSL_SUPPORT,
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
CONF_TIMEOUT,
|
|
||||||
CONF_METHOD,
|
CONF_METHOD,
|
||||||
|
CONF_TIMEOUT,
|
||||||
CONF_TRIGGER_ID,
|
CONF_TRIGGER_ID,
|
||||||
CONF_URL,
|
CONF_URL,
|
||||||
CONF_ESP8266_DISABLE_SSL_SUPPORT,
|
__version__,
|
||||||
)
|
)
|
||||||
from esphome.core import Lambda, CORE
|
from esphome.core import CORE, Lambda
|
||||||
from esphome.components import esp32
|
|
||||||
|
|
||||||
DEPENDENCIES = ["network"]
|
DEPENDENCIES = ["network"]
|
||||||
AUTO_LOAD = ["json"]
|
AUTO_LOAD = ["json"]
|
||||||
|
@ -99,7 +99,7 @@ CONFIG_SCHEMA = cv.All(
|
||||||
cv.Optional(CONF_FOLLOW_REDIRECTS, True): cv.boolean,
|
cv.Optional(CONF_FOLLOW_REDIRECTS, True): cv.boolean,
|
||||||
cv.Optional(CONF_REDIRECT_LIMIT, 3): cv.int_,
|
cv.Optional(CONF_REDIRECT_LIMIT, 3): cv.int_,
|
||||||
cv.Optional(
|
cv.Optional(
|
||||||
CONF_TIMEOUT, default="5s"
|
CONF_TIMEOUT, default="4.5s"
|
||||||
): cv.positive_time_period_milliseconds,
|
): cv.positive_time_period_milliseconds,
|
||||||
cv.SplitDefault(CONF_ESP8266_DISABLE_SSL_SUPPORT, esp8266=False): cv.All(
|
cv.SplitDefault(CONF_ESP8266_DISABLE_SSL_SUPPORT, esp8266=False): cv.All(
|
||||||
cv.only_on_esp8266, cv.boolean
|
cv.only_on_esp8266, cv.boolean
|
||||||
|
|
|
@ -80,7 +80,7 @@ class HttpRequestComponent : public Component {
|
||||||
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_{5000};
|
uint16_t timeout_{4500};
|
||||||
uint32_t watchdog_timeout_{0};
|
uint32_t watchdog_timeout_{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
||||||
esp_http_client_set_header(client, header.name, header.value);
|
esp_http_client_set_header(client, header.name, header.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int body_len = body.length();
|
const int body_len = body.length();
|
||||||
|
|
||||||
esp_err_t err = esp_http_client_open(client, body_len);
|
esp_err_t err = esp_http_client_open(client, body_len);
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
|
@ -109,20 +109,64 @@ std::shared_ptr<HttpContainer> HttpRequestIDF::start(std::string url, std::strin
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
container->content_length = esp_http_client_fetch_headers(client);
|
auto is_ok = [](int code) { return code >= HttpStatus_Ok && code < HttpStatus_MultipleChoices; };
|
||||||
const auto status_code = esp_http_client_get_status_code(client);
|
|
||||||
container->status_code = status_code;
|
|
||||||
|
|
||||||
if (status_code < 200 || status_code >= 300) {
|
container->content_length = esp_http_client_fetch_headers(client);
|
||||||
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), status_code);
|
container->status_code = esp_http_client_get_status_code(client);
|
||||||
|
if (is_ok(container->status_code)) {
|
||||||
|
container->duration_ms = millis() - start;
|
||||||
|
return container;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->follow_redirects_) {
|
||||||
|
auto is_redirect = [](int code) {
|
||||||
|
return code == HttpStatus_MovedPermanently || code == HttpStatus_Found || code == HttpStatus_SeeOther ||
|
||||||
|
code == HttpStatus_TemporaryRedirect || code == HttpStatus_PermanentRedirect;
|
||||||
|
};
|
||||||
|
auto num_redirects = this->redirect_limit_;
|
||||||
|
while (is_redirect(container->status_code) && num_redirects > 0) {
|
||||||
|
err = esp_http_client_set_redirection(client);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_http_client_set_redirection failed: %s", esp_err_to_name(err));
|
||||||
this->status_momentary_error("failed", 1000);
|
this->status_momentary_error("failed", 1000);
|
||||||
esp_http_client_cleanup(client);
|
esp_http_client_cleanup(client);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||||
|
char url[256]{};
|
||||||
|
if (esp_http_client_get_url(client, url, sizeof(url) - 1) == ESP_OK) {
|
||||||
|
ESP_LOGV(TAG, "redirecting to url: %s", url);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
err = esp_http_client_open(client, 0);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
ESP_LOGE(TAG, "esp_http_client_open failed: %s", esp_err_to_name(err));
|
||||||
|
this->status_momentary_error("failed", 1000);
|
||||||
|
esp_http_client_cleanup(client);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
container->content_length = esp_http_client_fetch_headers(client);
|
||||||
|
container->status_code = esp_http_client_get_status_code(client);
|
||||||
|
if (is_ok(container->status_code)) {
|
||||||
container->duration_ms = millis() - start;
|
container->duration_ms = millis() - start;
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
num_redirects--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (num_redirects == 0) {
|
||||||
|
ESP_LOGW(TAG, "Reach redirect limit count=%d", this->redirect_limit_);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url.c_str(), container->status_code);
|
||||||
|
this->status_momentary_error("failed", 1000);
|
||||||
|
esp_http_client_cleanup(client);
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
int HttpContainerIDF::read(uint8_t *buf, size_t max_len) {
|
int HttpContainerIDF::read(uint8_t *buf, size_t max_len) {
|
||||||
const uint32_t start = millis();
|
const uint32_t start = millis();
|
||||||
watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
|
watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
"""Constants used by esphome."""
|
"""Constants used by esphome."""
|
||||||
|
|
||||||
__version__ = "2024.7.1"
|
__version__ = "2024.7.2"
|
||||||
|
|
||||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||||
VALID_SUBSTITUTIONS_CHARACTERS = (
|
VALID_SUBSTITUTIONS_CHARACTERS = (
|
||||||
|
|
Loading…
Reference in a new issue