ESPHome 2025.5.0
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->has_own_name_ = false;
16 } else {
17 this->has_own_name_ = true;
18 }
19}
20
21// Entity Internal
22bool EntityBase::is_internal() const { return this->internal_; }
23void EntityBase::set_internal(bool internal) { this->internal_ = internal; }
24
25// Entity Disabled by Default
27void EntityBase::set_disabled_by_default(bool disabled_by_default) { this->disabled_by_default_ = disabled_by_default; }
28
29// Entity Icon
30std::string EntityBase::get_icon() const {
31 if (this->icon_c_str_ == nullptr) {
32 return "";
33 }
34 return this->icon_c_str_;
35}
36void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; }
37
38// Entity Category
40void EntityBase::set_entity_category(EntityCategory entity_category) { this->entity_category_ = entity_category; }
41
42// Entity Object ID
43std::string EntityBase::get_object_id() const {
44 // Check if `App.get_friendly_name()` is constant or dynamic.
46 // `App.get_friendly_name()` is dynamic.
48 } else {
49 // `App.get_friendly_name()` is constant.
50 if (this->object_id_c_str_ == nullptr) {
51 return "";
52 }
53 return this->object_id_c_str_;
54 }
55}
56void EntityBase::set_object_id(const char *object_id) {
57 this->object_id_c_str_ = object_id;
58 this->calc_object_id_();
59}
60
61// Calculate Object ID Hash from Entity Name
63 // Check if `App.get_friendly_name()` is constant or dynamic.
65 // `App.get_friendly_name()` is dynamic.
66 const auto object_id = str_sanitize(str_snake_case(App.get_friendly_name()));
67 // FNV-1 hash
68 this->object_id_hash_ = fnv1_hash(object_id);
69 } else {
70 // `App.get_friendly_name()` is constant.
71 // FNV-1 hash
73 }
74}
75
77
79 if (this->device_class_ == nullptr) {
80 return "";
81 }
82 return this->device_class_;
83}
84
85void EntityBase_DeviceClass::set_device_class(const char *device_class) { this->device_class_ = device_class; }
86
88 if (this->unit_of_measurement_ == nullptr)
89 return "";
90 return this->unit_of_measurement_;
91}
92void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
93 this->unit_of_measurement_ = unit_of_measurement;
94}
95
96} // 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:74
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:85
void set_unit_of_measurement(const char *unit_of_measurement)
Manually set the unit of measurement.
void set_object_id(const char *object_id)
uint32_t object_id_hash_
Definition entity_base.h:59
bool is_internal() const
const char * object_id_c_str_
Definition entity_base.h:57
uint32_t get_object_id_hash()
const StringRef & get_name() const
void set_entity_category(EntityCategory entity_category)
std::string get_icon() const
bool is_disabled_by_default() const
EntityCategory entity_category_
Definition entity_base.h:63
void set_name(const char *name)
void set_icon(const char *icon)
void set_disabled_by_default(bool disabled_by_default)
const char * icon_c_str_
Definition entity_base.h:58
std::string get_object_id() const
EntityCategory get_entity_category() const
void set_internal(bool internal)
StringRef is a reference to a string owned by something else.
Definition string_ref.h:21
constexpr bool empty() const
Definition string_ref.h:70
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