mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 00:35:44 +00:00
[event] Store event types in flash memory
This commit is contained in:
@@ -2147,7 +2147,7 @@ message ListEntitiesEventResponse {
|
|||||||
EntityCategory entity_category = 7;
|
EntityCategory entity_category = 7;
|
||||||
string device_class = 8;
|
string device_class = 8;
|
||||||
|
|
||||||
repeated string event_types = 9 [(container_pointer_no_template) = "FixedVector<const char *>"];
|
repeated string event_types = 9 [(container_pointer_no_template) = "std::vector<const char *>"];
|
||||||
uint32 device_id = 10 [(field_ifdef) = "USE_DEVICES"];
|
uint32 device_id = 10 [(field_ifdef) = "USE_DEVICES"];
|
||||||
}
|
}
|
||||||
message EventResponse {
|
message EventResponse {
|
||||||
|
|||||||
@@ -2788,7 +2788,7 @@ class ListEntitiesEventResponse final : public InfoResponseProtoMessage {
|
|||||||
#endif
|
#endif
|
||||||
StringRef device_class_ref_{};
|
StringRef device_class_ref_{};
|
||||||
void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; }
|
void set_device_class(const StringRef &ref) { this->device_class_ref_ = ref; }
|
||||||
const FixedVector<const char *> *event_types{};
|
const std::vector<const char *> *event_types{};
|
||||||
void encode(ProtoWriteBuffer buffer) const override;
|
void encode(ProtoWriteBuffer buffer) const override;
|
||||||
void calculate_size(ProtoSize &size) const override;
|
void calculate_size(ProtoSize &size) const override;
|
||||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
#include "esphome/core/entity_base.h"
|
#include "esphome/core/entity_base.h"
|
||||||
@@ -30,13 +31,23 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
|
|||||||
this->types_ = event_types;
|
this->types_ = event_types;
|
||||||
this->last_event_type_ = nullptr; // Reset when types change
|
this->last_event_type_ = nullptr; // Reset when types change
|
||||||
}
|
}
|
||||||
|
/// Set the event types supported by this event (from vector).
|
||||||
|
void set_event_types(const std::vector<const char *> &event_types) {
|
||||||
|
this->types_ = event_types;
|
||||||
|
this->last_event_type_ = nullptr; // Reset when types change
|
||||||
|
}
|
||||||
|
/// Set the event types supported by this event (from C array).
|
||||||
|
template<size_t N> void set_event_types(const char *const (&event_types)[N]) {
|
||||||
|
this->types_.assign(event_types, event_types + N);
|
||||||
|
this->last_event_type_ = nullptr; // Reset when types change
|
||||||
|
}
|
||||||
|
|
||||||
// Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
|
// Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
|
||||||
void set_event_types(std::initializer_list<std::string> event_types) = delete;
|
void set_event_types(std::initializer_list<std::string> event_types) = delete;
|
||||||
void set_event_types(const FixedVector<std::string> &event_types) = delete;
|
void set_event_types(const std::vector<std::string> &event_types) = delete;
|
||||||
|
|
||||||
/// Return the event types supported by this event.
|
/// Return the event types supported by this event.
|
||||||
const FixedVector<const char *> &get_event_types() const { return this->types_; }
|
const std::vector<const char *> &get_event_types() const { return this->types_; }
|
||||||
|
|
||||||
/// Return the last triggered event type (pointer to string in types_), or nullptr if no event triggered yet.
|
/// Return the last triggered event type (pointer to string in types_), or nullptr if no event triggered yet.
|
||||||
const char *get_last_event_type() const { return this->last_event_type_; }
|
const char *get_last_event_type() const { return this->last_event_type_; }
|
||||||
@@ -45,7 +56,7 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
CallbackManager<void(const std::string &event_type)> event_callback_;
|
CallbackManager<void(const std::string &event_type)> event_callback_;
|
||||||
FixedVector<const char *> types_;
|
std::vector<const char *> types_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Last triggered event type - must point to entry in types_ to ensure valid lifetime.
|
/// Last triggered event type - must point to entry in types_ to ensure valid lifetime.
|
||||||
|
|||||||
Reference in New Issue
Block a user