Skip to content

Instantly share code, notes, and snippets.

@jtcressy
Created December 16, 2023 20:39
Show Gist options
  • Save jtcressy/3b8ee2b5378168da460e490ff444d83d to your computer and use it in GitHub Desktop.
Save jtcressy/3b8ee2b5378168da460e490ff444d83d to your computer and use it in GitHub Desktop.
home assistant package - sonos soundboard
## Sonos Soundboard Machine - Use this file as a yaml dashboard, placed at <config-dir>/dashboards/sonos_soundboard.yaml
## NOTE: Requires mushroom cards and card-mod from HACS:
# https://github.com/piitaya/lovelace-mushroom
# https://github.com/thomasloven/lovelace-card-mod
title: Soundboard
views:
- icon: mdi:bullhorn
title: Soundboard
path: soundboard
type: panel
badges: []
cards:
- type: horizontal-stack
cards:
- type: custom:select-list-card
entity: input_select.soundboard_sounds
icon: mdi:bullhorn
truncate: true
show_toggle: true
scroll_to_selected: true
max_options: 15
- type: vertical-stack
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: call-service
service: script.sonos_soundboard_announce
target: {}
entity: input_select.soundboard_sounds
name: Play Selected Sound
show_state: true
theme: Mushroom Shadow
- type: custom:mushroom-number-card
entity: input_number.soundboard_volume
display_mode: slider
icon: mdi:volume-high
icon_type: entity-picture
fill_container: false
icon_color: purple
name: Volume
- type: custom:mushroom-select-card
entity: input_select.soundboard_area
name: Target Room/Area
icon: mdi:home
## Sonos Soundboard Machine - Use this file as a package https://www.home-assistant.io/docs/configuration/packages/
# Requirements:
# 1. Place soundboard clips in mp3 format in "<config dir>/media/soundboard".
# 2. Have sonos speakers connected with the 'sonos' integration. https://www.home-assistant.io/integrations/sonos/
# 3. Sonos media_player entities assigned to areas/rooms
## Notes:
# - Automation to populate input_select.soundboard_sounds may get stuck on config reload - just manually run automation.update_soundboard_media_soundboard
# - Automation to populate input_select.soundboard_area may get stuck sometimes too - just manually run automation.update_soundboard_areas_list
# - Placing new files in media/soundboard will automatically trigger a refresh, but it takes a few seconds; it's not instant.
input_select:
soundboard_sounds:
name: Soundboard Sounds
options:
- none
icon: mdi:bullhorn
soundboard_area:
name: Room/Area to Play Soundboard Sound
options:
- 'all'
initial: 'all'
icon: mdi:home
input_number:
soundboard_volume:
name: Soundboard Volume
initial: 30
min: 0
max: 100
step: 1
sensor:
- platform: folder
folder: /config/media/soundboard
filter: '*.mp3'
folder_watcher:
- folder: /config/media/soundboard
patterns:
- '*.mp3'
automation:
- alias: "Update Soundboard (/media/soundboard)"
trigger:
- platform: state
entity_id:
- sensor.soundboard
- input_select.soundboard_sounds
- platform: event
event_type: folder_watcher
event_data:
folder: /config/media/soundboard
action:
- service: input_select.set_options
target:
entity_id: input_select.soundboard_sounds
data:
options: |
{{ state_attr('sensor.soundboard', 'file_list') | replace(state_attr('sensor.soundboard', 'path'), '') | replace('.mp3', '') }}
- alias: "Update Soundboard Areas List"
trigger:
- platform: homeassistant
event: start
- platform: event
event_type: core_config_updated
- platform: state
entity_id:
- input_select.soundboard_area
action:
- service: input_select.set_options
target:
entity_id: input_select.soundboard_area
data:
options: |
{{ ['all'] + (expand(integration_entities('sonos'))|selectattr('domain','eq', 'media_player') | list | map(attribute='entity_id') | map('area_id') | unique | list)}}
script:
sonos_soundboard_announce:
alias: "Play Selected Soundboard Sound"
sequence:
- variables:
selected_area: >
{{ states('input_select.soundboard_area') }}
all_areas: >
{{ (expand(integration_entities('sonos'))|selectattr('domain','eq', 'media_player') | list | map(attribute='entity_id') | map('area_id') | unique | list) }}
target_area: >
{{ (all_areas if selected_area == 'all' else [selected_area]) }}
default_volume: "{{states('input_number.soundboard_volume')}}"
folder_sensor: "sensor.soundboard"
media_path: soundboard
filename: |
{{ states('input_select.soundboard_sounds') }}.mp3
media_player_ids: >
{{ expand(integration_entities('sonos'))|selectattr('domain','eq', 'media_player')|selectattr('entity_id', 'in', ((all_areas if selected_area == "all" else [selected_area]) | map('area_entities') | sum(start = [])))|map(attribute='entity_id')|list|join(",") }}
- service: media_player.play_media
data:
media_content_id: media-source://media_source/media/{{media_path}}/{{filename}}
media_content_type: audio/mpeg
announce: true
extra:
volume: '{{ volume | default(default_volume, true) }}'
target:
entity_id: '{{media_player_ids}}'
mode: single
icon: mdi:bullhorn
lovelace:
dashboards:
sonos-soundboard:
mode: yaml
title: Soundboard
icon: mdi:bullhorn
show_in_sidebar: true
filename: dashboards/sonos_soundboard.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment