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;
94 ESPDEPRECATED(
"set_internal() is undefined behavior at runtime — components and Home Assistant are NOT "
95 "notified. Use the 'internal:' YAML key instead. Will be removed in 2027.3.0.",
97 void set_internal(
bool internal) { this->
flags_.
internal = internal; }
118 const char *
get_icon_to(std::span<char, MAX_ICON_LENGTH> buffer)
const;
123 if (this->
device_ ==
nullptr) {
157 ESPDEPRECATED(
"Use make_entity_preference<T>() instead, or preferences won't be migrated. "
158 "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.",
176 static_assert(std::is_trivially_copyable<T>::value,
"T must be trivially copyable");
181 friend void ::setup();
182 friend void ::original_setup();
218#ifdef USE_ENTITY_DEVICE_CLASS
221#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
224#ifdef USE_ENTITY_ICON
230#ifdef USE_ENTITY_ICON
231#define LOG_ENTITY_ICON(tag, prefix, obj) log_entity_icon(tag, prefix, obj)
232void log_entity_icon(
const char *tag,
const char *prefix,
const EntityBase &obj);
234#define LOG_ENTITY_ICON(tag, prefix, obj) ((void) 0)
235inline void log_entity_icon(
const char *,
const char *,
const EntityBase &) {}
238#define LOG_ENTITY_DEVICE_CLASS(tag, prefix, obj) log_entity_device_class(tag, prefix, obj)
241#define LOG_ENTITY_UNIT_OF_MEASUREMENT(tag, prefix, obj) log_entity_unit_of_measurement(tag, prefix, obj)
297 const T *current = had_state ? &this->
get_state() :
nullptr;
298 if (new_state.has_value()) {
299 if (current !=
nullptr && *current == new_state.value())
301 }
else if (!had_state) {
306 optional<T> old_state;
308 old_state = current !=
nullptr ? optional<T>(*current) : nullopt;
311 if (new_state.has_value()) {
317 if (new_state.has_value() && (had_state || this->get_trigger_on_initial_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
const StringRef & get_name() const
const char * get_icon_to(std::span< char, MAX_ICON_LENGTH > buffer) const
void set_device_(Device *device)
uint32_t get_object_id_hash() 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_
uint32_t get_device_id() const
bool is_disabled_by_default() 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.
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...
Base class for entities that track a typed state value with change-detection and callbacks.
virtual bool get_trigger_on_initial_state() const =0
Subclasses return whether callbacks should fire on the very first state.
virtual const T & get_state() const =0
Return the current state value. Only valid when has_state() is true.
virtual bool set_new_state(const optional< T > &new_state)
Apply a new state, de-duplicating and firing callbacks as needed.
void add_full_state_callback(F &&callback)
virtual void set_state_value(const T &value)=0
Subclasses implement this to store the actual value into their own storage.
void invalidate_state()
Clear the state — sets has_state() to false and fires callbacks with nullopt.
LazyCallbackManager< void(T)> state_callbacks_
void add_on_state_callback(F &&callback)
T get_state_default(T default_value) const
Return the current state if available, otherwise return the provided default.
LazyCallbackManager< void(optional< T > previous, optional< T > current)> full_state_callbacks_
StringRef is a reference to a string owned by something else.
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