9static const char *
const TAG =
"entity_base";
29 this->
name_ = friendly;
41 if (object_id_hash != 0) {
48#ifdef USE_ENTITY_DEVICE_CLASS
51#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
52 this->
uom_idx_ = (entity_fields >> ENTITY_FIELD_UOM_SHIFT) & 0xFF;
55 this->
icon_idx_ = (entity_fields >> ENTITY_FIELD_ICON_SHIFT) & 0xFF;
57 this->
flags_.
internal = (entity_fields >> ENTITY_FIELD_INTERNAL_SHIFT) & 1;
69#ifdef USE_ENTITY_DEVICE_CLASS
72 const uint8_t idx = 0;
78 ESPHOME_strncpy_P(buffer.data(), dc, buffer.size() - 1);
79 buffer[buffer.size() - 1] =
'\0';
89#ifdef USE_ENTITY_DEVICE_CLASS
96#ifdef USE_ENTITY_DEVICE_CLASS
106#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
112std::string EntityBase::get_unit_of_measurement()
const {
118#ifdef USE_ENTITY_ICON
121 const uint8_t idx = 0;
127 ESPHOME_strncpy_P(buffer.data(), icon, buffer.size() - 1);
128 buffer[buffer.size() - 1] =
'\0';
129 return buffer.data();
138#ifdef USE_ENTITY_ICON
145#ifdef USE_ENTITY_ICON
154std::string EntityBase::get_object_id()
const {
155 char buf[OBJECT_ID_MAX_LEN];
157 return std::string(buf,
len);
166 size_t len = std::min(this->
name_.
size(), buf_size - 1);
167 for (
size_t i = 0; i <
len; i++) {
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
223#ifdef USE_ENTITY_ICON
225 char icon_buf[MAX_ICON_LENGTH];
227 if (icon[0] !=
'\0') {
228 ESP_LOGCONFIG(
tag,
"%s Icon: '%s'", prefix, icon);
234 char dc_buf[MAX_DEVICE_CLASS_LENGTH];
237 ESP_LOGCONFIG(
tag,
"%s Device Class: '%s'", prefix, dc);
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
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...
uint8_t device_class_idx_
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.
constexpr const char * c_str() const
constexpr bool empty() const
constexpr size_type size() const
struct @65::@66 __attribute__
Providing packet encoding functions for exchanging data with a remote host.
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.
void log_entity_unit_of_measurement(const char *tag, const char *prefix, const EntityBase &obj)
const char * entity_device_class_lookup(uint8_t index)
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.
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.
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)
uint8_t disabled_by_default