strinferf

This commit is contained in:
J. Nick Koston
2026-01-09 16:43:28 -10:00
parent fef7b6093d
commit 2eb98c19f7
5 changed files with 15 additions and 17 deletions

View File

@@ -1414,15 +1414,15 @@ void APIConnection::on_water_heater_command_request(const WaterHeaterCommandRequ
#endif #endif
#ifdef USE_EVENT #ifdef USE_EVENT
void APIConnection::send_event(event::Event *event, std::string_view event_type) { void APIConnection::send_event(event::Event *event, StringRef event_type) {
// MessageCreator stores const char* - data() is safe as event types are null-terminated from codegen // get_last_event_type() returns StringRef pointing to null-terminated string literals from codegen
this->send_message_smart_(event, MessageCreator(event_type.data()), EventResponse::MESSAGE_TYPE, this->send_message_smart_(event, MessageCreator(event_type.c_str()), EventResponse::MESSAGE_TYPE,
EventResponse::ESTIMATED_SIZE); EventResponse::ESTIMATED_SIZE);
} }
uint16_t APIConnection::try_send_event_response(event::Event *event, std::string_view event_type, APIConnection *conn, uint16_t APIConnection::try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
uint32_t remaining_size, bool is_single) { uint32_t remaining_size, bool is_single) {
EventResponse resp; EventResponse resp;
resp.event_type = StringRef(event_type.data(), event_type.size()); resp.event_type = event_type;
return fill_and_encode_entity_state(event, resp, EventResponse::MESSAGE_TYPE, conn, remaining_size, is_single); return fill_and_encode_entity_state(event, resp, EventResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
} }

View File

@@ -173,7 +173,7 @@ class APIConnection final : public APIServerConnection {
#endif #endif
#ifdef USE_EVENT #ifdef USE_EVENT
void send_event(event::Event *event, std::string_view event_type); void send_event(event::Event *event, StringRef event_type);
#endif #endif
#ifdef USE_UPDATE #ifdef USE_UPDATE
@@ -469,7 +469,7 @@ class APIConnection final : public APIServerConnection {
bool is_single); bool is_single);
#endif #endif
#ifdef USE_EVENT #ifdef USE_EVENT
static uint16_t try_send_event_response(event::Event *event, std::string_view event_type, APIConnection *conn, static uint16_t try_send_event_response(event::Event *event, StringRef event_type, APIConnection *conn,
uint32_t remaining_size, bool is_single); 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); static uint16_t try_send_event_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, bool is_single);
#endif #endif

View File

@@ -2,12 +2,12 @@
#include <cstring> #include <cstring>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/entity_base.h" #include "esphome/core/entity_base.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
namespace esphome { namespace esphome {
namespace event { namespace event {
@@ -45,10 +45,8 @@ class Event : public EntityBase, public EntityBase_DeviceClass {
/// 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 FixedVector<const char *> &get_event_types() const { return this->types_; }
/// Return the last triggered event type, or empty string_view if no event triggered yet. /// Return the last triggered event type, or empty StringRef if no event triggered yet.
std::string_view get_last_event_type() const { StringRef get_last_event_type() const { return StringRef::from_maybe_nullptr(this->last_event_type_); }
return this->last_event_type_ != nullptr ? std::string_view(this->last_event_type_) : std::string_view();
}
/// Check if an event has been triggered. /// Check if an event has been triggered.
bool has_event() const { return this->last_event_type_ != nullptr; } bool has_event() const { return this->last_event_type_ != nullptr; }

View File

@@ -618,8 +618,8 @@ void PrometheusHandler::event_row_(AsyncResponseStream *stream, event::Event *ob
stream->print(ESPHOME_F("\",name=\"")); stream->print(ESPHOME_F("\",name=\""));
stream->print(relabel_name_(obj).c_str()); stream->print(relabel_name_(obj).c_str());
stream->print(ESPHOME_F("\",last_event_type=\"")); stream->print(ESPHOME_F("\",last_event_type=\""));
// get_last_event_type() returns string_view; data() is safe as event types are null-terminated // get_last_event_type() returns StringRef pointing to null-terminated string literals from codegen
stream->print(obj->get_last_event_type().data()); stream->print(obj->get_last_event_type().c_str());
stream->print(ESPHOME_F("\"} ")); stream->print(ESPHOME_F("\"} "));
stream->print(ESPHOME_F("1.0")); stream->print(ESPHOME_F("1.0"));
stream->print(ESPHOME_F("\n")); stream->print(ESPHOME_F("\n"));

View File

@@ -8,13 +8,13 @@ event:
on_event: on_event:
- logger.log: Event fired - logger.log: Event fired
- lambda: |- - lambda: |-
// Test get_last_event_type() returns std::string_view // Test get_last_event_type() returns StringRef
if (id(some_event).has_event()) { if (id(some_event).has_event()) {
auto event_type = id(some_event).get_last_event_type(); auto event_type = id(some_event).get_last_event_type();
// Compare with string literal using == // Compare with string literal using ==
if (event_type == "template_event_type1") { if (event_type == "template_event_type1") {
ESP_LOGD("test", "Event type is template_event_type1"); ESP_LOGD("test", "Event type is template_event_type1");
} }
// Log using %.*s format for string_view // Log using %.*s format for StringRef
ESP_LOGD("test", "Event type: %.*s", (int) event_type.size(), event_type.data()); ESP_LOGD("test", "Event type: %.*s", (int) event_type.size(), event_type.c_str());
} }