mirror of
https://github.com/esphome/esphome.git
synced 2026-09-01 02:26:01 +00:00
Add 4-arg overloads to App.register_<entity>() that call configure_entity_() and push_back in a single function. Codegen defers App.register_<entity>(var) emission until finalize_entity_strings(), which then emits a single combined App.register_<entity>(var, name, hash, packed) call instead of the previous two-statement pair (App.register_X(var); var->configure_entity_(...)). Apollo R-PRO-1 (ESP32-S3 IDF, 122 components, 164 entities), same toolchain: text: -1248 bytes main.cpp: -164 lines No behavior change. configure_entity_ remains protected on EntityBase; the Application class is now a friend so the new overloads can call it.
30 lines
916 B
Python
30 lines
916 B
Python
"""Shared helpers for component tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
INTERNAL_BIT = 1 << 24
|
|
|
|
|
|
def extract_packed_value(main_cpp: str, var_name: str) -> int:
|
|
"""Extract the packed-fields argument from the entity's configure call.
|
|
|
|
Matches both legacy form ``var->configure_entity_(name, hash, packed)`` and the
|
|
combined form ``App.register_<entity>(var, name, hash, packed)``.
|
|
"""
|
|
escaped_var = re.escape(var_name)
|
|
legacy_pattern = (
|
|
rf"{escaped_var}->configure_entity_\("
|
|
r'"(?:\\.|[^"\\])*"'
|
|
r",\s*\w+,\s*(\d+)\)"
|
|
)
|
|
combined_pattern = (
|
|
rf"App\.register_\w+\(\s*{escaped_var}\s*,\s*"
|
|
r'"(?:\\.|[^"\\])*"'
|
|
r",\s*\w+,\s*(\d+)\)"
|
|
)
|
|
match = re.search(combined_pattern, main_cpp) or re.search(legacy_pattern, main_cpp)
|
|
assert match, f"configure call not found for {var_name}"
|
|
return int(match.group(1))
|