diff --git a/esphome/components/version/version_text_sensor.cpp b/esphome/components/version/version_text_sensor.cpp index 2e5686008b..bac0e44f75 100644 --- a/esphome/components/version/version_text_sensor.cpp +++ b/esphome/components/version/version_text_sensor.cpp @@ -25,7 +25,9 @@ void VersionTextSensor::setup() { if (!this->hide_timestamp_) { size_t len = strlen(version_str); ESPHOME_strncat_P(version_str, BUILT_STR, sizeof(version_str) - len - 1); - ESPHOME_strncat_P(version_str, ESPHOME_BUILD_TIME_STR, sizeof(version_str) - strlen(version_str) - 1); + char time_buf[esphome::Application::BUILD_TIME_STR_SIZE]; + App.get_build_time_string(time_buf); + strncat(version_str, time_buf, sizeof(version_str) - strlen(version_str) - 1); } strncat(version_str, ")", sizeof(version_str) - strlen(version_str) - 1); diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 4b572417c1..a4ca0d9109 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -375,9 +375,8 @@ json::SerializationBuffer<> WebServer::get_config_json() { JsonObject root = builder.root(); root[ESPHOME_F("title")] = App.get_friendly_name().empty() ? App.get_name().c_str() : App.get_friendly_name().c_str(); - char comment_buffer[ESPHOME_COMMENT_SIZE]; - App.get_comment_string(comment_buffer); - root[ESPHOME_F("comment")] = comment_buffer; + auto comment = App.get_comment(); + root[ESPHOME_F("comment")] = comment.c_str(); #if defined(USE_WEBSERVER_OTA_DISABLED) || !defined(USE_WEBSERVER_OTA) root[ESPHOME_F("ota")] = false; // Note: USE_WEBSERVER_OTA_DISABLED only affects web_server, not captive_portal #else diff --git a/esphome/core/application.cpp b/esphome/core/application.cpp index c6597897dc..4d0a582f14 100644 --- a/esphome/core/application.cpp +++ b/esphome/core/application.cpp @@ -749,4 +749,29 @@ void Application::get_build_time_string(std::span buf buffer[buffer.size() - 1] = '\0'; } +size_t Application::get_comment_size() { return ESPHOME_COMMENT_SIZE; } + +void Application::get_comment_string(char *buffer, size_t size) { + ESPHOME_strncpy_P(buffer, ESPHOME_COMMENT_STR, size); + buffer[size - 1] = '\0'; +} + +std::string Application::get_comment() { + char buffer[ESPHOME_COMMENT_SIZE]; + this->get_comment_string(buffer, sizeof(buffer)); + return std::string(buffer); +} + +uint32_t Application::get_config_hash() { return ESPHOME_CONFIG_HASH; } + +uint32_t Application::get_config_version_hash() { return fnv1a_hash_extend(ESPHOME_CONFIG_HASH, ESPHOME_VERSION); } + +time_t Application::get_build_time() { return ESPHOME_BUILD_TIME; } + +std::string Application::get_compilation_time() { + char buf[BUILD_TIME_STR_SIZE]; + this->get_build_time_string(buf); + return std::string(buf); +} + } // namespace esphome diff --git a/esphome/core/application.h b/esphome/core/application.h index 5b3e3dfed6..625c0a3304 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -6,7 +6,6 @@ #include #include #include -#include "esphome/core/build_info_data.h" #include "esphome/core/component.h" #include "esphome/core/defines.h" #include "esphome/core/hal.h" @@ -274,19 +273,14 @@ class Application { return ""; } + /// Get the size of the comment buffer (including null terminator) + static size_t get_comment_size(); + /// Copy the comment string into the provided buffer - /// Buffer must be ESPHOME_COMMENT_SIZE bytes (compile-time enforced) - void get_comment_string(std::span buffer) { - ESPHOME_strncpy_P(buffer.data(), ESPHOME_COMMENT_STR, buffer.size()); - buffer[buffer.size() - 1] = '\0'; - } + void get_comment_string(char *buffer, size_t size); /// Get the comment of this Application as a string - std::string get_comment() { - char buffer[ESPHOME_COMMENT_SIZE]; - this->get_comment_string(buffer); - return std::string(buffer); - } + std::string get_comment(); bool is_name_add_mac_suffix_enabled() const { return this->name_add_mac_suffix_; } @@ -294,26 +288,22 @@ class Application { static constexpr size_t BUILD_TIME_STR_SIZE = 26; /// Get the config hash as a 32-bit integer - constexpr uint32_t get_config_hash() { return ESPHOME_CONFIG_HASH; } + uint32_t get_config_hash(); /// Get the config hash extended with ESPHome version - constexpr uint32_t get_config_version_hash() { return fnv1a_hash_extend(ESPHOME_CONFIG_HASH, ESPHOME_VERSION); } + uint32_t get_config_version_hash(); /// Get the build time as a Unix timestamp - constexpr time_t get_build_time() { return ESPHOME_BUILD_TIME; } + time_t get_build_time(); /// Copy the build time string into the provided buffer - /// Buffer must be BUILD_TIME_STR_SIZE bytes (compile-time enforced) + /// Buffer must be BUILD_TIME_STR_SIZE bytes void get_build_time_string(std::span buffer); /// Get the build time as a string (deprecated, use get_build_time_string() instead) // Remove before 2026.7.0 ESPDEPRECATED("Use get_build_time_string() instead. Removed in 2026.7.0", "2026.1.0") - std::string get_compilation_time() { - char buf[BUILD_TIME_STR_SIZE]; - this->get_build_time_string(buf); - return std::string(buf); - } + std::string get_compilation_time(); /// Get the cached time in milliseconds from when the current component started its loop execution inline uint32_t IRAM_ATTR HOT get_loop_component_start_time() const { return this->loop_component_start_time_; }