ESPHome  2024.11.0
ota_http_request.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "esphome/core/defines.h"
6 #include "esphome/core/helpers.h"
7 
8 #include <memory>
9 #include <string>
10 #include <utility>
11 
12 #include "../http_request.h"
13 
14 namespace esphome {
15 namespace http_request {
16 
17 static const uint8_t MD5_SIZE = 32;
18 
19 enum OtaHttpRequestError : uint8_t {
21  OTA_BAD_URL = 0x11,
23 };
24 
25 class OtaHttpRequestComponent : public ota::OTAComponent, public Parented<HttpRequestComponent> {
26  public:
27  void setup() override;
28  void dump_config() override;
29  float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
30 
31  void set_md5_url(const std::string &md5_url);
32  void set_md5(const std::string &md5) { this->md5_expected_ = md5; }
33  void set_password(const std::string &password) { this->password_ = password; }
34  void set_url(const std::string &url);
35  void set_username(const std::string &username) { this->username_ = username; }
36 
37  std::string md5_computed() { return this->md5_computed_; }
38  std::string md5_expected() { return this->md5_expected_; }
39 
40  void flash();
41 
42  protected:
43  void cleanup_(std::unique_ptr<ota::OTABackend> backend, const std::shared_ptr<HttpContainer> &container);
44  uint8_t do_ota_();
45  std::string get_url_with_auth_(const std::string &url);
46  bool http_get_md5_();
47  bool validate_url_(const std::string &url);
48 
49  std::string md5_computed_{};
50  std::string md5_expected_{};
51  std::string md5_url_{};
52  std::string password_{};
53  std::string username_{};
54  std::string url_{};
55  int status_ = -1;
56  bool update_started_ = false;
57  static const uint16_t HTTP_RECV_BUFFER = 256; // the firmware GET chunk size
58 };
59 
60 } // namespace http_request
61 } // namespace esphome
void cleanup_(std::unique_ptr< ota::OTABackend > backend, const std::shared_ptr< HttpContainer > &container)
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition: component.cpp:26
void set_password(const std::string &password)
std::string get_url_with_auth_(const std::string &url)
void set_md5_url(const std::string &md5_url)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_username(const std::string &username)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522