From 52f47d0b91be2f8ea8fdfcfa9e0f6bbd933f6b68 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 23 Apr 2026 14:57:31 -0500 Subject: [PATCH] [core] Import yaml_util once per build in generate_cpp_contents Address copilot review: wrap_to_code is called once per component, so the lazy `from esphome import yaml_util` inside it was doing a (cached) sys.modules lookup per component. Move the import up to generate_cpp_contents, pass the module down, rename the helper to _wrap_to_code to mark it private. --- esphome/__main__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/esphome/__main__.py b/esphome/__main__.py index 7556773a01..781bcd6288 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -667,9 +667,7 @@ def run_miniterm(config: ConfigType, port: str, args) -> int: return 0 -def wrap_to_code(name, comp): - from esphome import yaml_util - +def _wrap_to_code(name, comp, yaml_util): coro = coroutine(comp.to_code) @functools.wraps(comp.to_code) @@ -702,11 +700,13 @@ def write_cpp(config: ConfigType, native_idf: bool = False) -> int: def generate_cpp_contents(config: ConfigType) -> None: + from esphome import yaml_util + _LOGGER.info("Generating C++ source...") for name, component, conf in iter_component_configs(CORE.config): if component.to_code is not None: - coro = wrap_to_code(name, component) + coro = _wrap_to_code(name, component, yaml_util) CORE.add_job(coro, conf) CORE.flush_tasks()