From f1221602f4277665e37c6516fd98842578053fca Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:26:24 -1000 Subject: [PATCH 1/4] [binary_sensor] Move deleted copy/move to public (clang-tidy modernize-use-equals-delete) --- esphome/components/binary_sensor/automation.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index 4154cfd31e..bab6e8577e 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -105,10 +105,10 @@ class MultiClickTriggerBase : public Trigger<>, public Component { void set_invalid_cooldown(uint32_t invalid_cooldown) { this->invalid_cooldown_ = invalid_cooldown; } void cancel(); - - protected: MultiClickTriggerBase(const MultiClickTriggerBase &) = delete; MultiClickTriggerBase &operator=(const MultiClickTriggerBase &) = delete; + + protected: void on_state_(bool state); void schedule_cooldown_(); void schedule_is_valid_(uint32_t min_length); From 869bafe33839693863f1485c75357a60900ed760 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:48:07 -1000 Subject: [PATCH 2/4] [binary_sensor] Replace truncation guard with ESPHOME_DEBUG_ASSERT --- esphome/components/binary_sensor/automation.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index bab6e8577e..c52a27fa73 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -133,10 +133,9 @@ template class MultiClickTrigger : public MultiClickTriggerBase { : MultiClickTriggerBase(parent) { size_t i = 0; for (const auto &t : timing) { - if (i >= N) - break; this->timing_storage_[i++] = t; } + ESPHOME_DEBUG_ASSERT(i == N); this->timing_ = this->timing_storage_.data(); this->timing_count_ = N; } From 3e7dcc649036d79cf2edd9e34a050d77691a2e48 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 15:54:44 -1000 Subject: [PATCH 3/4] [binary_sensor] Assert size before writing, not after --- esphome/components/binary_sensor/automation.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index c52a27fa73..aa3d5d7fa4 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -131,11 +131,11 @@ template class MultiClickTrigger : public MultiClickTriggerBase { public: MultiClickTrigger(BinarySensor *parent, std::initializer_list timing) : MultiClickTriggerBase(parent) { + ESPHOME_DEBUG_ASSERT(timing.size() == N); size_t i = 0; for (const auto &t : timing) { this->timing_storage_[i++] = t; } - ESPHOME_DEBUG_ASSERT(i == N); this->timing_ = this->timing_storage_.data(); this->timing_count_ = N; } From a20b0a96b90f21f32b4a9325abc1ea6ed41312e6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 27 Mar 2026 16:14:55 -1000 Subject: [PATCH 4/4] Use init_array_from helper for optimal codegen Uses memcpy for trivially copyable types, element-wise copy otherwise. ESPHOME_DEBUG_ASSERT catches size mismatches in integration tests. --- esphome/components/binary_sensor/automation.h | 6 +----- esphome/core/helpers.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/esphome/components/binary_sensor/automation.h b/esphome/components/binary_sensor/automation.h index aa3d5d7fa4..1875910aff 100644 --- a/esphome/components/binary_sensor/automation.h +++ b/esphome/components/binary_sensor/automation.h @@ -131,11 +131,7 @@ template class MultiClickTrigger : public MultiClickTriggerBase { public: MultiClickTrigger(BinarySensor *parent, std::initializer_list timing) : MultiClickTriggerBase(parent) { - ESPHOME_DEBUG_ASSERT(timing.size() == N); - size_t i = 0; - for (const auto &t : timing) { - this->timing_storage_[i++] = t; - } + init_array_from(this->timing_storage_, timing); this->timing_ = this->timing_storage_.data(); this->timing_count_ = N; } diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 82c6b3833c..51feaa57c5 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -497,6 +497,21 @@ template::max()> index_type capacity_{0}; }; +/// Initialize a std::array from an initializer_list. Uses memcpy for trivially copyable types (optimal codegen), +/// falls back to element-wise copy for non-trivially copyable types (e.g. TemplatableValue). +/// N is set by code generation; ESPHOME_DEBUG_ASSERT catches mismatches in debug/integration tests. +template inline void init_array_from(std::array &dest, std::initializer_list src) { + ESPHOME_DEBUG_ASSERT(src.size() == N); + if constexpr (std::is_trivially_copyable_v) { + __builtin_memcpy(dest.data(), src.begin(), N * sizeof(T)); + } else { + size_t i = 0; + for (const auto &v : src) { + dest[i++] = v; + } + } +} + /// Fixed-capacity vector - allocates once at runtime, never reallocates /// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append) /// when size is known at initialization but not at compile time