ESPHome 2025.5.0
Loading...
Searching...
No Matches
mdns_component.cpp
Go to the documentation of this file.
2#ifdef USE_MDNS
4#include "esphome/core/log.h"
6#include "mdns_component.h"
7
8#ifdef USE_API
10#endif
11#ifdef USE_DASHBOARD_IMPORT
13#endif
14
15namespace esphome {
16namespace mdns {
17
18static const char *const TAG = "mdns";
19
20#ifndef USE_WEBSERVER_PORT
21#define USE_WEBSERVER_PORT 80 // NOLINT
22#endif
23
25 this->hostname_ = App.get_name();
26
27 this->services_.clear();
28#ifdef USE_API
29 if (api::global_api_server != nullptr) {
30 MDNSService service{};
31 service.service_type = "_esphomelib";
32 service.proto = "_tcp";
33 service.port = api::global_api_server->get_port();
34 if (!App.get_friendly_name().empty()) {
35 service.txt_records.push_back({"friendly_name", App.get_friendly_name()});
36 }
37 service.txt_records.push_back({"version", ESPHOME_VERSION});
38 service.txt_records.push_back({"mac", get_mac_address()});
39 const char *platform = nullptr;
40#ifdef USE_ESP8266
41 platform = "ESP8266";
42#endif
43#ifdef USE_ESP32
44 platform = "ESP32";
45#endif
46#ifdef USE_RP2040
47 platform = "RP2040";
48#endif
49#ifdef USE_LIBRETINY
50 platform = lt_cpu_get_model_name();
51#endif
52 if (platform != nullptr) {
53 service.txt_records.push_back({"platform", platform});
54 }
55
56 service.txt_records.push_back({"board", ESPHOME_BOARD});
57
58#if defined(USE_WIFI)
59 service.txt_records.push_back({"network", "wifi"});
60#elif defined(USE_ETHERNET)
61 service.txt_records.push_back({"network", "ethernet"});
62#endif
63
64#ifdef USE_API_NOISE
65 if (api::global_api_server->get_noise_ctx()->has_psk()) {
66 service.txt_records.push_back({"api_encryption", "Noise_NNpsk0_25519_ChaChaPoly_SHA256"});
67 } else {
68 service.txt_records.push_back({"api_encryption_supported", "Noise_NNpsk0_25519_ChaChaPoly_SHA256"});
69 }
70#endif
71
72#ifdef ESPHOME_PROJECT_NAME
73 service.txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME});
74 service.txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION});
75#endif // ESPHOME_PROJECT_NAME
76
77#ifdef USE_DASHBOARD_IMPORT
78 service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
79#endif
80
81 this->services_.push_back(service);
82 }
83#endif // USE_API
84
85#ifdef USE_PROMETHEUS
86 {
87 MDNSService service{};
88 service.service_type = "_prometheus-http";
89 service.proto = "_tcp";
90 service.port = USE_WEBSERVER_PORT;
91 this->services_.push_back(service);
92 }
93#endif
94
95#ifdef USE_WEBSERVER
96 {
97 MDNSService service{};
98 service.service_type = "_http";
99 service.proto = "_tcp";
100 service.port = USE_WEBSERVER_PORT;
101 this->services_.push_back(service);
102 }
103#endif
104
105 this->services_.insert(this->services_.end(), this->services_extra_.begin(), this->services_extra_.end());
106
107 if (this->services_.empty()) {
108 // Publish "http" service if not using native API
109 // This is just to have *some* mDNS service so that .local resolution works
110 MDNSService service{};
111 service.service_type = "_http";
112 service.proto = "_tcp";
113 service.port = USE_WEBSERVER_PORT;
114 service.txt_records.push_back({"version", ESPHOME_VERSION});
115 this->services_.push_back(service);
116 }
117}
118
120 ESP_LOGCONFIG(TAG, "mDNS:");
121 ESP_LOGCONFIG(TAG, " Hostname: %s", this->hostname_.c_str());
122 ESP_LOGV(TAG, " Services:");
123 for (const auto &service : this->services_) {
124 ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(),
125 const_cast<TemplatableValue<uint16_t> &>(service.port).value());
126 for (const auto &record : service.txt_records) {
127 ESP_LOGV(TAG, " TXT: %s = %s", record.key.c_str(),
128 const_cast<TemplatableValue<std::string> &>(record.value).value().c_str());
129 }
130 }
131}
132
133} // namespace mdns
134} // namespace esphome
135#endif
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
const std::string & get_name() const
Get the name of this Application set by pre_setup().
uint16_t get_port() const
std::vector< MDNSService > services_
APIServer * global_api_server
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition helpers.cpp:726
Application App
Global storage of Application pointer - only one Application can exist.