ESPHome 2026.1.0
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) { this->set_name(name, 0); }
13void EntityBase::set_name(const char *name, uint32_t object_id_hash) {
14 this->name_ = StringRef(name);
15 if (this->name_.empty()) {
16#ifdef USE_DEVICES
17 if (this->device_ != nullptr) {
18 this->name_ = StringRef(this->device_->get_name());
19 } else
20#endif
21 {
22 // Bug-for-bug compatibility with OLD behavior:
23 // - With MAC suffix: OLD code used App.get_friendly_name() directly (no fallback)
24 // - Without MAC suffix: OLD code used pre-computed object_id with fallback to device name
25 const std::string &friendly = App.get_friendly_name();
27 // MAC suffix enabled - use friendly_name directly (even if empty) for compatibility
28 this->name_ = StringRef(friendly);
29 } else {
30 // No MAC suffix - fallback to device name if friendly_name is empty
31 this->name_ = StringRef(!friendly.empty() ? friendly : App.get_name());
32 }
33 }
34 this->flags_.has_own_name = false;
35 // Dynamic name - must calculate hash at runtime
36 this->calc_object_id_();
37 } else {
38 this->flags_.has_own_name = true;
39 // Static name - use pre-computed hash if provided
40 if (object_id_hash != 0) {
41 this->object_id_hash_ = object_id_hash;
42 } else {
43 this->calc_object_id_();
44 }
45 }
46}
47
48// Entity Icon
49std::string EntityBase::get_icon() const {
50#ifdef USE_ENTITY_ICON
51 if (this->icon_c_str_ == nullptr) {
52 return "";
53 }
54 return this->icon_c_str_;
55#else
56 return "";
57#endif
58}
59void EntityBase::set_icon(const char *icon) {
60#ifdef USE_ENTITY_ICON
61 this->icon_c_str_ = icon;
62#else
63 // No-op when USE_ENTITY_ICON is not defined
64#endif
65}
66
67// Entity Object ID - computed on-demand from name
68std::string EntityBase::get_object_id() const {
69 char buf[OBJECT_ID_MAX_LEN];
70 size_t len = this->write_object_id_to(buf, sizeof(buf));
71 return std::string(buf, len);
72}
73
74// Calculate Object ID Hash directly from name using snake_case + sanitize
76 this->object_id_hash_ = fnv1_hash_object_id(this->name_.c_str(), this->name_.size());
77}
78
79size_t EntityBase::write_object_id_to(char *buf, size_t buf_size) const {
80 size_t len = std::min(this->name_.size(), buf_size - 1);
81 for (size_t i = 0; i < len; i++) {
82 buf[i] = to_sanitized_char(to_snake_case_char(this->name_[i]));
83 }
84 buf[len] = '\0';
85 return len;
86}
87
88StringRef EntityBase::get_object_id_to(std::span<char, OBJECT_ID_MAX_LEN> buf) const {
89 size_t len = this->write_object_id_to(buf.data(), buf.size());
90 return StringRef(buf.data(), len);
91}
92
94
95std::string EntityBase_DeviceClass::get_device_class() {
96 if (this->device_class_ == nullptr) {
97 return "";
98 }
99 return this->device_class_;
100}
101
102void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
103
104std::string EntityBase_UnitOfMeasurement::get_unit_of_measurement() {
105 if (this->unit_of_measurement_ == nullptr)
106 return "";
107 return this->unit_of_measurement_;
108}
109void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
110 this->unit_of_measurement_ = unit_of_measurement;
111}
112
113} // 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 std::string & get_name() const
Get the name of this Application set by pre_setup().
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 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...
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)
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:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr bool empty() const
Definition string_ref.h:75
constexpr size_type size() const
Definition string_ref.h:74
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:574
std::string size_t len
Definition helpers.h:595
uint32_t fnv1_hash_object_id(const char *str, size_t len)
Calculate FNV-1 hash of a string while applying snake_case + sanitize transformations.
Definition helpers.h:584
constexpr char to_snake_case_char(char c)
Convert a single char to snake_case: lowercase and space to underscore.
Definition helpers.h:568
Application App
Global storage of Application pointer - only one Application can exist.