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