Merge branch 'mdns_mac_storage_reduce_ram' into integration

This commit is contained in:
J. Nick Koston
2025-11-23 21:46:19 -06:00
10 changed files with 40 additions and 13 deletions

View File

@@ -184,10 +184,8 @@ async def to_code(config):
# Calculate compile-time dynamic TXT value count
# Dynamic values are those that cannot be stored in flash at compile time
# Note: MAC address is now stored in a fixed char[13] buffer, not dynamic storage
dynamic_txt_count = 0
if "api" in CORE.config:
# Always: get_mac_address()
dynamic_txt_count += 1
# User-provided templatable TXT values (only lambdas, not static strings)
dynamic_txt_count += sum(
1
@@ -196,8 +194,10 @@ async def to_code(config):
if cg.is_template(txt_value)
)
# Ensure at least 1 to avoid zero-size array
cg.add_define("MDNS_DYNAMIC_TXT_COUNT", max(1, dynamic_txt_count))
# Only add define if we actually need dynamic storage
if dynamic_txt_count > 0:
cg.add_define("USE_MDNS_DYNAMIC_TXT")
cg.add_define("MDNS_DYNAMIC_TXT_COUNT", dynamic_txt_count)
# Enable storage if verbose logging is enabled (for dump_config)
if get_logger_level() in ("VERBOSE", "VERY_VERBOSE"):

View File

@@ -86,7 +86,7 @@ void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUN
txt_records.push_back({MDNS_STR(TXT_FRIENDLY_NAME), MDNS_STR(friendly_name.c_str())});
}
txt_records.push_back({MDNS_STR(TXT_VERSION), MDNS_STR(VALUE_VERSION)});
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(this->add_dynamic_txt_value(get_mac_address()))});
txt_records.push_back({MDNS_STR(TXT_MAC), MDNS_STR(this->mac_address_)});
#ifdef USE_ESP8266
MDNS_STATIC_CONST_CHAR(PLATFORM_ESP8266, "ESP8266");

View File

@@ -60,18 +60,38 @@ class MDNSComponent : public Component {
void on_shutdown() override;
#ifdef USE_MDNS_DYNAMIC_TXT
/// Add a dynamic TXT value and return pointer to it for use in MDNSTXTRecord
const char *add_dynamic_txt_value(const std::string &value) {
this->dynamic_txt_values_.push_back(value);
return this->dynamic_txt_values_[this->dynamic_txt_values_.size() - 1].c_str();
}
#endif
/// Storage for runtime-generated TXT values (MAC address, user lambdas)
protected:
/// Common setup logic called by all platform-specific setup() implementations
void on_setup() {
#ifdef USE_API
// Populate MAC address buffer once during setup
get_mac_address_into_buffer(std::span<char, 13>(this->mac_address_));
#endif
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
#endif
}
#ifdef USE_MDNS_DYNAMIC_TXT
/// Storage for runtime-generated TXT values from user lambdas
/// Pre-sized at compile time via MDNS_DYNAMIC_TXT_COUNT to avoid heap allocations.
/// Static/compile-time values (version, board, etc.) are stored directly in flash and don't use this.
StaticVector<std::string, MDNS_DYNAMIC_TXT_COUNT> dynamic_txt_values_;
#endif
protected:
#ifdef USE_API
/// Fixed buffer for MAC address (populated once in setup())
char mac_address_[13]; // 12 hex chars + null terminator
#endif
#ifdef USE_MDNS_STORE_SERVICES
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services_{};
#endif

View File

@@ -12,8 +12,9 @@ namespace esphome::mdns {
static const char *const TAG = "mdns";
void MDNSComponent::setup() {
this->on_setup();
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;

View File

@@ -12,8 +12,9 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup();
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;

View File

@@ -9,6 +9,7 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup();
// Host platform doesn't have actual mDNS implementation
}

View File

@@ -12,8 +12,9 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup();
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;

View File

@@ -12,8 +12,9 @@
namespace esphome::mdns {
void MDNSComponent::setup() {
this->on_setup();
#ifdef USE_MDNS_STORE_SERVICES
this->compile_records_(this->services_);
const auto &services = this->services_;
#else
StaticVector<MDNSService, MDNS_SERVICE_COUNT> services;

View File

@@ -88,7 +88,8 @@
#define USE_MDNS
#define USE_MDNS_STORE_SERVICES
#define MDNS_SERVICE_COUNT 3
#define MDNS_DYNAMIC_TXT_COUNT 3
#define USE_MDNS_DYNAMIC_TXT
#define MDNS_DYNAMIC_TXT_COUNT 2
#define SNTP_SERVER_COUNT 3
#define USE_MEDIA_PLAYER
#define USE_NEXTION_TFT_UPLOAD

View File

@@ -1,4 +1,5 @@
sensor:
- platform: stts22h
i2c_id: i2c_bus
name: Temperature
update_interval: 15s