[mdns] Address review feedback: tighten guards, comments, validator

- 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).
This commit is contained in:
J. Nick Koston
2026-04-23 20:34:29 -05:00
parent ae41427523
commit 4f8cfff4ac
5 changed files with 14 additions and 17 deletions
+1 -3
View File
@@ -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
+4 -3
View File
@@ -25,8 +25,8 @@
namespace esphome::mdns {
#ifdef USE_MDNS_EVENT_DRIVEN_POLLING
/// Platform-specific MDNS.update() trampoline. Defined in mdns_<platform>.cpp so the
/// shared code can schedule it without including the platform's mDNS header.
/// MDNS.update() trampoline. Defined in exactly one mdns_<platform>.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<MDNSService, MDNS_SERVICE_COUNT> 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<MDNSService, MDNS_SERVICE_COUNT> &services, char *mac_address_buf);
+5 -5
View File
@@ -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
+2 -6
View File
@@ -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")
+2
View File
@@ -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