-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalog-clock.yaml
259 lines (231 loc) · 7.69 KB
/
analog-clock.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -------------------------------------
# ---- Implemented on the 'ED' clock ----
# https://github.com/markusressel/ESPHome-Analog-Clock
# -------------------------------------
substitutions:
devicename: analog-clock
num_leds: "60"
esphome:
name: ${devicename}
platform: ESP8266
board: esp01_1m
on_boot:
# set "loading" effect while initializing
priority: 500.0
then:
# enable clock effect after boot
- light.turn_on:
id: light_ring
brightness: 100%
effect: Sync
wifi:
#power_save_mode: light # power reduced from 1.2w to 0.8w
#fast_connect: true
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
hidden: True
manual_ip:
static_ip: !secret ip_analog_clock
gateway: !secret ip_gateway
subnet: 255.255.255.0
domain: .homelan
ap: # Enable fallback hotspot (captive portal) in case wifi connection fails
ssid: "${devicename} Hotspot"
password: !secret password
captive_portal:
# Enable logging
logger:
# Enable Home Assistant API
api:
reboot_timeout: 0s # Dont reboot..
encryption:
key: !secret encryption_key
ota:
password: !secret password
web_server:
port: 80
auth:
username: admin
password: !secret password
globals:
- id: clock_brightness
type: int
restore_value: yes
initial_value: '255'
- id: clock_indicators_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: clock_seconds_enabled
type: bool
restore_value: yes
initial_value: 'true'
- id: wide_hour_hand_enabled
type: bool
restore_value: yes
initial_value: 'false'
# Sync time with Home Assistant.
time:
- platform: homeassistant
id: sntp_time
timezone: "Australia/Sydney"
on_time_sync:
then:
- light.turn_on:
id: light_ring
effect: Clock
# https://esphome.io/components/time.html
switch:
- platform: template
name: "${devicename} Clock Indicators"
icon: mdi:progress-clock
lambda: !lambda |-
return id(clock_indicators_enabled);
turn_on_action:
globals.set:
id: clock_indicators_enabled
value: 'true'
turn_off_action:
globals.set:
id: clock_indicators_enabled
value: 'false'
- platform: template
name: "${devicename} Clock Seconds"
icon: mdi:update
lambda: !lambda |-
return id(clock_seconds_enabled);
turn_on_action:
globals.set:
id: clock_seconds_enabled
value: 'true'
turn_off_action:
globals.set:
id: clock_seconds_enabled
value: 'false'
- platform: template
name: "${devicename} Wide Hour Hand"
lambda: !lambda |-
return id(wide_hour_hand_enabled);
turn_on_action:
globals.set:
id: wide_hour_hand_enabled
value: 'true'
turn_off_action:
globals.set:
id: wide_hour_hand_enabled
value: 'false'
light:
- platform: neopixelbus
type: GRB
variant: WS2811
pin: GPIO3
id: light_ring
internal: False
num_leds: "${num_leds}"
method: ESP8266_DMA
name: "${devicename} NeoPixel Light"
color_correct: [50%, 50%, 50%]
effects:
- flicker:
- strobe:
- random:
- addressable_rainbow:
width: ${num_leds}
- addressable_color_wipe:
- addressable_scan:
- addressable_twinkle:
- addressable_random_twinkle:
- addressable_fireworks:
- addressable_flicker:
- addressable_lambda:
name: "Clock"
update_interval: 32ms
lambda: |-
static boolean initialized;
static Color clock_ring_colors [${num_leds}];
if (initialized == false) {
std::fill_n(clock_ring_colors, it.size(), Color::BLACK);
initialized = true;
}
auto time = id(sntp_time).now();
if (!time.is_valid()) {
return;
}
// calculate led indices
int second_idx = (int) (time.second * (it.size() / 60));
int minute_idx = (int) (time.minute * (it.size() / 60));
// this idx would "jump" in big steps
int hour_idx = (int) ((time.hour % 12) * (it.size() / 12));
// this moves one led at a time
int hour_inc_min_idx = hour_idx + (int) (((float) time.minute / 12) * (it.size() / 60));
// the hour hand may be bigger than one led, so we use an array for that
std::vector<int> hour_idx_vector;
if (id(wide_hour_hand_enabled)) {
hour_idx_vector.push_back((hour_inc_min_idx - 1 + it.size()) % (it.size()));
hour_idx_vector.push_back(hour_inc_min_idx);
hour_idx_vector.push_back((hour_inc_min_idx + 1) % it.size());
} else {
hour_idx_vector.push_back(hour_inc_min_idx);
}
// fade out old colors
for (int i = 0; i < it.size(); i++) {
Color old_color = clock_ring_colors[i];
int percentage = 2;
int red = old_color.red;
int red_sub = std::max(1, (int) ((red / 100.0) * percentage));
int green = old_color.green;
int green_sub = std::max(1, (int) ((green / 100.0) * percentage));
int blue = old_color.blue;
int blue_sub = std::max(1, (int) ((blue / 100.0) * percentage));
if (red <= red_sub) { red = 0; } else { red -= red_sub; }
if (green <= green_sub) { green = 0; } else { green -= green_sub; }
if (blue <= blue_sub) { blue = 0; } else { blue -= blue_sub; }
Color new_color = Color(red, green, blue, 0);
clock_ring_colors[i] = new_color;
}
int indicator_brightness = id(clock_brightness) / 3;
Color indicator_color = Color(indicator_brightness, indicator_brightness, indicator_brightness);
// set base colors
Color hour_color = Color(0, id(clock_brightness), 0);
Color minute_color = Color(0, 0, id(clock_brightness));
Color second_color = Color(id(clock_brightness), 0, 0);
if (!id(clock_seconds_enabled)) {
second_color = Color::BLACK;
}
// merge colors if they overlap
// if minute idx inside hour idx range
if (std::find(std::begin(hour_idx_vector), std::end(hour_idx_vector), minute_idx) != std::end(hour_idx_vector)) {
minute_color += hour_color;
}
if (second_idx == minute_idx) {
minute_color += second_color;
second_color = minute_color;
} else if (std::find(std::begin(hour_idx_vector), std::end(hour_idx_vector), second_idx) != std::end(hour_idx_vector)) {
second_color += hour_color;
}
// === set colors from bottom to top
// indicators
if (id(clock_indicators_enabled)) {
for (int i = 0; i < it.size(); i += (int) ((float) it.size() / 12)) {
clock_ring_colors[i] = indicator_color;
}
}
// hours
for (int i = 0; i < hour_idx_vector.size(); i++) {
clock_ring_colors[hour_idx_vector[i]] = hour_color;
}
// minutes
clock_ring_colors[minute_idx] = minute_color;
// seconds
if (id(clock_seconds_enabled)) {
clock_ring_colors[second_idx] = second_color;
}
// apply colors to light
for (int i = 0; i < it.size(); i++) {
it[i] = clock_ring_colors[i];
}
- addressable_scan:
name: Sync
move_interval: 100ms
scan_width: 1