mirror of
https://github.com/esphome/esphome.git
synced 2024-11-17 10:55:36 +01:00
Add mDNS config dump (#2576)
This commit is contained in:
parent
cac5b356db
commit
f2ebfe7aef
@ -13,13 +13,16 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace mdns {
|
namespace mdns {
|
||||||
|
|
||||||
|
static const char *const TAG = "mdns";
|
||||||
|
|
||||||
#ifndef WEBSERVER_PORT
|
#ifndef WEBSERVER_PORT
|
||||||
#define WEBSERVER_PORT 80 // NOLINT
|
#define WEBSERVER_PORT 80 // NOLINT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
std::vector<MDNSService> MDNSComponent::compile_services_() {
|
void MDNSComponent::compile_records_() {
|
||||||
std::vector<MDNSService> res;
|
this->hostname_ = App.get_name();
|
||||||
|
|
||||||
|
this->services_.clear();
|
||||||
#ifdef USE_API
|
#ifdef USE_API
|
||||||
if (api::global_api_server != nullptr) {
|
if (api::global_api_server != nullptr) {
|
||||||
MDNSService service{};
|
MDNSService service{};
|
||||||
@ -50,7 +53,7 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
|||||||
service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
|
service.txt_records.push_back({"package_import_url", dashboard_import::get_package_import_url()});
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
res.push_back(service);
|
this->services_.push_back(service);
|
||||||
}
|
}
|
||||||
#endif // USE_API
|
#endif // USE_API
|
||||||
|
|
||||||
@ -60,11 +63,11 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
|||||||
service.service_type = "_prometheus-http";
|
service.service_type = "_prometheus-http";
|
||||||
service.proto = "_tcp";
|
service.proto = "_tcp";
|
||||||
service.port = WEBSERVER_PORT;
|
service.port = WEBSERVER_PORT;
|
||||||
res.push_back(service);
|
this->services_.push_back(service);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (res.empty()) {
|
if (this->services_.empty()) {
|
||||||
// Publish "http" service if not using native API
|
// Publish "http" service if not using native API
|
||||||
// This is just to have *some* mDNS service so that .local resolution works
|
// This is just to have *some* mDNS service so that .local resolution works
|
||||||
MDNSService service{};
|
MDNSService service{};
|
||||||
@ -72,11 +75,21 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
|||||||
service.proto = "_tcp";
|
service.proto = "_tcp";
|
||||||
service.port = WEBSERVER_PORT;
|
service.port = WEBSERVER_PORT;
|
||||||
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
service.txt_records.push_back({"version", ESPHOME_VERSION});
|
||||||
res.push_back(service);
|
this->services_.push_back(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MDNSComponent::dump_config() {
|
||||||
|
ESP_LOGCONFIG(TAG, "mDNS:");
|
||||||
|
ESP_LOGCONFIG(TAG, " Hostname: %s", this->hostname_.c_str());
|
||||||
|
ESP_LOGV(TAG, " Services:");
|
||||||
|
for (const auto &service : this->services_) {
|
||||||
|
ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), service.port);
|
||||||
|
for (const auto &record : service.txt_records) {
|
||||||
|
ESP_LOGV(TAG, " TXT: %s = %s", record.key.c_str(), record.value.c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
std::string MDNSComponent::compile_hostname_() { return App.get_name(); }
|
|
||||||
|
|
||||||
} // namespace mdns
|
} // namespace mdns
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
@ -26,6 +26,7 @@ struct MDNSService {
|
|||||||
class MDNSComponent : public Component {
|
class MDNSComponent : public Component {
|
||||||
public:
|
public:
|
||||||
void setup() override;
|
void setup() override;
|
||||||
|
void dump_config() override;
|
||||||
|
|
||||||
#if defined(USE_ESP8266) && defined(USE_ARDUINO)
|
#if defined(USE_ESP8266) && defined(USE_ARDUINO)
|
||||||
void loop() override;
|
void loop() override;
|
||||||
@ -33,8 +34,9 @@ class MDNSComponent : public Component {
|
|||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector<MDNSService> compile_services_();
|
std::vector<MDNSService> services_{};
|
||||||
std::string compile_hostname_();
|
std::string hostname_;
|
||||||
|
void compile_records_();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace mdns
|
} // namespace mdns
|
||||||
|
@ -7,13 +7,12 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace mdns {
|
namespace mdns {
|
||||||
|
|
||||||
static const char *const TAG = "mdns";
|
|
||||||
|
|
||||||
void MDNSComponent::setup() {
|
void MDNSComponent::setup() {
|
||||||
MDNS.begin(compile_hostname_().c_str());
|
this->compile_records_();
|
||||||
|
|
||||||
auto services = compile_services_();
|
MDNS.begin(this->hostname_.c_str());
|
||||||
for (const auto &service : services) {
|
|
||||||
|
for (const auto &service : this->services_) {
|
||||||
MDNS.addService(service.service_type.c_str(), service.proto.c_str(), service.port);
|
MDNS.addService(service.service_type.c_str(), service.proto.c_str(), service.port);
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
MDNS.addServiceTxt(service.service_type.c_str(), service.proto.c_str(), record.key.c_str(), record.value.c_str());
|
MDNS.addServiceTxt(service.service_type.c_str(), service.proto.c_str(), record.key.c_str(), record.value.c_str());
|
||||||
|
@ -9,14 +9,13 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace mdns {
|
namespace mdns {
|
||||||
|
|
||||||
static const char *const TAG = "mdns";
|
|
||||||
|
|
||||||
void MDNSComponent::setup() {
|
void MDNSComponent::setup() {
|
||||||
network::IPAddress addr = network::get_ip_address();
|
this->compile_records_();
|
||||||
MDNS.begin(compile_hostname_().c_str(), (uint32_t) addr);
|
|
||||||
|
|
||||||
auto services = compile_services_();
|
network::IPAddress addr = network::get_ip_address();
|
||||||
for (const auto &service : services) {
|
MDNS.begin(this->hostname_.c_str(), (uint32_t) addr);
|
||||||
|
|
||||||
|
for (const auto &service : this->services_) {
|
||||||
// Strip the leading underscore from the proto and service_type. While it is
|
// 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
|
// part of the wire protocol to have an underscore, and for example ESP-IDF
|
||||||
// expects the underscore to be there, the ESP8266 implementation always adds
|
// expects the underscore to be there, the ESP8266 implementation always adds
|
||||||
|
@ -11,18 +11,19 @@ namespace mdns {
|
|||||||
static const char *const TAG = "mdns";
|
static const char *const TAG = "mdns";
|
||||||
|
|
||||||
void MDNSComponent::setup() {
|
void MDNSComponent::setup() {
|
||||||
|
this->compile_records_();
|
||||||
|
|
||||||
esp_err_t err = mdns_init();
|
esp_err_t err = mdns_init();
|
||||||
if (err != ESP_OK) {
|
if (err != ESP_OK) {
|
||||||
ESP_LOGW(TAG, "MDNS init failed: %s", esp_err_to_name(err));
|
ESP_LOGW(TAG, "mDNS init failed: %s", esp_err_to_name(err));
|
||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
mdns_hostname_set(compile_hostname_().c_str());
|
mdns_hostname_set(this->hostname_.c_str());
|
||||||
mdns_instance_name_set(compile_hostname_().c_str());
|
mdns_instance_name_set(this->hostname_.c_str());
|
||||||
|
|
||||||
auto services = compile_services_();
|
for (const auto &service : this->services_) {
|
||||||
for (const auto &service : services) {
|
|
||||||
std::vector<mdns_txt_item_t> txt_records;
|
std::vector<mdns_txt_item_t> txt_records;
|
||||||
for (const auto &record : service.txt_records) {
|
for (const auto &record : service.txt_records) {
|
||||||
mdns_txt_item_t it{};
|
mdns_txt_item_t it{};
|
||||||
|
Loading…
Reference in New Issue
Block a user