From 7c01aac3867d9f03c27cf5a9223926d8ea21702e Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Oct 2026 13:24:26 -1000 Subject: [PATCH] [http_request] Build ota.http_request.flash with register_apply_action --- .../components/http_request/ota/__init__.py | 45 ++++--------------- .../components/http_request/ota/automation.h | 40 ----------------- 2 files changed, 8 insertions(+), 77 deletions(-) delete mode 100644 esphome/components/http_request/ota/automation.h diff --git a/esphome/components/http_request/ota/__init__.py b/esphome/components/http_request/ota/__init__.py index 784e4ee47a5..1cc112c55c4 100644 --- a/esphome/components/http_request/ota/__init__.py +++ b/esphome/components/http_request/ota/__init__.py @@ -3,9 +3,8 @@ import esphome.codegen as cg from esphome.components.ota import BASE_OTA_SCHEMA, OTAComponent, ota_to_code import esphome.config_validation as cv from esphome.const import CONF_ID, CONF_PASSWORD, CONF_URL, CONF_USERNAME -from esphome.core import ID, coroutine_with_priority +from esphome.core import coroutine_with_priority from esphome.coroutine import CoroPriority -from esphome.cpp_generator import MockObj, TemplateArgsType from esphome.types import ConfigType from .. import CONF_HTTP_REQUEST_ID, HttpRequestComponent, http_request_ns @@ -21,9 +20,6 @@ CONF_MD5_URL = "md5_url" OtaHttpRequestComponent = http_request_ns.class_( "OtaHttpRequestComponent", OTAComponent ) -OtaHttpRequestComponentFlashAction = http_request_ns.class_( - "OtaHttpRequestComponentFlashAction", automation.Action -) CONFIG_SCHEMA = cv.All( cv.Schema( @@ -68,38 +64,13 @@ OTA_HTTP_REQUEST_FLASH_ACTION_SCHEMA = cv.All( ) -@automation.register_action( +automation.register_apply_action( "ota.http_request.flash", - OtaHttpRequestComponentFlashAction, OTA_HTTP_REQUEST_FLASH_ACTION_SCHEMA, - synchronous=True, + automation.ApplyField(CONF_MD5_URL, "set_md5_url", cg.std_string), + automation.ApplyField(CONF_MD5, "set_md5", cg.std_string), + automation.ApplyField(CONF_PASSWORD, "set_password", cg.std_string), + automation.ApplyField(CONF_USERNAME, "set_username", cg.std_string), + automation.ApplyField(CONF_URL, "set_url", cg.std_string), + automation.ApplyCall("flash()"), ) -async def ota_http_request_action_to_code( - config: ConfigType, - action_id: ID, - template_arg: cg.TemplateArguments, - args: TemplateArgsType, -) -> MockObj: - paren = await cg.get_variable(config[CONF_ID]) - var = cg.new_Pvariable(action_id, template_arg, paren) - - if md5_url := config.get(CONF_MD5_URL): - template_ = await cg.templatable(md5_url, args, cg.std_string) - cg.add(var.set_md5_url(template_)) - - if md5_str := config.get(CONF_MD5): - template_ = await cg.templatable(md5_str, args, cg.std_string) - cg.add(var.set_md5(template_)) - - if password_str := config.get(CONF_PASSWORD): - template_ = await cg.templatable(password_str, args, cg.std_string) - cg.add(var.set_password(template_)) - - if username_str := config.get(CONF_USERNAME): - template_ = await cg.templatable(username_str, args, cg.std_string) - cg.add(var.set_username(template_)) - - template_ = await cg.templatable(config[CONF_URL], args, cg.std_string) - cg.add(var.set_url(template_)) - - return var diff --git a/esphome/components/http_request/ota/automation.h b/esphome/components/http_request/ota/automation.h deleted file mode 100644 index 487f6b70a1e..00000000000 --- a/esphome/components/http_request/ota/automation.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "ota_http_request.h" - -#include "esphome/core/automation.h" - -namespace esphome::http_request { - -template class OtaHttpRequestComponentFlashAction final : public Action { - public: - OtaHttpRequestComponentFlashAction(OtaHttpRequestComponent *parent) : parent_(parent) {} - TEMPLATABLE_VALUE(std::string, md5_url) - TEMPLATABLE_VALUE(std::string, md5) - TEMPLATABLE_VALUE(std::string, password) - TEMPLATABLE_VALUE(std::string, url) - TEMPLATABLE_VALUE(std::string, username) - - void play(const Ts &...x) override { - if (this->md5_url_.has_value()) { - this->parent_->set_md5_url(this->md5_url_.value(x...)); - } - if (this->md5_.has_value()) { - this->parent_->set_md5(this->md5_.value(x...)); - } - if (this->password_.has_value()) { - this->parent_->set_password(this->password_.value(x...)); - } - if (this->username_.has_value()) { - this->parent_->set_username(this->username_.value(x...)); - } - this->parent_->set_url(this->url_.value(x...)); - - this->parent_->flash(); - // Normally never reached due to reboot - } - - protected: - OtaHttpRequestComponent *parent_; -}; - -} // namespace esphome::http_request