@@ -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
0 commit comments