ESPHome  2025.2.0
event.cpp
Go to the documentation of this file.
1 #include "event.h"
2 
3 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace event {
7 
8 static const char *const TAG = "event";
9 
10 void Event::trigger(const std::string &event_type) {
11  auto found = types_.find(event_type);
12  if (found == types_.end()) {
13  ESP_LOGE(TAG, "'%s': invalid event type for trigger(): %s", this->get_name().c_str(), event_type.c_str());
14  return;
15  }
16  last_event_type = &(*found);
17  ESP_LOGD(TAG, "'%s' Triggered event '%s'", this->get_name().c_str(), last_event_type->c_str());
18  this->event_callback_.call(event_type);
19 }
20 
21 void Event::add_on_event_callback(std::function<void(const std::string &event_type)> &&callback) {
22  this->event_callback_.add(std::move(callback));
23 }
24 
25 } // namespace event
26 } // namespace esphome
void add_on_event_callback(std::function< void(const std::string &event_type)> &&callback)
Definition: event.cpp:21
void trigger(const std::string &event_type)
Definition: event.cpp:10
constexpr const char * c_str() const
Definition: string_ref.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::set< std::string > types_
Definition: event.h:35
CallbackManager< void(const std::string &event_type)> event_callback_
Definition: event.h:34
const StringRef & get_name() const
Definition: entity_base.cpp:10
const std::string * last_event_type
Definition: event.h:26