From 04ffa7464363692e0139ec4ce446a11134344e0d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 Jan 2026 21:44:40 -1000 Subject: [PATCH] if we are going ot break it, string view --- esphome/components/api/api_connection.cpp | 6 ++++-- esphome/components/fan/automation.h | 6 +++--- esphome/components/fan/fan.cpp | 6 +++--- esphome/components/fan/fan.h | 8 ++++++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index fb3548d117a..989536aca0d 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -442,8 +442,10 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co } if (traits.supports_direction()) msg.direction = static_cast(fan->direction); - if (traits.supports_preset_modes() && fan->has_preset_mode()) - msg.preset_mode = StringRef(fan->get_preset_mode()); + if (traits.supports_preset_modes() && fan->has_preset_mode()) { + auto preset = fan->get_preset_mode(); + msg.preset_mode = StringRef(preset.data(), preset.size()); + } return fill_and_encode_entity_state(fan, msg, FanStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single); } uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size, diff --git a/esphome/components/fan/automation.h b/esphome/components/fan/automation.h index d6becb66fd0..8175caefeaf 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -212,18 +212,18 @@ class FanPresetSetTrigger : public Trigger { public: FanPresetSetTrigger(Fan *state) { state->add_on_state_callback([this, state]() { - const auto *preset_mode = state->get_preset_mode(); + auto preset_mode = state->get_preset_mode(); auto should_trigger = preset_mode != this->last_preset_mode_; this->last_preset_mode_ = preset_mode; if (should_trigger) { - this->trigger(preset_mode); + this->trigger(std::string(preset_mode)); } }); this->last_preset_mode_ = state->get_preset_mode(); } protected: - const char *last_preset_mode_{""}; + std::string_view last_preset_mode_{}; }; } // namespace fan diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index b56ed72d9ac..e24507678aa 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -61,7 +61,7 @@ void FanCall::perform() { if (this->direction_.has_value()) { ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_))); } - if (this->has_preset_mode()) { + if (this->preset_mode_ != nullptr) { ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_); } this->parent_.control(*this); @@ -201,8 +201,8 @@ void Fan::publish_state() { if (traits.supports_direction()) { ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction))); } - if (this->has_preset_mode()) { - ESP_LOGD(TAG, " Preset Mode: %s", this->get_preset_mode()); + if (this->preset_mode_ != nullptr) { + ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_); } this->state_callback_.call(); #if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY) diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index 17462c41080..eddcb7c2d3f 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -128,8 +130,10 @@ class Fan : public EntityBase { /// Set the restore mode of this fan. void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } - /// Get the current preset mode (returns pointer to string stored in traits, or empty string if not set) - const char *get_preset_mode() const { return this->preset_mode_ != nullptr ? this->preset_mode_ : ""; } + /// Get the current preset mode (returns view of string stored in traits, or empty view if not set) + std::string_view get_preset_mode() const { + return this->preset_mode_ != nullptr ? std::string_view(this->preset_mode_) : std::string_view(); + } /// Check if a preset mode is currently active bool has_preset_mode() const { return this->preset_mode_ != nullptr; }