ESPHome 2026.6.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#include <vector>
7
12
13namespace esphome::event {
14
15#define LOG_EVENT(prefix, type, obj) \
16 if ((obj) != nullptr) { \
17 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
18 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
19 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
20 }
21
22class Event : public EntityBase {
23 public:
24 void trigger(const std::string &event_type);
25
27 void set_event_types(std::initializer_list<const char *> event_types) {
28 this->types_ = event_types;
29 this->last_event_type_ = nullptr; // Reset when types change
30 }
32 void set_event_types(const FixedVector<const char *> &event_types);
34 void set_event_types(const std::vector<const char *> &event_types);
35
36 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
37 void set_event_types(std::initializer_list<std::string> event_types) = delete;
38 void set_event_types(const FixedVector<std::string> &event_types) = delete;
39 void set_event_types(const std::vector<std::string> &event_types) = delete;
40
42 const FixedVector<const char *> &get_event_types() const { return this->types_; }
43
45 StringRef get_last_event_type() const { return StringRef::from_maybe_nullptr(this->last_event_type_); }
46
48 const char *get_event_type(uint8_t index) const {
49 return index < this->types_.size() ? this->types_[index] : nullptr;
50 }
51
53 uint8_t get_last_event_type_index() const {
54 if (this->last_event_type_ == nullptr)
55 return std::numeric_limits<uint8_t>::max();
56 // Most events have <3 types, uint8_t is sufficient for all reasonable scenarios
57 const uint8_t size = static_cast<uint8_t>(this->types_.size());
58 for (uint8_t i = 0; i < size; i++) {
59 if (this->types_[i] == this->last_event_type_)
60 return i;
61 }
62 return std::numeric_limits<uint8_t>::max();
63 }
64
66 bool has_event() const { return this->last_event_type_ != nullptr; }
67
68 template<typename F> void add_on_event_callback(F &&callback) {
69 this->event_callback_.add(std::forward<F>(callback));
70 }
71
72 protected:
75
76 private:
79 const char *last_event_type_{nullptr};
80};
81
82} // namespace esphome::event
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
size_t size() const
Definition helpers.h:686
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
LazyCallbackManager< void(StringRef event_type)> event_callback_
Definition event.h:73
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:27
bool has_event() const
Check if an event has been triggered.
Definition event.h:66
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:42
void set_event_types(const FixedVector< std::string > &event_types)=delete
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:53
void trigger(const std::string &event_type)
Definition event.cpp:10
StringRef get_last_event_type() const
Return the last triggered event type, or empty StringRef if no event triggered yet.
Definition event.h:45
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:48
FixedVector< const char * > types_
Definition event.h:74
void set_event_types(const std::vector< std::string > &event_types)=delete
void add_on_event_callback(F &&callback)
Definition event.h:68
uint16_t size
Definition helpers.cpp:25