ESPHome 2025.6.3
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()) {
15 this->flags_.has_own_name = false;
16 } else {
17 this->flags_.has_own_name = true;
18 }
19}
20
21// Entity Icon
22std::string EntityBase::get_icon() const {
23 if (this->icon_c_str_ == nullptr) {
24 return "";
25 }
26 return this->icon_c_str_;
27}
28void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; }
29
30// Entity Object ID
31std::string EntityBase::get_object_id() const {
32 // Check if `App.get_friendly_name()` is constant or dynamic.
34 // `App.get_friendly_name()` is dynamic.
36 } else {
37 // `App.get_friendly_name()` is constant.
38 if (this->object_id_c_str_ == nullptr) {
39 return "";
40 }
41 return this->object_id_c_str_;
42 }
43}
44void EntityBase::set_object_id(const char *object_id) {
45 this->object_id_c_str_ = object_id;
46 this->calc_object_id_();
47}
48
49// Calculate Object ID Hash from Entity Name
51 // Check if `App.get_friendly_name()` is constant or dynamic.
53 // `App.get_friendly_name()` is dynamic.
54 const auto object_id = str_sanitize(str_snake_case(App.get_friendly_name()));
55 // FNV-1 hash
56 this->object_id_hash_ = fnv1_hash(object_id);
57 } else {
58 // `App.get_friendly_name()` is constant.
59 // FNV-1 hash
61 }
62}
63
65
67 if (this->device_class_ == nullptr) {
68 return "";
69 }
70 return this->device_class_;
71}
72
73void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
74
76 if (this->unit_of_measurement_ == nullptr)
77 return "";
78 return this->unit_of_measurement_;
79}
80void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
81 this->unit_of_measurement_ = unit_of_measurement;
82}
83
84} // 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
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.
Definition entity_base.h:88
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.
Definition entity_base.h:99
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:67
const char * object_id_c_str_
Definition entity_base.h:65
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:66
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:186
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
Definition helpers.cpp:299
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:292