if we are going ot break it, string view

This commit is contained in:
J. Nick Koston
2026-01-08 21:44:40 -10:00
parent ff0b1a24c7
commit 04ffa74643
4 changed files with 16 additions and 10 deletions
+4 -2
View File
@@ -442,8 +442,10 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co
}
if (traits.supports_direction())
msg.direction = static_cast<enums::FanDirection>(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,
+3 -3
View File
@@ -212,18 +212,18 @@ class FanPresetSetTrigger : public Trigger<std::string> {
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
+3 -3
View File
@@ -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)
+6 -2
View File
@@ -1,5 +1,7 @@
#pragma once
#include <string_view>
#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; }