ESPHome  2024.12.2
mdns_component.cpp
Go to the documentation of this file.
1 #include "esphome/core/defines.h"
2 #ifdef USE_MDNS
3 #include "mdns_component.h"
4 #include "esphome/core/version.h"
6 #include "esphome/core/log.h"
7 
8 #ifdef USE_API
10 #endif
11 #ifdef USE_DASHBOARD_IMPORT
13 #endif
14 
15 namespace esphome {
16 namespace mdns {
17 
18 static 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  service.txt_records.push_back({"api_encryption", "Noise_NNpsk0_25519_ChaChaPoly_SHA256"});
66 #endif
67 
68 #ifdef ESPHOME_PROJECT_NAME
69  service.txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME});
70  service.txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION});
71 #endif // ESPHOME_PROJECT_NAME
72 
73 #ifdef USE_DASHBOARD_IMPORT
74  service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
75 #endif
76 
77  this->services_.push_back(service);
78  }
79 #endif // USE_API
80 
81 #ifdef USE_PROMETHEUS
82  {
83  MDNSService service{};
84  service.service_type = "_prometheus-http";
85  service.proto = "_tcp";
86  service.port = USE_WEBSERVER_PORT;
87  this->services_.push_back(service);
88  }
89 #endif
90 
91 #ifdef USE_WEBSERVER
92  {
93  MDNSService service{};
94  service.service_type = "_http";
95  service.proto = "_tcp";
96  service.port = USE_WEBSERVER_PORT;
97  this->services_.push_back(service);
98  }
99 #endif
100 
101  this->services_.insert(this->services_.end(), this->services_extra_.begin(), this->services_extra_.end());
102 
103  if (this->services_.empty()) {
104  // Publish "http" service if not using native API
105  // This is just to have *some* mDNS service so that .local resolution works
106  MDNSService service{};
107  service.service_type = "_http";
108  service.proto = "_tcp";
109  service.port = USE_WEBSERVER_PORT;
110  service.txt_records.push_back({"version", ESPHOME_VERSION});
111  this->services_.push_back(service);
112  }
113 }
114 
116  ESP_LOGCONFIG(TAG, "mDNS:");
117  ESP_LOGCONFIG(TAG, " Hostname: %s", this->hostname_.c_str());
118  ESP_LOGV(TAG, " Services:");
119  for (const auto &service : this->services_) {
120  ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), service.port);
121  for (const auto &record : service.txt_records) {
122  ESP_LOGV(TAG, " TXT: %s = %s", record.key.c_str(), record.value.c_str());
123  }
124  }
125 }
126 
127 } // namespace mdns
128 } // namespace esphome
129 #endif
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:205
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:723
std::vector< MDNSService > services_
Application App
Global storage of Application pointer - only one Application can exist.
std::string get_package_import_url()
const std::string & get_name() const
Get the name of this Application set by pre_setup().
Definition: application.h:202
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< MDNSService > services_extra_
uint16_t get_port() const
Definition: api_server.cpp:378
APIServer * global_api_server
Definition: api_server.cpp:347