ESPHome  2024.10.3
entity_base.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <cstdint>
5 #include "string_ref.h"
6 
7 namespace esphome {
8 
9 enum EntityCategory : uint8_t {
13 };
14 
15 // The generic Entity base class that provides an interface common to all Entities.
16 class EntityBase {
17  public:
18  // Get/set the name of this Entity
19  const StringRef &get_name() const;
20  void set_name(const char *name);
21 
22  // Get whether this Entity has its own name or it should use the device friendly_name.
23  bool has_own_name() const { return this->has_own_name_; }
24 
25  // Get the sanitized name of this Entity as an ID.
26  std::string get_object_id() const;
27  void set_object_id(const char *object_id);
28 
29  // Get the unique Object ID of this Entity
30  uint32_t get_object_id_hash();
31 
32  // Get/set whether this Entity should be hidden from outside of ESPHome
33  bool is_internal() const;
34  void set_internal(bool internal);
35 
36  // Check if this object is declared to be disabled by default.
37  // That means that when the device gets added to Home Assistant (or other clients) it should
38  // not be added to the default view by default, and a user action is necessary to manually add it.
39  bool is_disabled_by_default() const;
40  void set_disabled_by_default(bool disabled_by_default);
41 
42  // Get/set the entity category.
44  void set_entity_category(EntityCategory entity_category);
45 
46  // Get/set this entity's icon
47  std::string get_icon() const;
48  void set_icon(const char *icon);
49 
50  protected:
53  virtual uint32_t hash_base() { return 0L; }
54  void calc_object_id_();
55 
57  const char *object_id_c_str_{nullptr};
58  const char *icon_c_str_{nullptr};
59  uint32_t object_id_hash_;
60  bool has_own_name_{false};
61  bool internal_{false};
62  bool disabled_by_default_{false};
64 };
65 
66 class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
67  public:
69  std::string get_device_class();
71  void set_device_class(const char *device_class);
72 
73  protected:
74  const char *device_class_{nullptr};
75 };
76 
77 class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming)
78  public:
80  std::string get_unit_of_measurement();
82  void set_unit_of_measurement(const char *unit_of_measurement);
83 
84  protected:
85  const char *unit_of_measurement_{nullptr};
86 };
87 
88 } // namespace esphome
bool has_own_name() const
Definition: entity_base.h:23
void set_disabled_by_default(bool disabled_by_default)
Definition: entity_base.cpp:27
const char * name
Definition: stm32flash.h:78
virtual uint32_t hash_base()
The hash_base() function has been deprecated.
Definition: entity_base.h:53
void set_internal(bool internal)
Definition: entity_base.cpp:23
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_object_id(const char *object_id)
Definition: entity_base.cpp:56
const char * icon_c_str_
Definition: entity_base.h:58
std::string get_object_id() const
Definition: entity_base.cpp:43
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
EntityCategory
Definition: entity_base.h:9
uint32_t object_id_hash_
Definition: entity_base.h:59
EntityCategory get_entity_category() const
Definition: entity_base.cpp:39
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