ESPHome 2026.3.0-dev
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
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;
46 void loop() override;
47
49 void set_url(const std::string &url) {
50 if (this->validate_url_(url)) {
51 this->url_ = url;
52 }
53 this->etag_ = "";
54 this->last_modified_ = "";
55 }
56
58 template<typename V> void add_request_header(const std::string &header, V value) {
59 this->request_headers_.push_back(std::pair<std::string, TemplatableValue<std::string>>(header, value));
60 }
61
66 void release();
67
68 void add_on_finished_callback(std::function<void(bool)> &&callback);
69 void add_on_error_callback(std::function<void()> &&callback);
70
71 protected:
72 bool validate_url_(const std::string &url);
73 void end_connection_();
74
77
78 std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
86
87 std::string url_{""};
88
89 std::vector<std::pair<std::string, TemplatableValue<std::string>>> request_headers_;
90
94 std::string etag_ = "";
98 std::string last_modified_ = "";
99
101};
102
103template<typename... Ts> class OnlineImageSetUrlAction : public Action<Ts...> {
104 public:
106 TEMPLATABLE_VALUE(std::string, url)
107 TEMPLATABLE_VALUE(bool, update)
108 void play(const Ts &...x) override {
109 this->parent_->set_url(this->url_.value(x...));
110 if (this->update_.value(x...)) {
111 this->parent_->update();
112 }
113 }
114
115 protected:
117};
118
119template<typename... Ts> class OnlineImageReleaseAction : public Action<Ts...> {
120 public:
122 void play(const Ts &...x) override { this->parent_->release(); }
123
124 protected:
126};
127
128class DownloadFinishedTrigger : public Trigger<bool> {
129 public:
131 parent->add_on_finished_callback([this](bool cached) { this->trigger(cached); });
132 }
133};
134
136 public:
138 parent->add_on_error_callback([this]() { this->trigger(); });
139 }
140};
141
142} // namespace esphome::online_image
Helper class to easily give an object a parent of type T.
Definition helpers.h:1618
This class simplifies creating components that periodically check a state.
Definition component.h:527
void trigger(const Ts &...x)
Definition automation.h:325
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)
CallbackManager< void(bool)> download_finished_callback_
std::vector< std::pair< std::string, TemplatableValue< std::string > > > request_headers_
void add_on_error_callback(std::function< void()> &&callback)
void add_request_header(const std::string &header, V value)
Add the request header.
void add_on_finished_callback(std::function< void(bool)> &&callback)
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_
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 release()
Release the buffer storing the image.
update void play(const Ts &...x) override
TEMPLATABLE_VALUE(std::string, url) TEMPLATABLE_VALUE(bool
A dynamic image that can be loaded and decoded at runtime.
uint16_t type
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.
if(written< 0)
Definition helpers.h:863
uint16_t x
Definition tt21100.cpp:5