28static constexpr size_t ESPHOME_DEVICE_NAME_MAX_LEN = 31;
32static constexpr size_t ESPHOME_FRIENDLY_NAME_MAX_LEN = 120;
35static constexpr size_t ESPHOME_DOMAIN_MAX_LEN = 20;
38static constexpr size_t OBJECT_ID_MAX_LEN = 128;
41static constexpr size_t MAX_STATE_LEN = 255;
46static constexpr size_t MAX_DEVICE_CLASS_LENGTH = 48;
50static constexpr size_t MAX_ICON_LENGTH = 64;
60static constexpr uint8_t ENTITY_FIELD_DC_SHIFT = 0;
61static constexpr uint8_t ENTITY_FIELD_UOM_SHIFT = 8;
62static constexpr uint8_t ENTITY_FIELD_ICON_SHIFT = 16;
63static constexpr uint8_t ENTITY_FIELD_INTERNAL_SHIFT = 24;
64static constexpr uint8_t ENTITY_FIELD_DISABLED_BY_DEFAULT_SHIFT = 25;
65static constexpr uint8_t ENTITY_FIELD_ENTITY_CATEGORY_SHIFT = 26;
81 ESPDEPRECATED(
"object_id mangles names and all object_id methods are planned for removal "
82 "(see https://github.com/esphome/backlog/issues/76). "
83 "Now is the time to stop using object_id. If still needed, use get_object_id_to() "
84 "which will remain available longer. get_object_id() will be removed in 2026.7.0",
86 std::
string get_object_id() const;
106 ESPDEPRECATED(
"set_internal() is undefined behavior at runtime — components and Home Assistant are NOT "
107 "notified. Use the 'internal:' YAML key instead. Will be removed in 2027.3.0.",
109 void set_internal(
bool internal) { this->
flags_.
internal = internal; }
128 static_assert(
sizeof(T) == 0,
"get_device_class_ref() unavailable on ESP8266 (rodata is RAM). "
129 "Use get_device_class_to() with a stack buffer.");
133 static_assert(
sizeof(T) == 0,
"get_device_class() unavailable on ESP8266 (rodata is RAM). "
134 "Use get_device_class_to() with a stack buffer.");
139 ESPDEPRECATED(
"Use get_device_class_to() instead. Will be removed in ESPHome 2026.9.0",
"2026.3.0")
147 ESPDEPRECATED(
"Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be "
148 "removed in ESPHome 2026.9.0",
150 std::
string get_unit_of_measurement() const;
155 const
char *
get_icon_to(std::span<
char, MAX_ICON_LENGTH> buffer) const;
161 static_assert(
sizeof(T) == 0,
162 "get_icon_ref() unavailable on ESP8266 (rodata is RAM). Use get_icon_to() with a stack buffer.");
165 template<
typename T =
int> std::string
get_icon()
const {
166 static_assert(
sizeof(T) == 0,
167 "get_icon() unavailable on ESP8266 (rodata is RAM). Use get_icon_to() with a stack buffer.");
172 ESPDEPRECATED(
"Use get_icon_to() instead. Will be removed in ESPHome 2026.9.0",
"2026.3.0")
181 if (this->
device_ ==
nullptr) {
215 ESPDEPRECATED(
"Use make_entity_preference<T>() instead, or preferences won't be migrated. "
216 "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.",
234 static_assert(std::is_trivially_copyable<T>::value,
"T must be trivially copyable");
239 friend void ::setup();
240 friend void ::original_setup();
273#ifdef USE_ENTITY_DEVICE_CLASS
276#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
279#ifdef USE_ENTITY_ICON
285#ifdef USE_ENTITY_ICON
286#define LOG_ENTITY_ICON(tag, prefix, obj) log_entity_icon(tag, prefix, obj)
289#define LOG_ENTITY_ICON(tag, prefix, obj) ((void) 0)
290inline void log_entity_icon(
const char *,
const char *,
const EntityBase &) {}
293#define LOG_ENTITY_DEVICE_CLASS(tag, prefix, obj) log_entity_device_class(tag, prefix, obj)
296#define LOG_ENTITY_UNIT_OF_MEASUREMENT(tag, prefix, obj) log_entity_unit_of_measurement(tag, prefix, obj)
334 if (this->
state_ != new_state) {
342 if (this->
state_callbacks_ !=
nullptr && new_state.has_value() && (this->trigger_on_initial_state_ || had_state))
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
bool has_own_name() const
ESPDEPRECATED("Use get_device_class_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") StringRef get_device_class_ref() 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())
void set_device_(Device *device)
std::string get_icon() const
ESPDEPRECATED("Use get_icon_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") std uint32_t get_device_id() 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...
ESPDEPRECATED("Use make_entity_preference<T>() instead, or preferences won't be migrated. " "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.", "2026.7.0") uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
uint8_t device_class_idx_
bool is_disabled_by_default() const
StringRef get_device_class_ref() const
ESPDEPRECATED("set_internal() is undefined behavior at runtime — components and Home Assistant are NOT " "notified. Use the 'internal:' YAML key instead. Will be removed in 2027.3.0.", "2026.3.0") void set_internal(bool internal)
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
ESPDEPRECATED("Use get_icon_to() instead. Will be removed in ESPHome 2026.9.0", "2026.3.0") StringRef get_icon_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
Device * get_device() const
void set_has_state(bool state)
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,...
EntityCategory get_entity_category() const
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...
An entity that has a state.
virtual const T & get_state() const
bool trigger_on_initial_state_
virtual bool set_new_state(const optional< T > &new_state)
Set a new state for this entity.
void add_full_state_callback(std::function< void(optional< T > previous, optional< T > current)> &&callback)
CallbackManager< void(T)> * state_callbacks_
void add_on_state_callback(std::function< void(T)> &&callback)
void set_trigger_on_initial_state(bool trigger_on_initial_state)
virtual bool has_state() const
virtual T get_state_default(T default_value) const
CallbackManager< void(optional< T > previous, optional< T > current)> * full_state_callbacks_
StringRef is a reference to a string owned by something else.
Providing packet encoding functions for exchanging data with a remote host.
void log_entity_icon(const char *tag, const char *prefix, const EntityBase &obj)
void log_entity_unit_of_measurement(const char *tag, const char *prefix, const EntityBase &obj)
const char * entity_device_class_lookup(uint8_t index)
@ ENTITY_CATEGORY_DIAGNOSTIC
const char * entity_uom_lookup(uint8_t index)
const char * entity_icon_lookup(uint8_t index)
void log_entity_device_class(const char *tag, const char *prefix, const EntityBase &obj)
uint8_t disabled_by_default