18static constexpr size_t ESPHOME_DEVICE_NAME_MAX_LEN = 31;
22static constexpr size_t ESPHOME_FRIENDLY_NAME_MAX_LEN = 120;
25static constexpr size_t ESPHOME_DOMAIN_MAX_LEN = 20;
28static constexpr size_t OBJECT_ID_MAX_LEN = 128;
31static constexpr size_t MAX_STATE_LEN = 255;
47 void set_name(
const char *name, uint32_t object_id_hash);
57 ESPDEPRECATED(
"object_id mangles names and all object_id methods are planned for removal "
58 "(see https://github.com/esphome/backlog/issues/76). "
59 "Now is the time to stop using object_id. If still needed, use get_object_id_to() "
60 "which will remain available longer. get_object_id() will be removed in 2026.7.0",
62 std::
string get_object_id() const;
94 "Use get_icon_ref() instead for better performance (avoids string copy). Will be removed in ESPHome 2026.5.0",
96 std::
string get_icon() const;
100#ifdef USE_ENTITY_ICON
110 if (this->
device_ ==
nullptr) {
145 ESPDEPRECATED(
"Use make_entity_preference<T>() instead, or preferences won't be migrated. "
146 "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.",
148 uint32_t get_preference_hash() {
164 static_assert(std::is_trivially_copyable<T>::value,
"T must be trivially copyable");
176#ifdef USE_ENTITY_ICON
198 ESPDEPRECATED(
"Use get_device_class_ref() instead for better performance (avoids string copy). Will be removed in "
201 std::string get_device_class();
217 ESPDEPRECATED(
"Use get_unit_of_measurement_ref() instead for better performance (avoids string copy). Will be "
218 "removed in ESPHome 2026.5.0",
220 std::string get_unit_of_measurement();
234#define LOG_ENTITY_ICON(tag, prefix, obj) log_entity_icon(tag, prefix, obj)
235void log_entity_icon(
const char *tag,
const char *prefix,
const EntityBase &obj);
237#define LOG_ENTITY_DEVICE_CLASS(tag, prefix, obj) log_entity_device_class(tag, prefix, obj)
240#define LOG_ENTITY_UNIT_OF_MEASUREMENT(tag, prefix, obj) log_entity_unit_of_measurement(tag, prefix, obj)
278 if (this->
state_ != new_state) {
StringRef get_device_class_ref() const
Get the device class as StringRef.
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.
StringRef get_unit_of_measurement_ref() const
Get the unit of measurement as StringRef.
struct esphome::EntityBase::EntityFlags flags_
void set_device(Device *device)
ESPPreferenceObject make_entity_preference_(size_t size, uint32_t version)
Non-template helper for make_entity_preference() to avoid code bloat.
bool has_own_name() 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
void set_entity_category(EntityCategory entity_category)
StringRef get_icon_ref() 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.
uint32_t get_device_id() const
bool is_disabled_by_default() const
void set_name(const char *name)
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
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)
Device * get_device() const
void set_disabled_by_default(bool disabled_by_default)
void set_has_state(bool state)
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...
void set_internal(bool internal)
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.
static constexpr StringRef from_lit(const CharT(&s)[N])
value_type const & value() const
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_device_class(const char *tag, const char *prefix, const EntityBase_DeviceClass &obj)
void log_entity_unit_of_measurement(const char *tag, const char *prefix, const EntityBase_UnitOfMeasurement &obj)
@ ENTITY_CATEGORY_DIAGNOSTIC
struct ESPDEPRECATED("Use std::index_sequence instead. Removed in 2026.6.0", "2025.12.0") seq
uint8_t disabled_by_default