ESPHome 2026.1.1
Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <limits>
5#include <string>
6#include <vector>
7
12
13namespace esphome {
14namespace event {
15
16#define LOG_EVENT(prefix, type, obj) \
17 if ((obj) != nullptr) { \
18 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
19 if (!(obj)->get_icon_ref().empty()) { \
20 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
21 } \
22 if (!(obj)->get_device_class_ref().empty()) { \
23 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
24 } \
25 }
26
27class Event : public EntityBase, public EntityBase_DeviceClass {
28 public:
29 void trigger(const std::string &event_type);
30
32 void set_event_types(std::initializer_list<const char *> event_types) {
33 this->types_ = event_types;
34 this->last_event_type_ = nullptr; // Reset when types change
35 }
37 void set_event_types(const FixedVector<const char *> &event_types);
39 void set_event_types(const std::vector<const char *> &event_types);
40
41 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
42 void set_event_types(std::initializer_list<std::string> event_types) = delete;
43 void set_event_types(const FixedVector<std::string> &event_types) = delete;
44 void set_event_types(const std::vector<std::string> &event_types) = delete;
45
47 const FixedVector<const char *> &get_event_types() const { return this->types_; }
48
50 StringRef get_last_event_type() const { return StringRef::from_maybe_nullptr(this->last_event_type_); }
51
53 const char *get_event_type(uint8_t index) const {
54 return index < this->types_.size() ? this->types_[index] : nullptr;
55 }
56
58 uint8_t get_last_event_type_index() const {
59 if (this->last_event_type_ == nullptr)
60 return std::numeric_limits<uint8_t>::max();
61 // Most events have <3 types, uint8_t is sufficient for all reasonable scenarios
62 const uint8_t size = static_cast<uint8_t>(this->types_.size());
63 for (uint8_t i = 0; i < size; i++) {
64 if (this->types_[i] == this->last_event_type_)
65 return i;
66 }
67 return std::numeric_limits<uint8_t>::max();
68 }
69
71 bool has_event() const { return this->last_event_type_ != nullptr; }
72
73 void add_on_event_callback(std::function<void(const std::string &event_type)> &&callback);
74
75 protected:
76 LazyCallbackManager<void(const std::string &event_type)> event_callback_;
78
79 private:
82 const char *last_event_type_{nullptr};
83};
84
85} // namespace event
86} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:188
size_t size() const
Definition helpers.h:345
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:53
void set_event_types(std::initializer_list< const char * > event_types)
Set the event types supported by this event (from initializer list).
Definition event.h:32
bool has_event() const
Check if an event has been triggered.
Definition event.h:71
void set_event_types(std::initializer_list< std::string > event_types)=delete
const FixedVector< const char * > & get_event_types() const
Return the event types supported by this event.
Definition event.h:47
void set_event_types(const FixedVector< std::string > &event_types)=delete
LazyCallbackManager< void(const std::string &event_type)> event_callback_
Definition event.h:76
uint8_t get_last_event_type_index() const
Return index of last triggered event type, or max uint8_t if no event triggered yet.
Definition event.h:58
void trigger(const std::string &event_type)
Definition event.cpp:11
StringRef get_last_event_type() const
Return the last triggered event type, or empty StringRef if no event triggered yet.
Definition event.h:50
void add_on_event_callback(std::function< void(const std::string &event_type)> &&callback)
Definition event.cpp:48
const char * get_event_type(uint8_t index) const
Return event type by index, or nullptr if index is out of bounds.
Definition event.h:53
FixedVector< const char * > types_
Definition event.h:77
void set_event_types(const std::vector< std::string > &event_types)=delete
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7