ESPHome  2024.4.1
mdns_component.cpp
Go to the documentation of this file.
1 #include "mdns_component.h"
2 #include "esphome/core/defines.h"
3 #include "esphome/core/version.h"
5 #include "esphome/core/log.h"
6 
7 #ifdef USE_API
9 #endif
10 #ifdef USE_DASHBOARD_IMPORT
12 #endif
13 
14 namespace esphome {
15 namespace mdns {
16 
17 static const char *const TAG = "mdns";
18 
19 #ifndef USE_WEBSERVER_PORT
20 #define USE_WEBSERVER_PORT 80 // NOLINT
21 #endif
22 
24  this->hostname_ = App.get_name();
25 
26  this->services_.clear();
27 #ifdef USE_API
28  if (api::global_api_server != nullptr) {
29  MDNSService service{};
30  service.service_type = "_esphomelib";
31  service.proto = "_tcp";
32  service.port = api::global_api_server->get_port();
33  if (!App.get_friendly_name().empty()) {
34  service.txt_records.push_back({"friendly_name", App.get_friendly_name()});
35  }
36  service.txt_records.push_back({"version", ESPHOME_VERSION});
37  service.txt_records.push_back({"mac", get_mac_address()});
38  const char *platform = nullptr;
39 #ifdef USE_ESP8266
40  platform = "ESP8266";
41 #endif
42 #ifdef USE_ESP32
43  platform = "ESP32";
44 #endif
45 #ifdef USE_RP2040
46  platform = "RP2040";
47 #endif
48 #ifdef USE_LIBRETINY
49  platform = lt_cpu_get_model_name();
50 #endif
51  if (platform != nullptr) {
52  service.txt_records.push_back({"platform", platform});
53  }
54 
55  service.txt_records.push_back({"board", ESPHOME_BOARD});
56 
57 #if defined(USE_WIFI)
58  service.txt_records.push_back({"network", "wifi"});
59 #elif defined(USE_ETHERNET)
60  service.txt_records.push_back({"network", "ethernet"});
61 #endif
62 
63 #ifdef USE_API_NOISE
64  service.txt_records.push_back({"api_encryption", "Noise_NNpsk0_25519_ChaChaPoly_SHA256"});
65 #endif
66 
67 #ifdef ESPHOME_PROJECT_NAME
68  service.txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME});
69  service.txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION});
70 #endif // ESPHOME_PROJECT_NAME
71 
72 #ifdef USE_DASHBOARD_IMPORT
73  service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
74 #endif
75 
76  this->services_.push_back(service);
77  }
78 #endif // USE_API
79 
80 #ifdef USE_PROMETHEUS
81  {
82  MDNSService service{};
83  service.service_type = "_prometheus-http";
84  service.proto = "_tcp";
85  service.port = USE_WEBSERVER_PORT;
86  this->services_.push_back(service);
87  }
88 #endif
89 
90 #ifdef USE_WEBSERVER
91  {
92  MDNSService service{};
93  service.service_type = "_http";
94  service.proto = "_tcp";
95  service.port = USE_WEBSERVER_PORT;
96  this->services_.push_back(service);
97  }
98 #endif
99 
100  this->services_.insert(this->services_.end(), this->services_extra_.begin(), this->services_extra_.end());
101 
102  if (this->services_.empty()) {
103  // Publish "http" service if not using native API
104  // This is just to have *some* mDNS service so that .local resolution works
105  MDNSService service{};
106  service.service_type = "_http";
107  service.proto = "_tcp";
108  service.port = USE_WEBSERVER_PORT;
109  service.txt_records.push_back({"version", ESPHOME_VERSION});
110  this->services_.push_back(service);
111  }
112 }
113 
115  ESP_LOGCONFIG(TAG, "mDNS:");
116  ESP_LOGCONFIG(TAG, " Hostname: %s", this->hostname_.c_str());
117  ESP_LOGV(TAG, " Services:");
118  for (const auto &service : this->services_) {
119  ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), service.port);
120  for (const auto &record : service.txt_records) {
121  ESP_LOGV(TAG, " TXT: %s = %s", record.key.c_str(), record.value.c_str());
122  }
123  }
124 }
125 
126 } // namespace mdns
127 } // namespace esphome
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:177
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:587
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:174
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
std::vector< MDNSService > services_extra_
uint16_t get_port() const
Definition: api_server.cpp:335
APIServer * global_api_server
Definition: api_server.cpp:314