ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
entity_base.cpp
Go to the documentation of this file.
5
6namespace esphome {
7
8static const char *const TAG = "entity_base";
9
10// Entity Name
11const StringRef &EntityBase::get_name() const { return this->name_; }
12void EntityBase::set_name(const char *name) {
13 this->name_ = StringRef(name);
14 if (this->name_.empty()) {
15#ifdef USE_DEVICES
16 if (this->device_ != nullptr) {
17 this->name_ = StringRef(this->device_->get_name());
18 } else
19#endif
20 {
22 }
23 this->flags_.has_own_name = false;
24 } else {
25 this->flags_.has_own_name = true;
26 }
27}
28
29// Entity Icon
30std::string EntityBase::get_icon() const {
31#ifdef USE_ENTITY_ICON
32 if (this->icon_c_str_ == nullptr) {
33 return "";
34 }
35 return this->icon_c_str_;
36#else
37 return "";
38#endif
39}
40void EntityBase::set_icon(const char *icon) {
41#ifdef USE_ENTITY_ICON
42 this->icon_c_str_ = icon;
43#else
44 // No-op when USE_ENTITY_ICON is not defined
45#endif
46}
47
48// Check if the object_id is dynamic (changes with MAC suffix)
52
53// Entity Object ID
54std::string EntityBase::get_object_id() const {
55 // Check if `App.get_friendly_name()` is constant or dynamic.
56 if (this->is_object_id_dynamic_()) {
57 // `App.get_friendly_name()` is dynamic.
59 }
60 // `App.get_friendly_name()` is constant.
61 return this->object_id_c_str_ == nullptr ? "" : this->object_id_c_str_;
62}
63void EntityBase::set_object_id(const char *object_id) {
64 this->object_id_c_str_ = object_id;
65 this->calc_object_id_();
66}
67
68void EntityBase::set_name_and_object_id(const char *name, const char *object_id) {
69 this->set_name(name);
70 this->object_id_c_str_ = object_id;
71 this->calc_object_id_();
72}
73
74// Calculate Object ID Hash from Entity Name
76 char buf[OBJECT_ID_MAX_LEN];
77 StringRef object_id = this->get_object_id_to(buf);
78 this->object_id_hash_ = fnv1_hash(object_id.c_str());
79}
80
81// Format dynamic object_id: sanitized snake_case of friendly_name
82static size_t format_dynamic_object_id(char *buf, size_t buf_size) {
83 const std::string &name = App.get_friendly_name();
84 size_t len = std::min(name.size(), buf_size - 1);
85 for (size_t i = 0; i < len; i++) {
86 buf[i] = to_sanitized_char(to_snake_case_char(name[i]));
87 }
88 buf[len] = '\0';
89 return len;
90}
91
92size_t EntityBase::write_object_id_to(char *buf, size_t buf_size) const {
93 if (this->is_object_id_dynamic_()) {
94 return format_dynamic_object_id(buf, buf_size);
95 }
96 const char *src = this->object_id_c_str_ == nullptr ? "" : this->object_id_c_str_;
97 size_t len = strlen(src);
98 if (len >= buf_size)
99 len = buf_size - 1;
100 memcpy(buf, src, len);
101 buf[len] = '\0';
102 return len;
103}
104
105StringRef EntityBase::get_object_id_to(std::span<char, OBJECT_ID_MAX_LEN> buf) const {
106 if (this->is_object_id_dynamic_()) {
107 size_t len = format_dynamic_object_id(buf.data(), buf.size());
108 return StringRef(buf.data(), len);
109 }
110 return this->object_id_c_str_ == nullptr ? StringRef() : StringRef(this->object_id_c_str_);
111}
112
114
115std::string EntityBase_DeviceClass::get_device_class() {
116 if (this->device_class_ == nullptr) {
117 return "";
118 }
119 return this->device_class_;
120}
121
122void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
123
124std::string EntityBase_UnitOfMeasurement::get_unit_of_measurement() {
125 if (this->unit_of_measurement_ == nullptr)
126 return "";
127 return this->unit_of_measurement_;
128}
129void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
130 this->unit_of_measurement_ = unit_of_measurement;
131}
132
133} // namespace esphome
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
const char * get_name()
Definition device.h:10
ESPDEPRECATED("Use get_device_class_ref() instead for better performance (avoids string copy). Will be removed in " "ESPHome 2026.5.0", "2025.11.0") std void set_device_class(const char *device_class)
Get the device class, using the manual override if set.
const char * device_class_
Device class override.
const char * unit_of_measurement_
Unit of measurement override.
ESPDEPRECATED("Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be " "removed in ESPHome 2026.5.0", "2025.11.0") std void set_unit_of_measurement(const char *unit_of_measurement)
Get the unit of measurement, using the manual override if set.
struct esphome::EntityBase::EntityFlags flags_
ESPDEPRECATED("object_id mangles names and all object_id methods are planned for removal " "(see https://github.com/esphome/backlog/issues/76). " "Now is the time to stop using object_id. If still needed, use get_object_id_to() " "which will remain available longer. get_object_id() will be removed in 2026.7.0", "2025.12.0") std void set_object_id(const char *object_id)
const char * object_id_c_str_
uint32_t get_object_id_hash()
const StringRef & get_name() const
size_t write_object_id_to(char *buf, size_t buf_size) const
Write object_id directly to buffer, returns length written (excluding null) Useful for building compo...
bool is_object_id_dynamic_() const
Check if the object_id is dynamic (changes with MAC suffix)
void set_name(const char *name)
ESPDEPRECATED("Use get_icon_ref() instead for better performance (avoids string copy). Will be removed in ESPHome 2026.5.0", "2025.11.0") std void set_icon(const char *icon)
void set_name_and_object_id(const char *name, const char *object_id)
const char * icon_c_str_
StringRef get_object_id_to(std::span< char, OBJECT_ID_MAX_LEN > buf) const
Get object_id with zero heap allocation For static case: returns StringRef to internal storage (buffe...
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
constexpr const char * c_str() const
Definition string_ref.h:69
constexpr bool empty() const
Definition string_ref.h:71
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr char to_sanitized_char(char c)
Sanitize a single char: keep alphanumerics, dashes, underscores; replace others with underscore.
Definition helpers.h:526
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
Definition helpers.cpp:199
std::string size_t len
Definition helpers.h:533
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:147
constexpr char to_snake_case_char(char c)
Convert a single char to snake_case: lowercase and space to underscore.
Definition helpers.h:521
Application App
Global storage of Application pointer - only one Application can exist.
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition helpers.cpp:192