From ebde654259119fa74b804d8fcaf6b17506cd06a5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Feb 2026 21:31:06 -1000 Subject: [PATCH] Restore template wrapper as protected register_component_ Rename the non-template to register_component_impl_ and add a protected template register_component_ that wraps it. This preserves the compiler optimization behavior (isra clones) while keeping the method inaccessible to external components. Co-Authored-By: J. Nick Koston --- esphome/core/application.cpp | 2 +- esphome/core/application.h | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index f963afa597..9c89a0947b 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -79,7 +79,7 @@ static void insertion_sort_by_priority(Iterator first, Iterator last) { } } -void Application::register_component_(Component *comp) { this->components_.push_back(comp); } +void Application::register_component_impl_(Component *comp) { this->components_.push_back(comp); } void Application::setup() { ESP_LOGI(TAG, "Running through setup()"); ESP_LOGV(TAG, "Sorting components by setup priority"); diff --git a/esphome/core/application.h b/esphome/core/application.h index ea70f8ac0d..8dcfafbb23 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -521,7 +521,14 @@ class Application { #endif #endif - __attribute__((noinline)) void register_component_(Component *comp); + /// Register a component - only callable from codegen-generated setup() via friend access. + template C *register_component_(C *c) { + static_assert(std::is_base_of::value, "Only Component subclasses can be registered"); + this->register_component_impl_((Component *) c); + return c; + } + + void register_component_impl_(Component *comp); void calculate_looping_components_(); void add_looping_components_by_state_(bool match_loop_done);