ESPHome  2024.10.2
entity_base.cpp
Go to the documentation of this file.
3 #include "esphome/core/helpers.h"
4 
5 namespace esphome {
6 
7 static const char *const TAG = "entity_base";
8 
9 // Entity Name
10 const StringRef &EntityBase::get_name() const { return this->name_; }
11 void 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
22 bool EntityBase::is_internal() const { return this->internal_; }
23 void EntityBase::set_internal(bool internal) { this->internal_ = internal; }
24 
25 // Entity Disabled by Default
27 void EntityBase::set_disabled_by_default(bool disabled_by_default) { this->disabled_by_default_ = disabled_by_default; }
28 
29 // Entity Icon
30 std::string EntityBase::get_icon() const {
31  if (this->icon_c_str_ == nullptr) {
32  return "";
33  }
34  return this->icon_c_str_;
35 }
36 void EntityBase::set_icon(const char *icon) { this->icon_c_str_ = icon; }
37 
38 // Entity Category
40 void EntityBase::set_entity_category(EntityCategory entity_category) { this->entity_category_ = entity_category; }
41 
42 // Entity Object ID
43 std::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 }
56 void 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 
76 uint32_t EntityBase::get_object_id_hash() { return this->object_id_hash_; }
77 
79  if (this->device_class_ == nullptr) {
80  return "";
81  }
82  return this->device_class_;
83 }
84 
85 void 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 }
92 void EntityBase_UnitOfMeasurement::set_unit_of_measurement(const char *unit_of_measurement) {
93  this->unit_of_measurement_ = unit_of_measurement;
94 }
95 
96 } // namespace esphome
void set_disabled_by_default(bool disabled_by_default)
Definition: entity_base.cpp:27
const char * name
Definition: stm32flash.h:78
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition: helpers.cpp:279
void set_internal(bool internal)
Definition: entity_base.cpp:23
std::string get_device_class()
Get the device class, using the manual override if set.
Definition: entity_base.cpp:78
void set_device_class(const char *device_class)
Manually set the device class.
Definition: entity_base.cpp:85
StringRef is a reference to a string owned by something else.
Definition: string_ref.h:21
void set_name(const char *name)
Definition: entity_base.cpp:11
void set_unit_of_measurement(const char *unit_of_measurement)
Manually set the unit of measurement.
Definition: entity_base.cpp:92
const std::string & get_friendly_name() const
Get the friendly name of this Application set by pre_setup().
Definition: application.h:205
void set_object_id(const char *object_id)
Definition: entity_base.cpp:56
const char * icon_c_str_
Definition: entity_base.h:58
constexpr bool empty() const
Definition: string_ref.h:70
std::string get_object_id() const
Definition: entity_base.cpp:43
const char *const TAG
Definition: spi.cpp:8
std::string get_icon() const
Definition: entity_base.cpp:30
EntityCategory entity_category_
Definition: entity_base.h:63
bool is_internal() const
Definition: entity_base.cpp:22
std::string get_unit_of_measurement()
Get the unit of measurement, using the manual override if set.
Definition: entity_base.cpp:87
Application App
Global storage of Application pointer - only one Application can exist.
EntityCategory
Definition: entity_base.h:9
uint32_t object_id_hash_
Definition: entity_base.h:59
bool is_name_add_mac_suffix_enabled() const
Definition: application.h:213
EntityCategory get_entity_category() const
Definition: entity_base.cpp:39
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores...
Definition: helpers.cpp:286
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition: helpers.cpp:182
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_icon(const char *icon)
Definition: entity_base.cpp:36
const char * object_id_c_str_
Definition: entity_base.h:57
bool is_disabled_by_default() const
Definition: entity_base.cpp:26
uint32_t get_object_id_hash()
Definition: entity_base.cpp:76
void set_entity_category(EntityCategory entity_category)
Definition: entity_base.cpp:40
const StringRef & get_name() const
Definition: entity_base.cpp:10