[event] Return std::string_view from get_last_event_type()

This commit is contained in:
J. Nick Koston
2026-01-09 08:07:57 -10:00
parent 3d54ccac65
commit 8a3e26e6e9
6 changed files with 30 additions and 12 deletions
+5 -4
View File
@@ -1414,14 +1414,15 @@ void APIConnection::on_water_heater_command_request(const WaterHeaterCommandRequ
#endif
#ifdef USE_EVENT
void APIConnection::send_event(event::Event *event, const char *event_type) {
this->send_message_smart_(event, MessageCreator(event_type), EventResponse::MESSAGE_TYPE,
void APIConnection::send_event(event::Event *event, std::string_view event_type) {
// MessageCreator stores const char* - data() is safe as event types are null-terminated from codegen
this->send_message_smart_(event, MessageCreator(event_type.data()), EventResponse::MESSAGE_TYPE,
EventResponse::ESTIMATED_SIZE);
}
uint16_t APIConnection::try_send_event_response(event::Event *event, const char *event_type, APIConnection *conn,
uint16_t APIConnection::try_send_event_response(event::Event *event, std::string_view event_type, APIConnection *conn,
uint32_t remaining_size, bool is_single) {
EventResponse resp;
resp.event_type = StringRef(event_type);
resp.event_type = StringRef(event_type.data(), event_type.size());
return fill_and_encode_entity_state(event, resp, EventResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
}
+2 -2
View File
@@ -173,7 +173,7 @@ class APIConnection final : public APIServerConnection {
#endif
#ifdef USE_EVENT
void send_event(event::Event *event, const char *event_type);
void send_event(event::Event *event, std::string_view event_type);
#endif
#ifdef USE_UPDATE
@@ -469,7 +469,7 @@ class APIConnection final : public APIServerConnection {
bool is_single);
#endif
#ifdef USE_EVENT
static uint16_t try_send_event_response(event::Event *event, const char *event_type, APIConnection *conn,
static uint16_t try_send_event_response(event::Event *event, std::string_view event_type, APIConnection *conn,
uint32_t remaining_size, bool is_single);
static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
#endif
+8 -2
View File
@@ -2,6 +2,7 @@
#include <cstring>
#include <string>
#include <string_view>
#include <vector>
#include "esphome/core/component.h"
@@ -44,8 +45,13 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
/// Return the event types supported by this event.
const FixedVector<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_; }
/// Return the last triggered event type, or empty string_view if no event triggered yet.
std::string_view get_last_event_type() const {
return this->last_event_type_ != nullptr ? std::string_view(this->last_event_type_) : std::string_view();
}
/// Check if an event has been triggered.
bool has_event() const { return this->last_event_type_ != nullptr; }
void add_on_event_callback(std::function<void(const std::string &event_type)> &&callback);
@@ -599,7 +599,7 @@ void PrometheusHandler::event_row_(AsyncResponseStream *stream, event::Event *ob
std::string &friendly_name) {
if (obj->is_internal() && !this->include_internal_)
return;
if (obj->get_last_event_type() != nullptr) {
if (obj->has_event()) {
// We have a valid event type, output this value
stream->print(ESPHOME_F("esphome_event_failed{id=\""));
stream->print(relabel_id_(obj).c_str());
@@ -618,7 +618,8 @@ void PrometheusHandler::event_row_(AsyncResponseStream *stream, event::Event *ob
stream->print(ESPHOME_F("\",name=\""));
stream->print(relabel_name_(obj).c_str());
stream->print(ESPHOME_F("\",last_event_type=\""));
stream->print(obj->get_last_event_type());
// get_last_event_type() returns string_view; data() is safe as event types are null-terminated
stream->print(obj->get_last_event_type().data());
stream->print(ESPHOME_F("\"} "));
stream->print(ESPHOME_F("1.0"));
stream->print(ESPHOME_F("\n"));
+1 -2
View File
@@ -1850,8 +1850,7 @@ void WebServer::handle_event_request(AsyncWebServerRequest *request, const UrlMa
}
static std::string get_event_type(event::Event *event) {
const char *last_type = event ? event->get_last_event_type() : nullptr;
return last_type ? last_type : "";
return event ? std::string(event->get_last_event_type()) : "";
}
std::string WebServer::event_state_json_generator(WebServer *web_server, void *source) {
+11
View File
@@ -7,3 +7,14 @@ event:
- template_event_type2
on_event:
- logger.log: Event fired
- lambda: |-
// Test get_last_event_type() returns std::string_view
if (id(some_event).has_event()) {
auto event_type = id(some_event).get_last_event_type();
// Compare with string literal using ==
if (event_type == "template_event_type1") {
ESP_LOGD("test", "Event type is template_event_type1");
}
// Log using %.*s format for string_view
ESP_LOGD("test", "Event type: %.*s", (int) event_type.size(), event_type.data());
}