-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrock5b-power-guard-32.yaml
110 lines (102 loc) · 3.05 KB
/
rock5b-power-guard-32.yaml
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
substitutions:
device_name: "rock5b-power-guard"
device_name_underscore: "rock5b_power_guard"
packages:
device_common: !include .device_common.yaml
deep_sleep:
sleep_duration: 120s
esphome:
name: ${device_name}
platformio_options:
board_build.flash_mode: dio
includes:
- includes/holding_gpio.h
on_boot:
- priority: 800
then:
- lambda: |-
holding_gpio = new HoldingGpio(GPIO_NUM_22);
- priority: 600
then:
- script.execute: handle_power_sensor
esp32:
board: upesy_wroom
variant: ESP32
flash_size: 4MB
framework:
type: esp-idf
status_led:
pin:
number: GPIO2 #D4
switch:
- platform: template
id: "${device_name_underscore}_relay"
name: "${device_name} relay"
lambda: 'return holding_gpio->get_state();'
internal: true
restore_mode: DISABLED
turn_on_action:
- lambda: 'holding_gpio->set_state(1);'
turn_off_action:
- lambda: 'holding_gpio->set_state(0);'
binary_sensor:
- platform: gpio
id: "${device_name_underscore}_external_power"
pin:
number: GPIO23 #D7
inverted: true
name: "${device_name} external power"
publish_initial_state: true
on_state:
- script.execute: handle_power_sensor
- platform: gpio
id: "${device_name_underscore}_rock5b_is_on"
pin:
number: GPIO19 #D6
inverted: true
name: "${device_name} rock5b is on"
publish_initial_state: true
script:
- id: turn_on_rock5b
mode: single
then:
- switch.turn_on: "${device_name_underscore}_relay"
- wait_until:
condition:
binary_sensor.is_on: "${device_name_underscore}_rock5b_is_on"
timeout: 60s
- id: turn_off_rock5b
mode: single
then:
- wait_until:
condition:
binary_sensor.is_off: "${device_name_underscore}_rock5b_is_on"
timeout: 180s
- switch.turn_off: "${device_name_underscore}_relay"
- id: handle_power_sensor
mode: single
then:
- if:
condition:
- binary_sensor.is_on: "${device_name_underscore}_external_power"
then:
- if:
condition:
- script.is_running: turn_off_rock5b
then:
- lambda: ESP_LOGW("PWATCHER", "Turn off script is running. Stopping it.");
- script.stop: turn_off_rock5b
- lambda: ESP_LOGW("PWATCHER", "Power is available");
- script.execute: turn_on_rock5b
- script.wait: turn_on_rock5b
else:
- lambda: ESP_LOGW("PWATCHER", "Power is unavailable");
- if:
condition:
- binary_sensor.is_on: "${device_name_underscore}_rock5b_is_on"
then:
- lambda: ESP_LOGW("PWATCHER", "ROCK5B is alive. Waiting for it to turn off.");
- script.execute: turn_off_rock5b
- script.wait: turn_off_rock5b
- lambda: ESP_LOGW("PWATCHER", "ROCK5B is off. Entering the deep sleep.");
- deep_sleep.enter: