ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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->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};
60 bool has_own_name_{false};
61 bool internal_{false};
64};
65
66class 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
77class 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
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)
bool has_own_name() const
Definition entity_base.h:23
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
virtual uint32_t hash_base()
The hash_base() function has been deprecated.
Definition entity_base.h:53
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:22
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