ESPHome  2022.12.8
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  service.txt_records.push_back({"version", ESPHOME_VERSION});
34  service.txt_records.push_back({"mac", get_mac_address()});
35  const char *platform = nullptr;
36 #ifdef USE_ESP8266
37  platform = "ESP8266";
38 #endif
39 #ifdef USE_ESP32
40  platform = "ESP32";
41 #endif
42 #ifdef USE_RP2040
43  platform = "RP2040";
44 #endif
45  if (platform != nullptr) {
46  service.txt_records.push_back({"platform", platform});
47  }
48 
49  service.txt_records.push_back({"board", ESPHOME_BOARD});
50 
51 #if defined(USE_WIFI)
52  service.txt_records.push_back({"network", "wifi"});
53 #elif defined(USE_ETHERNET)
54  service.txt_records.push_back({"network", "ethernet"});
55 #endif
56 
57 #ifdef ESPHOME_PROJECT_NAME
58  service.txt_records.push_back({"project_name", ESPHOME_PROJECT_NAME});
59  service.txt_records.push_back({"project_version", ESPHOME_PROJECT_VERSION});
60 #endif // ESPHOME_PROJECT_NAME
61 
62 #ifdef USE_DASHBOARD_IMPORT
63  service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
64 #endif
65 
66  this->services_.push_back(service);
67  }
68 #endif // USE_API
69 
70 #ifdef USE_PROMETHEUS
71  {
72  MDNSService service{};
73  service.service_type = "_prometheus-http";
74  service.proto = "_tcp";
75  service.port = USE_WEBSERVER_PORT;
76  this->services_.push_back(service);
77  }
78 #endif
79 
80 #ifdef USE_WEBSERVER
81  {
82  MDNSService service{};
83  service.service_type = "_http";
84  service.proto = "_tcp";
85  service.port = USE_WEBSERVER_PORT;
86  this->services_.push_back(service);
87  }
88 #endif
89 
90  if (this->services_.empty()) {
91  // Publish "http" service if not using native API
92  // This is just to have *some* mDNS service so that .local resolution works
93  MDNSService service{};
94  service.service_type = "_http";
95  service.proto = "_tcp";
96  service.port = USE_WEBSERVER_PORT;
97  service.txt_records.push_back({"version", ESPHOME_VERSION});
98  this->services_.push_back(service);
99  }
100 }
101 
103  ESP_LOGCONFIG(TAG, "mDNS:");
104  ESP_LOGCONFIG(TAG, " Hostname: %s", this->hostname_.c_str());
105  ESP_LOGV(TAG, " Services:");
106  for (const auto &service : this->services_) {
107  ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), service.port);
108  for (const auto &record : service.txt_records) {
109  ESP_LOGV(TAG, " TXT: %s = %s", record.key.c_str(), record.value.c_str());
110  }
111  }
112 }
113 
114 } // namespace mdns
115 } // namespace esphome
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:442
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 set_name().
Definition: application.h:135
Definition: a4988.cpp:4
uint16_t get_port() const
Definition: api_server.cpp:379
APIServer * global_api_server
Definition: api_server.cpp:286