ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
version_text_sensor.cpp
Go to the documentation of this file.
5#include "esphome/core/log.h"
8
9namespace esphome::version {
10
11static const char *const TAG = "version.text_sensor";
12
14 static const char HASH_PREFIX[] PROGMEM = ESPHOME_VERSION " (config hash 0x";
15 static const char VERSION_PREFIX[] PROGMEM = ESPHOME_VERSION;
16 static const char BUILT_STR[] PROGMEM = ", built ";
17
18 // Buffer size: HASH_PREFIX + 8 hex chars + BUILT_STR + BUILD_TIME_STR_SIZE + ")" + null
19 constexpr size_t buf_size =
20 sizeof(HASH_PREFIX) + 8 + sizeof(BUILT_STR) + esphome::Application::BUILD_TIME_STR_SIZE + 2;
21 char version_str[buf_size];
22
23 // hide_hash restores the pre-2026.1 base format by omitting
24 // the " (config hash 0x...)" suffix entirely.
25 if (this->hide_hash_) {
26 ESPHOME_strncpy_P(version_str, VERSION_PREFIX, sizeof(version_str));
27 } else {
28 ESPHOME_strncpy_P(version_str, HASH_PREFIX, sizeof(version_str));
29
30 size_t len = strlen(version_str);
31 snprintf(version_str + len, sizeof(version_str) - len, "%08" PRIx32, App.get_config_hash());
32 }
33
34 // Keep hide_timestamp behavior independent from hide_hash so all
35 // combinations remain available to users.
36 if (!this->hide_timestamp_) {
37 size_t len = strlen(version_str);
38 ESPHOME_strncat_P(version_str, BUILT_STR, sizeof(version_str) - len - 1);
39 ESPHOME_strncat_P(version_str, ESPHOME_BUILD_TIME_STR, sizeof(version_str) - strlen(version_str) - 1);
40 }
41
42 // The closing parenthesis is part of the config-hash suffix and must
43 // only be appended when that suffix is present.
44 if (!this->hide_hash_) {
45 strncat(version_str, ")", sizeof(version_str) - strlen(version_str) - 1);
46 }
47 version_str[sizeof(version_str) - 1] = '\0';
48 this->publish_state(version_str);
49}
50void VersionTextSensor::set_hide_hash(bool hide_hash) { this->hide_hash_ = hide_hash; }
51void VersionTextSensor::set_hide_timestamp(bool hide_timestamp) { this->hide_timestamp_ = hide_timestamp; }
52void VersionTextSensor::dump_config() { LOG_TEXT_SENSOR("", "Version Text Sensor", this); }
53
54} // namespace esphome::version
static constexpr size_t BUILD_TIME_STR_SIZE
Size of buffer required for build time string (including null terminator)
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
void publish_state(const std::string &state)
void set_hide_timestamp(bool hide_timestamp)
std::string size_t len
Definition helpers.h:817
Application App
Global storage of Application pointer - only one Application can exist.
const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM
Definition web_server.h:28