From ba7c06785a03f1f7c570b572e5aa677d0ed9d073 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Apr 2026 19:14:55 -0500 Subject: [PATCH] [mdns] Broadcast config_hash TXT record on _esphomelib._tcp (#16145) --- esphome/components/mdns/mdns_component.cpp | 40 ++++++++++++++++++++-- esphome/components/mdns/mdns_component.h | 33 +++++------------- esphome/components/mdns/mdns_host.cpp | 7 +++- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index e05373ac5d3..9bf27e71e4c 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -39,7 +39,39 @@ MDNS_STATIC_CONST_CHAR(SERVICE_TCP, "_tcp"); // Wrap build-time defines into flash storage MDNS_STATIC_CONST_CHAR(VALUE_VERSION, ESPHOME_VERSION); -void MDNSComponent::compile_records_(StaticVector &services, char *mac_address_buf) { +void MDNSComponent::setup_buffers_and_register_(PlatformRegisterFn platform_register) { +#ifdef USE_MDNS_STORE_SERVICES + auto &services = this->services_; +#else + StaticVector services_storage; + auto &services = services_storage; +#endif + +#ifdef USE_API +#ifdef USE_MDNS_STORE_SERVICES + get_mac_address_into_buffer(this->mac_address_); + char *mac_ptr = this->mac_address_; + format_hex_to(this->config_hash_str_, App.get_config_hash()); + char *cfg_ptr = this->config_hash_str_; +#else + char mac_address[MAC_ADDRESS_BUFFER_SIZE]; + char config_hash_str[CONFIG_HASH_STR_SIZE]; + get_mac_address_into_buffer(mac_address); + format_hex_to(config_hash_str, App.get_config_hash()); + char *mac_ptr = mac_address; + char *cfg_ptr = config_hash_str; +#endif +#else + char *mac_ptr = nullptr; + char *cfg_ptr = nullptr; +#endif + + this->compile_records_(services, mac_ptr, cfg_ptr); + platform_register(this, services); +} + +void MDNSComponent::compile_records_(StaticVector &services, char *mac_address_buf, + char *config_hash_buf) { // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES // in mdns/__init__.py. If you add a new service here, update both locations. @@ -47,6 +79,7 @@ void MDNSComponent::compile_records_(StaticVector &); - void setup_buffers_and_register_(PlatformRegisterFn platform_register) { -#ifdef USE_MDNS_STORE_SERVICES - auto &services = this->services_; -#else - StaticVector services_storage; - auto &services = services_storage; -#endif - -#ifdef USE_API -#ifdef USE_MDNS_STORE_SERVICES - get_mac_address_into_buffer(this->mac_address_); - char *mac_ptr = this->mac_address_; -#else - char mac_address[MAC_ADDRESS_BUFFER_SIZE]; - get_mac_address_into_buffer(mac_address); - char *mac_ptr = mac_address; -#endif -#else - char *mac_ptr = nullptr; -#endif - - this->compile_records_(services, mac_ptr); - platform_register(this, services); - } + void setup_buffers_and_register_(PlatformRegisterFn platform_register); #ifdef USE_MDNS_DYNAMIC_TXT /// Storage for runtime-generated TXT values from user lambdas @@ -159,6 +139,8 @@ class MDNSComponent final : public Component #if defined(USE_API) && defined(USE_MDNS_STORE_SERVICES) /// Fixed buffer for MAC address (only needed when services are stored) char mac_address_[MAC_ADDRESS_BUFFER_SIZE]; + /// Fixed buffer for config hash hex string (only needed when services are stored) + char config_hash_str_[CONFIG_HASH_STR_SIZE]; #endif #ifdef USE_MDNS_STORE_SERVICES StaticVector services_{}; @@ -167,7 +149,8 @@ class MDNSComponent final : public Component // 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); + void compile_records_(StaticVector &services, char *mac_address_buf, + char *config_hash_buf); }; } // namespace esphome::mdns diff --git a/esphome/components/mdns/mdns_host.cpp b/esphome/components/mdns/mdns_host.cpp index 4d902319b88..1e66a10df09 100644 --- a/esphome/components/mdns/mdns_host.cpp +++ b/esphome/components/mdns/mdns_host.cpp @@ -3,6 +3,8 @@ #include "esphome/components/network/ip_address.h" #include "esphome/components/network/util.h" +#include "esphome/core/application.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "mdns_component.h" @@ -13,10 +15,13 @@ void MDNSComponent::setup() { #ifdef USE_API get_mac_address_into_buffer(this->mac_address_); char *mac_ptr = this->mac_address_; + format_hex_to(this->config_hash_str_, App.get_config_hash()); + char *cfg_ptr = this->config_hash_str_; #else char *mac_ptr = nullptr; + char *cfg_ptr = nullptr; #endif - this->compile_records_(this->services_, mac_ptr); + this->compile_records_(this->services_, mac_ptr, cfg_ptr); #endif // Host platform doesn't have actual mDNS implementation }