mirror of
https://github.com/esphome/esphome.git
synced 2026-09-16 01:28:39 +00:00
address copilot review comments
This commit is contained in:
@@ -156,7 +156,7 @@ const char *Fan::find_preset_mode_(const char *preset_mode, size_t len) {
|
||||
|
||||
bool Fan::set_preset_mode_(const char *preset_mode, size_t len) {
|
||||
if (preset_mode == nullptr || len == 0) {
|
||||
// Treat nullptr/empty as clearing the preset mode
|
||||
// Treat nullptr or empty string as clearing the preset mode (no valid preset is "")
|
||||
if (this->preset_mode_ == nullptr) {
|
||||
return false; // No change
|
||||
}
|
||||
@@ -180,6 +180,8 @@ bool Fan::set_preset_mode_(const std::string &preset_mode) {
|
||||
}
|
||||
|
||||
bool Fan::set_preset_mode_(std::string_view 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());
|
||||
}
|
||||
|
||||
|
||||
@@ -131,8 +131,9 @@ 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 (static storage), or empty view if not set.
|
||||
/// Safe to use as the underlying string has static lifetime.
|
||||
/// 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).
|
||||
/// 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user