From da67c47a762568bb3376e591a56b893267267c7e Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Tue, 16 Dec 2025 18:10:08 +0000 Subject: [PATCH] Use PROGMEM format strings to reduce RAM usage on ESP8266 Replace str_sprintf() with snprintf_P() and PSTR() to keep format strings in flash instead of RAM. Also removes 'config hash 0x' prefix to save additional bytes. --- esphome/components/mqtt/mqtt_component.cpp | 5 +++-- esphome/components/version/version_text_sensor.cpp | 8 +++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/esphome/components/mqtt/mqtt_component.cpp b/esphome/components/mqtt/mqtt_component.cpp index 200f1f99a3..fe520bd175 100644 --- a/esphome/components/mqtt/mqtt_component.cpp +++ b/esphome/components/mqtt/mqtt_component.cpp @@ -154,8 +154,9 @@ bool MQTTComponent::send_discovery_() { device_info[MQTT_DEVICE_MANUFACTURER] = model == nullptr ? ESPHOME_PROJECT_NAME : std::string(ESPHOME_PROJECT_NAME, model - ESPHOME_PROJECT_NAME); #else - device_info[MQTT_DEVICE_SW_VERSION] = - str_sprintf(ESPHOME_VERSION " (config hash 0x%08" PRIx32 ")", App.get_config_hash()); + char sw_version[64]; + snprintf_P(sw_version, sizeof(sw_version), PSTR(ESPHOME_VERSION " (%08" PRIx32 ")"), App.get_config_hash()); + device_info[MQTT_DEVICE_SW_VERSION] = sw_version; device_info[MQTT_DEVICE_MODEL] = ESPHOME_BOARD; #if defined(USE_ESP8266) || defined(USE_ESP32) device_info[MQTT_DEVICE_MANUFACTURER] = "Espressif"; diff --git a/esphome/components/version/version_text_sensor.cpp b/esphome/components/version/version_text_sensor.cpp index f03c91e5f5..3b9d09d1e7 100644 --- a/esphome/components/version/version_text_sensor.cpp +++ b/esphome/components/version/version_text_sensor.cpp @@ -10,14 +10,16 @@ namespace version { static const char *const TAG = "version.text_sensor"; void VersionTextSensor::setup() { + char version_str[128]; if (this->hide_timestamp_) { - this->publish_state(str_sprintf(ESPHOME_VERSION " (config hash 0x%08" PRIx32 ")", App.get_config_hash())); + snprintf_P(version_str, sizeof(version_str), PSTR(ESPHOME_VERSION " (%08" PRIx32 ")"), App.get_config_hash()); } else { char build_time_str[esphome::Application::BUILD_TIME_STR_SIZE]; App.get_build_time_string(build_time_str); - this->publish_state(str_sprintf(ESPHOME_VERSION " (config hash 0x%08" PRIx32 ", built: %s)", App.get_config_hash(), - build_time_str)); + snprintf_P(version_str, sizeof(version_str), PSTR(ESPHOME_VERSION " (%08" PRIx32 ", built: %s)"), + App.get_config_hash(), build_time_str); } + this->publish_state(version_str); } float VersionTextSensor::get_setup_priority() const { return setup_priority::DATA; } void VersionTextSensor::set_hide_timestamp(bool hide_timestamp) { this->hide_timestamp_ = hide_timestamp; }