-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathirrigation.h
62 lines (57 loc) · 1.65 KB
/
irrigation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* A set of helpers that make the integration of ESPHome Sprinkler
* with the relays and the display easier and shorter.
*/
#pragma once
#include "esphome.h"
namespace esphome {
/*
* Toggle valve - start valve if inactive or shutdown controller
*/
void toggle(int valve, switch_::Switch *relay,
sprinkler::Sprinkler *sprinkler) {
if (!id(relay).state) {
id(sprinkler).start_single_valve(valve - 1);
} else {
id(sprinkler).shutdown();
}
}
/*
* Prevent starting single valve program when button state change was triggered
* by the controller
*
* Useful when turning valve on by push of a button button,
* that is also used to indicate that the valve is on.
*/
void conditional_on(int valve, switch_::Switch *relay,
sprinkler::Sprinkler *sprinkler) {
if (!id(relay).state) {
id(sprinkler).start_single_valve(valve - 1);
}
}
/*
* Prevent shutting down the conroller when button state change was triggered by
* the controller
*
* Useful when turning valve off by push of a button button,
* that is also used to indicate that the valve is on.
*/
void conditional_off(switch_::Switch *relay, sprinkler::Sprinkler *sprinkler) {
if (id(relay).state) {
id(sprinkler).shutdown();
}
}
/*
* Format remaining time of the curent valve as a string (minute:second)
*/
std::string remaining_time(sprinkler::Sprinkler *sprinkler) {
uint32_t remaining_time = id(sprinkler).time_remaining_active_valve().value_or(0);
if (remaining_time == 0) {
return "";
}
char result[12];
sprintf(result, "%02d:%02d", (int)(remaining_time / 60),
(int)(remaining_time % 60));
return result;
}
} // namespace esphome