ESPHome 2026.11.0-dev
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
11
12namespace esphome::event {
13
16
17#define LOG_EVENT(prefix, type, obj) \
18 if (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
19 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
20 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
21 }
22
23class Event : public EntityBase {
24 public:
26 void trigger(const char *event_type);
27 void trigger(const std::string &event_type) { this->trigger(event_type.c_str()); }
28
30 void set_event_types_static(const char *const *event_types, size_t count) {
31 this->types_.assign_static(event_types, count);
32 this->last_event_type_ = nullptr;
33 }
35 void set_event_types(std::initializer_list<const char *> event_types) {
36 this->set_event_types_copy_(event_types.begin(), event_types.size());
37 }
39 void set_event_types(const EventTypes &event_types) {
40 this->set_event_types_copy_(event_types.data(), event_types.size());
41 }
42 void set_event_types(const FixedVector<const char *> &event_types) {
43 this->set_event_types_copy_(event_types.begin(), event_types.size());
44 }
45
46 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
47 void set_event_types(std::initializer_list<std::string> event_types) = delete;
48 void set_event_types(const FixedVector<std::string> &event_types) = delete;
49
51 const EventTypes &get_event_types() const { return this->types_; }
52
54 StringRef get_last_event_type() const { return StringRef::from_maybe_nullptr(this->last_event_type_); }
55
57 const char *get_event_type(uint8_t index) const {
58 return index < this->types_.size() ? this->types_[index] : nullptr;
59 }
60
62 uint8_t get_last_event_type_index() const {
63 if (this->last_event_type_ == nullptr)
64 return std::numeric_limits<uint8_t>::max();
65 // Most events have <3 types, uint8_t is sufficient for all reasonable scenarios
66 const uint8_t size = static_cast<uint8_t>(this->types_.size());
67 for (uint8_t i = 0; i < size; i++) {
68 if (this->types_[i] == this->last_event_type_)
69 return i;
70 }
71 return std::numeric_limits<uint8_t>::max();
72 }
73
75 bool has_event() const { return this->last_event_type_ != nullptr; }
76
77 template<typename F> void add_on_event_callback(F &&callback) {
78 this->event_callback_.add(std::forward<F>(callback));
79 }
80
81 protected:
82 void set_event_types_copy_(const char *const *event_types, size_t count);
83
86
87 private:
90 const char *last_event_type_{nullptr};
91};
92
93} // namespace esphome::event
const T * data() const
Definition helpers.h:139
constexpr size_t size() const
Definition helpers.h:140
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:624
size_t size() const
Definition helpers.h:800
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:58
LazyCallbackManager< void(StringRef event_type)> event_callback_
Definition event.h:84
void set_event_types(std::initializer_list< const char * > event_types)
Runtime lists are copied; the strings must still outlive the event.
Definition event.h:35
bool has_event() const
Check if an event has been triggered.
Definition event.h:75
void set_event_types(std::initializer_list< std::string > event_types)=delete
void trigger(const char *event_type)
Trigger an event; the type is matched against the configured types by string compare.
Definition event.cpp:10
void set_event_types(const FixedVector< std::string > &event_types)=delete
void set_event_types(const FixedVector< const char * > &event_types)
Definition event.h:42
const EventTypes & get_event_types() const
Return the event types supported by this event.
Definition event.h:51
void set_event_types_static(const char *const *event_types, size_t count)
Codegen only: points at a table that outlives the event (see ConstVector::assign_static).
Definition event.h:30
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:62
void set_event_types(const EventTypes &event_types)
Copy the event types of another event, for components that wrap one.
Definition event.h:39
EventTypes types_
Definition event.h:85
void trigger(const std::string &event_type)
Definition event.h:27
StringRef get_last_event_type() const
Return the last triggered event type, or empty StringRef if no event triggered yet.
Definition event.h:54
void set_event_types_copy_(const char *const *event_types, size_t count)
Definition event.cpp:31
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:57
void add_on_event_callback(F &&callback)
Definition event.h:77
unsigned count
Definition gif.cpp:51
uint16_t size
Definition helpers.cpp:34