ESPHome 2025.7.5
Loading...
Searching...
No Matches
entity_base.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <cstdint>
5#include "string_ref.h"
6#include "helpers.h"
7#include "log.h"
8
9#ifdef USE_DEVICES
10#include "device.h"
11#endif
12
13namespace esphome {
14
20
21// The generic Entity base class that provides an interface common to all Entities.
23 public:
24 // Get/set the name of this Entity
25 const StringRef &get_name() const;
26 void set_name(const char *name);
27
28 // Get whether this Entity has its own name or it should use the device friendly_name.
29 bool has_own_name() const { return this->flags_.has_own_name; }
30
31 // Get the sanitized name of this Entity as an ID.
32 std::string get_object_id() const;
33 void set_object_id(const char *object_id);
34
35 // Get the unique Object ID of this Entity
36 uint32_t get_object_id_hash();
37
38 // Get/set whether this Entity should be hidden outside ESPHome
39 bool is_internal() const { return this->flags_.internal; }
40 void set_internal(bool internal) { this->flags_.internal = internal; }
41
42 // Check if this object is declared to be disabled by default.
43 // That means that when the device gets added to Home Assistant (or other clients) it should
44 // not be added to the default view by default, and a user action is necessary to manually add it.
45 bool is_disabled_by_default() const { return this->flags_.disabled_by_default; }
46 void set_disabled_by_default(bool disabled_by_default) { this->flags_.disabled_by_default = disabled_by_default; }
47
48 // Get/set the entity category.
50 void set_entity_category(EntityCategory entity_category) {
51 this->flags_.entity_category = static_cast<uint8_t>(entity_category);
52 }
53
54 // Get/set this entity's icon
55 std::string get_icon() const;
56 void set_icon(const char *icon);
57
58#ifdef USE_DEVICES
59 // Get/set this entity's device id
60 uint32_t get_device_id() const {
61 if (this->device_ == nullptr) {
62 return 0; // No device set, return 0
63 }
64 return this->device_->get_device_id();
65 }
66 void set_device(Device *device) { this->device_ = device; }
67#endif
68
69 // Check if this entity has state
70 bool has_state() const { return this->flags_.has_state; }
71
72 // Set has_state - for components that need to manually set this
73 void set_has_state(bool state) { this->flags_.has_state = state; }
74
75 protected:
78 virtual uint32_t hash_base() { return 0L; }
79 void calc_object_id_();
80
82 const char *object_id_c_str_{nullptr};
83#ifdef USE_ENTITY_ICON
84 const char *icon_c_str_{nullptr};
85#endif
86 uint32_t object_id_hash_{};
87#ifdef USE_DEVICES
89#endif
90
91 // Bit-packed flags to save memory (1 byte instead of 5)
92 struct EntityFlags {
93 uint8_t has_own_name : 1;
94 uint8_t internal : 1;
96 uint8_t has_state : 1;
97 uint8_t entity_category : 2; // Supports up to 4 categories
98 uint8_t reserved : 2; // Reserved for future use
99 } flags_{};
100};
101
102class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
103 public:
105 std::string get_device_class();
107 void set_device_class(const char *device_class);
108
109 protected:
110 const char *device_class_{nullptr};
111};
112
113class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming)
114 public:
116 std::string get_unit_of_measurement();
118 void set_unit_of_measurement(const char *unit_of_measurement);
119
120 protected:
121 const char *unit_of_measurement_{nullptr};
122};
123
128template<typename T> class StatefulEntityBase : public EntityBase {
129 public:
130 virtual bool has_state() const { return this->state_.has_value(); }
131 virtual const T &get_state() const { return this->state_.value(); }
132 virtual T get_state_default(T default_value) const { return this->state_.value_or(default_value); }
133 void invalidate_state() { this->set_state_({}); }
134
135 void add_full_state_callback(std::function<void(optional<T> previous, optional<T> current)> &&callback) {
136 if (this->full_state_callbacks_ == nullptr)
137 this->full_state_callbacks_ = new CallbackManager<void(optional<T> previous, optional<T> current)>(); // NOLINT
138 this->full_state_callbacks_->add(std::move(callback));
139 }
140 void add_on_state_callback(std::function<void(T)> &&callback) {
141 if (this->state_callbacks_ == nullptr)
142 this->state_callbacks_ = new CallbackManager<void(T)>(); // NOLINT
143 this->state_callbacks_->add(std::move(callback));
144 }
145
146 void set_trigger_on_initial_state(bool trigger_on_initial_state) {
147 this->trigger_on_initial_state_ = trigger_on_initial_state;
148 }
149
150 protected:
159 if (this->state_ != state) {
160 // call the full state callbacks with the previous and new state
161 if (this->full_state_callbacks_ != nullptr)
162 this->full_state_callbacks_->call(this->state_, state);
163 // trigger legacy callbacks only if the new state is valid and either the trigger on initial state is enabled or
164 // the previous state was valid
165 auto had_state = this->has_state();
166 this->state_ = state;
167 if (this->state_callbacks_ != nullptr && state.has_value() && (this->trigger_on_initial_state_ || had_state))
168 this->state_callbacks_->call(state.value());
169 return true;
170 }
171 return false;
172 }
174 // callbacks with full state and previous state
177};
178} // namespace esphome
uint32_t get_device_id()
Definition device.h:8
std::string get_device_class()
Get the device class, using the manual override if set.
void set_device_class(const char *device_class)
Manually set the device class.
const char * device_class_
Device class override.
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
const char * unit_of_measurement_
Unit of measurement override.
void set_unit_of_measurement(const char *unit_of_measurement)
Manually set the unit of measurement.
struct esphome::EntityBase::EntityFlags flags_
void set_device(Device *device)
Definition entity_base.h:66
void set_object_id(const char *object_id)
bool has_own_name() const
Definition entity_base.h:29
uint32_t object_id_hash_
Definition entity_base.h:86
bool is_internal() const
Definition entity_base.h:39
const char * object_id_c_str_
Definition entity_base.h:82
uint32_t get_object_id_hash()
const StringRef & get_name() const
void set_entity_category(EntityCategory entity_category)
Definition entity_base.h:50
std::string get_icon() const
uint32_t get_device_id() const
Definition entity_base.h:60
bool is_disabled_by_default() const
Definition entity_base.h:45
void set_name(const char *name)
void set_icon(const char *icon)
void set_disabled_by_default(bool disabled_by_default)
Definition entity_base.h:46
void set_has_state(bool state)
Definition entity_base.h:73
const char * icon_c_str_
Definition entity_base.h:84
virtual uint32_t hash_base()
The hash_base() function has been deprecated.
Definition entity_base.h:78
bool has_state() const
Definition entity_base.h:70
std::string get_object_id() const
EntityCategory get_entity_category() const
Definition entity_base.h:49
void set_internal(bool internal)
Definition entity_base.h:40
An entity that has a state.
virtual const T & get_state() const
bool set_state_(const optional< T > &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.
Definition string_ref.h:22
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
@ ENTITY_CATEGORY_NONE
Definition entity_base.h:16
@ ENTITY_CATEGORY_CONFIG
Definition entity_base.h:17
@ ENTITY_CATEGORY_DIAGNOSTIC
Definition entity_base.h:18