From 774cdd33bc35c5e787d8971ebbd8bed09c3e9a7a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Oct 2025 15:27:44 -0500 Subject: [PATCH] cleaner --- esphome/components/select/select_call.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/esphome/components/select/select_call.cpp b/esphome/components/select/select_call.cpp index 72cbbdc0ef..e1cabbd3d4 100644 --- a/esphome/components/select/select_call.cpp +++ b/esphome/components/select/select_call.cpp @@ -46,7 +46,12 @@ SelectCall &SelectCall::with_option(const char *option) { } SelectCall &SelectCall::with_index(size_t index) { - this->index_ = index; + if (index >= this->parent_->size()) { + ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", this->parent_->get_name().c_str(), index); + this->index_ = {}; // Store nullopt for invalid index + } else { + this->index_ = index; + } return *this; } @@ -71,12 +76,7 @@ optional SelectCall::calculate_target_index_(const char *name) { ESP_LOGW(TAG, "'%s' - No option set", name); return {}; } - auto idx = this->index_.value(); - if (idx >= options.size()) { - ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", name, idx); - return {}; - } - return idx; + return this->index_.value(); } // SELECT_OP_NEXT or SELECT_OP_PREVIOUS @@ -114,6 +114,7 @@ void SelectCall::perform() { return; } + // Calculate target index (with_index() and with_option() already validate bounds/existence) auto target_index = this->calculate_target_index_(name); if (!target_index.has_value()) { return;