ESPHome 2026.10.2
Loading...
Searching...
No Matches
online_image.h
Go to the documentation of this file.
1#pragma once
2
3#include "download_buffer.h"
10
11namespace esphome::online_image {
12
13using t_http_codes = enum {
14 HTTP_CODE_OK = 200,
15 HTTP_CODE_NOT_MODIFIED = 304,
16 HTTP_CODE_NOT_FOUND = 404,
17};
18
24class OnlineImage final : public PollingComponent,
26 public Parented<esphome::http_request::HttpRequestComponent> {
27 public:
41 OnlineImage(const std::string &url, int width, int height, runtime_image::ImageFormat format, image::ImageType type,
42 image::Transparency transparency, image::Image *placeholder, uint32_t buffer_size,
43 bool is_big_endian = false);
44
45 void update() override;
47 void update_if(bool update) {
48 if (update)
49 this->update();
50 }
51 void loop() override;
52
54 void set_url(const std::string &url) {
55 if (this->validate_url_(url)) {
56 this->url_ = url;
57 }
58 this->etag_ = "";
59 this->last_modified_ = "";
60 }
61
63 template<typename V> void add_request_header(const std::string &header, V value) {
64 this->request_headers_.push_back(std::pair<std::string, TemplatableValue<std::string>>(header, value));
65 }
66
71 void release();
72
73 template<typename F> void add_on_finished_callback(F &&callback) {
74 this->download_finished_callback_.add(std::forward<F>(callback));
75 }
76 template<typename F> void add_on_error_callback(F &&callback) {
77 this->download_error_callback_.add(std::forward<F>(callback));
78 }
79
80 protected:
81 bool validate_url_(const std::string &url);
82 void end_connection_();
83
86
87 std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
95
96 std::string url_;
97
98 std::vector<std::pair<std::string, TemplatableValue<std::string>>> request_headers_;
99
103 std::string etag_;
107 std::string last_modified_;
108
110};
111
112} // namespace esphome::online_image
Helper class to easily give an object a parent of type T.
Definition helpers.h:2004
This class simplifies creating components that periodically check a state.
Definition component.h:510
Primary TemplatableValue: stores either a constant value or a function pointer.
Definition automation.h:159
Buffer for managing downloaded data.
Download an image from a given URL, and decode it using the specified decoder.
std::string etag_
The value of the ETag HTTP header provided in the last response.
OnlineImage(const std::string &url, int width, int height, runtime_image::ImageFormat format, image::ImageType type, image::Transparency transparency, image::Image *placeholder, uint32_t buffer_size, bool is_big_endian=false)
Construct a new OnlineImage object.
bool validate_url_(const std::string &url)
void add_on_finished_callback(F &&callback)
CallbackManager< void(bool)> download_finished_callback_
std::vector< std::pair< std::string, TemplatableValue< std::string > > > request_headers_
void add_request_header(const std::string &header, V value)
Add the request header.
void set_url(const std::string &url)
Set the URL to download the image from.
std::shared_ptr< http_request::HttpContainer > downloader_
CallbackManager< void()> download_error_callback_
void update_if(bool update)
Download now when update is true (the update flag of online_image.set_url).
size_t download_buffer_initial_size_
This is the initial size of the download buffer, not the current size.
std::string last_modified_
The value of the Last-Modified HTTP header provided in the last response.
void add_on_error_callback(F &&callback)
void release()
Release the buffer storing the image.
A dynamic image that can be loaded and decoded at runtime.
uint16_t type
const char * format
enum { HTTP_CODE_OK=200, HTTP_CODE_NOT_MODIFIED=304, HTTP_CODE_NOT_FOUND=404, } t_http_codes
ImageFormat
Image format types that can be decoded dynamically.
static void uint32_t