ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
event.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <string>
5#include <vector>
6
10
11namespace esphome {
12namespace event {
13
14#define LOG_EVENT(prefix, type, obj) \
15 if ((obj) != nullptr) { \
16 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17 if (!(obj)->get_icon_ref().empty()) { \
18 ESP_LOGCONFIG(TAG, "%s Icon: '%s'", prefix, (obj)->get_icon_ref().c_str()); \
19 } \
20 if (!(obj)->get_device_class_ref().empty()) { \
21 ESP_LOGCONFIG(TAG, "%s Device Class: '%s'", prefix, (obj)->get_device_class_ref().c_str()); \
22 } \
23 }
24
25class Event : public EntityBase, public EntityBase_DeviceClass {
26 public:
27 void trigger(const std::string &event_type);
28
30 void set_event_types(std::initializer_list<const char *> event_types) {
31 this->types_ = event_types;
32 this->last_event_type_ = nullptr; // Reset when types change
33 }
35 void set_event_types(const FixedVector<const char *> &event_types);
37 void set_event_types(const std::vector<const char *> &event_types);
38
39 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
40 void set_event_types(std::initializer_list<std::string> event_types) = delete;
41 void set_event_types(const FixedVector<std::string> &event_types) = delete;
42 void set_event_types(const std::vector<std::string> &event_types) = delete;
43
45 const FixedVector<const char *> &get_event_types() const { return this->types_; }
46
48 const char *get_last_event_type() const { return this->last_event_type_; }
49
50 void add_on_event_callback(std::function<void(const std::string &event_type)> &&callback);
51
52 protected:
53 CallbackManager<void(const std::string &event_type)> event_callback_;
55
56 private:
59 const char *last_event_type_{nullptr};
60};
61
62} // namespace event
63} // namespace esphome
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:184
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:30
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:45
void set_event_types(const FixedVector< std::string > &event_types)=delete
void trigger(const std::string &event_type)
Definition event.cpp:11
void add_on_event_callback(std::function< void(const std::string &event_type)> &&callback)
Definition event.cpp:48
FixedVector< const char * > types_
Definition event.h:54
void set_event_types(const std::vector< std::string > &event_types)=delete
const char * get_last_event_type() const
Return the last triggered event type (pointer to string in types_), or nullptr if no event triggered ...
Definition event.h:48
CallbackManager< void(const std::string &event_type)> event_callback_
Definition event.h:53
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7