From 4f8cfff4accdf8724693728932ede3170c9e6b84 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 23 Apr 2026 20:34:29 -0500 Subject: [PATCH] [mdns] Address review feedback: tighten guards, comments, validator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Drop redundant wifi_component.h / ethernet_component.h includes from the platform .cpp files — mdns_component.h already pulls them in transitively under their listener defines. - Guard initialized_ with USE_RP2040 && USE_MDNS_EVENT_DRIVEN_POLLING instead of USE_RP2040 alone, making the coupling with the listener-driven path explicit. - Short comment on mdns_pump_update noting ODR is preserved by FILTER_SOURCE_FILES compiling exactly one platform cpp per build. - Inline comment on ESP8266 setup() noting AFTER_CONNECTION priority is why the unconditional start_polling_window_() is safe. - Collapse the disabled/platform early-return in _require_network_interface; drop the redundant CORE.using_arduino check (ESP8266/RP2040 are always Arduino). - Add USE_MDNS_EVENT_DRIVEN_POLLING and USE_MDNS_WIFI_LISTENER to defines.h for static-analysis discoverability (ethernet listener is mutually exclusive with wifi on these platforms, so one representative is enough). --- esphome/components/mdns/__init__.py | 4 +--- esphome/components/mdns/mdns_component.h | 7 ++++--- esphome/components/mdns/mdns_esp8266.cpp | 10 +++++----- esphome/components/mdns/mdns_rp2040.cpp | 8 ++------ esphome/core/defines.h | 2 ++ 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/esphome/components/mdns/__init__.py b/esphome/components/mdns/__init__.py index e7e1323f402..741669280c8 100644 --- a/esphome/components/mdns/__init__.py +++ b/esphome/components/mdns/__init__.py @@ -70,9 +70,7 @@ def _require_network_interface(config: ConfigType) -> ConfigType: window. Reject at config time rather than silently producing a component that never initializes. """ - if config.get(CONF_DISABLED): - return config - if not CORE.using_arduino or not (CORE.is_esp8266 or CORE.is_rp2040): + if config.get(CONF_DISABLED) or not (CORE.is_esp8266 or CORE.is_rp2040): return config full_config = fv.full_config.get() has_wifi = "wifi" in full_config diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index 0e848ba975d..95bf2bf27fa 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -25,8 +25,8 @@ namespace esphome::mdns { #ifdef USE_MDNS_EVENT_DRIVEN_POLLING -/// Platform-specific MDNS.update() trampoline. Defined in mdns_.cpp so the -/// shared code can schedule it without including the platform's mDNS header. +/// MDNS.update() trampoline. Defined in exactly one mdns_.cpp per build +/// (FILTER_SOURCE_FILES in __init__.py enforces this), so ODR is preserved. void mdns_pump_update(); #endif @@ -159,7 +159,8 @@ class MDNSComponent final : public Component #ifdef USE_MDNS_STORE_SERVICES StaticVector services_{}; #endif -#ifdef USE_RP2040 +#if defined(USE_RP2040) && defined(USE_MDNS_EVENT_DRIVEN_POLLING) + // RP2040 defers MDNS.begin() until the first IP-up event; this tracks that. bool initialized_{false}; #endif void compile_records_(StaticVector &services, char *mac_address_buf); diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index bc121542583..d77d9685dd8 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -8,9 +8,8 @@ #include "esphome/core/hal.h" #include "esphome/core/log.h" #include "mdns_component.h" -#ifdef USE_MDNS_EVENT_DRIVEN_POLLING -#include "esphome/components/wifi/wifi_component.h" -#endif +// wifi_component.h is pulled in transitively by mdns_component.h when +// USE_MDNS_WIFI_LISTENER is defined. namespace esphome::mdns { @@ -46,8 +45,9 @@ void mdns_pump_update() { MDNS.update(); } void MDNSComponent::setup() { this->setup_buffers_and_register_(register_esp8266); #ifdef USE_MDNS_EVENT_DRIVEN_POLLING - // LEAmDNS's own LwipIntf::statusChangeCB drives _restart() on netif changes; we only - // need to arm the polling window around the initial probe/announce and each reconnect. + // LEAmDNS's own LwipIntf::statusChangeCB drives _restart() on netif changes; we just + // arm the window around the initial probe/announce and each reconnect. Unconditional + // here is safe: setup_priority::AFTER_CONNECTION guarantees the network is up. wifi::global_wifi_component->add_ip_state_listener(this); this->start_polling_window_(); #endif diff --git a/esphome/components/mdns/mdns_rp2040.cpp b/esphome/components/mdns/mdns_rp2040.cpp index 1b3c13178d7..a8b0af4ad62 100644 --- a/esphome/components/mdns/mdns_rp2040.cpp +++ b/esphome/components/mdns/mdns_rp2040.cpp @@ -6,12 +6,8 @@ #include "esphome/core/application.h" #include "esphome/core/log.h" #include "mdns_component.h" -#ifdef USE_MDNS_WIFI_LISTENER -#include "esphome/components/wifi/wifi_component.h" -#endif -#ifdef USE_MDNS_ETHERNET_LISTENER -#include "esphome/components/ethernet/ethernet_component.h" -#endif +// wifi_component.h / ethernet_component.h are pulled in transitively by +// mdns_component.h when their respective listener defines are active. // Arduino-Pico's PolledTimeout.h (pulled in by ESP8266mDNS.h) redefines IRAM_ATTR to empty. #pragma push_macro("IRAM_ATTR") diff --git a/esphome/core/defines.h b/esphome/core/defines.h index 80247f69da1..53b78a97df5 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -111,6 +111,8 @@ #define MDNS_SERVICE_COUNT 3 #define USE_MDNS_DYNAMIC_TXT #define MDNS_DYNAMIC_TXT_COUNT 2 +#define USE_MDNS_EVENT_DRIVEN_POLLING +#define USE_MDNS_WIFI_LISTENER #define MICRONOVA_LISTENER_COUNT 1 #define USE_MICRONOVA_WRITER #define SERIAL_PROXY_COUNT 2