From 535c3eb2a221a62e68612cc7fa4322866e6f5fb9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Jan 2026 11:32:02 -1000 Subject: [PATCH] [sprinkler] Fix scheduler deprecation warnings and heap churn with FixedVector (#13251) --- esphome/components/sprinkler/sprinkler.cpp | 6 ++++-- esphome/components/sprinkler/sprinkler.h | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/esphome/components/sprinkler/sprinkler.cpp b/esphome/components/sprinkler/sprinkler.cpp index ca9f85abd8..2813b4450b 100644 --- a/esphome/components/sprinkler/sprinkler.cpp +++ b/esphome/components/sprinkler/sprinkler.cpp @@ -332,6 +332,7 @@ Sprinkler::Sprinkler(const std::string &name) { // The `name` is needed to set timers up, hence non-default constructor // replaces `set_name()` method previously existed this->name_ = name; + this->timer_.init(2); this->timer_.push_back({this->name_ + "sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)}); this->timer_.push_back({this->name_ + "vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)}); } @@ -1574,7 +1575,8 @@ const LogString *Sprinkler::state_as_str_(SprinklerState state) { void Sprinkler::start_timer_(const SprinklerTimerIndex timer_index) { if (this->timer_duration_(timer_index) > 0) { - this->set_timeout(this->timer_[timer_index].name, this->timer_duration_(timer_index), + // FixedVector ensures timer_ can't be resized, so .c_str() pointers remain valid + this->set_timeout(this->timer_[timer_index].name.c_str(), this->timer_duration_(timer_index), this->timer_cbf_(timer_index)); this->timer_[timer_index].start_time = millis(); this->timer_[timer_index].active = true; @@ -1585,7 +1587,7 @@ void Sprinkler::start_timer_(const SprinklerTimerIndex timer_index) { bool Sprinkler::cancel_timer_(const SprinklerTimerIndex timer_index) { this->timer_[timer_index].active = false; - return this->cancel_timeout(this->timer_[timer_index].name); + return this->cancel_timeout(this->timer_[timer_index].name.c_str()); } bool Sprinkler::timer_active_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].active; } diff --git a/esphome/components/sprinkler/sprinkler.h b/esphome/components/sprinkler/sprinkler.h index 25e2d42446..273c0e9208 100644 --- a/esphome/components/sprinkler/sprinkler.h +++ b/esphome/components/sprinkler/sprinkler.h @@ -3,6 +3,7 @@ #include "esphome/core/automation.h" #include "esphome/core/component.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/components/number/number.h" #include "esphome/components/switch/switch.h" @@ -553,8 +554,8 @@ class Sprinkler : public Component { /// Sprinkler valve operator objects std::vector valve_op_{2}; - /// Valve control timers - std::vector timer_{}; + /// Valve control timers - FixedVector enforces that this can never grow beyond init() size + FixedVector timer_; /// Other Sprinkler instances we should be aware of (used to check if pumps are in use) std::vector other_controllers_;