From 76defeac39bb129a6cc0c278829b4310da71a0cb Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 1 Oct 2025 23:26:29 +0200 Subject: [PATCH] preen --- esphome/components/mdns/mdns_component.cpp | 26 ++++++-------------- esphome/components/mdns/mdns_component.h | 10 +++----- esphome/components/mdns/mdns_esp32.cpp | 3 +-- esphome/components/mdns/mdns_esp8266.cpp | 3 +-- esphome/components/mdns/mdns_libretiny.cpp | 3 +-- esphome/components/mdns/mdns_rp2040.cpp | 3 +-- esphome/components/openthread/openthread.cpp | 8 +++--- esphome/core/helpers.h | 3 +++ 8 files changed, 21 insertions(+), 38 deletions(-) diff --git a/esphome/components/mdns/mdns_component.cpp b/esphome/components/mdns/mdns_component.cpp index 58952e94e9..eb9a355d40 100644 --- a/esphome/components/mdns/mdns_component.cpp +++ b/esphome/components/mdns/mdns_component.cpp @@ -73,14 +73,13 @@ MDNS_STATIC_CONST_CHAR(NETWORK_THREAD, "thread"); void MDNSComponent::compile_records_() { this->hostname_ = App.get_name(); - this->services_count_ = 0; // IMPORTANT: The #ifdef blocks below must match COMPONENTS_WITH_MDNS_SERVICES // in mdns/__init__.py. If you add a new service here, update both locations. #ifdef USE_API if (api::global_api_server != nullptr) { - auto &service = this->services_[this->services_count_++]; + auto &service = this->services_[this->services_.count()++]; service.service_type = MDNS_STR(SERVICE_ESPHOMELIB); service.proto = MDNS_STR(SERVICE_TCP); service.port = api::global_api_server->get_port(); @@ -159,29 +158,23 @@ void MDNSComponent::compile_records_() { #endif // USE_API #ifdef USE_PROMETHEUS - auto &prom_service = this->services_[this->services_count_++]; + auto &prom_service = this->services_[this->services_.count()++]; prom_service.service_type = MDNS_STR(SERVICE_PROMETHEUS); prom_service.proto = MDNS_STR(SERVICE_TCP); prom_service.port = USE_WEBSERVER_PORT; #endif #ifdef USE_WEBSERVER - auto &web_service = this->services_[this->services_count_++]; + auto &web_service = this->services_[this->services_.count()++]; web_service.service_type = MDNS_STR(SERVICE_HTTP); web_service.proto = MDNS_STR(SERVICE_TCP); web_service.port = USE_WEBSERVER_PORT; #endif -#ifdef USE_MDNS_EXTRA_SERVICES - for (const auto &extra_service : this->services_extra_) { - this->services_[this->services_count_++] = extra_service; - } -#endif - #if !defined(USE_API) && !defined(USE_PROMETHEUS) && !defined(USE_WEBSERVER) && !defined(USE_MDNS_EXTRA_SERVICES) // Publish "http" service if not using native API or any other services // This is just to have *some* mDNS service so that .local resolution works - auto &fallback_service = this->services_[this->services_count_++]; + auto &fallback_service = this->services_[this->services_.count()++]; fallback_service.service_type = "_http"; fallback_service.proto = "_tcp"; fallback_service.port = USE_WEBSERVER_PORT; @@ -189,12 +182,6 @@ void MDNSComponent::compile_records_() { #endif } -#ifdef USE_MDNS_EXTRA_SERVICES -void MDNSComponent::add_extra_service(MDNSService service) { - this->services_[this->services_count_++] = std::move(service); -} -#endif - void MDNSComponent::dump_config() { ESP_LOGCONFIG(TAG, "mDNS:\n" @@ -202,8 +189,7 @@ void MDNSComponent::dump_config() { this->hostname_.c_str()); #if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE ESP_LOGV(TAG, " Services:"); - for (uint8_t i = 0; i < this->services_count_; i++) { - const auto &service = this->services_[i]; + for (const auto &service : this->services_) { ESP_LOGV(TAG, " - %s, %s, %d", service.service_type.c_str(), service.proto.c_str(), const_cast &>(service.port).value()); for (const auto &record : service.txt_records) { @@ -214,6 +200,8 @@ void MDNSComponent::dump_config() { #endif } +StaticVector MDNSComponent::get_services() { return this->services_; } + } // namespace mdns } // namespace esphome #endif diff --git a/esphome/components/mdns/mdns_component.h b/esphome/components/mdns/mdns_component.h index e653e9384f..4e1320c0e5 100644 --- a/esphome/components/mdns/mdns_component.h +++ b/esphome/components/mdns/mdns_component.h @@ -1,10 +1,10 @@ #pragma once #include "esphome/core/defines.h" #ifdef USE_MDNS -#include #include #include "esphome/core/automation.h" #include "esphome/core/component.h" +#include "esphome/core/helpers.h" namespace esphome { namespace mdns { @@ -39,17 +39,15 @@ class MDNSComponent : public Component { float get_setup_priority() const override { return setup_priority::AFTER_CONNECTION; } #ifdef USE_MDNS_EXTRA_SERVICES - void add_extra_service(MDNSService service); + void add_extra_service(MDNSService service) { services_.push_back(std::move(service)); } #endif - const std::array &get_services() const { return services_; } - uint8_t get_services_count() const { return services_count_; } + StaticVector get_services(); void on_shutdown() override; protected: - std::array services_{}; - uint8_t services_count_{0}; + StaticVector services_{}; std::string hostname_; void compile_records_(); }; diff --git a/esphome/components/mdns/mdns_esp32.cpp b/esphome/components/mdns/mdns_esp32.cpp index 69a6fd3b8a..ffd86afec1 100644 --- a/esphome/components/mdns/mdns_esp32.cpp +++ b/esphome/components/mdns/mdns_esp32.cpp @@ -25,8 +25,7 @@ void MDNSComponent::setup() { mdns_hostname_set(this->hostname_.c_str()); mdns_instance_name_set(this->hostname_.c_str()); - for (uint8_t i = 0; i < this->services_count_; i++) { - const auto &service = this->services_[i]; + for (const auto &service : this->services_) { std::vector txt_records; for (const auto &record : service.txt_records) { mdns_txt_item_t it{}; diff --git a/esphome/components/mdns/mdns_esp8266.cpp b/esphome/components/mdns/mdns_esp8266.cpp index 9d701fe856..2c90d57021 100644 --- a/esphome/components/mdns/mdns_esp8266.cpp +++ b/esphome/components/mdns/mdns_esp8266.cpp @@ -16,8 +16,7 @@ void MDNSComponent::setup() { MDNS.begin(this->hostname_.c_str()); - for (uint8_t i = 0; i < this->services_count_; i++) { - const auto &service = this->services_[i]; + 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 diff --git a/esphome/components/mdns/mdns_libretiny.cpp b/esphome/components/mdns/mdns_libretiny.cpp index 7f3a05e515..7a41ec9dce 100644 --- a/esphome/components/mdns/mdns_libretiny.cpp +++ b/esphome/components/mdns/mdns_libretiny.cpp @@ -16,8 +16,7 @@ void MDNSComponent::setup() { MDNS.begin(this->hostname_.c_str()); - for (uint8_t i = 0; i < this->services_count_; i++) { - const auto &service = this->services_[i]; + 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 diff --git a/esphome/components/mdns/mdns_rp2040.cpp b/esphome/components/mdns/mdns_rp2040.cpp index 395d335ffd..95894323f4 100644 --- a/esphome/components/mdns/mdns_rp2040.cpp +++ b/esphome/components/mdns/mdns_rp2040.cpp @@ -16,8 +16,7 @@ void MDNSComponent::setup() { MDNS.begin(this->hostname_.c_str()); - for (uint8_t i = 0; i < this->services_count_; i++) { - const auto &service = this->services_[i]; + 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 diff --git a/esphome/components/openthread/openthread.cpp b/esphome/components/openthread/openthread.cpp index 0fc77a9c81..698688b425 100644 --- a/esphome/components/openthread/openthread.cpp +++ b/esphome/components/openthread/openthread.cpp @@ -144,11 +144,9 @@ void OpenThreadSrpComponent::setup() { } // Get mdns services and copy their data (strings are copied with strdup below) - const auto &mdns_services = this->mdns_->get_services(); - uint8_t mdns_count = this->mdns_->get_services_count(); - ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", mdns_count); - for (uint8_t i = 0; i < mdns_count; i++) { - const auto &service = mdns_services[i]; + auto mdns_services = this->mdns_->get_services(); + ESP_LOGD(TAG, "Setting up SRP services. count = %d\n", mdns_services.size()); + for (const auto &service : mdns_services) { otSrpClientBuffersServiceEntry *entry = otSrpClientBuffersAllocateService(instance); if (!entry) { ESP_LOGW(TAG, "Failed to allocate service entry"); diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index a28718de5a..d3852c3969 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -130,6 +130,9 @@ template class StaticVector { size_t size() const { return count_; } bool empty() const { return count_ == 0; } + // Direct access to increment size for efficient initialization + size_t &count() { return count_; } + T &operator[](size_t i) { return data_[i]; } const T &operator[](size_t i) const { return data_[i]; }