Skip to content

Commit c1f5989

Browse files
committed
chore(sec_touch): use c++ code to support ESPHOME 2025.11.0
1 parent 5d46a86 commit c1f5989

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

components/sec_touch/fan/_fan_mode.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "_fan_mode.h"
33
#include "vector"
44

5-
// Define the global static FanModeMap that is shared across all fans
6-
static const std::unordered_map<FanModeEnum::FanMode, FanModeEnum::FanModeData> FanModeMap = {
5+
// Define the global static FanModeList that preserves the intended order
6+
static const std::vector<std::pair<FanModeEnum::FanMode, FanModeEnum::FanModeData>> FanModeList = {
77
{FanModeEnum::FanMode::NORMAL, {"Normal", 0, 6}},
88
{FanModeEnum::FanMode::BURST, {"Burst", 7, esphome::nullopt}},
99
{FanModeEnum::FanMode::AUTOMATIC_HUMIDITY, {"Automatic Humidity", 8, esphome::nullopt}},
@@ -12,39 +12,42 @@ static const std::unordered_map<FanModeEnum::FanMode, FanModeEnum::FanModeData>
1212
{FanModeEnum::FanMode::SLEEP, {"Sleep", 11, esphome::nullopt}},
1313
};
1414

15-
// Static function to get a reference to the FanModeMap
16-
const std::unordered_map<FanModeEnum::FanMode, FanModeEnum::FanModeData> &FanModeEnum::getFanModeMap() {
17-
return FanModeMap;
15+
// Static function to get a reference to the FanModeList
16+
const std::vector<std::pair<FanModeEnum::FanMode, FanModeEnum::FanModeData>> &FanModeEnum::getFanModeList() {
17+
return FanModeList;
1818
}
1919

2020
// Convert FanMode to string
2121
std::string_view FanModeEnum::to_string(FanMode mode) {
22-
auto it = getFanModeMap().find(mode);
23-
return (it != getFanModeMap().end()) ? it->second.str : "Unknown-Error-Mode";
22+
for (const auto &pair : getFanModeList()) {
23+
if (pair.first == mode)
24+
return pair.second.str;
25+
}
26+
return "Unknown-Error-Mode";
2427
}
2528

2629
// Convert string to FanMode
2730
esphome::optional<FanModeEnum::FanMode> FanModeEnum::from_string(std::string_view str) {
28-
for (const auto &pair : getFanModeMap()) {
31+
for (const auto &pair : getFanModeList()) {
2932
if (pair.second.str == str) {
3033
return pair.first; // Return the FanMode (key)
3134
}
3235
}
3336
return esphome::nullopt;
3437
}
3538

36-
// Get a list of string values dynamically from the map
39+
// Get a list of string values dynamically from the list
3740
std::vector<std::string_view> FanModeEnum::getStringValues() {
3841
std::vector<std::string_view> values;
39-
for (const auto &pair : getFanModeMap()) {
42+
for (const auto &pair : getFanModeList()) {
4043
values.push_back(pair.second.str);
4144
}
4245
return values;
4346
}
4447

4548
FanModeEnum::FanMode FanModeEnum::get_fan_mode_fromSpeed(int speed) {
46-
// Iterate through FanModeMap to find the correct FanMode based on speed
47-
for (const auto &pair : getFanModeMap()) {
49+
// Iterate through FanModeList to find the correct FanMode based on speed
50+
for (const auto &pair : getFanModeList()) {
4851
const FanModeData &data = pair.second;
4952

5053
// Check if speed is within the range
@@ -53,8 +56,14 @@ FanModeEnum::FanMode FanModeEnum::get_fan_mode_fromSpeed(int speed) {
5356
}
5457
}
5558

56-
// Return an invalid mode if no matching FanMode is found
59+
// Return default mode if no matching FanMode is found
5760
return FanMode::NORMAL; // Default or error mode
5861
}
5962

60-
int FanModeEnum::get_start_speed(FanMode mode) { return getFanModeMap().at(mode).start_speed; }
63+
int FanModeEnum::get_start_speed(FanMode mode) {
64+
for (const auto &pair : getFanModeList()) {
65+
if (pair.first == mode)
66+
return pair.second.start_speed;
67+
}
68+
return 0;
69+
}

components/sec_touch/fan/_fan_mode.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22

3-
#include <unordered_map>
3+
#include <vector>
44
#include <string_view>
5+
#include <utility>
56
#include "esphome/core/optional.h"
6-
#include "vector"
77

88
class FanModeEnum {
99
public:
@@ -16,16 +16,16 @@ class FanModeEnum {
1616
esphome::optional<int> end_speed;
1717
};
1818

19-
// Static function to get the FanModeMap
20-
static const std::unordered_map<FanMode, FanModeData> &getFanModeMap();
19+
// Static function to get the FanMode list (preserves order)
20+
static const std::vector<std::pair<FanMode, FanModeData>> &getFanModeList();
2121

2222
// Convert FanMode to string
2323
static std::string_view to_string(FanMode mode);
2424

2525
// Convert string to FanMode
2626
static esphome::optional<FanMode> from_string(std::string_view str);
2727

28-
// Get a list of string values dynamically from the map
28+
// Get a list of string values dynamically from the list
2929
static std::vector<std::string_view> getStringValues();
3030

3131
static FanMode get_fan_mode_fromSpeed(int speed);

components/sec_touch/fan/sec_touch_fan.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ SecTouchFan::SecTouchFan(SECTouchComponent *parent, int level_id, int label_id)
1818
std::string_view mode_from_hardware_str = FanModeEnum::to_string(mode_from_hardware);
1919

2020
bool needs_preset_publish = false;
21-
if (this->preset_mode != mode_from_hardware_str) {
21+
const char *current_preset = this->get_preset_mode();
22+
if (current_preset == nullptr || std::string_view(current_preset) != mode_from_hardware_str) {
2223
ESP_LOGD(TAG, "Preset mode changed to %s", mode_from_hardware_str.data());
23-
this->preset_mode = std::string(mode_from_hardware_str);
24+
// set_preset_mode_ will validate and store a pointer into traits
25+
this->set_preset_mode_(std::string(mode_from_hardware_str));
2426
needs_preset_publish = true;
2527
}
2628

2729
bool need_speed_publish = this->assign_new_speed_if_needed(real_speed_from_device);
2830

2931
if (!need_speed_publish && !needs_preset_publish) {
32+
const char *log_p = this->get_preset_mode() ? this->get_preset_mode() : "Unknown";
3033
ESP_LOGD(TAG, "No update needed for fan with property_id %d (state %d) (speed %d)(preset %s)", property_id,
31-
this->state, this->speed, this->preset_mode.c_str());
34+
this->state, this->speed, log_p);
3235
return;
3336
}
3437

@@ -107,11 +110,15 @@ void SecTouchFan::control(const fan::FanCall &call) {
107110
ESP_LOGD(TAG, "Control called");
108111

109112
bool new_preset_found = false;
110-
if (!call.get_preset_mode().empty()) {
111-
if (call.get_preset_mode() != this->preset_mode) {
112-
this->preset_mode = call.get_preset_mode();
113+
if (call.has_preset_mode()) {
114+
const char *pm = call.get_preset_mode();
115+
const char *current = this->get_preset_mode();
116+
if (current == nullptr || strcmp(pm, current) != 0) {
117+
// Store the preset mode (validates and points into traits)
118+
this->set_preset_mode_(pm);
113119
new_preset_found = true;
114-
ESP_LOGI("SecTouchFan", "NEW Fan preset mode: %s", this->preset_mode.c_str());
120+
const char *logged = this->get_preset_mode() ? this->get_preset_mode() : pm;
121+
ESP_LOGI("SecTouchFan", "NEW Fan preset mode: %s", logged);
115122
}
116123
}
117124

@@ -157,16 +164,17 @@ void SecTouchFan::control(const fan::FanCall &call) {
157164

158165
// ON
159166
if (new_preset_found) {
160-
FanModeEnum::FanMode calculated_mode =
161-
FanModeEnum::from_string(this->preset_mode).value_or(FanModeEnum::FanMode::NORMAL);
167+
const char *current = this->get_preset_mode();
168+
std::string_view cur_sv = current ? std::string_view(current) : std::string_view("");
169+
FanModeEnum::FanMode calculated_mode = FanModeEnum::from_string(cur_sv).value_or(FanModeEnum::FanMode::NORMAL);
162170
if (calculated_mode == FanModeEnum::FanMode::NORMAL) {
163171
this->speed = 1;
164172
} else {
165173
this->speed = FanModeEnum::get_start_speed(calculated_mode);
166174
}
167175
}
168-
169-
ESP_LOGI(TAG, "[Update for %d] - [%s] speed: %d", this->level_id, this->preset_mode.c_str(), this->speed);
176+
const char *log_preset = this->get_preset_mode() ? this->get_preset_mode() : "Unknown";
177+
ESP_LOGI(TAG, "[Update for %d] - [%s] speed: %d", this->level_id, log_preset, this->speed);
170178
this->parent->add_set_task(
171179
SetDataTask::create(TaskTargetType::LEVEL, this->level_id, std::to_string(this->speed).c_str()));
172180

@@ -225,12 +233,12 @@ void SecTouchFan::update_label_mode() {
225233
return;
226234
}
227235

228-
if (this->preset_mode.empty()) {
236+
if (!this->has_preset_mode()) {
229237
level_text_sensor->publish_state("Unknown");
230238
return;
231239
}
232240

233-
level_text_sensor->publish_state(this->preset_mode.c_str());
241+
level_text_sensor->publish_state(this->get_preset_mode());
234242
}
235243

236244
// Print method for debugging

components/sec_touch/fan/sec_touch_fan.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "esphome/components/text_sensor/text_sensor.h"
66
#include "../sec_touch.h"
77
#include "_fan_mode.h"
8+
#include <vector>
89

910
namespace esphome {
1011
namespace sec_touch {
@@ -31,9 +32,9 @@ class SecTouchFan : public Component, public fan::Fan {
3132

3233
void setup() override {
3334
this->traits_ = fan::FanTraits(false, true, false, 11);
34-
std::set<std::string> preset_modes;
35+
std::vector<const char *> preset_modes;
3536
for (auto str_view : FanModeEnum::getStringValues()) {
36-
preset_modes.insert(std::string(str_view));
37+
preset_modes.push_back(str_view.data());
3738
}
3839

3940
this->traits_.set_supported_preset_modes(preset_modes);

0 commit comments

Comments
 (0)