mirror of
https://github.com/esphome/esphome.git
synced 2026-05-29 06:27:24 +08:00
stringref
This commit is contained in:
@@ -675,15 +675,13 @@ uint16_t APIConnection::try_send_climate_state(EntityBase *entity, APIConnection
|
|||||||
if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
|
if (traits.get_supports_fan_modes() && climate->fan_mode.has_value())
|
||||||
resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
|
resp.fan_mode = static_cast<enums::ClimateFanMode>(climate->fan_mode.value());
|
||||||
if (!traits.get_supported_custom_fan_modes().empty() && climate->has_custom_fan_mode()) {
|
if (!traits.get_supported_custom_fan_modes().empty() && climate->has_custom_fan_mode()) {
|
||||||
auto mode = climate->get_custom_fan_mode();
|
resp.custom_fan_mode = climate->get_custom_fan_mode();
|
||||||
resp.custom_fan_mode = StringRef(mode.data(), mode.size());
|
|
||||||
}
|
}
|
||||||
if (traits.get_supports_presets() && climate->preset.has_value()) {
|
if (traits.get_supports_presets() && climate->preset.has_value()) {
|
||||||
resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
|
resp.preset = static_cast<enums::ClimatePreset>(climate->preset.value());
|
||||||
}
|
}
|
||||||
if (!traits.get_supported_custom_presets().empty() && climate->has_custom_preset()) {
|
if (!traits.get_supported_custom_presets().empty() && climate->has_custom_preset()) {
|
||||||
auto preset = climate->get_custom_preset();
|
resp.custom_preset = climate->get_custom_preset();
|
||||||
resp.custom_preset = StringRef(preset.data(), preset.size());
|
|
||||||
}
|
}
|
||||||
if (traits.get_supports_swing_modes())
|
if (traits.get_supports_swing_modes())
|
||||||
resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
|
resp.swing_mode = static_cast<enums::ClimateSwingMode>(climate->swing_mode);
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ void BedJetClimate::control(const ClimateCall &call) {
|
|||||||
} else if (preset == "EXT HT") {
|
} else if (preset == "EXT HT") {
|
||||||
result = this->parent_->button_ext_heat();
|
result = this->parent_->button_ext_heat();
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGW(TAG, "Unsupported preset: %.*s", (int) preset.size(), preset.data());
|
ESP_LOGW(TAG, "Unsupported preset: %.*s", (int) preset.size(), preset.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,10 +209,10 @@ void BedJetClimate::control(const ClimateCall &call) {
|
|||||||
}
|
}
|
||||||
} else if (call.has_custom_fan_mode()) {
|
} else if (call.has_custom_fan_mode()) {
|
||||||
auto fan_mode = call.get_custom_fan_mode();
|
auto fan_mode = call.get_custom_fan_mode();
|
||||||
auto fan_index = bedjet_fan_speed_to_step(fan_mode.data());
|
auto fan_index = bedjet_fan_speed_to_step(fan_mode.c_str());
|
||||||
if (fan_index <= 19) {
|
if (fan_index <= 19) {
|
||||||
ESP_LOGV(TAG, "[%s] Converted fan mode %.*s to bedjet fan step %d", this->get_name().c_str(),
|
ESP_LOGV(TAG, "[%s] Converted fan mode %.*s to bedjet fan step %d", this->get_name().c_str(),
|
||||||
(int) fan_mode.size(), fan_mode.data(), fan_index);
|
(int) fan_mode.size(), fan_mode.c_str(), fan_index);
|
||||||
bool result = this->parent_->set_fan_index(fan_index);
|
bool result = this->parent_->set_fan_index(fan_index);
|
||||||
if (result) {
|
if (result) {
|
||||||
this->set_custom_fan_mode_(fan_mode);
|
this->set_custom_fan_mode_(fan_mode);
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
#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/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include "esphome/core/preferences.h"
|
#include "esphome/core/preferences.h"
|
||||||
|
#include "esphome/core/string_ref.h"
|
||||||
#include "climate_mode.h"
|
#include "climate_mode.h"
|
||||||
#include "climate_traits.h"
|
#include "climate_traits.h"
|
||||||
|
|
||||||
@@ -112,12 +111,8 @@ class ClimateCall {
|
|||||||
const optional<ClimateFanMode> &get_fan_mode() const;
|
const optional<ClimateFanMode> &get_fan_mode() const;
|
||||||
const optional<ClimateSwingMode> &get_swing_mode() const;
|
const optional<ClimateSwingMode> &get_swing_mode() const;
|
||||||
const optional<ClimatePreset> &get_preset() const;
|
const optional<ClimatePreset> &get_preset() const;
|
||||||
std::string_view get_custom_fan_mode() const {
|
StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
|
||||||
return this->custom_fan_mode_ != nullptr ? std::string_view(this->custom_fan_mode_) : std::string_view();
|
StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
|
||||||
}
|
|
||||||
std::string_view get_custom_preset() const {
|
|
||||||
return this->custom_preset_ != nullptr ? std::string_view(this->custom_preset_) : std::string_view();
|
|
||||||
}
|
|
||||||
bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
|
bool has_custom_fan_mode() const { return this->custom_fan_mode_ != nullptr; }
|
||||||
bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
|
bool has_custom_preset() const { return this->custom_preset_ != nullptr; }
|
||||||
|
|
||||||
@@ -272,15 +267,11 @@ class Climate : public EntityBase {
|
|||||||
/// The active swing mode of the climate device.
|
/// The active swing mode of the climate device.
|
||||||
ClimateSwingMode swing_mode{CLIMATE_SWING_OFF};
|
ClimateSwingMode swing_mode{CLIMATE_SWING_OFF};
|
||||||
|
|
||||||
/// Get the active custom fan mode (read-only access). Returns std::string_view.
|
/// Get the active custom fan mode (read-only access). Returns StringRef.
|
||||||
std::string_view get_custom_fan_mode() const {
|
StringRef get_custom_fan_mode() const { return StringRef::from_maybe_nullptr(this->custom_fan_mode_); }
|
||||||
return this->custom_fan_mode_ != nullptr ? std::string_view(this->custom_fan_mode_) : std::string_view();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the active custom preset (read-only access). Returns std::string_view.
|
/// Get the active custom preset (read-only access). Returns StringRef.
|
||||||
std::string_view get_custom_preset() const {
|
StringRef get_custom_preset() const { return StringRef::from_maybe_nullptr(this->custom_preset_); }
|
||||||
return this->custom_preset_ != nullptr ? std::string_view(this->custom_preset_) : std::string_view();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend ClimateCall;
|
friend ClimateCall;
|
||||||
@@ -292,7 +283,7 @@ class Climate : public EntityBase {
|
|||||||
/// Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
|
/// Set custom fan mode. Reset primary fan mode. Return true if fan mode has been changed.
|
||||||
bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
|
bool set_custom_fan_mode_(const char *mode) { return this->set_custom_fan_mode_(mode, strlen(mode)); }
|
||||||
bool set_custom_fan_mode_(const char *mode, size_t len);
|
bool set_custom_fan_mode_(const char *mode, size_t len);
|
||||||
bool set_custom_fan_mode_(std::string_view mode) { return this->set_custom_fan_mode_(mode.data(), mode.size()); }
|
bool set_custom_fan_mode_(StringRef mode) { return this->set_custom_fan_mode_(mode.c_str(), mode.size()); }
|
||||||
/// Clear custom fan mode.
|
/// Clear custom fan mode.
|
||||||
void clear_custom_fan_mode_();
|
void clear_custom_fan_mode_();
|
||||||
|
|
||||||
@@ -302,7 +293,7 @@ class Climate : public EntityBase {
|
|||||||
/// Set custom preset. Reset primary preset. Return true if preset has been changed.
|
/// Set custom preset. Reset primary preset. Return true if preset has been changed.
|
||||||
bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
|
bool set_custom_preset_(const char *preset) { return this->set_custom_preset_(preset, strlen(preset)); }
|
||||||
bool set_custom_preset_(const char *preset, size_t len);
|
bool set_custom_preset_(const char *preset, size_t len);
|
||||||
bool set_custom_preset_(std::string_view preset) { return this->set_custom_preset_(preset.data(), preset.size()); }
|
bool set_custom_preset_(StringRef preset) { return this->set_custom_preset_(preset.c_str(), preset.size()); }
|
||||||
/// Clear custom preset.
|
/// Clear custom preset.
|
||||||
void clear_custom_preset_();
|
void clear_custom_preset_();
|
||||||
|
|
||||||
|
|||||||
@@ -65,14 +65,14 @@ void AirConditioner::control(const ClimateCall &call) {
|
|||||||
if (call.get_preset().has_value()) {
|
if (call.get_preset().has_value()) {
|
||||||
ctrl.preset = Converters::to_midea_preset(call.get_preset().value());
|
ctrl.preset = Converters::to_midea_preset(call.get_preset().value());
|
||||||
} else if (call.has_custom_preset()) {
|
} else if (call.has_custom_preset()) {
|
||||||
// get_custom_preset() returns string_view; Converters expects null-terminated const char*
|
// c_str() is safe as custom presets are null-terminated strings from codegen
|
||||||
ctrl.preset = Converters::to_midea_preset(call.get_custom_preset().data());
|
ctrl.preset = Converters::to_midea_preset(call.get_custom_preset().c_str());
|
||||||
}
|
}
|
||||||
if (call.get_fan_mode().has_value()) {
|
if (call.get_fan_mode().has_value()) {
|
||||||
ctrl.fanMode = Converters::to_midea_fan_mode(call.get_fan_mode().value());
|
ctrl.fanMode = Converters::to_midea_fan_mode(call.get_fan_mode().value());
|
||||||
} else if (call.has_custom_fan_mode()) {
|
} else if (call.has_custom_fan_mode()) {
|
||||||
// get_custom_fan_mode() returns string_view; Converters expects null-terminated const char*
|
// c_str() is safe as custom fan modes are null-terminated strings from codegen
|
||||||
ctrl.fanMode = Converters::to_midea_fan_mode(call.get_custom_fan_mode().data());
|
ctrl.fanMode = Converters::to_midea_fan_mode(call.get_custom_fan_mode().c_str());
|
||||||
}
|
}
|
||||||
this->base_.control(ctrl);
|
this->base_.control(ctrl);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -357,7 +357,7 @@ bool MQTTClimateComponent::publish_state_() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this->device_->has_custom_preset())
|
if (this->device_->has_custom_preset())
|
||||||
payload = this->device_->get_custom_preset().data();
|
payload = this->device_->get_custom_preset().c_str();
|
||||||
if (!this->publish(this->get_preset_state_topic(), payload))
|
if (!this->publish(this->get_preset_state_topic(), payload))
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
@@ -429,7 +429,7 @@ bool MQTTClimateComponent::publish_state_() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this->device_->has_custom_fan_mode())
|
if (this->device_->has_custom_fan_mode())
|
||||||
payload = this->device_->get_custom_fan_mode().data();
|
payload = this->device_->get_custom_fan_mode().c_str();
|
||||||
if (!this->publish(this->get_fan_mode_state_topic(), payload))
|
if (!this->publish(this->get_fan_mode_state_topic(), payload))
|
||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -222,7 +222,7 @@ void ThermostatClimate::control(const climate::ClimateCall &call) {
|
|||||||
if (call.has_custom_preset()) {
|
if (call.has_custom_preset()) {
|
||||||
// setup_complete_ blocks modifying/resetting the temps immediately after boot
|
// setup_complete_ blocks modifying/resetting the temps immediately after boot
|
||||||
if (this->setup_complete_) {
|
if (this->setup_complete_) {
|
||||||
this->change_custom_preset_(call.get_custom_preset().data());
|
this->change_custom_preset_(call.get_custom_preset());
|
||||||
} else {
|
} else {
|
||||||
// Use the base class method which handles pointer lookup internally
|
// Use the base class method which handles pointer lookup internally
|
||||||
this->set_custom_preset_(call.get_custom_preset());
|
this->set_custom_preset_(call.get_custom_preset());
|
||||||
@@ -1218,11 +1218,12 @@ void ThermostatClimate::change_preset_(climate::ClimatePreset preset) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThermostatClimate::change_custom_preset_(const char *custom_preset) {
|
void ThermostatClimate::change_custom_preset_(const char *custom_preset, size_t len) {
|
||||||
// Linear search through custom preset configurations
|
// Linear search through custom preset configurations
|
||||||
const ThermostatClimateTargetTempConfig *config = nullptr;
|
const ThermostatClimateTargetTempConfig *config = nullptr;
|
||||||
for (const auto &entry : this->custom_preset_config_) {
|
for (const auto &entry : this->custom_preset_config_) {
|
||||||
if (strcmp(entry.name, custom_preset) == 0) {
|
// Compare first len chars, then verify entry.name ends there (same length)
|
||||||
|
if (strncmp(entry.name, custom_preset, len) == 0 && entry.name[len] == '\0') {
|
||||||
config = &entry.config;
|
config = &entry.config;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -214,7 +214,13 @@ class ThermostatClimate : public climate::Climate, public Component {
|
|||||||
/// Change to a provided preset setting; will reset temperature, mode, fan, and swing modes accordingly
|
/// Change to a provided preset setting; will reset temperature, mode, fan, and swing modes accordingly
|
||||||
void change_preset_(climate::ClimatePreset preset);
|
void change_preset_(climate::ClimatePreset preset);
|
||||||
/// Change to a provided custom preset setting; will reset temperature, mode, fan, and swing modes accordingly
|
/// Change to a provided custom preset setting; will reset temperature, mode, fan, and swing modes accordingly
|
||||||
void change_custom_preset_(const char *custom_preset);
|
void change_custom_preset_(const char *custom_preset) {
|
||||||
|
this->change_custom_preset_(custom_preset, strlen(custom_preset));
|
||||||
|
}
|
||||||
|
void change_custom_preset_(const char *custom_preset, size_t len);
|
||||||
|
void change_custom_preset_(StringRef custom_preset) {
|
||||||
|
this->change_custom_preset_(custom_preset.c_str(), custom_preset.size());
|
||||||
|
}
|
||||||
|
|
||||||
/// Applies the temperature, mode, fan, and swing modes of the provided config.
|
/// Applies the temperature, mode, fan, and swing modes of the provided config.
|
||||||
/// This is agnostic of custom vs built in preset
|
/// This is agnostic of custom vs built in preset
|
||||||
|
|||||||
@@ -1543,15 +1543,15 @@ std::string WebServer::climate_json_(climate::Climate *obj, JsonDetail start_con
|
|||||||
root[ESPHOME_F("fan_mode")] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value()));
|
root[ESPHOME_F("fan_mode")] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value()));
|
||||||
}
|
}
|
||||||
if (!traits.get_supported_custom_fan_modes().empty() && obj->has_custom_fan_mode()) {
|
if (!traits.get_supported_custom_fan_modes().empty() && obj->has_custom_fan_mode()) {
|
||||||
// get_custom_fan_mode() returns string_view pointing to null-terminated string literals from codegen
|
// c_str() is safe as custom fan modes are null-terminated strings from codegen
|
||||||
root[ESPHOME_F("custom_fan_mode")] = obj->get_custom_fan_mode().data();
|
root[ESPHOME_F("custom_fan_mode")] = obj->get_custom_fan_mode().c_str();
|
||||||
}
|
}
|
||||||
if (traits.get_supports_presets() && obj->preset.has_value()) {
|
if (traits.get_supports_presets() && obj->preset.has_value()) {
|
||||||
root[ESPHOME_F("preset")] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
|
root[ESPHOME_F("preset")] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
|
||||||
}
|
}
|
||||||
if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) {
|
if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) {
|
||||||
// get_custom_preset() returns string_view pointing to null-terminated string literals from codegen
|
// c_str() is safe as custom presets are null-terminated strings from codegen
|
||||||
root[ESPHOME_F("custom_preset")] = obj->get_custom_preset().data();
|
root[ESPHOME_F("custom_preset")] = obj->get_custom_preset().c_str();
|
||||||
}
|
}
|
||||||
if (traits.get_supports_swing_modes()) {
|
if (traits.get_supports_swing_modes()) {
|
||||||
root[ESPHOME_F("swing_mode")] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));
|
root[ESPHOME_F("swing_mode")] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ climate:
|
|||||||
if (preset == "Default Preset") {
|
if (preset == "Default Preset") {
|
||||||
ESP_LOGD("test", "Preset is Default Preset");
|
ESP_LOGD("test", "Preset is Default Preset");
|
||||||
}
|
}
|
||||||
// Log using %.*s format for string_view
|
// Log using %.*s format for StringRef
|
||||||
ESP_LOGD("test", "Custom preset: %.*s", (int) preset.size(), preset.data());
|
ESP_LOGD("test", "Custom preset: %.*s", (int) preset.size(), preset.c_str());
|
||||||
}
|
}
|
||||||
idle_action:
|
idle_action:
|
||||||
- logger.log: idle_action
|
- logger.log: idle_action
|
||||||
|
|||||||
Reference in New Issue
Block a user