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_ENTITY_NAME_MAX_LEN = 120;
37static constexpr size_t ENTITY_NAME_BUF_SIZE = ESPHOME_ENTITY_NAME_MAX_LEN + 1;
40static constexpr size_t ESPHOME_DOMAIN_MAX_LEN = 20;
43static constexpr size_t OBJECT_ID_MAX_LEN = 128;
46static constexpr size_t MAX_STATE_LEN = 255;
51static constexpr size_t MAX_DEVICE_CLASS_LENGTH = 48;
55static constexpr size_t MAX_ICON_LENGTH = 64;
65static constexpr uint8_t ENTITY_FIELD_DC_SHIFT = 0;
66static constexpr uint8_t ENTITY_FIELD_UOM_SHIFT = 8;
67static constexpr uint8_t ENTITY_FIELD_ICON_SHIFT = 16;
68static constexpr uint8_t ENTITY_FIELD_INTERNAL_SHIFT = 24;
69static constexpr uint8_t ENTITY_FIELD_DISABLED_BY_DEFAULT_SHIFT = 25;
70static constexpr uint8_t ENTITY_FIELD_ENTITY_CATEGORY_SHIFT = 26;
78 ESPDEPRECATED(
"Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0",
"2026.11.0")
86 ESPDEPRECATED(
"Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0",
"2026.11.0")
169 if (this->
device_ ==
nullptr) {
203 ESPDEPRECATED(
"Use make_entity_preference<T>() instead, or preferences won't be migrated. "
204 "See https://github.com/esphome/backlog/issues/85. Will be removed in 2027.1.0.",
222 static_assert(std::is_trivially_copyable<T>::value,
"T must be trivially copyable");
228 static_assert(std::is_trivially_copyable<T>::value,
"T must be trivially copyable");
233 friend void ::setup();
234 friend void ::original_setup();
270#ifdef USE_ENTITY_DEVICE_CLASS
273#ifdef USE_ENTITY_UNIT_OF_MEASUREMENT
276#ifdef USE_ENTITY_ICON
285#ifdef USE_ENTITY_ICON
286#define LOG_ENTITY_ICON(tag, prefix, obj) log_entity_icon(tag, prefix, obj)
287void log_entity_icon(
const char *tag,
const char *prefix,
const EntityBase &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)
352 const T *current = had_state ? &this->
get_state() :
nullptr;
353 if (new_state.has_value()) {
354 if (current !=
nullptr && *current == new_state.value())
356 }
else if (!had_state) {
361 optional<T> old_state;
363 old_state = current !=
nullptr ? optional<T>(*current) : nullopt;
366 if (new_state.has_value()) {
372 if (new_state.has_value() && (had_state || this->get_trigger_on_initial_state()))
struct esphome::EntityBase::EntityFlags flags_
const ProgmemStringRef & get_name_progmem() const
The name as stored, for consumers that read flash themselves (e.g. API progmem fields).
ESPPreferenceObject make_entity_preference_(size_t size, uint32_t version)
Non-template helpers for make_entity_preference() to avoid code bloat.
const char * get_device_class_to(std::span< char, MAX_DEVICE_CLASS_LENGTH > buffer) const
ESPDEPRECATED("Use get_name_to() or get_log_name() instead. Will be removed in ESPHome 2027.5.0", "2026.11.0") const StringRef &get_name() const
The name is in flash here; the first call makes a RAM copy that is kept for the life of the device.
ProgmemStringRef get_device_class_progmem() const
The device class as stored (in flash on ESP8266), for consumers that read flash themselves.
bool has_own_name() const
size_t write_name_to(char *buf, size_t buf_size) const
Copy the name into buf (null terminated), returns its length.
StringRef get_name_to(std::span< char, ENTITY_NAME_BUF_SIZE > buffer) const
Get the name of this Entity, copied out of flash into buffer.
const char * get_icon_to(std::span< char, MAX_ICON_LENGTH > buffer) const
void set_device_(Device *device)
bool name_equals(const StringRef &other) const
uint32_t get_object_id_hash() const
ProgmemStringRef get_icon_progmem() const
The icon as stored (in flash on ESP8266), for consumers that read flash themselves.
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
uint32_t entity_preference_key_(uint32_t version)
Key for both helpers; preference hash migration goes here.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
ESPPreferenceObject make_entity_preference(uint32_t version, bool in_flash)
As make_entity_preference(), stored in flash or RTC memory where the backend supports the choice.
StringRef get_unit_of_measurement_ref() const
Device * get_device() const
void set_has_state(bool state)
const StringRef & get_name_to(std::span< char, ENTITY_NAME_BUF_SIZE >) 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,...
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)
A string that may live in flash (on ESP8266): pointer and length, with no byte-reading operations.
constexpr const char * progmem_ptr() const
size_t write_to(char *buf, size_t buf_size) const
Copy into buf (null terminated, truncated to fit), returns the length copied.
constexpr const StringRef & ram_ref() const
The string is in RAM here, so it can be viewed directly.
bool equals(const StringRef &other) const
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.
bool log_entity_header(const char *tag, const char *prefix, const char *type, const EntityBase *obj)
Log "<prefix><type> '<name>'" for a dump_config header; returns false when obj is null.
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