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.
This commit is contained in:
David Woodhouse
2025-12-16 19:22:42 +00:00
parent 175250deb0
commit a30052e7c0

View File

@@ -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; }