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);