ESPHome 2026.11.0-dev
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
8ESPHOME_LOG_TAG(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", LOG_STR_ARG(this->get_log_name()), event_type);
21 return;
22 }
23 this->last_event_type_ = found;
24 ESP_LOGV(TAG, "'%s' >> '%s'", LOG_STR_ARG(this->get_log_name()), 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
31void Event::set_event_types_copy_(const char *const *event_types, size_t count) {
32 this->types_.assign_copy(event_types, count);
33 this->last_event_type_ = nullptr; // Reset when types change
34}
35
36} // namespace esphome::event
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
LazyCallbackManager< void(StringRef event_type)> event_callback_
Definition event.h:84
void trigger(const char *event_type)
Trigger an event; the type is matched against the configured types by string compare.
Definition event.cpp:10
EventTypes types_
Definition event.h:85
void set_event_types_copy_(const char *const *event_types, size_t count)
Definition event.cpp:31
uint8_t type
unsigned count
Definition gif.cpp:51
ESPHOME_LOG_TAG(TAG, "event")