From 296442d8f16c83bc68fb1c71c3d5ed03be155d8b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 2 Aug 2025 13:59:20 -1000 Subject: [PATCH] [core] Fix compilation errors when platform sections have no entities (#10023) --- .../alarm_control_panel/__init__.py | 1 - esphome/components/binary_sensor/__init__.py | 1 - esphome/components/button/__init__.py | 1 - esphome/components/climate/__init__.py | 1 - esphome/components/cover/__init__.py | 1 - esphome/components/datetime/__init__.py | 2 -- esphome/components/event/__init__.py | 1 - esphome/components/fan/__init__.py | 1 - esphome/components/light/__init__.py | 1 - esphome/components/lock/__init__.py | 1 - esphome/components/number/__init__.py | 1 - esphome/components/select/__init__.py | 1 - esphome/components/sensor/__init__.py | 1 - esphome/components/switch/__init__.py | 1 - esphome/components/text/__init__.py | 1 - esphome/components/text_sensor/__init__.py | 1 - esphome/components/update/__init__.py | 1 - esphome/components/valve/__init__.py | 1 - esphome/core/config.py | 21 ++++++++++++++++--- 19 files changed, 18 insertions(+), 22 deletions(-) diff --git a/esphome/components/alarm_control_panel/__init__.py b/esphome/components/alarm_control_panel/__init__.py index b076175eb8..058e061d1e 100644 --- a/esphome/components/alarm_control_panel/__init__.py +++ b/esphome/components/alarm_control_panel/__init__.py @@ -348,4 +348,3 @@ async def alarm_control_panel_is_armed_to_code( @coroutine_with_priority(100.0) async def to_code(config): cg.add_global(alarm_control_panel_ns.using) - cg.add_define("USE_ALARM_CONTROL_PANEL") diff --git a/esphome/components/binary_sensor/__init__.py b/esphome/components/binary_sensor/__init__.py index 376a399637..b56fde1ffd 100644 --- a/esphome/components/binary_sensor/__init__.py +++ b/esphome/components/binary_sensor/__init__.py @@ -654,7 +654,6 @@ async def binary_sensor_is_off_to_code(config, condition_id, template_arg, args) @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_BINARY_SENSOR") cg.add_global(binary_sensor_ns.using) diff --git a/esphome/components/button/__init__.py b/esphome/components/button/__init__.py index ed2670a5c5..a23958989e 100644 --- a/esphome/components/button/__init__.py +++ b/esphome/components/button/__init__.py @@ -137,4 +137,3 @@ async def button_press_to_code(config, action_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): cg.add_global(button_ns.using) - cg.add_define("USE_BUTTON") diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py index 9530ecdcca..4af3a619b5 100644 --- a/esphome/components/climate/__init__.py +++ b/esphome/components/climate/__init__.py @@ -519,5 +519,4 @@ async def climate_control_to_code(config, action_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_CLIMATE") cg.add_global(climate_ns.using) diff --git a/esphome/components/cover/__init__.py b/esphome/components/cover/__init__.py index cd97a38ecc..0e01eb336f 100644 --- a/esphome/components/cover/__init__.py +++ b/esphome/components/cover/__init__.py @@ -265,5 +265,4 @@ async def cover_control_to_code(config, action_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_COVER") cg.add_global(cover_ns.using) diff --git a/esphome/components/datetime/__init__.py b/esphome/components/datetime/__init__.py index 4788810965..1d84b75f26 100644 --- a/esphome/components/datetime/__init__.py +++ b/esphome/components/datetime/__init__.py @@ -164,7 +164,6 @@ async def register_datetime(var, config): cg.add(getattr(cg.App, f"register_{entity_type}")(var)) CORE.register_platform_component(entity_type, var) await setup_datetime_core_(var, config) - cg.add_define(f"USE_DATETIME_{config[CONF_TYPE]}") async def new_datetime(config, *args): @@ -175,7 +174,6 @@ async def new_datetime(config, *args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_DATETIME") cg.add_global(datetime_ns.using) diff --git a/esphome/components/event/__init__.py b/esphome/components/event/__init__.py index 3aff96a48e..1948570ecd 100644 --- a/esphome/components/event/__init__.py +++ b/esphome/components/event/__init__.py @@ -145,5 +145,4 @@ async def event_fire_to_code(config, action_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_EVENT") cg.add_global(event_ns.using) diff --git a/esphome/components/fan/__init__.py b/esphome/components/fan/__init__.py index 0b1d39575d..3fb217a24e 100644 --- a/esphome/components/fan/__init__.py +++ b/esphome/components/fan/__init__.py @@ -400,5 +400,4 @@ async def fan_is_on_off_to_code(config, condition_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_FAN") cg.add_global(fan_ns.using) diff --git a/esphome/components/light/__init__.py b/esphome/components/light/__init__.py index 7ab899edb2..fa39721ee2 100644 --- a/esphome/components/light/__init__.py +++ b/esphome/components/light/__init__.py @@ -285,5 +285,4 @@ async def new_light(config, *args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_LIGHT") cg.add_global(light_ns.using) diff --git a/esphome/components/lock/__init__.py b/esphome/components/lock/__init__.py index e62d9f3e2b..7977efd264 100644 --- a/esphome/components/lock/__init__.py +++ b/esphome/components/lock/__init__.py @@ -158,4 +158,3 @@ async def lock_is_off_to_code(config, condition_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): cg.add_global(lock_ns.using) - cg.add_define("USE_LOCK") diff --git a/esphome/components/number/__init__.py b/esphome/components/number/__init__.py index 90a1619e4c..4a83d5fc5f 100644 --- a/esphome/components/number/__init__.py +++ b/esphome/components/number/__init__.py @@ -323,7 +323,6 @@ async def number_in_range_to_code(config, condition_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_NUMBER") cg.add_global(number_ns.using) diff --git a/esphome/components/select/__init__.py b/esphome/components/select/__init__.py index ed1f6c020d..dd3feccab5 100644 --- a/esphome/components/select/__init__.py +++ b/esphome/components/select/__init__.py @@ -126,7 +126,6 @@ async def new_select(config, *, options: list[str]): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_SELECT") cg.add_global(select_ns.using) diff --git a/esphome/components/sensor/__init__.py b/esphome/components/sensor/__init__.py index 23e6ad0f2c..2275027004 100644 --- a/esphome/components/sensor/__init__.py +++ b/esphome/components/sensor/__init__.py @@ -1139,5 +1139,4 @@ def _lstsq(a, b): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_SENSOR") cg.add_global(sensor_ns.using) diff --git a/esphome/components/switch/__init__.py b/esphome/components/switch/__init__.py index c09675069f..a595d43445 100644 --- a/esphome/components/switch/__init__.py +++ b/esphome/components/switch/__init__.py @@ -202,4 +202,3 @@ async def switch_is_off_to_code(config, condition_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): cg.add_global(switch_ns.using) - cg.add_define("USE_SWITCH") diff --git a/esphome/components/text/__init__.py b/esphome/components/text/__init__.py index 8362e09ac0..aa831d1f06 100644 --- a/esphome/components/text/__init__.py +++ b/esphome/components/text/__init__.py @@ -151,7 +151,6 @@ async def new_text( @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_TEXT") cg.add_global(text_ns.using) diff --git a/esphome/components/text_sensor/__init__.py b/esphome/components/text_sensor/__init__.py index 0341ab2f71..e4aa701a7b 100644 --- a/esphome/components/text_sensor/__init__.py +++ b/esphome/components/text_sensor/__init__.py @@ -232,7 +232,6 @@ async def new_text_sensor(config, *args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_TEXT_SENSOR") cg.add_global(text_sensor_ns.using) diff --git a/esphome/components/update/__init__.py b/esphome/components/update/__init__.py index 758267f412..50d8aaf139 100644 --- a/esphome/components/update/__init__.py +++ b/esphome/components/update/__init__.py @@ -126,7 +126,6 @@ async def new_update(config): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_UPDATE") cg.add_global(update_ns.using) diff --git a/esphome/components/valve/__init__.py b/esphome/components/valve/__init__.py index cb27546120..53254068af 100644 --- a/esphome/components/valve/__init__.py +++ b/esphome/components/valve/__init__.py @@ -235,5 +235,4 @@ async def valve_control_to_code(config, action_id, template_arg, args): @coroutine_with_priority(100.0) async def to_code(config): - cg.add_define("USE_VALVE") cg.add_global(valve_ns.using) diff --git a/esphome/core/config.py b/esphome/core/config.py index 3bc030ad50..6a87bab730 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -419,13 +419,28 @@ async def _add_automations(config): await automation.build_automation(trigger, [], conf) +# Datetime component has special subtypes that need additional defines +DATETIME_SUBTYPES = {"date", "time", "datetime"} + + @coroutine_with_priority(-100.0) -async def _add_platform_reserves() -> None: - # Generate compile-time entity count defines for static_entity_vector +async def _add_platform_defines() -> None: + # Generate compile-time defines for platforms that have actual entities + # Only add USE_* and count defines when there are entities for platform_name, count in sorted(CORE.platform_counts.items()): + if count <= 0: + continue + define_name = f"ESPHOME_ENTITY_{platform_name.upper()}_COUNT" cg.add_define(define_name, count) + # Datetime subtypes only use USE_DATETIME_* defines + if platform_name in DATETIME_SUBTYPES: + cg.add_define(f"USE_DATETIME_{platform_name.upper()}") + else: + # Regular platforms use USE_* defines + cg.add_define(f"USE_{platform_name.upper()}") + @coroutine_with_priority(100.0) async def to_code(config: ConfigType) -> None: @@ -449,7 +464,7 @@ async def to_code(config: ConfigType) -> None: cg.RawStatement(f"App.reserve_components({len(CORE.component_ids)});"), ) - CORE.add_job(_add_platform_reserves) + CORE.add_job(_add_platform_defines) CORE.add_job(_add_automations, config)