[sprinkler] Key timers by index and keep them in a fixed array (#20379)

This commit is contained in:
J. Nick Koston
2026-10-09 07:10:48 -10:00
committed by GitHub
parent 824834f847
commit 541562451e
2 changed files with 10 additions and 12 deletions
+7 -9
View File
@@ -322,13 +322,11 @@ optional<size_t> SprinklerValveRunRequest::valve_as_opt() {
SprinklerValveOperator *SprinklerValveRunRequest::valve_operator() { return this->valve_op_; }
Sprinkler::Sprinkler() : Sprinkler("") {}
Sprinkler::Sprinkler(const char *name) : name_(name) {
// The `name` is stored for dump_config logging
this->timer_.init(2);
// Timer names only need to be unique within this component instance
this->timer_.push_back({"sm", false, 0, 0, [this]() { this->sm_timer_callback_(); }});
this->timer_.push_back({"vs", false, 0, 0, [this]() { this->valve_selection_callback_(); }});
}
// The `name` is stored for dump_config logging; the scheduler keys each timer by its SprinklerTimerIndex
Sprinkler::Sprinkler(const char *name)
: name_(name),
timer_{{{false, 0, 0, [this]() { this->sm_timer_callback_(); }},
{false, 0, 0, [this]() { this->valve_selection_callback_(); }}}} {}
void Sprinkler::setup() {
this->all_valves_off_(true);
@@ -1510,7 +1508,7 @@ 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),
this->set_timeout(static_cast<uint32_t>(timer_index), this->timer_duration_(timer_index),
this->timer_cbf_(timer_index));
this->timer_[timer_index].start_time = millis();
this->timer_[timer_index].active = true;
@@ -1521,7 +1519,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(static_cast<uint32_t>(timer_index));
}
bool Sprinkler::timer_active_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].active; }
+3 -3
View File
@@ -7,6 +7,7 @@
#include "esphome/components/number/number.h"
#include "esphome/components/switch/switch.h"
#include <array>
#include <vector>
namespace esphome::sprinkler {
@@ -49,7 +50,6 @@ struct SprinklerQueueItem {
};
struct SprinklerTimer {
const char *name;
bool active;
uint32_t time;
uint32_t start_time;
@@ -564,8 +564,8 @@ class Sprinkler final : public Component {
/// Sprinkler valve operator objects
std::vector<SprinklerValveOperator> valve_op_{2};
/// Valve control timers - FixedVector enforces that this can never grow beyond init() size
FixedVector<SprinklerTimer> timer_;
/// Valve control timers, indexed by SprinklerTimerIndex (also used as the scheduler id)
std::array<SprinklerTimer, 2> timer_;
/// Other Sprinkler instances we should be aware of (used to check if pumps are in use)
std::vector<Sprinkler *> other_controllers_;