diff --git a/esphome/components/adc/__init__.py b/esphome/components/adc/__init__.py index c3ababbb84..1bd5121998 100644 --- a/esphome/components/adc/__init__.py +++ b/esphome/components/adc/__init__.py @@ -19,6 +19,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@esphome/core"] @@ -237,16 +238,18 @@ def validate_adc_pin(value): raise NotImplementedError -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "adc_sensor_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, - "adc_sensor_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "adc_sensor_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "adc_sensor_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "adc_sensor_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "adc_sensor_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "adc_sensor_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "adc_sensor_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/debug/__init__.py b/esphome/components/debug/__init__.py index b5cdef4f0e..0a5756b8bd 100644 --- a/esphome/components/debug/__init__.py +++ b/esphome/components/debug/__init__.py @@ -9,6 +9,7 @@ from esphome.const import ( CONF_LOOP_TIME, PlatformFramework, ) +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@OttoWinter"] DEPENDENCIES = ["logger"] @@ -47,14 +48,19 @@ async def to_code(config): await cg.register_component(var, config) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "debug_esp32.cpp": {PlatformFramework.ESP32_ARDUINO, PlatformFramework.ESP32_IDF}, - "debug_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "debug_host.cpp": {PlatformFramework.HOST_NATIVE}, - "debug_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "debug_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "debug_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "debug_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "debug_host.cpp": {PlatformFramework.HOST_NATIVE}, + "debug_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "debug_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/deep_sleep/__init__.py b/esphome/components/deep_sleep/__init__.py index 096d2eaa38..5df9a23e47 100644 --- a/esphome/components/deep_sleep/__init__.py +++ b/esphome/components/deep_sleep/__init__.py @@ -29,6 +29,7 @@ from esphome.const import ( PLATFORM_ESP8266, PlatformFramework, ) +from esphome.helpers import filter_source_files_from_platform WAKEUP_PINS = { VARIANT_ESP32: [ @@ -316,10 +317,12 @@ async def deep_sleep_action_to_code(config, action_id, template_arg, args): return var -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "deep_sleep_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, - "deep_sleep_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "deep_sleep_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "deep_sleep_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + } +) diff --git a/esphome/components/http_request/__init__.py b/esphome/components/http_request/__init__.py index 0eaecaac45..3c9f112e66 100644 --- a/esphome/components/http_request/__init__.py +++ b/esphome/components/http_request/__init__.py @@ -17,7 +17,7 @@ from esphome.const import ( __version__, ) from esphome.core import CORE, Lambda -from esphome.helpers import IS_MACOS +from esphome.helpers import IS_MACOS, filter_source_files_from_platform DEPENDENCIES = ["network"] AUTO_LOAD = ["json", "watchdog"] @@ -322,15 +322,17 @@ async def http_request_action_to_code(config, action_id, template_arg, args): return var -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "http_request_host.cpp": {PlatformFramework.HOST_NATIVE}, - "http_request_arduino.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP8266_ARDUINO, - PlatformFramework.RP2040_ARDUINO, - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, - "http_request_idf.cpp": {PlatformFramework.ESP32_IDF}, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "http_request_host.cpp": {PlatformFramework.HOST_NATIVE}, + "http_request_arduino.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP8266_ARDUINO, + PlatformFramework.RP2040_ARDUINO, + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + "http_request_idf.cpp": {PlatformFramework.ESP32_IDF}, + } +) diff --git a/esphome/components/i2c/__init__.py b/esphome/components/i2c/__init__.py index db52479783..8b47403d67 100644 --- a/esphome/components/i2c/__init__.py +++ b/esphome/components/i2c/__init__.py @@ -22,6 +22,7 @@ from esphome.const import ( ) from esphome.core import CORE, coroutine_with_priority import esphome.final_validate as fv +from esphome.helpers import filter_source_files_from_platform LOGGER = logging.getLogger(__name__) CODEOWNERS = ["@esphome/core"] @@ -208,14 +209,16 @@ def final_validate_device_schema( ) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "i2c_bus_arduino.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP8266_ARDUINO, - PlatformFramework.RP2040_ARDUINO, - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, - "i2c_bus_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "i2c_bus_arduino.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP8266_ARDUINO, + PlatformFramework.RP2040_ARDUINO, + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + "i2c_bus_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, + } +) diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py index e4f6f15576..287f424467 100644 --- a/esphome/components/libretiny/__init__.py +++ b/esphome/components/libretiny/__init__.py @@ -24,6 +24,7 @@ from esphome.const import ( __version__, ) from esphome.core import CORE +from esphome.helpers import filter_source_files_from_platform from . import gpio # noqa from .const import ( @@ -343,10 +344,12 @@ async def component_to_code(config): await cg.register_component(var, config) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "gpio_arduino.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "gpio_arduino.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/logger/__init__.py b/esphome/components/logger/__init__.py index 4f65907210..001f99623c 100644 --- a/esphome/components/logger/__init__.py +++ b/esphome/components/logger/__init__.py @@ -45,6 +45,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE, Lambda, coroutine_with_priority +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@esphome/core"] logger_ns = cg.esphome_ns.namespace("logger") @@ -447,18 +448,23 @@ async def logger_set_level_to_code(config, action_id, template_arg, args): return cg.new_Pvariable(action_id, template_arg, lambda_) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "logger_esp32.cpp": {PlatformFramework.ESP32_ARDUINO, PlatformFramework.ESP32_IDF}, - "logger_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "logger_host.cpp": {PlatformFramework.HOST_NATIVE}, - "logger_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "logger_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, - "task_log_buffer.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "logger_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "logger_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "logger_host.cpp": {PlatformFramework.HOST_NATIVE}, + "logger_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "logger_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + "task_log_buffer.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + } +) diff --git a/esphome/components/mdns/__init__.py b/esphome/components/mdns/__init__.py index 43d214d77b..22f416c94f 100644 --- a/esphome/components/mdns/__init__.py +++ b/esphome/components/mdns/__init__.py @@ -11,6 +11,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE, coroutine_with_priority +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@esphome/core"] DEPENDENCIES = ["network"] @@ -111,14 +112,19 @@ async def to_code(config): cg.add(var.add_extra_service(exp)) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "mdns_esp32.cpp": {PlatformFramework.ESP32_ARDUINO, PlatformFramework.ESP32_IDF}, - "mdns_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "mdns_host.cpp": {PlatformFramework.HOST_NATIVE}, - "mdns_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "mdns_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "mdns_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "mdns_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "mdns_host.cpp": {PlatformFramework.HOST_NATIVE}, + "mdns_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "mdns_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py index 101761be9d..989a1a650b 100644 --- a/esphome/components/mqtt/__init__.py +++ b/esphome/components/mqtt/__init__.py @@ -57,6 +57,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE, coroutine_with_priority +from esphome.helpers import filter_source_files_from_platform DEPENDENCIES = ["network"] @@ -599,9 +600,11 @@ async def mqtt_disable_to_code(config, action_id, template_arg, args): return cg.new_Pvariable(action_id, template_arg, paren) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "mqtt_backend_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "mqtt_backend_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + } +) diff --git a/esphome/components/nextion/__init__.py b/esphome/components/nextion/__init__.py index 332c27409c..651e0ae5a4 100644 --- a/esphome/components/nextion/__init__.py +++ b/esphome/components/nextion/__init__.py @@ -1,6 +1,7 @@ import esphome.codegen as cg from esphome.components import uart from esphome.const import PlatformFramework +from esphome.helpers import filter_source_files_from_platform nextion_ns = cg.esphome_ns.namespace("nextion") Nextion = nextion_ns.class_("Nextion", cg.PollingComponent, uart.UARTDevice) @@ -10,14 +11,16 @@ CONF_NEXTION_ID = "nextion_id" CONF_PUBLISH_STATE = "publish_state" CONF_SEND_TO_NEXTION = "send_to_nextion" -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "nextion_upload_arduino.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP8266_ARDUINO, - PlatformFramework.RP2040_ARDUINO, - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, - "nextion_upload_idf.cpp": {PlatformFramework.ESP32_IDF}, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "nextion_upload_arduino.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP8266_ARDUINO, + PlatformFramework.RP2040_ARDUINO, + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + "nextion_upload_idf.cpp": {PlatformFramework.ESP32_IDF}, + } +) diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index bd7a97536c..83e637342b 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -10,6 +10,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE, coroutine_with_priority +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@esphome/core"] AUTO_LOAD = ["md5", "safe_mode"] @@ -123,14 +124,16 @@ async def ota_to_code(var, config): cg.add_define("USE_OTA_STATE_CALLBACK") -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "ota_backend_arduino_esp32.cpp": {PlatformFramework.ESP32_ARDUINO}, - "ota_backend_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, - "ota_backend_arduino_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "ota_backend_arduino_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "ota_backend_arduino_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "ota_backend_arduino_esp32.cpp": {PlatformFramework.ESP32_ARDUINO}, + "ota_backend_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, + "ota_backend_arduino_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "ota_backend_arduino_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "ota_backend_arduino_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/remote_receiver/__init__.py b/esphome/components/remote_receiver/__init__.py index f395aea3c8..82efe36879 100644 --- a/esphome/components/remote_receiver/__init__.py +++ b/esphome/components/remote_receiver/__init__.py @@ -18,6 +18,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE, TimePeriod +from esphome.helpers import filter_source_files_from_platform CONF_FILTER_SYMBOLS = "filter_symbols" CONF_RECEIVE_SYMBOLS = "receive_symbols" @@ -173,15 +174,17 @@ async def to_code(config): cg.add(var.set_idle_us(config[CONF_IDLE])) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "remote_receiver_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, - "remote_receiver_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "remote_receiver_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "remote_receiver_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "remote_receiver_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "remote_receiver_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/remote_transmitter/__init__.py b/esphome/components/remote_transmitter/__init__.py index 1e935354f9..dbda8a7752 100644 --- a/esphome/components/remote_transmitter/__init__.py +++ b/esphome/components/remote_transmitter/__init__.py @@ -15,6 +15,7 @@ from esphome.const import ( PlatformFramework, ) from esphome.core import CORE +from esphome.helpers import filter_source_files_from_platform AUTO_LOAD = ["remote_base"] @@ -98,15 +99,17 @@ async def to_code(config): ) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "remote_transmitter_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, - "remote_transmitter_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "remote_transmitter_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "remote_transmitter_esp32.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + "remote_transmitter_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "remote_transmitter_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/socket/__init__.py b/esphome/components/socket/__init__.py index 26031a8da5..dcbab9d240 100644 --- a/esphome/components/socket/__init__.py +++ b/esphome/components/socket/__init__.py @@ -1,5 +1,6 @@ import esphome.codegen as cg import esphome.config_validation as cv +from esphome.core import CORE CODEOWNERS = ["@esphome/core"] @@ -40,3 +41,21 @@ async def to_code(config): elif impl == IMPLEMENTATION_BSD_SOCKETS: cg.add_define("USE_SOCKET_IMPL_BSD_SOCKETS") cg.add_define("USE_SOCKET_SELECT_SUPPORT") + + +def FILTER_SOURCE_FILES() -> list[str]: + """Return list of socket implementation files that aren't selected by the user.""" + if not hasattr(CORE, "config") or "socket" not in CORE.config: + return [] + + impl = CORE.config["socket"][CONF_IMPLEMENTATION] + + # Build list of files to exclude based on selected implementation + excluded = [] + if impl != IMPLEMENTATION_LWIP_TCP: + excluded.append("lwip_raw_tcp_impl.cpp") + if impl != IMPLEMENTATION_BSD_SOCKETS: + excluded.append("bsd_sockets_impl.cpp") + if impl != IMPLEMENTATION_LWIP_SOCKETS: + excluded.append("lwip_sockets_impl.cpp") + return excluded diff --git a/esphome/components/spi/__init__.py b/esphome/components/spi/__init__.py index 43fd6920ac..d949da0a60 100644 --- a/esphome/components/spi/__init__.py +++ b/esphome/components/spi/__init__.py @@ -35,6 +35,7 @@ from esphome.const import ( ) from esphome.core import CORE, coroutine_with_priority import esphome.final_validate as fv +from esphome.helpers import filter_source_files_from_platform CODEOWNERS = ["@esphome/core", "@clydebarrow"] spi_ns = cg.esphome_ns.namespace("spi") @@ -426,14 +427,16 @@ def final_validate_device_schema(name: str, *, require_mosi: bool, require_miso: ) -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "spi_arduino.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP8266_ARDUINO, - PlatformFramework.RP2040_ARDUINO, - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, - "spi_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "spi_arduino.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP8266_ARDUINO, + PlatformFramework.RP2040_ARDUINO, + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + "spi_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, + } +) diff --git a/esphome/components/uart/__init__.py b/esphome/components/uart/__init__.py index 03341b5ff3..1bfcef8f61 100644 --- a/esphome/components/uart/__init__.py +++ b/esphome/components/uart/__init__.py @@ -31,6 +31,7 @@ from esphome.const import ( ) from esphome.core import CORE import esphome.final_validate as fv +from esphome.helpers import filter_source_files_from_platform from esphome.yaml_util import make_data_base CODEOWNERS = ["@esphome/core"] @@ -441,15 +442,17 @@ async def uart_write_to_code(config, action_id, template_arg, args): return var -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "uart_component_esp32_arduino.cpp": {PlatformFramework.ESP32_ARDUINO}, - "uart_component_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, - "uart_component_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "uart_component_host.cpp": {PlatformFramework.HOST_NATIVE}, - "uart_component_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, - "uart_component_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "uart_component_esp32_arduino.cpp": {PlatformFramework.ESP32_ARDUINO}, + "uart_component_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, + "uart_component_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "uart_component_host.cpp": {PlatformFramework.HOST_NATIVE}, + "uart_component_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO}, + "uart_component_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/components/wifi/__init__.py b/esphome/components/wifi/__init__.py index 47c59af241..a5a88862bf 100644 --- a/esphome/components/wifi/__init__.py +++ b/esphome/components/wifi/__init__.py @@ -43,6 +43,7 @@ from esphome.const import ( ) from esphome.core import CORE, HexInt, coroutine_with_priority import esphome.final_validate as fv +from esphome.helpers import filter_source_files_from_platform from . import wpa2_eap @@ -529,13 +530,15 @@ async def wifi_set_sta_to_code(config, action_id, template_arg, args): return var -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "wifi_component_esp32_arduino.cpp": {PlatformFramework.ESP32_ARDUINO}, - "wifi_component_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, - "wifi_component_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, - "wifi_component_libretiny.cpp": { - PlatformFramework.BK72XX_ARDUINO, - PlatformFramework.RTL87XX_ARDUINO, - PlatformFramework.LN882X_ARDUINO, - }, -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "wifi_component_esp32_arduino.cpp": {PlatformFramework.ESP32_ARDUINO}, + "wifi_component_esp_idf.cpp": {PlatformFramework.ESP32_IDF}, + "wifi_component_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO}, + "wifi_component_libretiny.cpp": { + PlatformFramework.BK72XX_ARDUINO, + PlatformFramework.RTL87XX_ARDUINO, + PlatformFramework.LN882X_ARDUINO, + }, + } +) diff --git a/esphome/core/config.py b/esphome/core/config.py index cfff50a5c8..4dc2e67e1d 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -41,6 +41,7 @@ from esphome.const import ( from esphome.core import CORE, coroutine_with_priority from esphome.helpers import ( copy_file_if_changed, + filter_source_files_from_platform, fnv1a_32bit_hash, get_str_env, walk_files, @@ -555,8 +556,13 @@ async def to_code(config: ConfigType) -> None: # Platform-specific source files for core -PLATFORM_SOURCE_FILES: dict[str, set[PlatformFramework]] = { - "ring_buffer.cpp": {PlatformFramework.ESP32_ARDUINO, PlatformFramework.ESP32_IDF}, - # Note: lock_free_queue.h and event_pool.h are header files and don't need to be filtered - # as they are only included when needed by the preprocessor -} +FILTER_SOURCE_FILES = filter_source_files_from_platform( + { + "ring_buffer.cpp": { + PlatformFramework.ESP32_ARDUINO, + PlatformFramework.ESP32_IDF, + }, + # Note: lock_free_queue.h and event_pool.h are header files and don't need to be filtered + # as they are only included when needed by the preprocessor + } +) diff --git a/esphome/helpers.py b/esphome/helpers.py index bf0e3b5cf7..153dcf6a6a 100644 --- a/esphome/helpers.py +++ b/esphome/helpers.py @@ -1,4 +1,5 @@ import codecs +from collections.abc import Callable from contextlib import suppress import ipaddress import logging @@ -7,8 +8,12 @@ from pathlib import Path import platform import re import tempfile +from typing import TYPE_CHECKING from urllib.parse import urlparse +if TYPE_CHECKING: + from esphome.const import PlatformFramework + _LOGGER = logging.getLogger(__name__) IS_MACOS = platform.system() == "Darwin" @@ -505,3 +510,54 @@ _DISALLOWED_CHARS = re.compile(r"[^a-zA-Z0-9-_]") def sanitize(value): """Same behaviour as `helpers.cpp` method `str_sanitize`.""" return _DISALLOWED_CHARS.sub("_", value) + + +def filter_source_files_from_platform( + files_map: dict[str, set["PlatformFramework"]], +) -> Callable[[], list[str]]: + """Helper to build a FILTER_SOURCE_FILES function from platform mapping. + + Args: + files_map: Dict mapping filename to set of PlatformFramework enums + that should compile this file + + Returns: + Function that returns list of files to exclude for current platform + """ + from esphome.const import ( + KEY_CORE, + KEY_TARGET_FRAMEWORK, + KEY_TARGET_PLATFORM, + PlatformFramework, + ) + from esphome.core import CORE + + # Pre-build lookup map from (platform, framework) tuples to PlatformFramework enum + _PLATFORM_FRAMEWORK_LOOKUP = {pf.value: pf for pf in PlatformFramework} + + def filter_source_files() -> list[str]: + # Get current platform/framework + core_data = CORE.data.get(KEY_CORE, {}) + target_platform = core_data.get(KEY_TARGET_PLATFORM) + target_framework = core_data.get(KEY_TARGET_FRAMEWORK) + + if not target_platform or not target_framework: + return [] + + # Direct lookup of current PlatformFramework + current_platform_framework = _PLATFORM_FRAMEWORK_LOOKUP.get( + (target_platform, target_framework) + ) + + if not current_platform_framework: + return [] + + # Return files that should be excluded for current platform + excluded = [] + for filename, platforms in files_map.items(): + if current_platform_framework not in platforms: + excluded.append(filename) + + return excluded + + return filter_source_files diff --git a/esphome/loader.py b/esphome/loader.py index 21e822da73..4a6847bd89 100644 --- a/esphome/loader.py +++ b/esphome/loader.py @@ -11,26 +11,13 @@ import sys from types import ModuleType from typing import Any -from esphome.const import ( - KEY_CORE, - KEY_TARGET_FRAMEWORK, - KEY_TARGET_PLATFORM, - SOURCE_FILE_EXTENSIONS, - Framework, - Platform, - PlatformFramework, -) +from esphome.const import SOURCE_FILE_EXTENSIONS from esphome.core import CORE import esphome.core.config from esphome.types import ConfigType _LOGGER = logging.getLogger(__name__) -# Build unified lookup table from PlatformFramework enum -_PLATFORM_FRAMEWORK_LOOKUP: dict[ - tuple[Platform, Framework | None], PlatformFramework -] = {pf.value: pf for pf in PlatformFramework} - @dataclass(frozen=True, order=True) class FileResource: @@ -123,28 +110,13 @@ class ComponentManifest: """Return a list of all file resources defined in the package of this component.""" ret: list[FileResource] = [] - # Get current platform-framework combination - core_data: dict[str, Any] = CORE.data.get(KEY_CORE, {}) - target_platform: Platform | None = core_data.get(KEY_TARGET_PLATFORM) - target_framework: Framework | None = core_data.get(KEY_TARGET_FRAMEWORK) + # Get filter function for source files + filter_source_files_func = getattr(self.module, "FILTER_SOURCE_FILES", None) - # Get platform-specific files mapping - platform_source_files: dict[str, set[PlatformFramework]] = getattr( - self.module, "PLATFORM_SOURCE_FILES", {} - ) - - # Get current PlatformFramework - lookup_key = (target_platform, target_framework) - current_platform_framework: PlatformFramework | None = ( - _PLATFORM_FRAMEWORK_LOOKUP.get(lookup_key) - ) - - # Build set of allowed filenames for current platform - allowed_filenames: set[str] = set() - if current_platform_framework and platform_source_files: - for filename, platforms in platform_source_files.items(): - if current_platform_framework in platforms: - allowed_filenames.add(filename) + # Get list of files to exclude + excluded_files: set[str] = set() + if filter_source_files_func is not None: + excluded_files = set(filter_source_files_func()) # Process all resources for resource in ( @@ -157,9 +129,8 @@ class ComponentManifest: if not importlib.resources.files(self.package).joinpath(resource).is_file(): continue - # Check platform restrictions only if file is platform-specific - # Common files (not in platform_source_files) are always included - if resource in platform_source_files and resource not in allowed_filenames: + # Skip excluded files + if resource in excluded_files: continue ret.append(FileResource(self.package, resource))