mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
Add mDNS config dump (#2576)
This commit is contained in:
parent
cac5b356db
commit
f2ebfe7aef
5 changed files with 40 additions and 26 deletions
|
@ -13,13 +13,16 @@
|
|||
namespace esphome {
|
||||
namespace mdns {
|
||||
|
||||
static const char *const TAG = "mdns";
|
||||
|
||||
#ifndef WEBSERVER_PORT
|
||||
#define WEBSERVER_PORT 80 // NOLINT
|
||||
#endif
|
||||
|
||||
std::vector<MDNSService> MDNSComponent::compile_services_() {
|
||||
std::vector<MDNSService> res;
|
||||
void MDNSComponent::compile_records_() {
|
||||
this->hostname_ = App.get_name();
|
||||
|
||||
this->services_.clear();
|
||||
#ifdef USE_API
|
||||
if (api::global_api_server != nullptr) {
|
||||
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()});
|
||||
#endif
|
||||
|
||||
res.push_back(service);
|
||||
this->services_.push_back(service);
|
||||
}
|
||||
#endif // USE_API
|
||||
|
||||
|
@ -60,11 +63,11 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
|||
service.service_type = "_prometheus-http";
|
||||
service.proto = "_tcp";
|
||||
service.port = WEBSERVER_PORT;
|
||||
res.push_back(service);
|
||||
this->services_.push_back(service);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (res.empty()) {
|
||||
if (this->services_.empty()) {
|
||||
// Publish "http" service if not using native API
|
||||
// This is just to have *some* mDNS service so that .local resolution works
|
||||
MDNSService service{};
|
||||
|
@ -72,11 +75,21 @@ std::vector<MDNSService> MDNSComponent::compile_services_() {
|
|||
service.proto = "_tcp";
|
||||
service.port = WEBSERVER_PORT;
|
||||
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 esphome
|
||||
|
|
|
@ -26,6 +26,7 @@ struct MDNSService {
|
|||
class MDNSComponent : public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
|
||||
#if defined(USE_ESP8266) && defined(USE_ARDUINO)
|
||||
void loop() override;
|
||||
|
@ -33,8 +34,9 @@ class MDNSComponent : public Component {
|
|||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||
|
||||
protected:
|
||||
std::vector<MDNSService> compile_services_();
|
||||
std::string compile_hostname_();
|
||||
std::vector<MDNSService> services_{};
|
||||
std::string hostname_;
|
||||
void compile_records_();
|
||||
};
|
||||
|
||||
} // namespace mdns
|
||||
|
|
|
@ -7,13 +7,12 @@
|
|||
namespace esphome {
|
||||
namespace mdns {
|
||||
|
||||
static const char *const TAG = "mdns";
|
||||
|
||||
void MDNSComponent::setup() {
|
||||
MDNS.begin(compile_hostname_().c_str());
|
||||
this->compile_records_();
|
||||
|
||||
auto services = compile_services_();
|
||||
for (const auto &service : services) {
|
||||
MDNS.begin(this->hostname_.c_str());
|
||||
|
||||
for (const auto &service : this->services_) {
|
||||
MDNS.addService(service.service_type.c_str(), service.proto.c_str(), service.port);
|
||||
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());
|
||||
|
|
|
@ -9,14 +9,13 @@
|
|||
namespace esphome {
|
||||
namespace mdns {
|
||||
|
||||
static const char *const TAG = "mdns";
|
||||
|
||||
void MDNSComponent::setup() {
|
||||
network::IPAddress addr = network::get_ip_address();
|
||||
MDNS.begin(compile_hostname_().c_str(), (uint32_t) addr);
|
||||
this->compile_records_();
|
||||
|
||||
auto services = compile_services_();
|
||||
for (const auto &service : services) {
|
||||
network::IPAddress addr = network::get_ip_address();
|
||||
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
|
||||
// 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
|
||||
|
|
|
@ -11,18 +11,19 @@ namespace mdns {
|
|||
static const char *const TAG = "mdns";
|
||||
|
||||
void MDNSComponent::setup() {
|
||||
this->compile_records_();
|
||||
|
||||
esp_err_t err = mdns_init();
|
||||
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();
|
||||
return;
|
||||
}
|
||||
|
||||
mdns_hostname_set(compile_hostname_().c_str());
|
||||
mdns_instance_name_set(compile_hostname_().c_str());
|
||||
mdns_hostname_set(this->hostname_.c_str());
|
||||
mdns_instance_name_set(this->hostname_.c_str());
|
||||
|
||||
auto services = compile_services_();
|
||||
for (const auto &service : services) {
|
||||
for (const auto &service : this->services_) {
|
||||
std::vector<mdns_txt_item_t> txt_records;
|
||||
for (const auto &record : service.txt_records) {
|
||||
mdns_txt_item_t it{};
|
||||
|
|
Loading…
Reference in a new issue