From a30052e7c033c2a5da3a96b6440d1435e444b459 Mon Sep 17 00:00:00 2001 From: David Woodhouse Date: Tue, 16 Dec 2025 19:22:42 +0000 Subject: [PATCH] Use PROGMEM for version text sensor strings on ESP8266 Build version string incrementally from PROGMEM literal prefix, avoiding format strings in RAM. Copy from PROGMEM on ESP8266, use directly on other platforms. --- .../version/version_text_sensor.cpp | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/esphome/components/version/version_text_sensor.cpp b/esphome/components/version/version_text_sensor.cpp index f03c91e5f5..67611e55a5 100644 --- a/esphome/components/version/version_text_sensor.cpp +++ b/esphome/components/version/version_text_sensor.cpp @@ -10,14 +10,28 @@ namespace version { static const char *const TAG = "version.text_sensor"; void VersionTextSensor::setup() { - if (this->hide_timestamp_) { - this->publish_state(str_sprintf(ESPHOME_VERSION " (config hash 0x%08" PRIx32 ")", App.get_config_hash())); - } else { + static const char prefix[] PROGMEM = ESPHOME_VERSION " (config hash 0x"; + char version_str[128]; + +#ifdef USE_ESP8266 + strcpy_P(version_str, prefix); +#else + strcpy(version_str, prefix); +#endif + + char hash_str[9]; + snprintf(hash_str, sizeof(hash_str), "%08" PRIx32, App.get_config_hash()); + strcat(version_str, hash_str); + + if (!this->hide_timestamp_) { + strcat(version_str, ", built: "); 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)); + strcat(version_str, build_time_str); } + + strcat(version_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; }