mirror of
https://github.com/esphome/esphome.git
synced 2026-02-25 21:43:14 -07:00
[http_request] Retry update check on startup until network is ready (#14228)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -24,8 +24,29 @@ namespace http_request {
|
||||
static const char *const TAG = "http_request.update";
|
||||
|
||||
static const size_t MAX_READ_SIZE = 256;
|
||||
static constexpr uint32_t INITIAL_CHECK_INTERVAL_ID = 0;
|
||||
static constexpr uint32_t INITIAL_CHECK_INTERVAL_MS = 10000;
|
||||
static constexpr uint8_t INITIAL_CHECK_MAX_ATTEMPTS = 6;
|
||||
|
||||
void HttpRequestUpdate::setup() { this->ota_parent_->add_state_listener(this); }
|
||||
void HttpRequestUpdate::setup() {
|
||||
this->ota_parent_->add_state_listener(this);
|
||||
|
||||
// Check periodically until network is ready
|
||||
// Only if update interval is > total retry window to avoid redundant checks
|
||||
if (this->get_update_interval() != SCHEDULER_DONT_RUN &&
|
||||
this->get_update_interval() > INITIAL_CHECK_INTERVAL_MS * INITIAL_CHECK_MAX_ATTEMPTS) {
|
||||
this->initial_check_remaining_ = INITIAL_CHECK_MAX_ATTEMPTS;
|
||||
this->set_interval(INITIAL_CHECK_INTERVAL_ID, INITIAL_CHECK_INTERVAL_MS, [this]() {
|
||||
bool connected = network::is_connected();
|
||||
if (--this->initial_check_remaining_ == 0 || connected) {
|
||||
this->cancel_interval(INITIAL_CHECK_INTERVAL_ID);
|
||||
if (connected) {
|
||||
this->update();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void HttpRequestUpdate::on_ota_state(ota::OTAState state, float progress, uint8_t error) {
|
||||
if (state == ota::OTAState::OTA_IN_PROGRESS) {
|
||||
@@ -45,6 +66,7 @@ void HttpRequestUpdate::update() {
|
||||
ESP_LOGD(TAG, "Network not connected, skipping update check");
|
||||
return;
|
||||
}
|
||||
this->cancel_interval(INITIAL_CHECK_INTERVAL_ID);
|
||||
#ifdef USE_ESP32
|
||||
xTaskCreate(HttpRequestUpdate::update_task, "update_task", 8192, (void *) this, 1, &this->update_task_handle_);
|
||||
#else
|
||||
|
||||
@@ -40,6 +40,7 @@ class HttpRequestUpdate final : public update::UpdateEntity, public PollingCompo
|
||||
#ifdef USE_ESP32
|
||||
TaskHandle_t update_task_handle_{nullptr};
|
||||
#endif
|
||||
uint8_t initial_check_remaining_{0};
|
||||
};
|
||||
|
||||
} // namespace http_request
|
||||
|
||||
Reference in New Issue
Block a user