ESPHome 2026.1.4
Loading...
Searching...
No Matches
sprinkler.cpp
Go to the documentation of this file.
1#include "automation.h"
2#include "sprinkler.h"
3
6#include "esphome/core/log.h"
7#include <cinttypes>
8#include <utility>
9
10namespace esphome::sprinkler {
11
12static const char *const TAG = "sprinkler";
13
15 float value;
16 if (!this->restore_value_) {
17 value = this->initial_value_;
18 } else {
20 if (!this->pref_.load(&value)) {
21 if (!std::isnan(this->initial_value_)) {
22 value = this->initial_value_;
23 } else {
24 value = this->traits.get_min_value();
25 }
26 }
27 }
28 this->publish_state(value);
29}
30
32 this->set_trigger_->trigger(value);
33
34 this->publish_state(value);
35
36 if (this->restore_value_)
37 this->pref_.save(&value);
38}
39
40void SprinklerControllerNumber::dump_config() { LOG_NUMBER("", "Sprinkler Controller Number", this); }
41
43 : turn_on_trigger_(new Trigger<>()), turn_off_trigger_(new Trigger<>()) {}
44
46 if (!this->f_.has_value())
47 return;
48 auto s = (*this->f_)();
49 if (!s.has_value())
50 return;
51
52 this->publish_state(*s);
53}
54
56 if (this->prev_trigger_ != nullptr) {
58 }
59
60 if (state) {
61 this->prev_trigger_ = this->turn_on_trigger_;
63 } else {
64 this->prev_trigger_ = this->turn_off_trigger_;
66 }
67
68 this->publish_state(state);
69}
70
71void SprinklerControllerSwitch::set_state_lambda(std::function<optional<bool>()> &&f) { this->f_ = f; }
73
76
78
79void SprinklerControllerSwitch::dump_config() { LOG_SWITCH("", "Sprinkler Switch", this); }
80
83 : controller_(controller), valve_(valve) {}
84
86 uint32_t now = App.get_loop_component_start_time();
87 if (now >= this->start_millis_) { // dummy check
88 switch (this->state_) {
89 case STARTING:
90 if (now > (this->start_millis_ + this->start_delay_)) {
91 this->run_(); // start_delay_ has been exceeded, so ensure both valves are on and update the state
92 }
93 break;
94
95 case ACTIVE:
96 if (now > (this->start_millis_ + this->start_delay_ + this->run_duration_)) {
97 this->stop(); // start_delay_ + run_duration_ has been exceeded, start shutting down
98 }
99 break;
100
101 case STOPPING:
102 if (now > (this->stop_millis_ + this->stop_delay_)) {
103 this->kill_(); // stop_delay_has been exceeded, ensure all valves are off
104 }
105 break;
106
107 default:
108 break;
109 }
110 } else { // perhaps millis() rolled over...or something else is horribly wrong!
111 this->stop(); // bail out (TODO: handle this highly unlikely situation better...)
112 }
113}
114
116 if (controller != nullptr) {
117 this->controller_ = controller;
118 }
119}
120
122 if (valve != nullptr) {
123 if (this->state_ != IDLE) { // Only kill if not already idle
124 this->kill_(); // ensure everything is off before we let go!
125 }
126 this->state_ = IDLE; // reset state
127 this->run_duration_ = 0; // reset to ensure the valve isn't started without updating it
128 this->start_millis_ = 0; // reset because (new) valve has not been started yet
129 this->stop_millis_ = 0; // reset because (new) valve has not been started yet
130 this->valve_ = valve; // finally, set the pointer to the new valve
131 }
132}
133
134void SprinklerValveOperator::set_run_duration(uint32_t run_duration) {
135 if (run_duration) {
136 this->run_duration_ = run_duration * 1000;
137 }
138}
139
140void SprinklerValveOperator::set_start_delay(uint32_t start_delay, bool start_delay_is_valve_delay) {
141 this->start_delay_is_valve_delay_ = start_delay_is_valve_delay;
142 this->start_delay_ = start_delay * 1000; // because 1000 milliseconds is one second
143}
144
145void SprinklerValveOperator::set_stop_delay(uint32_t stop_delay, bool stop_delay_is_valve_delay) {
146 this->stop_delay_is_valve_delay_ = stop_delay_is_valve_delay;
147 this->stop_delay_ = stop_delay * 1000; // because 1000 milliseconds is one second
148}
149
151 if (!this->run_duration_) { // can't start if zero run duration
152 return;
153 }
154 if (this->start_delay_ && (this->pump_switch() != nullptr)) {
155 this->state_ = STARTING; // STARTING state requires both a pump and a start_delay_
156 if (this->start_delay_is_valve_delay_) {
157 this->pump_on_();
158 } else if (!this->pump_switch()->state) { // if the pump is already on, wait to switch on the valve
159 this->valve_on_(); // to ensure consistent run time
160 }
161 } else {
162 this->run_(); // there is no start_delay_, so just start the pump and valve
163 }
164 this->stop_millis_ = 0;
165 this->start_millis_ = millis(); // save the time the start request was made
166}
167
169 if ((this->state_ == IDLE) || (this->state_ == STOPPING)) { // can't stop if already stopped or stopping
170 return;
171 }
172 if (this->stop_delay_ && (this->pump_switch() != nullptr)) {
173 this->state_ = STOPPING; // STOPPING state requires both a pump and a stop_delay_
174 if (this->stop_delay_is_valve_delay_) {
175 this->pump_off_();
176 } else {
177 this->valve_off_();
178 }
179 if (this->pump_switch()->state) { // if the pump is still on at this point, it may be in use...
180 this->valve_off_(); // ...so just switch the valve off now to ensure consistent run time
181 }
182 } else {
183 this->kill_(); // there is no stop_delay_, so just stop the pump and valve
184 }
185 this->stop_millis_ = millis(); // save the time the stop request was made
186}
187
188uint32_t SprinklerValveOperator::run_duration() { return this->run_duration_ / 1000; }
189
191 if (this->start_millis_ == 0) {
192 return this->run_duration(); // hasn't been started yet
193 }
194
195 if (this->stop_millis_) {
196 if (this->stop_millis_ - this->start_millis_ >= this->start_delay_ + this->run_duration_) {
197 return 0; // valve was active for more than its configured duration, so we are done
198 } else {
199 // we're stopped; return time remaining
200 return (this->run_duration_ - (this->stop_millis_ - this->start_millis_)) / 1000;
201 }
202 }
203
204 auto completed_millis = this->start_millis_ + this->start_delay_ + this->run_duration_;
205 if (completed_millis > millis()) {
206 return (completed_millis - millis()) / 1000; // running now
207 }
208 return 0; // run completed
209}
210
212
214 if ((this->controller_ == nullptr) || (this->valve_ == nullptr)) {
215 return nullptr;
216 }
217 if (this->valve_->pump_switch_index.has_value()) {
219 }
220 return nullptr;
221}
222
224 auto *pump = this->pump_switch();
225 if ((this->valve_ == nullptr) || (pump == nullptr)) { // safety first!
226 return;
227 }
228 if (this->controller_ == nullptr) { // safety first!
229 pump->turn_off(); // if no controller was set, just switch off the pump
230 } else { // ...otherwise, do it "safely"
231 auto state = this->state_; // this is silly, but...
232 this->state_ = BYPASS; // ...exclude me from the pump-in-use check that set_pump_state() does
233 this->controller_->set_pump_state(pump, false);
234 this->state_ = state;
235 }
236}
237
239 auto *pump = this->pump_switch();
240 if ((this->valve_ == nullptr) || (pump == nullptr)) { // safety first!
241 return;
242 }
243 if (this->controller_ == nullptr) { // safety first!
244 pump->turn_on(); // if no controller was set, just switch on the pump
245 } else { // ...otherwise, do it "safely"
246 auto state = this->state_; // this is silly, but...
247 this->state_ = BYPASS; // ...exclude me from the pump-in-use check that set_pump_state() does
248 this->controller_->set_pump_state(pump, true);
249 this->state_ = state;
250 }
251}
252
254 if ((this->valve_ == nullptr) || (this->valve_->valve_switch == nullptr)) { // safety first!
255 return;
256 }
257 if (this->valve_->valve_switch->state) {
258 this->valve_->valve_switch->turn_off();
259 }
260}
261
263 if ((this->valve_ == nullptr) || (this->valve_->valve_switch == nullptr)) { // safety first!
264 return;
265 }
266 if (!this->valve_->valve_switch->state) {
267 this->valve_->valve_switch->turn_on();
268 }
269}
270
272 this->state_ = IDLE;
273 this->valve_off_();
274 this->pump_off_();
275}
276
278 this->state_ = ACTIVE;
279 this->valve_on_();
280 this->pump_on_();
281}
282
284SprinklerValveRunRequest::SprinklerValveRunRequest(size_t valve_number, uint32_t run_duration,
285 SprinklerValveOperator *valve_op)
286 : valve_number_(valve_number), run_duration_(run_duration), valve_op_(valve_op) {}
287
289bool SprinklerValveRunRequest::has_valve_operator() { return !(this->valve_op_ == nullptr); }
290
292
294
295void SprinklerValveRunRequest::set_valve(size_t valve_number) {
296 this->valve_number_ = valve_number;
297 this->run_duration_ = 0;
298 this->valve_op_ = nullptr;
299 this->has_valve_ = true;
300}
301
303 if (valve_op != nullptr) {
304 this->valve_op_ = valve_op;
305 }
306}
307
309 this->has_valve_ = false;
310 this->origin_ = USER;
311 this->run_duration_ = 0;
312 this->valve_op_ = nullptr;
313}
314
316
318
320 if (this->has_valve_) {
321 return this->valve_number_;
322 }
323 return nullopt;
324}
325
327
329
331Sprinkler::Sprinkler(const std::string &name) {
332 // The `name` is needed to set timers up, hence non-default constructor
333 // replaces `set_name()` method previously existed
334 this->name_ = name;
335 this->timer_.init(2);
336 this->timer_.push_back({this->name_ + "sm", false, 0, 0, std::bind(&Sprinkler::sm_timer_callback_, this)});
337 this->timer_.push_back({this->name_ + "vs", false, 0, 0, std::bind(&Sprinkler::valve_selection_callback_, this)});
338}
339
340void Sprinkler::setup() { this->all_valves_off_(true); }
341
343 for (auto &vo : this->valve_op_) {
344 vo.loop();
345 }
346 if (this->prev_req_.has_request() && this->prev_req_.has_valve_operator() &&
347 this->prev_req_.valve_operator()->state() == IDLE) {
348 this->prev_req_.reset();
349 }
350}
351
353 auto new_valve_number = this->number_of_valves();
354 this->valve_.resize(new_valve_number + 1);
355 SprinklerValve *new_valve = &this->valve_[new_valve_number];
356
357 new_valve->controller_switch = valve_sw;
358 new_valve->controller_switch->set_state_lambda([this, new_valve_number]() -> optional<bool> {
359 auto *valve = this->valve_switch(new_valve_number);
360 auto *pump = this->valve_pump_switch(new_valve_number);
361 if (valve == nullptr) {
362 return false;
363 }
364 if (pump != nullptr) {
365 return valve->state && pump->state;
366 }
367 return valve->state;
368 });
369
370 new_valve->valve_turn_off_automation =
371 make_unique<Automation<>>(new_valve->controller_switch->get_turn_off_trigger());
372 new_valve->valve_shutdown_action = make_unique<sprinkler::ShutdownAction<>>(this);
373 new_valve->valve_turn_off_automation->add_actions({new_valve->valve_shutdown_action.get()});
374
375 new_valve->valve_turn_on_automation = make_unique<Automation<>>(new_valve->controller_switch->get_turn_on_trigger());
376 new_valve->valve_resumeorstart_action = make_unique<sprinkler::StartSingleValveAction<>>(this);
377 new_valve->valve_resumeorstart_action->set_valve_to_start(new_valve_number);
378 new_valve->valve_turn_on_automation->add_actions({new_valve->valve_resumeorstart_action.get()});
379
380 if (enable_sw != nullptr) {
381 new_valve->enable_switch = enable_sw;
382 }
383}
384
385void Sprinkler::add_controller(Sprinkler *other_controller) { this->other_controllers_.push_back(other_controller); }
386
388 this->controller_sw_ = controller_switch;
389 controller_switch->set_state_lambda([this]() -> optional<bool> {
390 for (size_t valve_number = 0; valve_number < this->number_of_valves(); valve_number++) {
391 if (this->valve_[valve_number].controller_switch->state) {
392 return true;
393 }
394 }
395 return this->active_req_.has_request();
396 });
397
398 this->sprinkler_turn_off_automation_ = make_unique<Automation<>>(controller_switch->get_turn_off_trigger());
399 this->sprinkler_shutdown_action_ = make_unique<sprinkler::ShutdownAction<>>(this);
401
402 this->sprinkler_turn_on_automation_ = make_unique<Automation<>>(controller_switch->get_turn_on_trigger());
403 this->sprinkler_resumeorstart_action_ = make_unique<sprinkler::ResumeOrStartAction<>>(this);
405}
406
408 this->auto_adv_sw_ = auto_adv_switch;
409}
410
412 this->queue_enable_sw_ = queue_enable_switch;
413}
414
416 this->reverse_sw_ = reverse_switch;
417}
418
420 this->standby_sw_ = standby_switch;
421
422 this->sprinkler_standby_turn_on_automation_ = make_unique<Automation<>>(standby_switch->get_turn_on_trigger());
423 this->sprinkler_standby_shutdown_action_ = make_unique<sprinkler::ShutdownAction<>>(this);
425}
426
428 this->multiplier_number_ = multiplier_number;
429}
430
432 this->repeat_number_ = repeat_number;
433}
434
435void Sprinkler::configure_valve_switch(size_t valve_number, switch_::Switch *valve_switch, uint32_t run_duration) {
436 if (this->is_a_valid_valve(valve_number)) {
437 this->valve_[valve_number].valve_switch = valve_switch;
438 this->valve_[valve_number].run_duration = run_duration;
439 }
440}
441
442void Sprinkler::configure_valve_pump_switch(size_t valve_number, switch_::Switch *pump_switch) {
443 if (this->is_a_valid_valve(valve_number)) {
444 for (size_t i = 0; i < this->pump_.size(); i++) { // check each existing registered pump
445 if (this->pump_[i] == pump_switch) { // if the "new" pump matches one we already have...
446 this->valve_[valve_number].pump_switch_index = i; // ...save its index in the pump vector...
447 return; // ...and we are done
448 }
449 } // if we end up here, no pumps matched, so add a new one
450 this->pump_.push_back(pump_switch);
451 this->valve_[valve_number].pump_switch_index = this->pump_.size() - 1; // save the index to the new pump
452 }
453}
454
456 SprinklerControllerNumber *run_duration_number) {
457 if (this->is_a_valid_valve(valve_number)) {
458 this->valve_[valve_number].run_duration_number = run_duration_number;
459 }
460}
461
463 if (!divider.has_value()) {
464 return;
465 }
466 if (divider.value() > 0) {
467 this->set_multiplier(1.0 / divider.value());
468 this->set_repeat(divider.value() - 1);
469 } else if (divider.value() == 0) {
470 this->set_multiplier(1.0);
471 this->set_repeat(0);
472 }
473}
474
476 if ((!multiplier.has_value()) || (multiplier.value() < 0)) {
477 return;
478 }
479 this->multiplier_ = multiplier.value();
480 if (this->multiplier_number_ == nullptr) {
481 return;
482 }
483 if (this->multiplier_number_->state == multiplier.value()) {
484 return;
485 }
486 auto call = this->multiplier_number_->make_call();
487 call.set_value(multiplier.value());
488 call.perform();
489}
490
492 this->next_prev_ignore_disabled_ = ignore_disabled;
493}
494
495void Sprinkler::set_pump_start_delay(uint32_t start_delay) {
496 this->start_delay_is_valve_delay_ = false;
497 this->start_delay_ = start_delay;
498}
499
500void Sprinkler::set_pump_stop_delay(uint32_t stop_delay) {
501 this->stop_delay_is_valve_delay_ = false;
502 this->stop_delay_ = stop_delay;
503}
504
505void Sprinkler::set_valve_start_delay(uint32_t start_delay) {
506 this->start_delay_is_valve_delay_ = true;
507 this->start_delay_ = start_delay;
508}
509
510void Sprinkler::set_valve_stop_delay(uint32_t stop_delay) {
511 this->stop_delay_is_valve_delay_ = true;
512 this->stop_delay_ = stop_delay;
513}
514
515void Sprinkler::set_pump_switch_off_during_valve_open_delay(bool pump_switch_off_during_valve_open_delay) {
516 this->pump_switch_off_during_valve_open_delay_ = pump_switch_off_during_valve_open_delay;
517}
518
519void Sprinkler::set_valve_open_delay(const uint32_t valve_open_delay) {
520 if (valve_open_delay > 0) {
521 this->valve_overlap_ = false;
522 this->switching_delay_ = valve_open_delay;
523 } else {
524 this->switching_delay_.reset();
525 }
526}
527
528void Sprinkler::set_valve_overlap(uint32_t valve_overlap) {
529 if (valve_overlap > 0) {
530 this->valve_overlap_ = true;
531 this->switching_delay_ = valve_overlap;
532 } else {
533 this->switching_delay_.reset();
534 }
535 this->pump_switch_off_during_valve_open_delay_ = false; // incompatible option
536}
537
538void Sprinkler::set_manual_selection_delay(uint32_t manual_selection_delay) {
539 if (manual_selection_delay > 0) {
540 this->manual_selection_delay_ = manual_selection_delay;
541 } else {
543 }
544}
545
546void Sprinkler::set_valve_run_duration(const optional<size_t> valve_number, const optional<uint32_t> run_duration) {
547 if (!valve_number.has_value() || !run_duration.has_value()) {
548 return;
549 }
550 if (!this->is_a_valid_valve(valve_number.value())) {
551 return;
552 }
553 this->valve_[valve_number.value()].run_duration = run_duration.value();
554 if (this->valve_[valve_number.value()].run_duration_number == nullptr) {
555 return;
556 }
557 if (this->valve_[valve_number.value()].run_duration_number->state == run_duration.value()) {
558 return;
559 }
560 auto call = this->valve_[valve_number.value()].run_duration_number->make_call();
561 if (this->valve_[valve_number.value()].run_duration_number->traits.get_unit_of_measurement_ref() == MIN_STR) {
562 call.set_value(run_duration.value() / 60.0);
563 } else {
564 call.set_value(run_duration.value());
565 }
566 call.perform();
567}
568
569void Sprinkler::set_auto_advance(const bool auto_advance) {
570 if (this->auto_adv_sw_ == nullptr) {
571 return;
572 }
573 if (this->auto_adv_sw_->state == auto_advance) {
574 return;
575 }
576 if (auto_advance) {
577 this->auto_adv_sw_->turn_on();
578 } else {
579 this->auto_adv_sw_->turn_off();
580 }
581}
582
584 this->target_repeats_ = repeat;
585 if (this->repeat_number_ == nullptr) {
586 return;
587 }
588 if (this->repeat_number_->state == repeat.value()) {
589 return;
590 }
591 auto call = this->repeat_number_->make_call();
592 call.set_value(repeat.value_or(0));
593 call.perform();
594}
595
596void Sprinkler::set_queue_enable(bool queue_enable) {
597 if (this->queue_enable_sw_ == nullptr) {
598 return;
599 }
600 if (this->queue_enable_sw_->state == queue_enable) {
601 return;
602 }
603 if (queue_enable) {
604 this->queue_enable_sw_->turn_on();
605 } else {
606 this->queue_enable_sw_->turn_off();
607 }
608}
609
610void Sprinkler::set_reverse(const bool reverse) {
611 if (this->reverse_sw_ == nullptr) {
612 return;
613 }
614 if (this->reverse_sw_->state == reverse) {
615 return;
616 }
617 if (reverse) {
618 this->reverse_sw_->turn_on();
619 } else {
620 this->reverse_sw_->turn_off();
621 }
622}
623
624void Sprinkler::set_standby(const bool standby) {
625 if (this->standby_sw_ == nullptr) {
626 return;
627 }
628 if (this->standby_sw_->state == standby) {
629 return;
630 }
631 if (standby) {
632 this->standby_sw_->turn_on();
633 } else {
634 this->standby_sw_->turn_off();
635 }
636}
637
638uint32_t Sprinkler::valve_run_duration(const size_t valve_number) {
639 if (!this->is_a_valid_valve(valve_number)) {
640 return 0;
641 }
642 if (this->valve_[valve_number].run_duration_number != nullptr) {
643 if (this->valve_[valve_number].run_duration_number->traits.get_unit_of_measurement_ref() == MIN_STR) {
644 return static_cast<uint32_t>(roundf(this->valve_[valve_number].run_duration_number->state * 60));
645 } else {
646 return static_cast<uint32_t>(roundf(this->valve_[valve_number].run_duration_number->state));
647 }
648 }
649 return this->valve_[valve_number].run_duration;
650}
651
652uint32_t Sprinkler::valve_run_duration_adjusted(const size_t valve_number) {
653 uint32_t run_duration = 0;
654
655 if (this->is_a_valid_valve(valve_number)) {
656 run_duration = this->valve_run_duration(valve_number);
657 }
658 run_duration = static_cast<uint32_t>(roundf(run_duration * this->multiplier()));
659 // run_duration must not be less than any of these
660 if ((run_duration < this->start_delay_) || (run_duration < this->stop_delay_) ||
661 (run_duration < this->switching_delay_.value_or(0) * 2)) {
662 return std::max(this->switching_delay_.value_or(0) * 2, std::max(this->start_delay_, this->stop_delay_));
663 }
664 return run_duration;
665}
666
668 if (this->auto_adv_sw_ != nullptr) {
669 return this->auto_adv_sw_->state;
670 }
671 return true;
672}
673
675 if (this->multiplier_number_ != nullptr) {
676 return this->multiplier_number_->state;
677 }
678 return this->multiplier_;
679}
680
682 if (this->repeat_number_ != nullptr) {
683 return static_cast<uint32_t>(roundf(this->repeat_number_->state));
684 }
685 return this->target_repeats_;
686}
687
689 // if there is an active valve and auto-advance is enabled, we may be repeating, so return the count
690 if (this->active_req_.has_request() && this->auto_advance()) {
691 return this->repeat_count_;
692 }
693 return nullopt;
694}
695
697 if (this->queue_enable_sw_ != nullptr) {
698 return this->queue_enable_sw_->state;
699 }
700 return true;
701}
702
704 if (this->reverse_sw_ != nullptr) {
705 return this->reverse_sw_->state;
706 }
707 return false;
708}
709
711 if (this->standby_sw_ != nullptr) {
712 return this->standby_sw_->state;
713 }
714 return false;
715}
716
718 if (this->standby()) {
719 this->log_standby_warning_(LOG_STR("start_from_queue"));
720 return;
721 }
722 if (this->multiplier() == 0) {
723 this->log_multiplier_zero_warning_(LOG_STR("start_from_queue"));
724 return;
725 }
726 if (this->queued_valves_.empty()) {
727 return; // if there is nothing in the queue, don't do anything
728 }
729 if (this->queue_enabled() && this->active_valve().has_value()) {
730 return; // if there is already a valve running from the queue, do nothing
731 }
732
733 this->set_auto_advance(false);
734 this->set_queue_enable(true);
735
736 this->reset_cycle_states_(); // just in case auto-advance is switched on later
737 this->repeat_count_ = 0;
738 this->fsm_kick_(); // will automagically pick up from the queue (it has priority)
739}
740
742 if (this->standby()) {
743 this->log_standby_warning_(LOG_STR("start_full_cycle"));
744 return;
745 }
746 if (this->multiplier() == 0) {
747 this->log_multiplier_zero_warning_(LOG_STR("start_full_cycle"));
748 return;
749 }
750 if (this->auto_advance() && this->active_valve().has_value()) {
751 return; // if auto-advance is already enabled and there is already a valve running, do nothing
752 }
753
754 this->set_queue_enable(false);
755
756 this->prep_full_cycle_();
757 this->repeat_count_ = 0;
758 // if there is no active valve already, start the first valve in the cycle
759 if (!this->active_req_.has_request()) {
760 this->fsm_kick_();
761 }
762}
763
765 if (this->standby()) {
766 this->log_standby_warning_(LOG_STR("start_single_valve"));
767 return;
768 }
769 if (this->multiplier() == 0) {
770 this->log_multiplier_zero_warning_(LOG_STR("start_single_valve"));
771 return;
772 }
773 if (!valve_number.has_value() || (valve_number == this->active_valve())) {
774 return;
775 }
776
777 this->set_auto_advance(false);
778 this->set_queue_enable(false);
779
780 this->reset_cycle_states_(); // just in case auto-advance is switched on later
781 this->repeat_count_ = 0;
782 this->fsm_request_(valve_number.value(), run_duration.value_or(0));
783}
784
786 if (valve_number.has_value()) {
787 if (this->is_a_valid_valve(valve_number.value()) && (this->queued_valves_.size() < this->max_queue_size_)) {
788 SprinklerQueueItem item{valve_number.value(), run_duration.value()};
789 this->queued_valves_.insert(this->queued_valves_.begin(), item);
790 ESP_LOGD(TAG, "Valve %zu placed into queue with run duration of %" PRIu32 " seconds", valve_number.value_or(0),
791 run_duration.value_or(0));
792 }
793 }
794}
795
797 this->queued_valves_.clear();
798 ESP_LOGD(TAG, "Queue cleared");
799}
800
802 if (this->standby()) {
803 this->log_standby_warning_(LOG_STR("next_valve"));
804 return;
805 }
806
807 if (this->state_ == IDLE) {
808 this->reset_cycle_states_(); // just in case auto-advance is switched on later
809 }
810
811 this->manual_valve_ = this->next_valve_number_(
812 this->manual_valve_.value_or(this->active_req_.valve_as_opt().value_or(this->number_of_valves() - 1)),
813 !this->next_prev_ignore_disabled_, true);
814
815 if (!this->manual_valve_.has_value()) {
816 ESP_LOGD(TAG, "next_valve was called but no valve could be started; perhaps next_prev_ignore_disabled allows only "
817 "enabled valves and no valves are enabled?");
818 return;
819 }
820
824 } else {
825 this->fsm_request_(this->manual_valve_.value());
826 }
827}
828
830 if (this->standby()) {
831 this->log_standby_warning_(LOG_STR("previous_valve"));
832 return;
833 }
834
835 if (this->state_ == IDLE) {
836 this->reset_cycle_states_(); // just in case auto-advance is switched on later
837 }
838
839 this->manual_valve_ =
840 this->previous_valve_number_(this->manual_valve_.value_or(this->active_req_.valve_as_opt().value_or(0)),
841 !this->next_prev_ignore_disabled_, true);
842
843 if (!this->manual_valve_.has_value()) {
844 ESP_LOGD(TAG, "previous_valve was called but no valve could be started; perhaps next_prev_ignore_disabled allows "
845 "only enabled valves and no valves are enabled?");
846 return;
847 }
848
852 } else {
853 this->fsm_request_(this->manual_valve_.value());
854 }
855}
856
857void Sprinkler::shutdown(bool clear_queue) {
859 this->active_req_.reset();
860 this->manual_valve_.reset();
861 this->next_req_.reset();
862 for (auto &vo : this->valve_op_) {
863 vo.stop();
864 }
866 if (clear_queue) {
867 this->clear_queued_valves();
868 this->repeat_count_ = 0;
869 }
870}
871
873 if (this->paused_valve_.has_value() || !this->active_req_.has_request()) {
874 return; // we can't pause if we're already paused or if there is no active valve
875 }
876 this->paused_valve_ = this->active_valve();
878 this->shutdown(false);
879 ESP_LOGD(TAG, "Paused valve %zu with %" PRIu32 " seconds remaining", this->paused_valve_.value_or(0),
880 this->resume_duration_.value_or(0));
881}
882
884 if (this->standby()) {
885 this->log_standby_warning_(LOG_STR("resume"));
886 return;
887 }
888
889 if (this->paused_valve_.has_value() && (this->resume_duration_.has_value())) {
890 // Resume only if valve has not been completed yet
891 if (!this->valve_cycle_complete_(this->paused_valve_.value())) {
892 ESP_LOGD(TAG, "Resuming valve %zu with %" PRIu32 " seconds remaining", this->paused_valve_.value_or(0),
893 this->resume_duration_.value_or(0));
894 this->fsm_request_(this->paused_valve_.value(), this->resume_duration_.value());
895 }
896 this->reset_resume();
897 } else {
898 ESP_LOGD(TAG, "No valve to resume!");
899 }
900}
901
903 if (this->paused_valve_.has_value() && (this->resume_duration_.has_value())) {
904 this->resume();
905 } else {
906 this->start_full_cycle();
907 }
908}
909
911 this->paused_valve_.reset();
912 this->resume_duration_.reset();
913}
914
915const char *Sprinkler::valve_name(const size_t valve_number) {
916 if (this->is_a_valid_valve(valve_number)) {
917 return this->valve_[valve_number].controller_switch->get_name().c_str();
918 }
919 return nullptr;
920}
921
928
930 if (!this->valve_overlap_ && this->prev_req_.has_request() && this->prev_req_.has_valve_operator() &&
931 (this->prev_req_.valve_operator()->state() == STARTING || this->prev_req_.valve_operator()->state() == ACTIVE)) {
932 return this->prev_req_.valve_as_opt();
933 }
934 return this->active_req_.valve_as_opt();
935}
936
938
940 if (!this->queued_valves_.empty()) {
941 return this->queued_valves_.back().valve_number;
942 }
943 return nullopt;
944}
945
947
948size_t Sprinkler::number_of_valves() { return this->valve_.size(); }
949
950bool Sprinkler::is_a_valid_valve(const size_t valve_number) { return (valve_number < this->number_of_valves()); }
951
953 if (pump_switch == nullptr) {
954 return false; // we can't do anything if there's nothing to check
955 }
956 // a pump must be considered "in use" if a (distribution) valve it supplies is active. this means:
957 // - at least one SprinklerValveOperator:
958 // - has a valve loaded that depends on this pump
959 // - is in a state that depends on the pump: (ACTIVE and _possibly_ STARTING/STOPPING)
960 // - if NO SprinklerValveOperator is active but there is a run request pending (active_req_.has_request()) and the
961 // controller state is STARTING, valve open delay is configured but NOT pump_switch_off_during_valve_open_delay_
962 for (auto &vo : this->valve_op_) { // first, check if any SprinklerValveOperator has a valve dependent on this pump
963 if ((vo.state() != BYPASS) && (vo.pump_switch() != nullptr)) {
964 // the SprinklerValveOperator is configured with a pump; now check if it is the pump of interest
965 if (vo.pump_switch() == pump_switch) {
966 // now if the SprinklerValveOperator has a pump and it is either ACTIVE, is STARTING with a valve delay or
967 // is STOPPING with a valve delay, its pump can be considered "in use", so just return indicating this now
968 if ((vo.state() == ACTIVE) ||
969 ((vo.state() == STARTING) && this->start_delay_ && this->start_delay_is_valve_delay_) ||
970 ((vo.state() == STOPPING) && this->stop_delay_ && this->stop_delay_is_valve_delay_)) {
971 return true;
972 }
973 }
974 }
975 } // if we end up here, no SprinklerValveOperator was in a "give-away" state indicating that the pump is in use...
977 this->active_req_.has_request() && (this->state_ != STOPPING)) {
978 // ...the controller is configured to keep the pump on during a valve open delay, so just return
979 // whether or not the next valve shares the same pump
980 auto *valve_pump = this->valve_pump_switch(this->active_req_.valve());
981 if (valve_pump == nullptr) {
982 return false; // valve has no pump, so this pump isn't in use by it
983 }
984 return pump_switch == valve_pump;
985 }
986 return false;
987}
988
990 if (pump_switch == nullptr) {
991 return; // we can't do anything if there's nothing to check
992 }
993
994 bool hold_pump_on = false;
995
996 for (auto &controller : this->other_controllers_) { // check if the pump is in use by another controller
997 if (controller != this) { // dummy check
998 if (controller->pump_in_use(pump_switch)) {
999 hold_pump_on = true; // if another controller says it's using this pump, keep it on
1000 }
1001 }
1002 }
1003 if (hold_pump_on) {
1004 ESP_LOGD(TAG, "Leaving pump on because another controller instance is using it");
1005 }
1006
1007 if (state) { // ...and now we can set the new state of the switch
1008 pump_switch->turn_on();
1009 } else if (!hold_pump_on && !this->pump_in_use(pump_switch)) {
1010 pump_switch->turn_off();
1011 }
1012}
1013
1015 uint32_t total_time_remaining = 0;
1016
1017 for (size_t valve = 0; valve < this->number_of_valves(); valve++) {
1018 total_time_remaining += this->valve_run_duration_adjusted(valve);
1019 }
1020
1021 if (this->valve_overlap_) {
1022 total_time_remaining -= this->switching_delay_.value_or(0) * (this->number_of_valves() - 1);
1023 } else {
1024 total_time_remaining += this->switching_delay_.value_or(0) * (this->number_of_valves() - 1);
1025 }
1026
1027 return total_time_remaining;
1028}
1029
1031 uint32_t total_time_remaining = 0;
1032 uint32_t valve_count = 0;
1033
1034 for (size_t valve = 0; valve < this->number_of_valves(); valve++) {
1035 if (this->valve_is_enabled_(valve)) {
1036 total_time_remaining += this->valve_run_duration_adjusted(valve);
1037 valve_count++;
1038 }
1039 }
1040
1041 if (valve_count) {
1042 if (this->valve_overlap_) {
1043 total_time_remaining -= this->switching_delay_.value_or(0) * (valve_count - 1);
1044 } else {
1045 total_time_remaining += this->switching_delay_.value_or(0) * (valve_count - 1);
1046 }
1047 }
1048
1049 return total_time_remaining;
1050}
1051
1053 uint32_t total_time_remaining = 0;
1054 uint32_t enabled_valve_count = 0;
1055 uint32_t incomplete_valve_count = 0;
1056
1057 for (size_t valve = 0; valve < this->number_of_valves(); valve++) {
1058 if (this->valve_is_enabled_(valve)) {
1059 enabled_valve_count++;
1060 if (!this->valve_cycle_complete_(valve)) {
1061 if (!this->active_valve().has_value() || (valve != this->active_valve().value())) {
1062 total_time_remaining += this->valve_run_duration_adjusted(valve);
1063 incomplete_valve_count++;
1064 } else {
1065 // to get here, there must be an active valve and this valve must be equal to 'valve'
1066 if (this->active_req_.valve_operator() == nullptr) { // no SVO has been assigned yet so it hasn't started
1067 total_time_remaining += this->valve_run_duration_adjusted(valve);
1068 incomplete_valve_count++;
1069 }
1070 }
1071 }
1072 }
1073 }
1074
1075 if (incomplete_valve_count >= enabled_valve_count) {
1076 incomplete_valve_count--;
1077 }
1078 if (incomplete_valve_count) {
1079 if (this->valve_overlap_) {
1080 total_time_remaining -= this->switching_delay_.value_or(0) * incomplete_valve_count;
1081 } else {
1082 total_time_remaining += this->switching_delay_.value_or(0) * incomplete_valve_count;
1083 }
1084 }
1085
1086 return total_time_remaining;
1087}
1088
1090 uint32_t total_time_remaining = 0;
1091 uint32_t valve_count = 0;
1092
1093 for (auto &valve : this->queued_valves_) {
1094 if (valve.run_duration) {
1095 total_time_remaining += valve.run_duration;
1096 } else {
1097 total_time_remaining += this->valve_run_duration_adjusted(valve.valve_number);
1098 }
1099 valve_count++;
1100 }
1101
1102 if (valve_count) {
1103 if (this->valve_overlap_) {
1104 total_time_remaining -= this->switching_delay_.value_or(0) * (valve_count - 1);
1105 } else {
1106 total_time_remaining += this->switching_delay_.value_or(0) * (valve_count - 1);
1107 }
1108 }
1109
1110 return total_time_remaining;
1111}
1112
1114 if (this->active_req_.has_request()) { // first try to return the value based on active_req_...
1115 if (this->active_req_.valve_operator() != nullptr) {
1116 return this->active_req_.valve_operator()->time_remaining();
1117 }
1118 }
1119 if (this->prev_req_.has_request()) { // try to return the value based on prev_req_...
1120 if (this->prev_req_.valve_operator() != nullptr) {
1121 return this->prev_req_.valve_operator()->time_remaining();
1122 }
1123 }
1124 return nullopt;
1125}
1126
1128 if (!this->time_remaining_active_valve().has_value() && this->state_ == IDLE) {
1129 return nullopt;
1130 }
1131
1132 auto total_time_remaining = this->time_remaining_active_valve().value_or(0);
1133 if (this->auto_advance()) {
1134 total_time_remaining += this->total_cycle_time_enabled_incomplete_valves();
1135 if (this->repeat().value_or(0) > 0) {
1136 total_time_remaining +=
1137 (this->total_cycle_time_enabled_valves() * (this->repeat().value_or(0) - this->repeat_count().value_or(0)));
1138 }
1139 }
1140
1141 if (this->queue_enabled()) {
1142 total_time_remaining += this->total_queue_time();
1143 }
1144 return total_time_remaining;
1145}
1146
1148 if (this->state_ != IDLE) {
1149 return true;
1150 }
1151
1152 for (auto &controller : this->other_controllers_) {
1153 if (controller != this) { // dummy check
1154 if (controller->controller_state() != IDLE) {
1155 return true;
1156 }
1157 }
1158 }
1159 return false;
1160}
1161
1163 if (this->is_a_valid_valve(valve_number)) {
1164 return this->valve_[valve_number].controller_switch;
1165 }
1166 return nullptr;
1167}
1168
1170 if (this->is_a_valid_valve(valve_number)) {
1171 return this->valve_[valve_number].enable_switch;
1172 }
1173 return nullptr;
1174}
1175
1176switch_::Switch *Sprinkler::valve_switch(const size_t valve_number) {
1177 if (this->is_a_valid_valve(valve_number)) {
1178 return this->valve_[valve_number].valve_switch;
1179 }
1180 return nullptr;
1181}
1182
1184 if (this->is_a_valid_valve(valve_number) && this->valve_[valve_number].pump_switch_index.has_value()) {
1185 return this->pump_[this->valve_[valve_number].pump_switch_index.value()];
1186 }
1187 return nullptr;
1188}
1189
1191 if (pump_index < this->pump_.size()) {
1192 return this->pump_[pump_index];
1193 }
1194 return nullptr;
1195}
1196
1197bool Sprinkler::valve_is_enabled_(const size_t valve_number) {
1198 if (this->is_a_valid_valve(valve_number)) {
1199 if (this->valve_[valve_number].enable_switch != nullptr) {
1200 return this->valve_[valve_number].enable_switch->state;
1201 } else {
1202 return true;
1203 }
1204 }
1205 return false;
1206}
1207
1208void Sprinkler::mark_valve_cycle_complete_(const size_t valve_number) {
1209 if (this->is_a_valid_valve(valve_number)) {
1210 ESP_LOGD(TAG, "Marking valve %u complete", valve_number);
1211 this->valve_[valve_number].valve_cycle_complete = true;
1212 }
1213}
1214
1215bool Sprinkler::valve_cycle_complete_(const size_t valve_number) {
1216 if (this->is_a_valid_valve(valve_number)) {
1217 return this->valve_[valve_number].valve_cycle_complete;
1218 }
1219 return false;
1220}
1221
1222optional<size_t> Sprinkler::next_valve_number_(const optional<size_t> first_valve, const bool include_disabled,
1223 const bool include_complete) {
1224 auto valve = first_valve.value_or(0);
1225 size_t start = first_valve.has_value() ? 1 : 0;
1226
1227 if (!this->is_a_valid_valve(valve)) {
1228 valve = 0;
1229 }
1230
1231 for (size_t offset = start; offset < this->number_of_valves(); offset++) {
1232 auto valve_of_interest = valve + offset;
1233 if (!this->is_a_valid_valve(valve_of_interest)) {
1234 valve_of_interest -= this->number_of_valves();
1235 }
1236
1237 if ((this->valve_is_enabled_(valve_of_interest) || include_disabled) &&
1238 (!this->valve_cycle_complete_(valve_of_interest) || include_complete)) {
1239 return valve_of_interest;
1240 }
1241 }
1242 return nullopt;
1243}
1244
1245optional<size_t> Sprinkler::previous_valve_number_(const optional<size_t> first_valve, const bool include_disabled,
1246 const bool include_complete) {
1247 auto valve = first_valve.value_or(this->number_of_valves() - 1);
1248 size_t start = first_valve.has_value() ? 1 : 0;
1249
1250 if (!this->is_a_valid_valve(valve)) {
1251 valve = this->number_of_valves() - 1;
1252 }
1253
1254 for (size_t offset = start; offset < this->number_of_valves(); offset++) {
1255 auto valve_of_interest = valve - offset;
1256 if (!this->is_a_valid_valve(valve_of_interest)) {
1257 valve_of_interest += this->number_of_valves();
1258 }
1259
1260 if ((this->valve_is_enabled_(valve_of_interest) || include_disabled) &&
1261 (!this->valve_cycle_complete_(valve_of_interest) || include_complete)) {
1262 return valve_of_interest;
1263 }
1264 }
1265 return nullopt;
1266}
1267
1269 if (this->reverse()) {
1270 return this->previous_valve_number_(first_valve, false, false);
1271 }
1272 return this->next_valve_number_(first_valve, false, false);
1273}
1274
1276 if (this->active_req_.has_request()) {
1277 this->prev_req_ = this->active_req_;
1278 } else {
1279 this->prev_req_.reset();
1280 }
1281
1282 if (this->next_req_.has_request()) {
1283 if (!this->next_req_.run_duration()) { // ensure the run duration is set correctly for consumption later on
1285 }
1286 return; // there is already a request pending
1287 } else if (this->queue_enabled() && !this->queued_valves_.empty()) {
1288 this->next_req_.set_valve(this->queued_valves_.back().valve_number);
1290 if (this->queued_valves_.back().run_duration) {
1291 this->next_req_.set_run_duration(this->queued_valves_.back().run_duration);
1292 this->queued_valves_.pop_back();
1293 } else if (this->multiplier()) {
1294 this->next_req_.set_run_duration(this->valve_run_duration_adjusted(this->queued_valves_.back().valve_number));
1295 this->queued_valves_.pop_back();
1296 } else {
1297 this->next_req_.reset();
1298 }
1299 } else if (this->auto_advance() && this->multiplier()) {
1300 if (this->next_valve_number_in_cycle_(first_valve).has_value()) {
1301 // if there is another valve to run as a part of a cycle, load that
1302 this->next_req_.set_valve(this->next_valve_number_in_cycle_(first_valve).value_or(0));
1305 this->valve_run_duration_adjusted(this->next_valve_number_in_cycle_(first_valve).value_or(0)));
1306 } else if ((this->repeat_count_++ < this->repeat().value_or(0))) {
1307 ESP_LOGD(TAG, "Repeating - starting cycle %" PRIu32 " of %" PRIu32, this->repeat_count_ + 1,
1308 this->repeat().value_or(0) + 1);
1309 // if there are repeats remaining and no more valves were left in the cycle, start a new cycle
1310 this->prep_full_cycle_();
1311 if (this->next_valve_number_in_cycle_().has_value()) { // this should always succeed here, but just in case...
1312 this->next_req_.set_valve(this->next_valve_number_in_cycle_().value_or(0));
1315 this->valve_run_duration_adjusted(this->next_valve_number_in_cycle_().value_or(0)));
1316 }
1317 }
1318 }
1319}
1320
1322 for (size_t valve_number = 0; valve_number < this->number_of_valves(); valve_number++) {
1323 if (this->valve_is_enabled_(valve_number))
1324 return true;
1325 }
1326 return false;
1327}
1328
1330 if (!req->has_request()) {
1331 return; // we can't do anything if the request contains nothing
1332 }
1333 if (!this->is_a_valid_valve(req->valve())) {
1334 return; // we can't do anything if the valve number isn't valid
1335 }
1336 for (auto &vo : this->valve_op_) { // find the first available SprinklerValveOperator, load it and start it up
1337 if (vo.state() == IDLE) {
1338 auto run_duration = req->run_duration() ? req->run_duration() : this->valve_run_duration_adjusted(req->valve());
1339 ESP_LOGD(TAG, "%s is starting valve %zu for %" PRIu32 " seconds, cycle %" PRIu32 " of %" PRIu32,
1340 LOG_STR_ARG(this->req_as_str_(req->request_is_from())), req->valve(), run_duration,
1341 this->repeat_count_ + 1, this->repeat().value_or(0) + 1);
1342 req->set_valve_operator(&vo);
1343 vo.set_controller(this);
1344 vo.set_valve(&this->valve_[req->valve()]);
1345 vo.set_run_duration(run_duration);
1346 vo.set_start_delay(this->start_delay_, this->start_delay_is_valve_delay_);
1347 vo.set_stop_delay(this->stop_delay_, this->stop_delay_is_valve_delay_);
1348 vo.start();
1349 return;
1350 }
1351 }
1352}
1353
1354void Sprinkler::all_valves_off_(const bool include_pump) {
1355 for (size_t valve_index = 0; valve_index < this->number_of_valves(); valve_index++) {
1356 auto *valve_sw = this->valve_[valve_index].valve_switch;
1357 if ((valve_sw != nullptr) && valve_sw->state) {
1358 valve_sw->turn_off();
1359 }
1360 if (include_pump) {
1361 this->set_pump_state(this->valve_pump_switch(valve_index), false);
1362 }
1363 }
1364 ESP_LOGD(TAG, "All valves stopped%s", include_pump ? ", including pumps" : "");
1365}
1366
1368 this->set_auto_advance(true);
1369
1370 if (!this->any_valve_is_enabled_()) {
1371 for (auto &valve : this->valve_) {
1372 if (valve.enable_switch != nullptr) {
1373 if (!valve.enable_switch->state) {
1374 valve.enable_switch->turn_on();
1375 }
1376 }
1377 }
1378 }
1379 this->reset_cycle_states_();
1380}
1381
1383 for (auto &valve : this->valve_) {
1384 valve.valve_cycle_complete = false;
1385 }
1386}
1387
1388void Sprinkler::fsm_request_(size_t requested_valve, uint32_t requested_run_duration) {
1389 this->next_req_.set_valve(requested_valve);
1390 this->next_req_.set_run_duration(requested_run_duration);
1391 // if state is IDLE or ACTIVE, call fsm_transition_() to start it immediately;
1392 // otherwise, fsm_transition() will pick up next_req_ at the next appropriate transition
1393 this->fsm_kick_();
1394}
1395
1397 if ((this->state_ == IDLE) || (this->state_ == ACTIVE)) {
1398 this->fsm_transition_();
1399 }
1400}
1401
1403 ESP_LOGVV(TAG, "fsm_transition_ called; state is %s", LOG_STR_ARG(this->state_as_str_(this->state_)));
1404 switch (this->state_) {
1405 case IDLE: // the system was off -> start it up
1406 // advances to ACTIVE
1408 break;
1409
1410 case ACTIVE:
1411 // advances to STOPPING or ACTIVE (again)
1413 break;
1414
1415 case STARTING: {
1416 // follows valve open delay interval
1417 uint32_t timer_duration = this->active_req_.run_duration();
1418 if (timer_duration > this->switching_delay_.value_or(0)) {
1419 timer_duration -= this->switching_delay_.value_or(0);
1420 }
1421 this->set_timer_duration_(sprinkler::TIMER_SM, timer_duration);
1423 this->start_valve_(&this->active_req_);
1424 this->state_ = ACTIVE;
1425 if (this->next_req_.has_request()) {
1426 // another valve has been requested, so restart the timer so we pick it up quickly
1429 }
1430 break;
1431 }
1432
1433 case STOPPING:
1434 // stop_delay_ has elapsed so just shut everything off
1435 this->active_req_.reset();
1436 this->manual_valve_.reset();
1437 this->all_valves_off_(true);
1438 this->state_ = IDLE;
1439 break;
1440
1441 default:
1442 break;
1443 }
1444 if (this->next_req_.has_request() && (this->state_ == IDLE)) {
1445 // another valve has been requested, so restart the timer so we pick it up quickly
1448 }
1449 ESP_LOGVV(TAG, "fsm_transition_ complete; new state is %s", LOG_STR_ARG(this->state_as_str_(this->state_)));
1450}
1451
1454
1455 if (this->next_req_.has_request()) { // there is a valve to run...
1456 this->active_req_.set_valve(this->next_req_.valve());
1459 this->next_req_.reset();
1460
1461 uint32_t timer_duration = this->active_req_.run_duration();
1462 if (timer_duration > this->switching_delay_.value_or(0)) {
1463 timer_duration -= this->switching_delay_.value_or(0);
1464 }
1465 this->set_timer_duration_(sprinkler::TIMER_SM, timer_duration);
1467 this->start_valve_(&this->active_req_);
1468 this->state_ = ACTIVE;
1469 }
1470}
1471
1473 if (!this->active_req_.has_request()) { // dummy check...
1475 return;
1476 }
1477
1478 if (!this->timer_active_(sprinkler::TIMER_SM)) { // only flag the valve as "complete" if the timer finished
1479 if ((this->active_req_.request_is_from() == CYCLE) || (this->active_req_.request_is_from() == USER)) {
1481 }
1482 } else {
1483 ESP_LOGD(TAG, "Valve cycle interrupted - NOT flagging valve as complete and stopping current valve");
1484 for (auto &vo : this->valve_op_) {
1485 vo.stop();
1486 }
1487 }
1488
1490
1491 if (this->next_req_.has_request()) { // there is another valve to run...
1492 auto *active_pump = this->valve_pump_switch(this->active_req_.valve());
1493 auto *next_pump = this->valve_pump_switch(this->next_req_.valve());
1494 bool same_pump = (active_pump != nullptr) && (next_pump != nullptr) && (active_pump == next_pump);
1495
1496 this->active_req_.set_valve(this->next_req_.valve());
1499 this->next_req_.reset();
1500
1501 // this->state_ = ACTIVE; // state isn't changing
1502 if (this->valve_overlap_ || !this->switching_delay_.has_value()) {
1503 uint32_t timer_duration = this->active_req_.run_duration();
1504 if (timer_duration > this->switching_delay_.value_or(0)) {
1505 timer_duration -= this->switching_delay_.value_or(0);
1506 }
1507 this->set_timer_duration_(sprinkler::TIMER_SM, timer_duration);
1509 this->start_valve_(&this->active_req_);
1510 } else {
1511 this->set_timer_duration_(
1513 this->switching_delay_.value() * 2 +
1514 (this->pump_switch_off_during_valve_open_delay_ && same_pump ? this->stop_delay_ : 0));
1516 this->state_ = STARTING;
1517 }
1518 } else { // there is NOT another valve to run...
1520 }
1521}
1522
1529
1530void Sprinkler::log_standby_warning_(const LogString *method_name) {
1531 ESP_LOGW(TAG, "%s called but standby is enabled; no action taken", LOG_STR_ARG(method_name));
1532}
1533
1534void Sprinkler::log_multiplier_zero_warning_(const LogString *method_name) {
1535 ESP_LOGW(TAG, "%s called but multiplier is set to zero; no action taken", LOG_STR_ARG(method_name));
1536}
1537
1539 switch (origin) {
1540 case USER:
1541 return LOG_STR("USER");
1542
1543 case CYCLE:
1544 return LOG_STR("CYCLE");
1545
1546 case QUEUE:
1547 return LOG_STR("QUEUE");
1548
1549 default:
1550 return LOG_STR("UNKNOWN");
1551 }
1552}
1553
1555 switch (state) {
1556 case IDLE:
1557 return LOG_STR("IDLE");
1558
1559 case STARTING:
1560 return LOG_STR("STARTING");
1561
1562 case ACTIVE:
1563 return LOG_STR("ACTIVE");
1564
1565 case STOPPING:
1566 return LOG_STR("STOPPING");
1567
1568 case BYPASS:
1569 return LOG_STR("BYPASS");
1570
1571 default:
1572 return LOG_STR("UNKNOWN");
1573 }
1574}
1575
1577 if (this->timer_duration_(timer_index) > 0) {
1578 // FixedVector ensures timer_ can't be resized, so .c_str() pointers remain valid
1579 this->set_timeout(this->timer_[timer_index].name.c_str(), this->timer_duration_(timer_index),
1580 this->timer_cbf_(timer_index));
1581 this->timer_[timer_index].start_time = millis();
1582 this->timer_[timer_index].active = true;
1583 }
1584 ESP_LOGVV(TAG, "Timer %zu started for %" PRIu32 " sec", static_cast<size_t>(timer_index),
1585 this->timer_duration_(timer_index) / 1000);
1586}
1587
1589 this->timer_[timer_index].active = false;
1590 return this->cancel_timeout(this->timer_[timer_index].name.c_str());
1591}
1592
1593bool Sprinkler::timer_active_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].active; }
1594
1595void Sprinkler::set_timer_duration_(const SprinklerTimerIndex timer_index, const uint32_t time) {
1596 this->timer_[timer_index].time = 1000 * time;
1597}
1598
1599uint32_t Sprinkler::timer_duration_(const SprinklerTimerIndex timer_index) { return this->timer_[timer_index].time; }
1600
1601std::function<void()> Sprinkler::timer_cbf_(const SprinklerTimerIndex timer_index) {
1602 return this->timer_[timer_index].func;
1603}
1604
1606 this->timer_[sprinkler::TIMER_VALVE_SELECTION].active = false;
1607 ESP_LOGVV(TAG, "Valve selection timer expired");
1608 if (this->manual_valve_.has_value()) {
1609 this->fsm_request_(this->manual_valve_.value());
1610 this->manual_valve_.reset();
1611 }
1612}
1613
1615 this->timer_[sprinkler::TIMER_SM].active = false;
1616 ESP_LOGVV(TAG, "State machine timer expired");
1617 this->fsm_transition_();
1618}
1619
1621 ESP_LOGCONFIG(TAG, "Sprinkler Controller -- %s", this->name_.c_str());
1622 if (this->manual_selection_delay_.has_value()) {
1623 ESP_LOGCONFIG(TAG, " Manual Selection Delay: %" PRIu32 " seconds", this->manual_selection_delay_.value_or(0));
1624 }
1625 if (this->repeat().has_value()) {
1626 ESP_LOGCONFIG(TAG, " Repeat Cycles: %" PRIu32 " times", this->repeat().value_or(0));
1627 }
1628 if (this->start_delay_) {
1629 if (this->start_delay_is_valve_delay_) {
1630 ESP_LOGCONFIG(TAG, " Pump Start Valve Delay: %" PRIu32 " seconds", this->start_delay_);
1631 } else {
1632 ESP_LOGCONFIG(TAG, " Pump Start Pump Delay: %" PRIu32 " seconds", this->start_delay_);
1633 }
1634 }
1635 if (this->stop_delay_) {
1636 if (this->stop_delay_is_valve_delay_) {
1637 ESP_LOGCONFIG(TAG, " Pump Stop Valve Delay: %" PRIu32 " seconds", this->stop_delay_);
1638 } else {
1639 ESP_LOGCONFIG(TAG, " Pump Stop Pump Delay: %" PRIu32 " seconds", this->stop_delay_);
1640 }
1641 }
1642 if (this->switching_delay_.has_value()) {
1643 if (this->valve_overlap_) {
1644 ESP_LOGCONFIG(TAG, " Valve Overlap: %" PRIu32 " seconds", this->switching_delay_.value_or(0));
1645 } else {
1646 ESP_LOGCONFIG(TAG,
1647 " Valve Open Delay: %" PRIu32 " seconds\n"
1648 " Pump Switch Off During Valve Open Delay: %s",
1650 }
1651 }
1652 for (size_t valve_number = 0; valve_number < this->number_of_valves(); valve_number++) {
1653 ESP_LOGCONFIG(TAG,
1654 " Valve %zu:\n"
1655 " Name: %s\n"
1656 " Run Duration: %" PRIu32 " seconds",
1657 valve_number, this->valve_name(valve_number), this->valve_run_duration(valve_number));
1658 }
1659 if (!this->pump_.empty()) {
1660 ESP_LOGCONFIG(TAG, " Total number of pumps: %zu", this->pump_.size());
1661 }
1662 if (!this->valve_.empty()) {
1663 ESP_LOGCONFIG(TAG, " Total number of valves: %zu", this->valve_.size());
1664 }
1665}
1666
1667} // namespace esphome::sprinkler
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:445
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_timeout(const std boo cancel_timeout)(const char *name)
Cancel a timeout function.
Definition component.h:465
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
Definition automation.h:238
void stop_action()
Stop any action connected to this trigger.
Definition automation.h:246
NumberCall & set_value(float value)
NumberCall make_call()
Definition number.h:35
void publish_state(float state)
Definition number.cpp:31
NumberTraits traits
Definition number.h:39
bool has_value() const
Definition optional.h:92
value_type value_or(U const &v) const
Definition optional.h:98
value_type const & value() const
Definition optional.h:94
void set_state_lambda(std::function< optional< bool >()> &&f)
Definition sprinkler.cpp:71
optional< std::function< optional< bool >()> > f_
Definition sprinkler.h:109
void set_controller_standby_switch(SprinklerControllerSwitch *standby_switch)
void add_controller(Sprinkler *other_controller)
add another controller to the controller so it can check if pumps/main valves are in use
FixedVector< SprinklerTimer > timer_
Valve control timers - FixedVector enforces that this can never grow beyond init() size.
Definition sprinkler.h:558
void resume()
resumes a cycle that was suspended using pause()
void fsm_transition_to_shutdown_()
starts up the system from IDLE state
optional< size_t > previous_valve_number_(optional< size_t > first_valve=nullopt, bool include_disabled=true, bool include_complete=true)
returns the number of the previous valve in the vector or nullopt if no valves match criteria
bool is_a_valid_valve(size_t valve_number)
returns true if valve number is valid
bool reverse()
returns true if reverse is enabled
bool next_prev_ignore_disabled_
When set to true, the next and previous actions will skip disabled valves.
Definition sprinkler.h:491
void reset_resume()
resets resume state
SprinklerControllerNumber * repeat_number_
Definition sprinkler.h:572
bool valve_is_enabled_(size_t valve_number)
returns true if valve number is enabled
SprinklerState state_
Sprinkler controller state.
Definition sprinkler.h:510
void next_valve()
advances to the next valve (numerically)
void log_standby_warning_(const LogString *method_name)
log error message when a method is called but standby is enabled
uint32_t repeat_count_
Number of times the full cycle has been repeated.
Definition sprinkler.h:540
void configure_valve_switch(size_t valve_number, switch_::Switch *valve_switch, uint32_t run_duration)
configure a valve's switch object and run duration. run_duration is time in seconds.
std::vector< SprinklerValve > valve_
Sprinkler valve objects.
Definition sprinkler.h:552
void queue_valve(optional< size_t > valve_number, optional< uint32_t > run_duration)
adds a valve into the queue.
void set_valve_run_duration(optional< size_t > valve_number, optional< uint32_t > run_duration)
set how long the valve should remain on/open. run_duration is time in seconds
optional< uint32_t > resume_duration_
Set from time_remaining() when paused.
Definition sprinkler.h:531
void set_repeat(optional< uint32_t > repeat)
set the number of times to repeat a full cycle
float multiplier()
returns the current value of the multiplier
bool auto_advance()
returns true if auto_advance is enabled
uint32_t total_queue_time()
returns the amount of time in seconds required for all valves in the queue
void fsm_transition_from_shutdown_()
starts up the system from IDLE state
optional< uint32_t > repeat_count()
if a cycle is active, returns the number of times the controller has repeated the cycle....
void all_valves_off_(bool include_pump=false)
turns off/closes all valves, including pump if include_pump is true
SprinklerValveRunRequest prev_req_
The previous run request the controller processed.
Definition sprinkler.h:519
void start_valve_(SprinklerValveRunRequest *req)
loads an available SprinklerValveOperator (valve_op_) based on req and starts it (switches it on).
optional< uint32_t > repeat()
returns the number of times the controller is set to repeat cycles, if at all. check with 'has_value(...
void mark_valve_cycle_complete_(size_t valve_number)
marks a valve's cycle as complete
void set_pump_stop_delay(uint32_t stop_delay)
set how long the pump should stop after the valve (when the pump is starting)
void log_multiplier_zero_warning_(const LogString *method_name)
log error message when a method is called but multiplier is zero
optional< size_t > paused_valve()
returns the number of the valve that is paused, if any. check with 'has_value()'
void set_controller_reverse_switch(SprinklerControllerSwitch *reverse_switch)
uint32_t start_delay_
Pump start/stop delay intervals.
Definition sprinkler.h:504
const LogString * req_as_str_(SprinklerValveRunRequestOrigin origin)
return the specified SprinklerValveRunRequestOrigin as a string
void set_valve_overlap(uint32_t valve_overlap)
set how long the controller should wait after opening a valve before closing the previous valve
optional< SprinklerValveRunRequestOrigin > active_valve_request_is_from()
returns what invoked the valve that is currently active, if any. check with 'has_value()'
void set_reverse(bool reverse)
if reverse is true, controller will iterate through all enabled valves in reverse (descending) order
SprinklerControllerNumber * multiplier_number_
Number components we'll present to the front end.
Definition sprinkler.h:571
std::vector< SprinklerValveOperator > valve_op_
Sprinkler valve operator objects.
Definition sprinkler.h:555
SprinklerControllerSwitch * enable_switch(size_t valve_number)
returns a pointer to a valve's enable switch object
void start_full_cycle()
starts a full cycle of all enabled valves and enables auto_advance.
SprinklerControllerSwitch * controller_sw_
Definition sprinkler.h:565
void set_valve_stop_delay(uint32_t stop_delay)
set how long the valve should stop after the pump (when the pump is stopping)
void set_controller_queue_enable_switch(SprinklerControllerSwitch *queue_enable_switch)
void set_pump_state(switch_::Switch *pump_switch, bool state)
switches on/off a pump "safely" by checking that the new state will not conflict with another control...
optional< uint32_t > target_repeats_
Set the number of times to repeat a full cycle.
Definition sprinkler.h:528
void set_controller_main_switch(SprinklerControllerSwitch *controller_switch)
configure important controller switches
switch_::Switch * valve_switch(size_t valve_number)
returns a pointer to a valve's switch object
optional< uint32_t > time_remaining_current_operation()
returns the amount of time remaining in seconds for all valves remaining, including the active valve,...
const LogString * state_as_str_(SprinklerState state)
return the specified SprinklerState state as a string
SprinklerControllerSwitch * reverse_sw_
Definition sprinkler.h:567
optional< uint32_t > switching_delay_
Valve switching delay.
Definition sprinkler.h:537
void fsm_kick_()
kicks the state machine to advance, starting it if it is not already active
void shutdown(bool clear_queue=false)
turns off all valves, effectively shutting down the system.
SprinklerValveRunRequest active_req_
The valve run request that is currently active.
Definition sprinkler.h:513
bool any_controller_is_active()
returns true if this or any sprinkler controller this controller knows about is active
void set_valve_open_delay(uint32_t valve_open_delay)
set how long the controller should wait to open/switch on the valve after it becomes active
std::vector< switch_::Switch * > pump_
Sprinkler valve pump switches.
Definition sprinkler.h:549
bool start_delay_is_valve_delay_
Pump start/stop delay interval types.
Definition sprinkler.h:500
optional< size_t > manual_valve_
The number of the manually selected valve currently selected.
Definition sprinkler.h:522
bool pump_switch_off_during_valve_open_delay_
Pump should be off during valve_open_delay interval.
Definition sprinkler.h:494
bool cancel_timer_(SprinklerTimerIndex timer_index)
SprinklerControllerSwitch * queue_enable_sw_
Definition sprinkler.h:566
void start_timer_(SprinklerTimerIndex timer_index)
Start/cancel/get status of valve timers.
void previous_valve()
advances to the previous valve (numerically)
switch_::Switch * valve_pump_switch_by_pump_index(size_t pump_index)
returns a pointer to a valve's pump switch object
void set_auto_advance(bool auto_advance)
if auto_advance is true, controller will iterate through all enabled valves
void prep_full_cycle_()
prepares for a full cycle by verifying auto-advance is on as well as one or more valve enable switche...
uint32_t valve_run_duration_adjusted(size_t valve_number)
returns valve_number's run duration (in seconds) adjusted by multiplier_
std::unique_ptr< ShutdownAction<> > sprinkler_shutdown_action_
Definition sprinkler.h:574
std::unique_ptr< ResumeOrStartAction<> > sprinkler_resumeorstart_action_
Definition sprinkler.h:576
void clear_queued_valves()
clears/removes all valves from the queue
bool any_valve_is_enabled_()
returns true if any valve is enabled
size_t number_of_valves()
returns the number of valves the controller is configured with
void set_pump_start_delay(uint32_t start_delay)
set how long the pump should start after the valve (when the pump is starting)
void set_standby(bool standby)
if standby is true, controller will refuse to activate any valves
std::vector< SprinklerQueueItem > queued_valves_
Queue of valves to activate next, regardless of auto-advance.
Definition sprinkler.h:546
bool queue_enabled()
returns true if the queue is enabled to run
void resume_or_start_full_cycle()
if a cycle was suspended using pause(), resumes it. otherwise calls start_full_cycle()
std::unique_ptr< Automation<> > sprinkler_turn_on_automation_
Definition sprinkler.h:579
void valve_selection_callback_()
callback functions for timers
optional< size_t > next_valve_number_(optional< size_t > first_valve=nullopt, bool include_disabled=true, bool include_complete=true)
returns the number of the next valve in the vector or nullopt if no valves match criteria
uint32_t total_cycle_time_all_valves()
returns the amount of time in seconds required for all valves
std::unique_ptr< Automation<> > sprinkler_turn_off_automation_
Definition sprinkler.h:578
optional< uint32_t > time_remaining_active_valve()
returns the amount of time remaining in seconds for the active valve, if any
optional< size_t > next_valve_number_in_cycle_(optional< size_t > first_valve=nullopt)
returns the number of the next valve that should be activated in a full cycle.
std::function< void()> timer_cbf_(SprinklerTimerIndex timer_index)
void set_pump_switch_off_during_valve_open_delay(bool pump_switch_off_during_valve_open_delay)
if pump_switch_off_during_valve_open_delay is true, the controller will switch off the pump during th...
std::unique_ptr< ShutdownAction<> > sprinkler_standby_shutdown_action_
Definition sprinkler.h:575
optional< uint32_t > manual_selection_delay_
Manual switching delay.
Definition sprinkler.h:534
void set_controller_repeat_number(SprinklerControllerNumber *repeat_number)
void start_single_valve(optional< size_t > valve_number, optional< uint32_t > run_duration=nullopt)
activates a single valve and disables auto_advance.
uint32_t valve_run_duration(size_t valve_number)
returns valve_number's run duration in seconds
void load_next_valve_run_request_(optional< size_t > first_valve=nullopt)
loads next_req_ with the next valve that should be activated, including its run duration.
void fsm_transition_()
advance controller state, advancing to target_valve if provided
void fsm_request_(size_t requested_valve, uint32_t requested_run_duration=0)
make a request of the state machine
optional< size_t > paused_valve_
The number of the valve to resume from (if paused)
Definition sprinkler.h:525
SprinklerValveRunRequest next_req_
The next run request for the controller to consume after active_req_ is complete.
Definition sprinkler.h:516
void configure_valve_run_duration_number(size_t valve_number, SprinklerControllerNumber *run_duration_number)
configure a valve's run duration number component
SprinklerControllerSwitch * auto_adv_sw_
Switches we'll present to the front end.
Definition sprinkler.h:564
void set_controller_multiplier_number(SprinklerControllerNumber *multiplier_number)
configure important controller number components
void set_manual_selection_delay(uint32_t manual_selection_delay)
set how long the controller should wait to activate a valve after next_valve() or previous_valve() is...
uint32_t total_cycle_time_enabled_incomplete_valves()
returns the amount of time in seconds required for all enabled & incomplete valves,...
bool valve_overlap_
Sprinkler valve cycle should overlap.
Definition sprinkler.h:497
switch_::Switch * valve_pump_switch(size_t valve_number)
returns a pointer to a valve's pump switch object
void set_divider(optional< uint32_t > divider)
sets the multiplier value to '1 / divider' and sets repeat value to divider
std::unique_ptr< Automation<> > sprinkler_standby_turn_on_automation_
Definition sprinkler.h:580
void set_valve_start_delay(uint32_t start_delay)
set how long the valve should start after the pump (when the pump is stopping)
void pause()
same as shutdown(), but also stores active_valve() and time_remaining() allowing resume() to continue...
void set_next_prev_ignore_disabled_valves(bool ignore_disabled)
enable/disable skipping of disabled valves by the next and previous actions
optional< size_t > active_valve()
returns the number of the valve that is currently active, if any. check with 'has_value()'
void set_queue_enable(bool queue_enable)
if queue_enable is true, controller will iterate through valves in the queue
bool pump_in_use(switch_::Switch *pump_switch)
returns true if the pump the pointer points to is in use
bool timer_active_(SprinklerTimerIndex timer_index)
returns true if the specified timer is active/running
optional< size_t > queued_valve()
returns the number of the next valve in the queue, if any. check with 'has_value()'
std::vector< Sprinkler * > other_controllers_
Other Sprinkler instances we should be aware of (used to check if pumps are in use)
Definition sprinkler.h:561
const char * valve_name(size_t valve_number)
returns a pointer to a valve's name string object; returns nullptr if valve_number is invalid
void start_from_queue()
starts the controller from the first valve in the queue and disables auto_advance.
void configure_valve_pump_switch(size_t valve_number, switch_::Switch *pump_switch)
configure a valve's associated pump switch object
void reset_cycle_states_()
resets the cycle state for all valves
float multiplier_
Sprinkler valve run time multiplier value.
Definition sprinkler.h:543
SprinklerControllerSwitch * control_switch(size_t valve_number)
returns a pointer to a valve's control switch object
void set_multiplier(optional< float > multiplier)
value multiplied by configured run times – used to extend or shorten the cycle
void set_timer_duration_(SprinklerTimerIndex timer_index, uint32_t time)
time is converted to milliseconds (ms) for set_timeout()
void set_controller_auto_adv_switch(SprinklerControllerSwitch *auto_adv_switch)
void fsm_transition_from_valve_run_()
transitions from ACTIVE state to ACTIVE (as in, next valve) or to a SHUTDOWN or IDLE state
bool standby()
returns true if standby is enabled
uint32_t total_cycle_time_enabled_valves()
returns the amount of time in seconds required for all enabled valves
bool valve_cycle_complete_(size_t valve_number)
returns true if valve's cycle is flagged as complete
optional< size_t > manual_valve()
returns the number of the valve that is manually selected, if any.
void add_valve(SprinklerControllerSwitch *valve_sw, SprinklerControllerSwitch *enable_sw=nullptr)
add a valve to the controller
SprinklerControllerSwitch * standby_sw_
Definition sprinkler.h:568
uint32_t timer_duration_(SprinklerTimerIndex timer_index)
returns time in milliseconds (ms)
void set_valve(SprinklerValve *valve)
void set_run_duration(uint32_t run_duration)
void set_start_delay(uint32_t start_delay, bool start_delay_is_valve_delay)
void set_controller(Sprinkler *controller)
void set_stop_delay(uint32_t stop_delay, bool stop_delay_is_valve_delay)
SprinklerValveRunRequestOrigin origin_
Definition sprinkler.h:173
SprinklerValveRunRequestOrigin request_is_from()
void set_request_from(SprinklerValveRunRequestOrigin origin)
SprinklerValveOperator * valve_operator()
void set_run_duration(uint32_t run_duration)
void set_valve_operator(SprinklerValveOperator *valve_op)
Base class for all switches.
Definition switch.h:39
void turn_on()
Turn this switch on.
Definition switch.cpp:21
void turn_off()
Turn this switch off.
Definition switch.cpp:25
bool state
The current reported state of the binary sensor.
Definition switch.h:56
void publish_state(bool state)
Publish a state to the front-end from the back-end.
Definition switch.cpp:57
optional< bool > get_initial_state_with_restore_mode()
Returns the initial state of the switch, after applying restore mode rules.
Definition switch.cpp:43
bool state
Definition fan.h:0
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition component.cpp:80
const char *const TAG
Definition spi.cpp:7
const std::string MIN_STR
Definition sprinkler.h:14
ESPPreferences * global_preferences
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
const nullopt_t nullopt((nullopt_t::init()))
std::unique_ptr< StartSingleValveAction<> > valve_resumeorstart_action
Definition sprinkler.h:68
std::unique_ptr< Automation<> > valve_turn_off_automation
Definition sprinkler.h:69
SprinklerControllerSwitch * enable_switch
Definition sprinkler.h:62
std::unique_ptr< ShutdownAction<> > valve_shutdown_action
Definition sprinkler.h:67
SprinklerControllerSwitch * controller_switch
Definition sprinkler.h:61
std::unique_ptr< Automation<> > valve_turn_on_automation
Definition sprinkler.h:70
optional< size_t > pump_switch_index
Definition sprinkler.h:65