From 03767474b728c811785d0fb1181ab59045c7e210 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 24 Nov 2025 15:28:45 -0600 Subject: [PATCH] [mdns] Extract common Arduino mDNS registration to shared header --- esphome/components/mdns/mdns_arduino.h | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 esphome/components/mdns/mdns_arduino.h diff --git a/esphome/components/mdns/mdns_arduino.h b/esphome/components/mdns/mdns_arduino.h new file mode 100644 index 00000000000..8d72c86f5c5 --- /dev/null +++ b/esphome/components/mdns/mdns_arduino.h @@ -0,0 +1,42 @@ +#pragma once +#include "esphome/core/defines.h" + +// Common Arduino mDNS registration for RP2040 and LibreTiny +#if defined(USE_MDNS) && (defined(USE_RP2040) || defined(USE_LIBRETINY)) + +#include "esphome/core/application.h" +#include "mdns_component.h" + +namespace esphome::mdns { + +/// Register mDNS services using Arduino-style mDNS library (RP2040/LibreTiny) +/// @param services The services to register +inline void register_arduino_mdns(MDNSComponent *, StaticVector &services) { + MDNS.begin(App.get_name().c_str()); + + for (const auto &service : services) { + // Strip the leading underscore from the proto and service_type. While it is + // part of the wire protocol to have an underscore, and for example ESP-IDF + // expects the underscore to be there, the RP2040/LibreTiny mDNS + // implementations always add the underscore themselves. + auto *proto = MDNS_STR_ARG(service.proto); + while (*proto == '_') { + proto++; + } + + auto *service_type = MDNS_STR_ARG(service.service_type); + while (*service_type == '_') { + service_type++; + } + + uint16_t port = const_cast &>(service.port).value(); + MDNS.addService(service_type, proto, port); + for (const auto &record : service.txt_records) { + MDNS.addServiceTxt(service_type, proto, MDNS_STR_ARG(record.key), MDNS_STR_ARG(record.value)); + } + } +} + +} // namespace esphome::mdns + +#endif