1
0
mirror of https://github.com/esphome/esphome.git synced 2025-06-15 14:56:59 +02:00

Add support for custom request headers in online_image component (#8985)

This commit is contained in:
Stanislav Meduna 2025-06-11 12:14:02 +02:00 committed by GitHub
parent e8aa7cff36
commit 052f558131
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 26 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import logging
from esphome import automation
import esphome.codegen as cg
from esphome.components.const import CONF_REQUEST_HEADERS
from esphome.components.http_request import CONF_HTTP_REQUEST_ID, HttpRequestComponent
from esphome.components.image import (
CONF_INVERT_ALPHA,
@ -24,6 +25,7 @@ from esphome.const import (
CONF_TYPE,
CONF_URL,
)
from esphome.core import Lambda
AUTO_LOAD = ["image"]
DEPENDENCIES = ["display", "http_request"]
@ -124,6 +126,9 @@ ONLINE_IMAGE_SCHEMA = (
cv.GenerateID(CONF_HTTP_REQUEST_ID): cv.use_id(HttpRequestComponent),
# Online Image specific options
cv.Required(CONF_URL): cv.url,
cv.Optional(CONF_REQUEST_HEADERS): cv.All(
cv.Schema({cv.string: cv.templatable(cv.string)})
),
cv.Required(CONF_FORMAT): cv.one_of(*IMAGE_FORMATS, upper=True),
cv.Optional(CONF_PLACEHOLDER): cv.use_id(Image_),
cv.Optional(CONF_BUFFER_SIZE, default=65536): cv.int_range(256, 65536),
@ -207,6 +212,13 @@ async def to_code(config):
await cg.register_component(var, config)
await cg.register_parented(var, config[CONF_HTTP_REQUEST_ID])
for key, value in config.get(CONF_REQUEST_HEADERS, {}).items():
if isinstance(value, Lambda):
template_ = await cg.templatable(value, [], cg.std_string)
cg.add(var.add_request_header(key, template_))
else:
cg.add(var.add_request_header(key, value))
if placeholder_id := config.get(CONF_PLACEHOLDER):
placeholder = await cg.get_variable(placeholder_id)
cg.add(var.set_placeholder(placeholder))

View File

@ -143,6 +143,10 @@ void OnlineImage::update() {
headers.push_back(accept_header);
for (auto &header : this->request_headers_) {
headers.push_back(http_request::Header{header.first, header.second.value()});
}
this->downloader_ = this->parent_->get(this->url_, headers, {ETAG_HEADER_NAME, LAST_MODIFIED_HEADER_NAME});
if (this->downloader_ == nullptr) {

View File

@ -67,6 +67,11 @@ class OnlineImage : public PollingComponent,
this->last_modified_ = "";
}
/** Add the request header */
template<typename V> void add_request_header(const std::string &header, V value) {
this->request_headers_.push_back(std::pair<std::string, TemplatableValue<std::string> >(header, value));
}
/**
* @brief Set the image that needs to be shown as long as the downloaded image
* is not available.
@ -153,6 +158,8 @@ class OnlineImage : public PollingComponent,
std::string url_{""};
std::vector<std::pair<std::string, TemplatableValue<std::string> > > request_headers_;
/** width requested on configuration, or 0 if non specified. */
const int fixed_width_;
/** height requested on configuration, or 0 if non specified. */

View File

@ -11,6 +11,9 @@ online_image:
format: PNG
type: BINARY
resize: 50x50
request_headers:
X-Test1: 'Test1'
X-Test2: !lambda 'static int x; return to_string(x++);'
on_download_finished:
lambda: |-
if (cached) {