From 84694a6122cf40e3097a12d3dae0ae178b7bd7ae Mon Sep 17 00:00:00 2001 From: Oliver Kleinecke Date: Wed, 19 Feb 2025 10:29:14 +0100 Subject: [PATCH] fix: rename split_to_int_array_ to split_to_int_vector_ and refactor lamp list handling for improved clarity --- .../components/dynamic_lamp/dynamic_lamp.cpp | 59 ++----------------- .../components/dynamic_lamp/dynamic_lamp.h | 21 +------ 2 files changed, 7 insertions(+), 73 deletions(-) diff --git a/esphome/components/dynamic_lamp/dynamic_lamp.cpp b/esphome/components/dynamic_lamp/dynamic_lamp.cpp index e6ff0a1d72..6d1945a203 100644 --- a/esphome/components/dynamic_lamp/dynamic_lamp.cpp +++ b/esphome/components/dynamic_lamp/dynamic_lamp.cpp @@ -269,7 +269,7 @@ bool DynamicLampComponent::add_timer(std::string lamp_list_str, bool timer_activ LampList DynamicLampComponent::build_lamp_list_from_list_str_(std::string lamp_list_str) { std::string delimiter = ","; - std::vector lamp_list_vector = this->split_to_int_array_(lamp_list_str, delimiter); + std::vector lamp_list_vector = this->split_to_int_vector_(lamp_list_str, delimiter); LampList lamp_list; memset(&lamp_list, 0, sizeof(lamp_list)); if (lamp_list_vector.size() > 16) { @@ -283,56 +283,7 @@ LampList DynamicLampComponent::build_lamp_list_from_list_str_(std::string lamp_l this->status_set_warning(); return lamp_list; } - switch (lamp_list_vector[i]) { - case 0: - lamp_list.lamp_0 = true; - break; - case 1: - lamp_list.lamp_1 = true; - break; - case 2: - lamp_list.lamp_2 = true; - break; - case 3: - lamp_list.lamp_3 = true; - break; - case 4: - lamp_list.lamp_4 = true; - break; - case 5: - lamp_list.lamp_5 = true; - break; - case 6: - lamp_list.lamp_6 = true; - break; - case 7: - lamp_list.lamp_7 = true; - break; - case 8: - lamp_list.lamp_8 = true; - break; - case 9: - lamp_list.lamp_9 = true; - break; - case 10: - lamp_list.lamp_10 = true; - break; - case 11: - lamp_list.lamp_11 = true; - break; - case 12: - lamp_list.lamp_12 = true; - break; - case 13: - lamp_list.lamp_13 = true; - break; - case 14: - lamp_list.lamp_14 = true; - break; - case 15: - lamp_list.lamp_15 = true; - break; - } + lamp_list[lamp_list_vector[i]] = true; } return lamp_list; } @@ -347,8 +298,8 @@ void DynamicLampComponent::read_timers_to_log() { for (uint8_t j = 0; j < 16; j++) { if (lamp_list[j]) { ESP_LOGV(TAG, "Timer found for lamp %s: %d, action: %d, hour: %d, minute: %d, monday: %d, tuesday: %d, wednesday: %d, thursday: %d, friday: %d, saturday: %d, sunday: %d", - this->active_lamps_[j].name, timer.active, timer.action, timer.hour, timer.minute, timer.monday, timer.tuesday, - timer.wednesday, timer.thursday, timer.friday, timer.saturday, timer.sunday); + this->active_lamps_[j].name, timer.active, timer.action, timer.hour, timer.minute, timer.monday, timer.tuesday, + timer.wednesday, timer.thursday, timer.friday, timer.saturday, timer.sunday); } } } @@ -376,7 +327,7 @@ void DynamicLampComponent::restore_lamp_values_(uint8_t lamp_number) { this->active_lamps_[lamp_number].active = false; } -std::vector DynamicLampComponent::split_to_int_array_(const std::string& s, const std::string& delimiter) { +std::vector DynamicLampComponent::split_to_int_vector_(const std::string& s, const std::string& delimiter) { std::vector tokens; size_t pos = 0; std::string token; diff --git a/esphome/components/dynamic_lamp/dynamic_lamp.h b/esphome/components/dynamic_lamp/dynamic_lamp.h index 59887b6c13..99021373df 100644 --- a/esphome/components/dynamic_lamp/dynamic_lamp.h +++ b/esphome/components/dynamic_lamp/dynamic_lamp.h @@ -68,24 +68,7 @@ struct CombinedLamp { bool used_outputs[16]; }; -struct LampList { - bool lamp_0 : 1; - bool lamp_1 : 1; - bool lamp_2 : 1; - bool lamp_3 : 1; - bool lamp_4 : 1; - bool lamp_5 : 1; - bool lamp_6 : 1; - bool lamp_7 : 1; - bool lamp_8 : 1; - bool lamp_9 : 1; - bool lamp_10 : 1; - bool lamp_11 : 1; - bool lamp_12 : 1; - bool lamp_13 : 1; - bool lamp_14 : 1; - bool lamp_15 : 1; -}; +std::vector LampList {}; struct DynamicLampTimer { unsigned char validation[3] = { 'D', 'L', 'T' }; @@ -137,7 +120,7 @@ class DynamicLampComponent : public Component { bool write_state_(uint8_t lamp_number, float state); uint8_t get_lamp_index_by_name_(std::string lamp_name); std::array get_lamp_outputs_by_name_(std::string lamp_name); - std::vector split_to_int_array_(const std::string& s, const std::string& delimiter); + std::vector split_to_int_vector_(const std::string& s, const std::string& delimiter); LampList build_lamp_list_from_list_str_(std::string lamp_list_str); CombinedLamp active_lamps_[16];