ESPHome 2025.9.0
Loading...
Searching...
No Matches
entity_base.cpp
Go to the documentation of this file.
5
6namespace esphome {
7
8static const char *const TAG = "entity_base";
9
10// Entity Name
11const StringRef &EntityBase::get_name() const { return this->name_; }
12void EntityBase::set_name(const char *name) {
13 this->name_ = StringRef(name);
14 if (this->name_.empty()) {
15#ifdef USE_DEVICES
16 if (this->device_ != nullptr) {
17 this->name_ = StringRef(this->device_->get_name());
18 } else
19#endif
20 {
22 }
23 this->flags_.has_own_name = false;
24 } else {
25 this->flags_.has_own_name = true;
26 }
27}
28
29// Entity Icon
30std::string EntityBase::get_icon() const {
31#ifdef USE_ENTITY_ICON
32 if (this->icon_c_str_ == nullptr) {
33 return "";
34 }
35 return this->icon_c_str_;
36#else
37 return "";
38#endif
39}
40void EntityBase::set_icon(const char *icon) {
41#ifdef USE_ENTITY_ICON
42 this->icon_c_str_ = icon;
43#else
44 // No-op when USE_ENTITY_ICON is not defined
45#endif
46}
47
48// Check if the object_id is dynamic (changes with MAC suffix)
52
53// Entity Object ID
54std::string EntityBase::get_object_id() const {
55 // Check if `App.get_friendly_name()` is constant or dynamic.
56 if (this->is_object_id_dynamic_()) {
57 // `App.get_friendly_name()` is dynamic.
59 }
60 // `App.get_friendly_name()` is constant.
61 return this->object_id_c_str_ == nullptr ? "" : this->object_id_c_str_;
62}
64 static constexpr auto EMPTY_STRING = StringRef::from_lit("");
65 // Return empty for dynamic case (MAC suffix)
66 if (this->is_object_id_dynamic_()) {
67 return EMPTY_STRING;
68 }
69 // For static case, return the string or empty if null
70 return this->object_id_c_str_ == nullptr ? EMPTY_STRING : StringRef(this->object_id_c_str_);
71}
72void EntityBase::set_object_id(const char *object_id) {
73 this->object_id_c_str_ = object_id;
74 this->calc_object_id_();
75}
76
77// Calculate Object ID Hash from Entity Name
82
84
86 if (this->device_class_ == nullptr) {
87 return "";
88 }
89 return this->device_class_;
90}
91
92void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
93
95 if (this->unit_of_measurement_ == nullptr)
96 return "";
97 return this->unit_of_measurement_;
98}
99void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
100 this->unit_of_measurement_ = unit_of_measurement;
101}
102
103} // 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)
const char * object_id_c_str_
uint32_t get_object_id_hash()
const StringRef & get_name() const
std::string get_icon() const
bool is_object_id_dynamic_() const
Check if the object_id is dynamic (changes with MAC suffix)
void set_name(const char *name)
void set_icon(const char *icon)
const char * icon_c_str_
std::string get_object_id() const
StringRef get_object_id_ref_for_api_() 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
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:46
const char *const TAG
Definition spi.cpp:8
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
Definition helpers.cpp:197
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:145
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:190