ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
http_request_update.h
Go to the documentation of this file.
1#pragma once
2
5
9
10#ifdef USE_ESP32
11#include <freertos/FreeRTOS.h>
12#endif
13
14namespace esphome::http_request {
15
16namespace testing {
17class HttpRequestUpdateTest;
18} // namespace testing
19
21
22// One manifest check: handed from update() to the task and back to the main loop.
23struct TaskResult {
25 const char *source_url{nullptr}; // the URL this check reads, fixed before it starts
27 const LogString *error_str{nullptr}; // set on failure, nullptr on success
28 bool owns_source_url{false}; // the check holds the buffer until it ends, see begin_check_()
29};
30
32 public:
33 void setup() override;
34 void update() override;
35
36 void perform(bool force) override;
37 void check() override { this->update(); }
38
39 ~HttpRequestUpdate() override { this->release_source_url_(); }
40
42 void set_source_url_static(const char *source_url) {
43 this->release_source_url_();
44 this->source_url_ = source_url;
45 }
47 void set_source_url(const char *source_url) { this->set_source_url(source_url, strlen(source_url)); }
48 void set_source_url(const std::string &source_url) { this->set_source_url(source_url.data(), source_url.size()); }
49 void set_source_url(const char *source_url, size_t len);
50 const char *get_source_url() const { return this->source_url_; }
51
52 void set_request_parent(HttpRequestComponent *request_parent) { this->request_parent_ = request_parent; }
53 void set_ota_parent(OtaHttpRequestComponent *ota_parent) { this->ota_parent_ = ota_parent; }
54
55 float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
56
57 void on_ota_state(ota::OTAState state, float progress, uint8_t error) override;
58
59 protected:
62
63 static void update_task(void *params);
64#ifdef USE_ESP32
65 TaskResult *task_result_{nullptr}; // set while a check runs on the update task
66#endif
68
69 private:
70 friend class testing::HttpRequestUpdateTest;
71 void release_source_url_();
72 // A check reads the URL it started with until it ends, on the update task or inline through an
73 // automation it triggers. Ownership of that buffer moves to the check, so a set_source_url in the
74 // meantime cannot free it; end_check_() hands it back or frees it on the main loop.
75 void begin_check_(TaskResult *result);
76 void end_check_(TaskResult *result);
77
78 bool source_url_owned_{false}; // source_url_ was copied by a run-time setter; sits in the padding above
79 const char *source_url_{nullptr};
80};
81
82} // namespace esphome::http_request
This class simplifies creating components that periodically check a state.
Definition component.h:519
void set_source_url(const std::string &source_url)
void set_request_parent(HttpRequestComponent *request_parent)
void set_ota_parent(OtaHttpRequestComponent *ota_parent)
void on_ota_state(ota::OTAState state, float progress, uint8_t error) override
void set_source_url(const char *source_url)
Set the manifest URL at run time; the string is copied, so any storage may be passed.
void set_source_url_static(const char *source_url)
Codegen only: the config literal lives in flash for the life of the program, so it is kept by pointer...
Listener interface for OTA state changes.
const UpdateState & state
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:55
const void size_t len
Definition hal.h:68