mirror of
https://github.com/esphome/esphome.git
synced 2025-10-25 21:23:53 +01:00
[climate] Remove unnecessary vector allocations in state save/restore
This commit is contained in:
@@ -385,12 +385,14 @@ void Climate::save_state_() {
|
|||||||
if (!traits.get_supported_custom_fan_modes().empty() && custom_fan_mode.has_value()) {
|
if (!traits.get_supported_custom_fan_modes().empty() && custom_fan_mode.has_value()) {
|
||||||
state.uses_custom_fan_mode = true;
|
state.uses_custom_fan_mode = true;
|
||||||
const auto &supported = traits.get_supported_custom_fan_modes();
|
const auto &supported = traits.get_supported_custom_fan_modes();
|
||||||
std::vector<std::string> vec{supported.begin(), supported.end()};
|
// std::set has consistent order (lexicographic for strings)
|
||||||
for (size_t i = 0; i < vec.size(); i++) {
|
size_t i = 0;
|
||||||
if (vec[i] == custom_fan_mode) {
|
for (const auto &mode : supported) {
|
||||||
|
if (mode == custom_fan_mode) {
|
||||||
state.custom_fan_mode = i;
|
state.custom_fan_mode = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (traits.get_supports_presets() && preset.has_value()) {
|
if (traits.get_supports_presets() && preset.has_value()) {
|
||||||
@@ -400,12 +402,14 @@ void Climate::save_state_() {
|
|||||||
if (!traits.get_supported_custom_presets().empty() && custom_preset.has_value()) {
|
if (!traits.get_supported_custom_presets().empty() && custom_preset.has_value()) {
|
||||||
state.uses_custom_preset = true;
|
state.uses_custom_preset = true;
|
||||||
const auto &supported = traits.get_supported_custom_presets();
|
const auto &supported = traits.get_supported_custom_presets();
|
||||||
std::vector<std::string> vec{supported.begin(), supported.end()};
|
// std::set has consistent order (lexicographic for strings)
|
||||||
for (size_t i = 0; i < vec.size(); i++) {
|
size_t i = 0;
|
||||||
if (vec[i] == custom_preset) {
|
for (const auto &preset : supported) {
|
||||||
|
if (preset == custom_preset) {
|
||||||
state.custom_preset = i;
|
state.custom_preset = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (traits.get_supports_swing_modes()) {
|
if (traits.get_supports_swing_modes()) {
|
||||||
@@ -549,22 +553,34 @@ void ClimateDeviceRestoreState::apply(Climate *climate) {
|
|||||||
climate->fan_mode = this->fan_mode;
|
climate->fan_mode = this->fan_mode;
|
||||||
}
|
}
|
||||||
if (!traits.get_supported_custom_fan_modes().empty() && this->uses_custom_fan_mode) {
|
if (!traits.get_supported_custom_fan_modes().empty() && this->uses_custom_fan_mode) {
|
||||||
// std::set has consistent order (lexicographic for strings), so this is ok
|
// std::set has consistent order (lexicographic for strings)
|
||||||
const auto &modes = traits.get_supported_custom_fan_modes();
|
const auto &modes = traits.get_supported_custom_fan_modes();
|
||||||
std::vector<std::string> modes_vec{modes.begin(), modes.end()};
|
if (custom_fan_mode < modes.size()) {
|
||||||
if (custom_fan_mode < modes_vec.size()) {
|
size_t i = 0;
|
||||||
climate->custom_fan_mode = modes_vec[this->custom_fan_mode];
|
for (const auto &mode : modes) {
|
||||||
|
if (i == this->custom_fan_mode) {
|
||||||
|
climate->custom_fan_mode = mode;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (traits.get_supports_presets() && !this->uses_custom_preset) {
|
if (traits.get_supports_presets() && !this->uses_custom_preset) {
|
||||||
climate->preset = this->preset;
|
climate->preset = this->preset;
|
||||||
}
|
}
|
||||||
if (!traits.get_supported_custom_presets().empty() && uses_custom_preset) {
|
if (!traits.get_supported_custom_presets().empty() && uses_custom_preset) {
|
||||||
// std::set has consistent order (lexicographic for strings), so this is ok
|
// std::set has consistent order (lexicographic for strings)
|
||||||
const auto &presets = traits.get_supported_custom_presets();
|
const auto &presets = traits.get_supported_custom_presets();
|
||||||
std::vector<std::string> presets_vec{presets.begin(), presets.end()};
|
if (custom_preset < presets.size()) {
|
||||||
if (custom_preset < presets_vec.size()) {
|
size_t i = 0;
|
||||||
climate->custom_preset = presets_vec[this->custom_preset];
|
for (const auto &preset : presets) {
|
||||||
|
if (i == this->custom_preset) {
|
||||||
|
climate->custom_preset = preset;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (traits.get_supports_swing_modes()) {
|
if (traits.get_supports_swing_modes()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user