ESPHome 2026.3.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 {
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 LOG_ENTITY_ICON(TAG, prefix, *(obj)); \
20 LOG_ENTITY_DEVICE_CLASS(TAG, prefix, *(obj)); \
21 }
22
23class Event : public EntityBase, public EntityBase_DeviceClass {
24 public:
25 void trigger(const std::string &event_type);
26
28 void set_event_types(std::initializer_list<const char *> event_types) {
29 this->types_ = event_types;
30 this->last_event_type_ = nullptr; // Reset when types change
31 }
33 void set_event_types(const FixedVector<const char *> &event_types);
35 void set_event_types(const std::vector<const char *> &event_types);
36
37 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
38 void set_event_types(std::initializer_list<std::string> event_types) = delete;
39 void set_event_types(const FixedVector<std::string> &event_types) = delete;
40 void set_event_types(const std::vector<std::string> &event_types) = delete;
41
43 const FixedVector<const char *> &get_event_types() const { return this->types_; }
44
46 StringRef get_last_event_type() const { return StringRef::from_maybe_nullptr(this->last_event_type_); }
47
49 const char *get_event_type(uint8_t index) const {
50 return index < this->types_.size() ? this->types_[index] : nullptr;
51 }
52
54 uint8_t get_last_event_type_index() const {
55 if (this->last_event_type_ == nullptr)
56 return std::numeric_limits<uint8_t>::max();
57 // Most events have <3 types, uint8_t is sufficient for all reasonable scenarios
58 const uint8_t size = static_cast<uint8_t>(this->types_.size());
59 for (uint8_t i = 0; i < size; i++) {
60 if (this->types_[i] == this->last_event_type_)
61 return i;
62 }
63 return std::numeric_limits<uint8_t>::max();
64 }
65
67 bool has_event() const { return this->last_event_type_ != nullptr; }
68
69 void add_on_event_callback(std::function<void(StringRef event_type)> &&callback);
70
71 protected:
74
75 private:
78 const char *last_event_type_{nullptr};
79};
80
81} // namespace event
82} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:299
size_t size() const
Definition helpers.h:456
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:72
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:28
bool has_event() const
Check if an event has been triggered.
Definition event.h:67
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:43
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:54
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:46
void add_on_event_callback(std::function< void(StringRef 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:49
FixedVector< const char * > types_
Definition event.h:73
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
size_t size
Definition helpers.h:854