ESPHome 2026.3.0
Loading...
Searching...
No Matches
entity_base.cpp
Go to the documentation of this file.
6
7namespace esphome {
8
9static const char *const TAG = "entity_base";
10
11// Entity Name
12const StringRef &EntityBase::get_name() const { return this->name_; }
13
14void EntityBase::configure_entity_(const char *name, uint32_t object_id_hash, uint32_t entity_fields) {
15 this->name_ = StringRef(name);
16 if (this->name_.empty()) {
17#ifdef USE_DEVICES
18 if (this->device_ != nullptr) {
19 this->name_ = StringRef(this->device_->get_name());
20 } else
21#endif
22 {
23 // Bug-for-bug compatibility with OLD behavior:
24 // - With MAC suffix: OLD code used App.get_friendly_name() directly (no fallback)
25 // - Without MAC suffix: OLD code used pre-computed object_id with fallback to device name
26 const auto &friendly = App.get_friendly_name();
28 // MAC suffix enabled - use friendly_name directly (even if empty) for compatibility
29 this->name_ = friendly;
30 } else {
31 // No MAC suffix - fallback to device name if friendly_name is empty
32 this->name_ = !friendly.empty() ? friendly : App.get_name();
33 }
34 }
35 this->flags_.has_own_name = false;
36 // Dynamic name - must calculate hash at runtime
37 this->calc_object_id_();
38 } else {
39 this->flags_.has_own_name = true;
40 // Static name - use pre-computed hash if provided
41 if (object_id_hash != 0) {
42 this->object_id_hash_ = object_id_hash;
43 } else {
44 this->calc_object_id_();
45 }
46 }
47 // Unpack entity string table indices and flags from entity_fields.
48#ifdef USE_ENTITY_DEVICE_CLASS
49 this->device_class_idx_ = (entity_fields >> ENTITY_FIELD_DC_SHIFT) & 0xFF;
50#endif
51#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
52 this->uom_idx_ = (entity_fields >> ENTITY_FIELD_UOM_SHIFT) & 0xFF;
53#endif
54#ifdef USE_ENTITY_ICON
55 this->icon_idx_ = (entity_fields >> ENTITY_FIELD_ICON_SHIFT) & 0xFF;
56#endif
57 this->flags_.internal = (entity_fields >> ENTITY_FIELD_INTERNAL_SHIFT) & 1;
58 this->flags_.disabled_by_default = (entity_fields >> ENTITY_FIELD_DISABLED_BY_DEFAULT_SHIFT) & 1;
59 this->flags_.entity_category = (entity_fields >> ENTITY_FIELD_ENTITY_CATEGORY_SHIFT) & 0x3;
60}
61
62// Weak default lookup functions — overridden by generated code in main.cpp
63__attribute__((weak)) const char *entity_device_class_lookup(uint8_t) { return ""; }
64__attribute__((weak)) const char *entity_uom_lookup(uint8_t) { return ""; }
65__attribute__((weak)) const char *entity_icon_lookup(uint8_t) { return ""; }
66
67// Entity device class — buffer-based API for PROGMEM safety on ESP8266
68const char *EntityBase::get_device_class_to([[maybe_unused]] std::span<char, MAX_DEVICE_CLASS_LENGTH> buffer) const {
69#ifdef USE_ENTITY_DEVICE_CLASS
70 const uint8_t idx = this->device_class_idx_;
71#else
72 const uint8_t idx = 0;
73#endif
74#ifdef USE_ESP8266
75 if (idx == 0)
76 return "";
77 const char *dc = entity_device_class_lookup(idx);
78 ESPHOME_strncpy_P(buffer.data(), dc, buffer.size() - 1);
79 buffer[buffer.size() - 1] = '\0';
80 return buffer.data();
81#else
83#endif
84}
85
86#ifndef USE_ESP8266
87// Deprecated device class accessors — not available on ESP8266 (rodata is RAM)
89#ifdef USE_ENTITY_DEVICE_CLASS
91#else
93#endif
94}
95std::string EntityBase::get_device_class() const {
96#ifdef USE_ENTITY_DEVICE_CLASS
97 return std::string(entity_device_class_lookup(this->device_class_idx_));
98#else
99 return std::string(entity_device_class_lookup(0));
100#endif
101}
102#endif // !USE_ESP8266
103
104// Entity unit of measurement (from index)
106#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
107 return StringRef(entity_uom_lookup(this->uom_idx_));
108#else
109 return StringRef(entity_uom_lookup(0));
110#endif
111}
112std::string EntityBase::get_unit_of_measurement() const {
113 return std::string(this->get_unit_of_measurement_ref().c_str());
114}
115
116// Entity icon — buffer-based API for PROGMEM safety on ESP8266
117const char *EntityBase::get_icon_to([[maybe_unused]] std::span<char, MAX_ICON_LENGTH> buffer) const {
118#ifdef USE_ENTITY_ICON
119 const uint8_t idx = this->icon_idx_;
120#else
121 const uint8_t idx = 0;
122#endif
123#ifdef USE_ESP8266
124 if (idx == 0)
125 return "";
126 const char *icon = entity_icon_lookup(idx);
127 ESPHOME_strncpy_P(buffer.data(), icon, buffer.size() - 1);
128 buffer[buffer.size() - 1] = '\0';
129 return buffer.data();
130#else
131 return entity_icon_lookup(idx);
132#endif
133}
134
135#ifndef USE_ESP8266
136// Deprecated icon accessors — not available on ESP8266 (rodata is RAM)
138#ifdef USE_ENTITY_ICON
140#else
141 return StringRef(entity_icon_lookup(0));
142#endif
143}
144std::string EntityBase::get_icon() const {
145#ifdef USE_ENTITY_ICON
146 return std::string(entity_icon_lookup(this->icon_idx_));
147#else
148 return std::string(entity_icon_lookup(0));
149#endif
150}
151#endif // !USE_ESP8266
152
153// Entity Object ID - computed on-demand from name
154std::string EntityBase::get_object_id() const {
155 char buf[OBJECT_ID_MAX_LEN];
156 size_t len = this->write_object_id_to(buf, sizeof(buf));
157 return std::string(buf, len);
158}
159
160// Calculate Object ID Hash directly from name using snake_case + sanitize
162 this->object_id_hash_ = fnv1_hash_object_id(this->name_.c_str(), this->name_.size());
163}
164
165size_t EntityBase::write_object_id_to(char *buf, size_t buf_size) const {
166 size_t len = std::min(this->name_.size(), buf_size - 1);
167 for (size_t i = 0; i < len; i++) {
168 buf[i] = to_sanitized_char(to_snake_case_char(this->name_[i]));
169 }
170 buf[len] = '\0';
171 return len;
172}
173
174StringRef EntityBase::get_object_id_to(std::span<char, OBJECT_ID_MAX_LEN> buf) const {
175 size_t len = this->write_object_id_to(buf.data(), buf.size());
176 return StringRef(buf.data(), len);
177}
178
180
181// Migrate preference data from old_key to new_key if they differ.
182// This helper is exposed so callers with custom key computation (like TextPrefs)
183// can use it for manual migration. See: https://github.com/esphome/backlog/issues/85
184//
185// FUTURE IMPLEMENTATION:
186// This will require raw load/save methods on ESPPreferenceObject that take uint8_t* and size.
187// void EntityBase::migrate_entity_preference_(size_t size, uint32_t old_key, uint32_t new_key) {
188// if (old_key == new_key)
189// return;
190// auto old_pref = global_preferences->make_preference(size, old_key);
191// auto new_pref = global_preferences->make_preference(size, new_key);
192// SmallBufferWithHeapFallback<64> buffer(size);
193// if (old_pref.load(buffer.data(), size)) {
194// new_pref.save(buffer.data(), size);
195// }
196// }
197
199 // This helper centralizes preference creation to enable fixing hash collisions.
200 // See: https://github.com/esphome/backlog/issues/85
201 //
202 // COLLISION PROBLEM: get_preference_hash() uses fnv1_hash on sanitized object_id.
203 // Multiple entity names can sanitize to the same object_id:
204 // - "Living Room" and "living_room" both become "living_room"
205 // - UTF-8 names like "温度" and "湿度" both become "__" (underscores)
206 // This causes entities to overwrite each other's stored preferences.
207 //
208 // FUTURE MIGRATION: When implementing get_preference_hash_v2() that hashes
209 // the original entity name (not sanitized object_id):
210 //
211 // uint32_t old_key = this->get_preference_hash() ^ version;
212 // uint32_t new_key = this->get_preference_hash_v2() ^ version;
213 // this->migrate_entity_preference_(size, old_key, new_key);
214 // return global_preferences->make_preference(size, new_key);
215 //
216#pragma GCC diagnostic push
217#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
218 uint32_t key = this->get_preference_hash() ^ version;
219#pragma GCC diagnostic pop
221}
222
223#ifdef USE_ENTITY_ICON
224void log_entity_icon(const char *tag, const char *prefix, const EntityBase &obj) {
225 char icon_buf[MAX_ICON_LENGTH];
226 const char *icon = obj.get_icon_to(icon_buf);
227 if (icon[0] != '\0') {
228 ESP_LOGCONFIG(tag, "%s Icon: '%s'", prefix, icon);
229 }
230}
231#endif
232
233void log_entity_device_class(const char *tag, const char *prefix, const EntityBase &obj) {
234 char dc_buf[MAX_DEVICE_CLASS_LENGTH];
235 const char *dc = obj.get_device_class_to(dc_buf);
236 if (dc[0] != '\0') {
237 ESP_LOGCONFIG(tag, "%s Device Class: '%s'", prefix, dc);
238 }
239}
240
241void log_entity_unit_of_measurement(const char *tag, const char *prefix, const EntityBase &obj) {
242 if (!obj.get_unit_of_measurement_ref().empty()) {
243 ESP_LOGCONFIG(tag, "%s Unit of Measurement: '%s'", prefix, obj.get_unit_of_measurement_ref().c_str());
244 }
245}
246
247} // namespace esphome
const StringRef & get_name() const
Get the name of this Application set by pre_setup().
const StringRef & 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
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
struct esphome::EntityBase::EntityFlags flags_
ESPPreferenceObject make_entity_preference_(size_t size, uint32_t version)
Non-template helper for make_entity_preference() to avoid code bloat.
const char * get_device_class_to(std::span< char, MAX_DEVICE_CLASS_LENGTH > buffer) const
StringRef get_icon_ref() const
std::string get_device_class() const
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
ESPDEPRECATED("Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be " "removed in ESPHome 2026.9.0", "2026.3.0") std const char * get_icon_to(std::span< char, MAX_ICON_LENGTH > buffer) const
Get the unit of measurement as std::string (deprecated, prefer get_unit_of_measurement_ref())
std::string get_icon() 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...
StringRef get_device_class_ref() const
ESPDEPRECATED("Use get_device_class_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") std StringRef get_unit_of_measurement_ref() const
void configure_entity_(const char *name, uint32_t object_id_hash, uint32_t entity_fields)
Combined entity setup from codegen: set name, object_id hash, entity string indices,...
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:76
constexpr size_type size() const
Definition string_ref.h:74
struct @65::@66 __attribute__
const char *const TAG
Definition spi.cpp:7
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char * tag
Definition log.h:74
void log_entity_icon(const char *tag, const char *prefix, const EntityBase &obj)
constexpr char to_sanitized_char(char c)
Sanitize a single char: keep alphanumerics, dashes, underscores; replace others with underscore.
Definition helpers.h:852
void log_entity_unit_of_measurement(const char *tag, const char *prefix, const EntityBase &obj)
const char * entity_device_class_lookup(uint8_t index)
std::string size_t len
Definition helpers.h:892
size_t size
Definition helpers.h:929
ESPPreferences * global_preferences
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:880
const char * entity_uom_lookup(uint8_t index)
const char * entity_icon_lookup(uint8_t index)
constexpr char to_snake_case_char(char c)
Convert a single char to snake_case: lowercase and space to underscore.
Definition helpers.h:846
Application App
Global storage of Application pointer - only one Application can exist.
void log_entity_device_class(const char *tag, const char *prefix, const EntityBase &obj)
static void uint32_t