From 1b6471f4b0ff273e15130bf5e77e1c31e577efdb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 7 Nov 2025 16:30:38 -0600 Subject: [PATCH] cleanups --- esphome/components/api/__init__.py | 3 +++ esphome/components/web_server/__init__.py | 3 +++ esphome/core/__init__.py | 5 +++++ esphome/core/config.py | 9 +++++++++ esphome/core/controller_registry.cpp | 7 ++++++- esphome/core/controller_registry.h | 9 +++++++-- esphome/core/defines.h | 2 ++ 7 files changed, 35 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 449572c0e5..023e4bf115 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -244,6 +244,9 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) + # Track controller registration for StaticVector sizing + CORE.register_controller() + cg.add(var.set_port(config[CONF_PORT])) if config[CONF_PASSWORD]: cg.add_define("USE_API_PASSWORD") diff --git a/esphome/components/web_server/__init__.py b/esphome/components/web_server/__init__.py index a7fdf30eef..17ad496f30 100644 --- a/esphome/components/web_server/__init__.py +++ b/esphome/components/web_server/__init__.py @@ -289,6 +289,9 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID], paren) await cg.register_component(var, config) + # Track controller registration for StaticVector sizing + CORE.register_controller() + version = config[CONF_VERSION] cg.add(paren.set_port(config[CONF_PORT])) diff --git a/esphome/core/__init__.py b/esphome/core/__init__.py index fed5265d6b..84c9e36002 100644 --- a/esphome/core/__init__.py +++ b/esphome/core/__init__.py @@ -910,6 +910,11 @@ class EsphomeCore: """ self.platform_counts[platform_name] += 1 + def register_controller(self) -> None: + """Track registration of a Controller for ControllerRegistry StaticVector sizing.""" + controller_count = self.data.setdefault("controller_registry_count", 0) + self.data["controller_registry_count"] = controller_count + 1 + @property def cpp_main_section(self): from esphome.cpp_generator import statement diff --git a/esphome/core/config.py b/esphome/core/config.py index 2740453808..3d98eda8bb 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -462,6 +462,15 @@ async def _add_platform_defines() -> None: cg.add_define(f"USE_{platform_name.upper()}") +@coroutine_with_priority(CoroPriority.FINAL) +async def _add_controller_registry_define() -> None: + # Generate StaticVector size for ControllerRegistry + controller_count = CORE.data.get("controller_registry_count", 0) + if controller_count > 0: + cg.add_define("USE_CONTROLLER_REGISTRY") + cg.add_define("CONTROLLER_REGISTRY_MAX", controller_count) + + @coroutine_with_priority(CoroPriority.CORE) async def to_code(config: ConfigType) -> None: cg.add_global(cg.global_ns.namespace("esphome").using) diff --git a/esphome/core/controller_registry.cpp b/esphome/core/controller_registry.cpp index b3f4c646a3..b22ec487d5 100644 --- a/esphome/core/controller_registry.cpp +++ b/esphome/core/controller_registry.cpp @@ -1,9 +1,12 @@ #include "esphome/core/controller_registry.h" + +#ifdef USE_CONTROLLER_REGISTRY + #include "esphome/core/controller.h" namespace esphome { -std::vector ControllerRegistry::controllers; +StaticVector ControllerRegistry::controllers; void ControllerRegistry::register_controller(Controller *controller) { controllers.push_back(controller); } @@ -168,3 +171,5 @@ void ControllerRegistry::notify_update(update::UpdateEntity *obj) { #endif } // namespace esphome + +#endif // USE_CONTROLLER_REGISTRY diff --git a/esphome/core/controller_registry.h b/esphome/core/controller_registry.h index 72485c356b..640a276a0a 100644 --- a/esphome/core/controller_registry.h +++ b/esphome/core/controller_registry.h @@ -1,7 +1,10 @@ #pragma once #include "esphome/core/defines.h" -#include + +#ifdef USE_CONTROLLER_REGISTRY + +#include "esphome/core/helpers.h" // Forward declarations namespace esphome { @@ -234,7 +237,9 @@ class ControllerRegistry { #endif protected: - static std::vector controllers; + static StaticVector controllers; }; } // namespace esphome + +#endif // USE_CONTROLLER_REGISTRY diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 2be32058ea..8230518071 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -28,6 +28,7 @@ #define USE_BUTTON #define USE_CAMERA #define USE_CLIMATE +#define USE_CONTROLLER_REGISTRY #define USE_COVER #define USE_DATETIME #define USE_DATETIME_DATE @@ -296,6 +297,7 @@ #define USE_DASHBOARD_IMPORT // Default counts for static analysis +#define CONTROLLER_REGISTRY_MAX 2 #define ESPHOME_COMPONENT_COUNT 50 #define ESPHOME_DEVICE_COUNT 10 #define ESPHOME_AREA_COUNT 10