diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 989536aca0..4bc19a8bad 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -442,10 +442,8 @@ 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()) { - auto preset = fan->get_preset_mode(); - msg.preset_mode = StringRef(preset.data(), preset.size()); - } + if (traits.supports_preset_modes() && fan->has_preset_mode()) + msg.preset_mode = fan->get_preset_mode(); 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 8175caefea..77abc2f13f 100644 --- a/esphome/components/fan/automation.h +++ b/esphome/components/fan/automation.h @@ -223,7 +223,7 @@ class FanPresetSetTrigger : public Trigger { } protected: - std::string_view last_preset_mode_{}; + StringRef last_preset_mode_{}; }; } // namespace fan diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index 79301a2e18..2e48d84eb9 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -179,10 +179,10 @@ bool Fan::set_preset_mode_(const std::string &preset_mode) { return this->set_preset_mode_(preset_mode.data(), preset_mode.size()); } -bool Fan::set_preset_mode_(std::string_view preset_mode) { +bool Fan::set_preset_mode_(StringRef preset_mode) { // Safe: find_preset_mode_ only uses the input for comparison and returns - // a pointer from traits, so the input string_view's lifetime doesn't matter. - return this->set_preset_mode_(preset_mode.data(), preset_mode.size()); + // a pointer from traits, so the input StringRef's lifetime doesn't matter. + return this->set_preset_mode_(preset_mode.c_str(), preset_mode.size()); } void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; } diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index f1b17d7e15..55d4ba8825 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -1,12 +1,11 @@ #pragma once -#include - #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/optional.h" #include "esphome/core/preferences.h" +#include "esphome/core/string_ref.h" #include "fan_traits.h" namespace esphome { @@ -131,12 +130,10 @@ class Fan : public EntityBase { void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } /// Get the current preset mode. - /// Returns a view of the string stored in traits, or empty view if not set. - /// The returned view points to string literals from codegen (static storage). + /// Returns a StringRef of the string stored in traits, or empty ref if not set. + /// The returned ref points to string literals from codegen (static storage). /// Traits are set once at startup and valid for the lifetime of the program. - std::string_view get_preset_mode() const { - return this->preset_mode_ != nullptr ? std::string_view(this->preset_mode_) : std::string_view(); - } + StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); } /// Check if a preset mode is currently active bool has_preset_mode() const { return this->preset_mode_ != nullptr; } @@ -157,7 +154,7 @@ class Fan : public EntityBase { bool set_preset_mode_(const char *preset_mode, size_t len); bool set_preset_mode_(const char *preset_mode); bool set_preset_mode_(const std::string &preset_mode); - bool set_preset_mode_(std::string_view preset_mode); + bool set_preset_mode_(StringRef preset_mode); /// Clear the preset mode void clear_preset_mode_(); /// Apply preset mode from a FanCall (handles speed-clears-preset convention) diff --git a/tests/components/fan/common.yaml b/tests/components/fan/common.yaml index ccc822be5a..099bbfef08 100644 --- a/tests/components/fan/common.yaml +++ b/tests/components/fan/common.yaml @@ -10,7 +10,7 @@ fan: has_direction: true speed_count: 3 -# Test lambdas using get_preset_mode() which returns std::string_view +# Test lambdas using get_preset_mode() which returns StringRef # These examples match the migration guide in the PR description binary_sensor: - platform: template @@ -44,13 +44,13 @@ binary_sensor: std::string preset = std::string(id(test_fan).get_preset_mode()); // Migration guide: Logging option 1 - // Use .data() - works because string_view points to null-terminated string in traits - ESP_LOGD("test", "Preset: %s", id(test_fan).get_preset_mode().data()); + // Use .c_str() - works because StringRef points to null-terminated string in traits + ESP_LOGD("test", "Preset: %s", id(test_fan).get_preset_mode().c_str()); // Migration guide: Logging option 2 // Use %.*s format (safer, no null-termination assumption) - auto preset_view = id(test_fan).get_preset_mode(); - ESP_LOGD("test", "Preset: %.*s", (int)preset_view.size(), preset_view.data()); + auto preset_ref = id(test_fan).get_preset_mode(); + ESP_LOGD("test", "Preset: %.*s", (int)preset_ref.size(), preset_ref.c_str()); // Test != comparison if (id(test_fan).get_preset_mode() != "Sleep") {