diff --git a/esphome/core/component_iterator.cpp b/esphome/core/component_iterator.cpp index e012412ab6..583f9c39af 100644 --- a/esphome/core/component_iterator.cpp +++ b/esphome/core/component_iterator.cpp @@ -20,18 +20,14 @@ void ComponentIterator::begin(bool include_internal) { template void ComponentIterator::process_platform_item_(const Container &items, bool (ComponentIterator::*on_item)(typename Container::value_type)) { - // Since static_vector doesn't have size(), we need to iterate differently - size_t index = 0; - for (auto *item : items) { - if (index++ == this->at_) { - if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { - this->at_++; - } - return; + if (this->at_ >= items.size()) { + this->advance_platform_(); + } else { + auto *item = items[this->at_]; + if ((item->is_internal() && !this->include_internal_) || (this->*on_item)(item)) { + this->at_++; } } - // If we get here, we've reached the end - this->advance_platform_(); } void ComponentIterator::advance_platform_() { diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index e937c3bdf2..04cac5072f 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -113,6 +113,8 @@ template class StaticVector { } } + size_t size() const { return count_; } + // For range-based for loops iterator begin() { return data_.begin(); } iterator end() { return data_.begin() + count_; }