From dad61299b86428fd3151473f61b9c9a9c6f3fa43 Mon Sep 17 00:00:00 2001 From: Kjell Braden Date: Thu, 14 Nov 2024 11:03:33 +0100 Subject: [PATCH] online_image: nicer code style for http_request_headers --- esphome/components/online_image/__init__.py | 4 +++- esphome/components/online_image/online_image.cpp | 7 +++---- esphome/components/online_image/online_image.h | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/esphome/components/online_image/__init__.py b/esphome/components/online_image/__init__.py index 9f2fba3e5f..e203483628 100644 --- a/esphome/components/online_image/__init__.py +++ b/esphome/components/online_image/__init__.py @@ -160,7 +160,9 @@ async def to_code(config): for key in config.get(CONF_HTTP_REQUEST_HEADERS, []): template_ = await cg.templatable( - config[CONF_HTTP_REQUEST_HEADERS][key], [], cg.optional.template(cg.const_char_ptr) + config[CONF_HTTP_REQUEST_HEADERS][key], + [], + cg.optional.template(cg.const_char_ptr), ) cg.add(var.add_header(key, template_)) diff --git a/esphome/components/online_image/online_image.cpp b/esphome/components/online_image/online_image.cpp index 44e8bdd7cb..797ae4dc9e 100644 --- a/esphome/components/online_image/online_image.cpp +++ b/esphome/components/online_image/online_image.cpp @@ -103,12 +103,11 @@ void OnlineImage::update() { ESP_LOGI(TAG, "Updating image"); } - std::list headers; - for (const auto &item : this->headers_) { - auto val = item.second(); + for (const auto &[key, value_template] : this->headers_) { + auto val = value_template(); if (val.has_value()) { - headers.push_back(http_request::Header{item.first, *val}); + headers.push_back(http_request::Header{key, *val}); } } diff --git a/esphome/components/online_image/online_image.h b/esphome/components/online_image/online_image.h index 023711b843..cd9fd1d188 100644 --- a/esphome/components/online_image/online_image.h +++ b/esphome/components/online_image/online_image.h @@ -62,7 +62,7 @@ class OnlineImage : public PollingComponent, } } - void add_header(const char *key, std::function()> &&f) { this->headers_.insert({key, f}); } + void add_header(const char *key, std::function()> &&f) { this->headers_.insert({key, f}); } /** * @brief Set the image that needs to be shown as long as the downloaded image @@ -124,7 +124,7 @@ class OnlineImage : public PollingComponent, image::Image *placeholder_{nullptr}; std::string url_{""}; - std::map()>> headers_{}; + std::map()>> headers_{}; /** width requested on configuration, or 0 if non specified. */ const int fixed_width_;