ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
entity_base.cpp
Go to the documentation of this file.
4
5namespace esphome {
6
7static const char *const TAG = "entity_base";
8
9// Entity Name
10const StringRef &EntityBase::get_name() const { return this->name_; }
11void EntityBase::set_name(const char *name) {
12 this->name_ = StringRef(name);
13 if (this->name_.empty()) {
14#ifdef USE_DEVICES
15 if (this->device_ != nullptr) {
16 this->name_ = StringRef(this->device_->get_name());
17 } else
18#endif
19 {
21 }
22 this->flags_.has_own_name = false;
23 } else {
24 this->flags_.has_own_name = true;
25 }
26}
27
28// Entity Icon
29std::string EntityBase::get_icon() const {
30#ifdef USE_ENTITY_ICON
31 if (this->icon_c_str_ == nullptr) {
32 return "";
33 }
34 return this->icon_c_str_;
35#else
36 return "";
37#endif
38}
39void EntityBase::set_icon(const char *icon) {
40#ifdef USE_ENTITY_ICON
41 this->icon_c_str_ = icon;
42#else
43 // No-op when USE_ENTITY_ICON is not defined
44#endif
45}
46
47// Entity Object ID
48std::string EntityBase::get_object_id() const {
49 // Check if `App.get_friendly_name()` is constant or dynamic.
51 // `App.get_friendly_name()` is dynamic.
53 } else {
54 // `App.get_friendly_name()` is constant.
55 if (this->object_id_c_str_ == nullptr) {
56 return "";
57 }
58 return this->object_id_c_str_;
59 }
60}
61void EntityBase::set_object_id(const char *object_id) {
62 this->object_id_c_str_ = object_id;
63 this->calc_object_id_();
64}
65
66// Calculate Object ID Hash from Entity Name
68
70
72 if (this->device_class_ == nullptr) {
73 return "";
74 }
75 return this->device_class_;
76}
77
78void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
79
81 if (this->unit_of_measurement_ == nullptr)
82 return "";
83 return this->unit_of_measurement_;
84}
85void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
86 this->unit_of_measurement_ = unit_of_measurement;
87}
88
89} // namespace esphome
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
const char * get_name()
Definition device.h:10
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_object_id(const char *object_id)
uint32_t object_id_hash_
Definition entity_base.h:94
const char * object_id_c_str_
Definition entity_base.h:90
uint32_t get_object_id_hash()
const StringRef & get_name() const
std::string get_icon() const
void set_name(const char *name)
void set_icon(const char *icon)
const char * icon_c_str_
Definition entity_base.h:92
std::string get_object_id() const
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
constexpr bool empty() const
Definition string_ref.h:71
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:134
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
Definition helpers.cpp:184
Application App
Global storage of Application pointer - only one Application can exist.
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition helpers.cpp:177