ESPHome 2026.10.2
Loading...
Searching...
No Matches
event.cpp
Go to the documentation of this file.
1#include "event.h"
4#include "esphome/core/log.h"
5
6namespace esphome::event {
7
8static const char *const TAG = "event";
9
10void Event::trigger(const char *event_type) {
11 // Linear search with strcmp - faster than std::set for small datasets (1-5 items typical)
12 const char *found = nullptr;
13 for (const char *type : this->types_) {
14 if (strcmp(type, event_type) == 0) {
15 found = type;
16 break;
17 }
18 }
19 if (found == nullptr) {
20 ESP_LOGE(TAG, "'%s': invalid event type for trigger(): %s", this->get_name().c_str(), event_type);
21 return;
22 }
23 this->last_event_type_ = found;
24 ESP_LOGV(TAG, "'%s' >> '%s'", this->get_name().c_str(), this->last_event_type_);
25 this->event_callback_.call(StringRef(found));
26#if defined(USE_EVENT) && defined(USE_CONTROLLER_REGISTRY)
27 ControllerRegistry::notify_event(this);
28#endif
29}
30
32 this->types_.init(event_types.size());
33 for (const char *type : event_types) {
34 this->types_.push_back(type);
35 }
36 this->last_event_type_ = nullptr; // Reset when types change
37}
38
39} // namespace esphome::event
const StringRef & get_name() const
Definition entity_base.h:71
Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates This avo...
Definition helpers.h:621
size_t size() const
Definition helpers.h:797
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:748
void init(size_t n)
Definition helpers.h:711
StringRef is a reference to a string owned by something else.
Definition string_ref.h:30
LazyCallbackManager< void(StringRef event_type)> event_callback_
Definition event.h:71
void set_event_types(std::initializer_list< const char * > event_types)
Set the event types supported by this event; called by generated code with string literals.
Definition event.h:28
void trigger(const char *event_type)
Trigger an event; the type is matched against the configured types by string compare.
Definition event.cpp:10
FixedVector< const char * > types_
Definition event.h:72
uint16_t type