1
0
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:
J. Nick Koston
2025-11-07 11:37:49 -06:00
parent a823fd322e
commit f4fea1a00f
3 changed files with 16 additions and 5 deletions

View File

@@ -2147,7 +2147,7 @@ message ListEntitiesEventResponse {
EntityCategory entity_category = 7;
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"];
}
message EventResponse {

View File

@@ -2788,7 +2788,7 @@ class ListEntitiesEventResponse final : public InfoResponseProtoMessage {
#endif
StringRef device_class_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 calculate_size(ProtoSize &size) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP

View File

@@ -2,6 +2,7 @@
#include <cstring>
#include <string>
#include <vector>
#include "esphome/core/component.h"
#include "esphome/core/entity_base.h"
@@ -30,13 +31,23 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
this->types_ = event_types;
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
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.
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.
const char *get_last_event_type() const { return this->last_event_type_; }
@@ -45,7 +56,7 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
protected:
CallbackManager<void(const std::string &event_type)> event_callback_;
FixedVector<const char *> types_;
std::vector<const char *> types_;
private:
/// Last triggered event type - must point to entry in types_ to ensure valid lifetime.