-
Notifications
You must be signed in to change notification settings - Fork 11
/
home-assistant-voice.yaml
1633 lines (1576 loc) · 58.4 KB
/
home-assistant-voice.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
substitutions:
# Phases of the Voice Assistant
# The voice assistant is ready to be triggered by a wake word
voice_assist_idle_phase_id: '1'
# The voice assistant is waiting for a voice command (after being triggered by the wake word)
voice_assist_waiting_for_command_phase_id: '2'
# The voice assistant is listening for a voice command
voice_assist_listening_for_command_phase_id: '3'
# The voice assistant is currently processing the command
voice_assist_thinking_phase_id: '4'
# The voice assistant is replying to the command
voice_assist_replying_phase_id: '5'
# The voice assistant is not ready
voice_assist_not_ready_phase_id: '10'
# The voice assistant encountered an error
voice_assist_error_phase_id: '11'
esphome:
name: home-assistant-voice
friendly_name: Home Assistant Voice
name_add_mac_suffix: true
min_version: 2024.9.0
platformio_options:
board_build.flash_mode: dio
on_boot:
priority: 375
then:
# Run the script to refresh the LED status
- script.execute: control_leds
- delay: 1s
- switch.turn_on: internal_speaker_amp
# If the hardware switch is ON, force the software switch to be ON too. This covers the case where the Mute hardware switch is operated when the device is turned off
- if:
condition:
binary_sensor.is_on: hardware_mute_switch
then:
- switch.template.publish:
id: master_mute_switch
state: ON
# If after 10 minutes, the device is still initializing (It did not yet connect to Home Assistant), turn off the init_in_progress variable and run the script to refresh the LED status
- delay: 10min
- if:
condition:
lambda: return id(init_in_progress);
then:
- lambda: id(init_in_progress) = false;
- script.execute: control_leds
esp32:
board: esp32-s3-devkitc-1
variant: esp32s3
flash_size: 16MB
framework:
type: esp-idf
version: recommended
sdkconfig_options:
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240: "y"
CONFIG_ESP32S3_DATA_CACHE_64KB: "y"
CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y"
CONFIG_ESP32S3_INSTRUCTION_CACHE_32KB: "y"
CONFIG_ESP32_S3_BOX_BOARD: "y"
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY: "y"
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP: "y"
# Settings based on https://github.com/espressif/esp-adf/issues/297#issuecomment-783811702
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM: "16"
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM: "512"
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER: "y"
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE: "0"
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM: "8"
CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM: "32"
CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED: "y"
CONFIG_ESP32_WIFI_TX_BA_WIN: "16"
CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED: "y"
CONFIG_ESP32_WIFI_RX_BA_WIN: "32"
CONFIG_LWIP_MAX_ACTIVE_TCP: "16"
CONFIG_LWIP_MAX_LISTENING_TCP: "16"
CONFIG_TCP_MAXRTX: "12"
CONFIG_TCP_SYNMAXRTX: "6"
CONFIG_TCP_MSS: "1436"
CONFIG_TCP_MSL: "60000"
CONFIG_TCP_SND_BUF_DEFAULT: "65535"
CONFIG_TCP_WND_DEFAULT: "65535" # Adjusted from linked settings to avoid compilation error
CONFIG_TCP_RECVMBOX_SIZE: "512"
CONFIG_TCP_QUEUE_OOSEQ: "y"
CONFIG_TCP_OVERSIZE_MSS: "y"
CONFIG_LWIP_WND_SCALE: "y"
CONFIG_TCP_RCV_SCALE: "3"
CONFIG_LWIP_TCPIP_RECVMBOX_SIZE: "512"
CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST: "y"
CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY: "y"
wifi:
id: wifi_id
on_connect:
- lambda: id(improv_ble_in_progress) = false;
- script.execute: control_leds
on_disconnect:
- script.execute: control_leds
logger:
level: DEBUG
logs:
sensor: WARN # avoids logging debug sensor updates
api:
id: api_id
on_client_connected:
- script.execute: control_leds
on_client_disconnected:
- script.execute: control_leds
ota:
- platform: esphome
id: ota_esphome
i2c:
sda: GPIO5
scl: GPIO6
frequency: 400kHz
psram:
mode: octal
speed: 80MHz
globals:
# Global index for our LEDs. So that switching between different animation does not lead to unwanted effects.
- id: global_led_animation_index
type: int
restore_value: no
initial_value: '0'
# Global initialization variable. Initialized to true and set to false once everything is connected. Only used to have a smooth "plugging" experience
- id: init_in_progress
type: bool
restore_value: no
initial_value: 'true'
# Global variable storing the state of ImprovBLE. Used to draw different LED animations
- id: improv_ble_in_progress
type: bool
restore_value: no
initial_value: 'false'
# Global variable tracking the phase of the voice assistant (defined above). Initialized to not_ready
- id: voice_assistant_phase
type: int
restore_value: no
initial_value: ${voice_assist_not_ready_phase_id}
# Global variable tracking if the dial was recently touched.
- id: dial_touched
type: bool
restore_value: no
initial_value: 'false'
# Global variable tracking if the LED color was recently changed.
- id: color_changed
type: bool
restore_value: no
initial_value: 'false'
# Global variable tracking if the jack has been plugged touched.
- id: jack_plugged_recently
type: bool
restore_value: no
initial_value: 'false'
# Global variable tracking if the jack has been unplugged touched.
- id: jack_unplugged_recently
type: bool
restore_value: no
initial_value: 'false'
# Global variable storing the first active timer
- id: first_active_timer
type: voice_assistant::Timer
restore_value: false
# Global variable storing if a timer is active
- id: is_timer_active
type: bool
restore_value: false
switch:
# This is the master mute switch. It is exposed to Home Assistant. The user can only turn it on and off if the hardware switch is off. (The hardware switch overrides the software one)
- platform: template
id: master_mute_switch
restore_mode: RESTORE_DEFAULT_OFF
icon: "mdi:microphone-off"
name: Mute
entity_category: config
turn_on_action:
- if:
condition:
binary_sensor.is_off: hardware_mute_switch
then:
- switch.template.publish:
id: master_mute_switch
state: ON
turn_off_action:
- if:
condition:
binary_sensor.is_off: hardware_mute_switch
then:
- switch.template.publish:
id: master_mute_switch
state: OFF
on_turn_on:
- script.execute: control_leds
on_turn_off:
- script.execute: control_leds
# Wake Word Sound Switch.
- platform: template
id: wake_sound
name: Wake sound
icon: "mdi:bullhorn"
entity_category: config
optimistic: true
restore_mode: RESTORE_DEFAULT_ON
# Internal switch to track when a timer is ringing on the device.
- platform: template
id: timer_ringing
optimistic: true
internal: true
restore_mode: ALWAYS_OFF
on_turn_off:
# Disable stop wake word
- lambda: id(stop).disable();
# Stop any current annoucement (ie: stop the timer ring mid playback)
- if:
condition:
lambda: return id(nabu_media_player)->state == media_player::MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING;
then:
lambda: |-
id(nabu_media_player)
->make_call()
.set_command(media_player::MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP)
.set_announcement(true)
.perform();
# Set back ducking ratio to zero
- nabu.set_ducking:
decibel_reduction: 0
duration: 1.0s
# Refresh the LED ring
- script.execute: control_leds
on_turn_on:
# Duck audio
- nabu.set_ducking:
decibel_reduction: 20
duration: 0.0s
# Enable stop wake word
- lambda: id(stop).enable();
# Ring timer
- script.execute: ring_timer
# Refresh LED
- script.execute: control_leds
# If 15 minutes have passed and the timer is still ringing, stop it.
- delay: 15min
- switch.turn_off: timer_ringing
- platform: gpio
pin: GPIO47
id: internal_speaker_amp
name: "Internal speaker amp"
entity_category: config
restore_mode: ALWAYS_OFF
internal: true
- platform: gpio
pin: GPIO46
id: grove_port_power
# Change this to ALWAYS_ON if you are planning to use the Grove port
restore_mode: RESTORE_DEFAULT_OFF
binary_sensor:
# Center Button. Used for many things (See on_multi_click)
- platform: gpio
id: center_button
pin:
number: GPIO0
inverted: true
on_press:
- script.execute: control_leds
on_release:
- script.execute: control_leds
on_multi_click:
# Simple Click:
# - Abort "things" in order
# - Timer
# - Announcements
# - Voice Assistant Pipeline run
# - Music
# - Starts the voice assistant if it is not yet running and if the device is not muted.
- timing:
- ON for at most 1s
- OFF for at least 0.25s
then:
- if:
condition:
lambda: return !id(init_in_progress) && !id(color_changed);
then:
- if:
condition:
switch.is_on: timer_ringing
then:
- switch.turn_off: timer_ringing
else:
- if:
condition:
lambda: return id(nabu_media_player)->state == media_player::MediaPlayerState::MEDIA_PLAYER_STATE_ANNOUNCING;
then:
- lambda: |
id(nabu_media_player)
->make_call()
.set_command(media_player::MediaPlayerCommand::MEDIA_PLAYER_COMMAND_STOP)
.set_announcement(true)
.perform();
else:
- if:
condition:
voice_assistant.is_running:
then:
- voice_assistant.stop:
else:
- if:
condition:
media_player.is_playing:
then:
- media_player.pause:
else:
- if:
condition:
and:
- switch.is_off: master_mute_switch
- not:
voice_assistant.is_running
then:
- script.execute:
id: play_sound
priority: true
sound_file: !lambda return id(center_button_press_sound);
- delay: 300ms
- voice_assistant.start:
# Double Click
# . Exposed as an event entity. To be used in automations inside Home Assistant
- timing:
- ON for at most 1s
- OFF for at most 0.25s
- ON for at most 1s
- OFF for at least 0.25s
then:
- if:
condition:
lambda: return !id(init_in_progress) && !id(color_changed);
then:
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(center_button_double_press_sound);
- event.trigger:
id: button_press_event
event_type: "double_press"
# Triple Click
# . Exposed as an event entity. To be used in automations inside Home Assistant
- timing:
- ON for at most 1s
- OFF for at most 0.25s
- ON for at most 1s
- OFF for at most 0.25s
- ON for at most 1s
- OFF for at least 0.25s
then:
- if:
condition:
lambda: return !id(init_in_progress) && !id(color_changed);
then:
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(center_button_triple_press_sound);
- event.trigger:
id: button_press_event
event_type: "triple_press"
# Long Press
# . Exposed as an event entity. To be used in automations inside Home Assistant
- timing:
- ON for at least 1s
then:
- if:
condition:
lambda: return !id(init_in_progress) && !id(color_changed);
then:
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(center_button_long_press_sound);
- light.turn_off: voice_assistant_leds
- event.trigger:
id: button_press_event
event_type: "long_press"
# Very important do not remove. Trust me :D
- timing:
# H ....
- ON for at most 0.2s
- OFF for 0s to 2s
- ON for at most 0.2s
- OFF for 0s to 2s
- ON for at most 0.2s
- OFF for 0s to 2s
- ON for at most 0.2s
- OFF for 0.5s to 2s
# A ._
- ON for at most 0.2s
- OFF for 0s to 2s
- ON for 0.2s to 2s
then:
- if:
condition:
lambda: return !id(init_in_progress);
then:
- light.turn_on:
brightness: 100%
id: voice_assistant_leds
effect: "Tick"
- script.execute:
id: play_sound
priority: true
sound_file: !lambda return id(easter_egg_tick_sound);
- delay: 4s
- light.turn_off: voice_assistant_leds
- script.execute:
id: play_sound
priority: true
sound_file: !lambda return id(easter_egg_tada_sound);
- light.turn_on:
brightness: 100%
id: voice_assistant_leds
effect: "Rainbow"
- event.trigger:
id: button_press_event
event_type: "easter_egg_press"
# Factory Reset Warning
# . Audible and Visible warning.
- timing:
- ON for at least 10s
then:
- if:
condition:
lambda: return !id(dial_touched);
then:
- light.turn_on:
brightness: 100%
id: voice_assistant_leds
effect: "Factory Reset Coming Up"
- script.execute:
id: play_sound
priority: true
sound_file: !lambda return id(factory_reset_initiated_sound);
- wait_until:
binary_sensor.is_off: center_button
- light.turn_off: voice_assistant_leds
- script.execute:
id: play_sound
priority: true
sound_file: !lambda return id(factory_reset_cancelled_sound);
# Factory Reset
- timing:
- ON for at least 22s
then:
- if:
condition:
lambda: return !id(dial_touched);
then:
- button.press: factory_reset_button
# Hardware mute switch (Side of the device)
- platform: gpio
id: hardware_mute_switch
internal: true
pin: GPIO3
on_press:
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(mute_switch_on_sound);
- switch.template.publish:
id: master_mute_switch
state: ON
on_release:
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(mute_switch_off_sound);
- switch.template.publish:
id: master_mute_switch
state: OFF
# Audio Jack PLugged sensor
- platform: gpio
id: jack_plugged
# Debouncing it a bit because it can be activated back and forth as you plug the audio jack
filters:
- delayed_on: 200ms
- delayed_off: 200ms
pin:
number: GPIO17
# When the jack is plugged in:
# - LED animation
# - Sound played
on_press:
- lambda: id(jack_plugged_recently) = true;
- script.execute: control_leds
- delay: 200ms
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(jack_connected_sound);
- delay: 800ms
- lambda: id(jack_plugged_recently) = false;
- script.execute: control_leds
# When the jack is unplugged:
# - LED animation
# - Sound played
on_release:
- lambda: id(jack_unplugged_recently) = true;
- script.execute: control_leds
- delay: 200ms
- script.execute:
id: play_sound
priority: false
sound_file: !lambda return id(jack_disconnected_sound);
- delay: 800ms
- lambda: id(jack_unplugged_recently) = false;
- script.execute: control_leds
light:
# Hardware LED ring. Not used because remapping needed
- platform: esp32_rmt_led_strip
id: leds_internal
pin: GPIO21
rmt_channel: 1
num_leds: 12
rgb_order: GRB
chipset: WS2812
default_transition_length: 0ms
power_supply: led_power
# Voice Assistant LED ring. Remapping of the internal LED.
# This light is not exposed. The device controls it
- platform: partition
id: voice_assistant_leds
internal: true
default_transition_length: 0ms
segments:
- id: leds_internal
from: 7
to: 11
- id: leds_internal
from: 0
to: 6
effects:
- addressable_lambda:
name: "Waiting for Command"
update_interval: 100ms
lambda: |-
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
if (i == id(global_led_animation_index) % 12) {
it[i] = color;
} else if (i == (id(global_led_animation_index) + 11) % 12) {
it[i] = color * 192;
} else if (i == (id(global_led_animation_index) + 10) % 12) {
it[i] = color * 128;
} else if (i == (id(global_led_animation_index) + 6) % 12) {
it[i] = color;
} else if (i == (id(global_led_animation_index) + 5) % 12) {
it[i] = color * 192;
} else if (i == (id(global_led_animation_index) + 4) % 12) {
it[i] = color * 128;
} else {
it[i] = Color::BLACK;
}
}
id(global_led_animation_index) = (id(global_led_animation_index) + 1) % 12;
- addressable_lambda:
name: "Listening For Command"
update_interval: 50ms
lambda: |-
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
if (i == id(global_led_animation_index) % 12) {
it[i] = color;
} else if (i == (id(global_led_animation_index) + 11) % 12) {
it[i] = color * 192;
} else if (i == (id(global_led_animation_index) + 10) % 12) {
it[i] = color * 128;
} else if (i == (id(global_led_animation_index) + 6) % 12) {
it[i] = color;
} else if (i == (id(global_led_animation_index) + 5) % 12) {
it[i] = color * 192;
} else if (i == (id(global_led_animation_index) + 4) % 12) {
it[i] = color * 128;
} else {
it[i] = Color::BLACK;
}
}
id(global_led_animation_index) = (id(global_led_animation_index) + 1) % 12;
- addressable_lambda:
name: "Thinking"
update_interval: 10ms
lambda: |-
static uint8_t brightness_step = 0;
static bool brightness_decreasing = true;
static uint8_t brightness_step_number = 10;
if (initial_run) {
brightness_step = 0;
brightness_decreasing = true;
}
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
if (i == id(global_led_animation_index) % 12) {
it[i] = color * uint8_t(255/brightness_step_number*(brightness_step_number-brightness_step));
} else if (i == (id(global_led_animation_index) + 6) % 12) {
it[i] = color * uint8_t(255/brightness_step_number*(brightness_step_number-brightness_step));
} else {
it[i] = Color::BLACK;
}
}
if (brightness_decreasing) {
brightness_step++;
} else {
brightness_step--;
}
if (brightness_step == 0 || brightness_step == brightness_step_number) {
brightness_decreasing = !brightness_decreasing;
}
- addressable_lambda:
name: "Replying"
update_interval: 50ms
lambda: |-
id(global_led_animation_index) = (12 + id(global_led_animation_index) - 1) % 12;
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
if (i == (id(global_led_animation_index)) % 12) {
it[i] = color;
} else if (i == ( id(global_led_animation_index) + 1) % 12) {
it[i] = color * 192;
} else if (i == ( id(global_led_animation_index) + 2) % 12) {
it[i] = color * 128;
} else if (i == ( id(global_led_animation_index) + 6) % 12) {
it[i] = color;
} else if (i == ( id(global_led_animation_index) + 7) % 12) {
it[i] = color * 192;
} else if (i == ( id(global_led_animation_index) + 8) % 12) {
it[i] = color * 128;
} else {
it[i] = Color::BLACK;
}
}
- addressable_lambda:
name: "Muted or Silent"
update_interval: 16ms
lambda: |-
static int8_t index = 0;
Color muted_color(255, 0, 0);
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
if ( light_color.get_state() ) {
it[i] = color;
} else {
it[i] = Color::BLACK;
}
}
if ( id(master_mute_switch).state ) {
it[2] = Color::BLACK;
it[3] = muted_color;
it[4] = Color::BLACK;
it[8] = Color::BLACK;
it[9] = muted_color;
it[10] = Color::BLACK;
}
if ( id(nabu_media_player).volume == 0.0f || id(nabu_media_player).is_muted() ) {
it[5] = Color::BLACK;
it[6] = muted_color;
it[7] = Color::BLACK;
}
- addressable_lambda:
name: "Volume Display"
update_interval: 50ms
lambda: |-
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
Color silenced_color(255, 0, 0);
auto volume_ratio = 12.0f * id(nabu_media_player).volume;
for (int i = 0; i < 12; i++) {
if (i <= volume_ratio) {
it[(6+i)%12] = color * min( 255.0f * (volume_ratio - i) , 255.0f ) ;
} else {
it[(6+i)%12] = Color::BLACK;
}
}
if (id(nabu_media_player).volume == 0.0f) {
it[6] = silenced_color;
}
- addressable_lambda:
name: "Center Button Touched"
update_interval: 16ms
lambda: |-
if (initial_run) {
// set voice_assistant_leds light to colors based on led_ring
auto led_ring_cv = id(led_ring).current_values;
auto va_leds_call = id(voice_assistant_leds).make_call();
va_leds_call.from_light_color_values(led_ring_cv);
va_leds_call.set_brightness( min ( max( id(led_ring).current_values.get_brightness() , 0.2f ) + 0.1f , 1.0f ) );
va_leds_call.set_state(true);
va_leds_call.perform();
}
auto light_color = id(voice_assistant_leds).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
for (int i = 0; i < 12; i++) {
it[i] = color;
}
- addressable_twinkle:
name: "Twinkle"
twinkle_probability: 50%
- addressable_lambda:
name: "Error"
update_interval: 10ms
lambda: |-
static uint8_t brightness_step = 0;
static bool brightness_decreasing = true;
static uint8_t brightness_step_number = 10;
if (initial_run) {
brightness_step = 0;
brightness_decreasing = true;
}
Color error_color(255, 0, 0);
for (int i = 0; i < 12; i++) {
it[i] = error_color * uint8_t(255/brightness_step_number*(brightness_step_number-brightness_step));
}
if (brightness_decreasing) {
brightness_step++;
} else {
brightness_step--;
}
if (brightness_step == 0 || brightness_step == brightness_step_number) {
brightness_decreasing = !brightness_decreasing;
}
- addressable_lambda:
name: "Timer Ring"
update_interval: 10ms
lambda: |-
static uint8_t brightness_step = 0;
static bool brightness_decreasing = true;
static uint8_t brightness_step_number = 10;
if (initial_run) {
brightness_step = 0;
brightness_decreasing = true;
}
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
Color muted_color(255, 0, 0);
for (int i = 0; i < 12; i++) {
it[i] = color * uint8_t(255/brightness_step_number*(brightness_step_number-brightness_step));
}
if ( id(master_mute_switch).state ) {
it[3] = muted_color;
it[9] = muted_color;
}
if (brightness_decreasing) {
brightness_step++;
} else {
brightness_step--;
}
if (brightness_step == 0 || brightness_step == brightness_step_number) {
brightness_decreasing = !brightness_decreasing;
}
- addressable_lambda:
name: "Timer Tick"
update_interval: 100ms
lambda: |-
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
Color muted_color(255, 0, 0);
auto timer_ratio = 12.0f * id(first_active_timer).seconds_left / max(id(first_active_timer).total_seconds , static_cast<uint32_t>(1));
uint8_t last_led_on = static_cast<uint8_t>(ceil(timer_ratio)) - 1;
for (int i = 0; i < 12; i++) {
float brightness_dip = ( i == id(global_led_animation_index) % 12 && i != last_led_on ) ? 0.9f : 1.0f ;
if (i <= timer_ratio) {
it[i] = color * min(255.0f * brightness_dip * (timer_ratio - i) , 255.0f * brightness_dip) ;
} else {
it[i] = Color::BLACK;
}
}
if (id(master_mute_switch).state) {
it[2] = Color::BLACK;
it[3] = muted_color;
it[4] = Color::BLACK;
it[8] = Color::BLACK;
it[9] = muted_color;
it[10] = Color::BLACK;
}
id(global_led_animation_index) = (12 + id(global_led_animation_index) - 1) % 12;
- addressable_rainbow:
name: "Rainbow"
width: 12
- addressable_lambda:
name: "Tick"
update_interval: 333ms
lambda: |-
static uint8_t index = 0;
Color color(255, 0, 0);
if (initial_run) {
index = 0;
}
for (int i = 0; i < 12; i++) {
if (i <= index ) {
it[i] = Color::BLACK;
} else {
it[i] = color;
}
}
index = (index + 1) % 12;
- addressable_lambda:
name: "Factory Reset Coming Up"
update_interval: 1s
lambda: |-
static uint8_t index = 0;
Color color(255, 0, 0);
if (initial_run) {
index = 0;
}
for (int i = 0; i < 12; i++) {
if (i <= index ) {
it[i] = color;
} else {
it[i] = Color::BLACK;
}
}
index = (index + 1) % 12;
- addressable_lambda:
name: "Jack Plugged"
update_interval: 40ms
lambda: |-
static uint8_t index = 0;
if (initial_run) {
index = 0;
}
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
if (index <= 6) {
for (int i = 0; i < 12; i++) {
if (i == index) {
it[i] = color;
} else if (i == (12 - index) % 12) {
it[i] = color;
} else {
it[i] = Color::BLACK;
}
}
}
index = (index + 1);
- addressable_lambda:
name: "Jack Unplugged"
update_interval: 40ms
lambda: |-
static uint8_t index = 0;
if (initial_run) {
index = 0;
}
auto light_color = id(led_ring).current_values;
Color color(light_color.get_red() * 255, light_color.get_green() * 255,
light_color.get_blue() * 255);
if (index <= 6) {
for (int i = 0; i < 12; i++) {
if (i == 6 - index) {
it[i] = color;
} else if (i == (6 + index) % 12) {
it[i] = color;
} else {
it[i] = Color::BLACK;
}
}
}
index = (index + 1);
# User facing LED ring. Remapping of the internal LEDs.
# Exposed to be used by the user.
- platform: partition
id: led_ring
name: LED Ring
entity_category: config
icon: "mdi:circle-outline"
default_transition_length: 0ms
restore_mode: RESTORE_DEFAULT_OFF
initial_state:
color_mode: rgb
brightness: 66%
red: 9.4%
green: 73.3%
blue: 94.9%
segments:
- id: leds_internal
from: 7
to: 11
- id: leds_internal
from: 0
to: 6
power_supply:
- id: led_power
pin: GPIO45
sensor:
# The dial. Used to control volume and Hue of the LED ring.
- platform: rotary_encoder
id: dial
pin_a: GPIO16
pin_b: GPIO18
resolution: 2
on_clockwise:
- lambda: id(dial_touched) = true;
- if:
condition:
binary_sensor.is_off: center_button
then:
- script.execute:
id: control_volume
increase_volume: true
else:
- script.execute:
id: control_hue
increase_hue: true
on_anticlockwise:
- lambda: id(dial_touched) = true;
- if:
condition:
binary_sensor.is_off: center_button
then:
- script.execute:
id: control_volume
increase_volume: false
else:
- script.execute:
id: control_hue
increase_hue: false
event:
# Event entity exposed to the user to automate on complex center button presses.
# The simple press is not exposed as it is used to control the device itself.
- platform: template
id: button_press_event
name: "Button press"
icon: mdi:button-pointer
device_class: button
event_types:
- double_press
- triple_press
- long_press
- easter_egg_press
script:
# Master script controlling the LEDs, based on different conditions : initialization in progress, wifi and api connected and voice assistant phase.
# For the sake of simplicity and re-usability, the script calls child scripts defined below.
# This script will be called every time one of these conditions is changing.
- id: control_leds
then:
- lambda: |
id(check_if_timers_active).execute();
if (id(is_timer_active)){
id(fetch_first_active_timer).execute();
}
if (id(improv_ble_in_progress)) {
id(control_leds_improv_ble_state).execute();
} else if (id(init_in_progress)) {
id(control_leds_init_state).execute();
} else if (!id(wifi_id).is_connected() || !id(api_id).is_connected()){
id(control_leds_no_ha_connection_state).execute();
} else if (id(center_button).state) {
id(control_leds_center_button_touched).execute();
} else if (id(jack_plugged_recently)) {
id(control_leds_jack_plugged_recently).execute();
} else if (id(jack_unplugged_recently)) {
id(control_leds_jack_unplugged_recently).execute();
} else if (id(dial_touched)) {
id(control_leds_dial_touched).execute();
} else if (id(timer_ringing).state) {
id(control_leds_timer_ringing).execute();
} else if (id(voice_assistant_phase) == ${voice_assist_waiting_for_command_phase_id}) {
id(control_leds_voice_assistant_waiting_for_command_phase).execute();
} else if (id(voice_assistant_phase) == ${voice_assist_listening_for_command_phase_id}) {
id(control_leds_voice_assistant_listening_for_command_phase).execute();
} else if (id(voice_assistant_phase) == ${voice_assist_thinking_phase_id}) {
id(control_leds_voice_assistant_thinking_phase).execute();
} else if (id(voice_assistant_phase) == ${voice_assist_replying_phase_id}) {