ESPHome 2025.6.3
Loading...
Searching...
No Matches
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
7namespace esphome {
8
14
15// The generic Entity base class that provides an interface common to all Entities.
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->flags_.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 outside ESPHome
33 bool is_internal() const { return this->flags_.internal; }
34 void set_internal(bool internal) { this->flags_.internal = 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 { return this->flags_.disabled_by_default; }
40 void set_disabled_by_default(bool disabled_by_default) { this->flags_.disabled_by_default = disabled_by_default; }
41
42 // Get/set the entity category.
44 void set_entity_category(EntityCategory entity_category) {
45 this->flags_.entity_category = static_cast<uint8_t>(entity_category);
46 }
47
48 // Get/set this entity's icon
49 std::string get_icon() const;
50 void set_icon(const char *icon);
51
52 // Check if this entity has state
53 bool has_state() const { return this->flags_.has_state; }
54
55 // Set has_state - for components that need to manually set this
56 void set_has_state(bool state) { this->flags_.has_state = state; }
57
58 protected:
61 virtual uint32_t hash_base() { return 0L; }
62 void calc_object_id_();
63
65 const char *object_id_c_str_{nullptr};
66 const char *icon_c_str_{nullptr};
67 uint32_t object_id_hash_{};
68
69 // Bit-packed flags to save memory (1 byte instead of 5)
70 struct EntityFlags {
71 uint8_t has_own_name : 1;
72 uint8_t internal : 1;
74 uint8_t has_state : 1;
75 uint8_t entity_category : 2; // Supports up to 4 categories
76 uint8_t reserved : 2; // Reserved for future use
77 } flags_{};
78};
79
80class EntityBase_DeviceClass { // NOLINT(readability-identifier-naming)
81 public:
83 std::string get_device_class();
85 void set_device_class(const char *device_class);
86
87 protected:
88 const char *device_class_{nullptr};
89};
90
91class EntityBase_UnitOfMeasurement { // NOLINT(readability-identifier-naming)
92 public:
94 std::string get_unit_of_measurement();
96 void set_unit_of_measurement(const char *unit_of_measurement);
97
98 protected:
99 const char *unit_of_measurement_{nullptr};
100};
101
102} // namespace esphome
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)
bool has_own_name() const
Definition entity_base.h:23
uint32_t object_id_hash_
Definition entity_base.h:67
bool is_internal() const
Definition entity_base.h:33
const char * object_id_c_str_
Definition entity_base.h:65
uint32_t get_object_id_hash()
const StringRef & get_name() const
void set_entity_category(EntityCategory entity_category)
Definition entity_base.h:44
std::string get_icon() const
bool is_disabled_by_default() const
Definition entity_base.h:39
void set_name(const char *name)
void set_icon(const char *icon)
void set_disabled_by_default(bool disabled_by_default)
Definition entity_base.h:40
void set_has_state(bool state)
Definition entity_base.h:56
const char * icon_c_str_
Definition entity_base.h:66
virtual uint32_t hash_base()
The hash_base() function has been deprecated.
Definition entity_base.h:61
bool has_state() const
Definition entity_base.h:53
std::string get_object_id() const
EntityCategory get_entity_category() const
Definition entity_base.h:43
void set_internal(bool internal)
Definition entity_base.h:34
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
@ ENTITY_CATEGORY_NONE
Definition entity_base.h:10
@ ENTITY_CATEGORY_CONFIG
Definition entity_base.h:11
@ ENTITY_CATEGORY_DIAGNOSTIC
Definition entity_base.h:12