Video
This is a multiroom configuration that allows you to stream music from any music app on your iPhone to speakers in your home. You can listen to music synchronously or individually in each room.
The following components are used:
Shairport-sync - https://github.com/mikebrady/shairport-sync
Snapcast - https://github.com/badaix/snapcast
mini-media-player - https://github.com/kalkih/mini-media-player
inotify - https://github.com/inotify-tools/inotify-tools/wiki
Install
-
First of all, you need to install shairport-sync on the main device (server). I will not write instructions for installing it here. You can find instructions in the attached links. We will broadcast an audio stream from your phone to the main device. The broadcast will be via Airplay. Shairport-sync must be configured with the following flags:
--with-pipe --with-metadata --with-mqtt-client
-
After installing shairport-sync, you should edit its settings in the file
/etc/shairport-sync.conf
(when configuring, you specify the path to store the configuration file).
There is a general group within which you can use the name tag to set the service name.
In the backend group, you need to uncomment the pipe parameter and specify
name = "/tmp/snapfifo"
This is the file that the audio stream from your phone will be sent to. It is necessary for the snapcast server to work. The Snapcast component reads this file and broadcasts the audio stream to its clients. At the same time, clients sync with the server and music plays synchronously in all zones. It is also highly recommended to install the client on the server itself,so that it is used for syncing. This will ensure minimal latency and smooth operation.
- Install snapcast server and snapcast client on the main device (server).
Edit the settings file snapcast server /etc/snapserver.conf
In the stream group, specify
stream = pipe:///tmp/snapfifo?name=Snapserver&sampleformat=44100:16:2
you can specify any desired name and audio format
- Add to configuration.yaml file:
media_player:
- platform: snapcast
host: 192.168.1.x # specify your own host
-
Install snapcast client on your clients (what will you play music on)
-
Install the mini-media-player component in your Home Assistant. In the mini-media-player settings, specify your snapcast clients and the necessary parameters (control buttons, power button, and so on). After that, cards will appear in your interface to manage all your snapcast clients.
Now you can use your multiroom media system.
In this configuration, you will stream music to just one Airplay device (your main device with shairport-sync). Then the audio stream will be transmitted to clients using snapcast server. This way simultaneous playback will work in all music zones. This is very convenient.
If you want to broadcast different sources of music to different zones, just install own shairport-sync on each client and choose each one individually.
Design
You can use Home Assistant themes to create your own unique interface design.
Metadata
To display the cover of a track (album) that is being played, do the following:
- Edit the
/etc/shairport-sync.conf file
In the metadata group, uncomment metadata and specify parameters:
metadata =
{
enabled = "yes";
include_cover_art = "yes";
cover_art_cache_directory = "/tmp/shairport-sync/.cache/coverart";
}
-
In the cover_art_сache_directory parameter, the album cover will be saved along the specified path. The file will be saved with a unique name. It is desirable that when you change the track, the album cover also changes. This is why we will use the Camera component to display the album cover in the Home Assistant interface.
-
In configuration.yaml file, specify:
camera:
- platform: local_file
file_path: /config/www/album_art.jpg
scan_interval: 20
The scan interval is specified in seconds. You can set your own value.
-
Next, we need to move the cover file to the /config/www/ folder every time you change a track. It is important that the cover always matches the track being played and the files always have the same name album_art.jpg (so that the camera component always accesses the same file). To do this, we will use the inotify component and a small script to move and rename cover files.
-
Inotify is a convenient component that tracks changes in folders and files. To install it:
sudo apt install inotify-tools
-
Next, create a script file with any name, for example metadata.sh with the following content:
#!/bin/bash
SRC_DIR="/tmp/shairport-sync/.cache/coverart"
DST_DIR="/xxxx/xxxx/hass_config/www" # specify the exact path in your case
# Function that will perform the necessary actions
# In our case copy to another directory
make_action(){
# Getting the destination directory
DIR_TO_COPY_TO=${1/${SRC_DIR}/${DST_DIR}}
# Creating it if it doesn't already exist
mkdir -p $DIR_TO_COPY_TO
# Copying the file
cp $1$2 $DIR_TO_COPY_TO
mv $1$2 /xxxx/xxxx/hass_config/www/album_art.jpg # specify the exact path in your case
}
IFS='
'
# Tracking the creation of new files
# Getting the output in the format we need
inotifywait -e create --format '%w %f' -m $SRC_DIR |\
(
while read
do
# Getting the directory name
DIR=$(echo $REPLY | cut -f 1 -d' ')
# Getting the file name
FILE=$(echo $REPLY | cut -f 2 -d' ')
# Passing the directory and file names to the function
make_action $DIR $FILE
done
)
-
After saving the file, make it executable and add it to autorun so that it starts every time the system starts.
-
To avoid displaying the camera component name specify in the Lovelace interface:
cards:
- type: picture-entity
entity: camera.local_file
show_state: false
name: " "
Now you will see which track / album is playing. You can even display this information on any additional display.
MQTT control
You can also control music playback via MQTT.
-
Install MQTT broker in your system.
-
Add to configuration.yaml file:
mqtt:
broker: 192.168.1.x # specify your own host
- Edit the
/etc/shairport-sync.conf
file
In the mqtt group, uncomment mqtt and specify parameters:
mqtt =
{
enabled = "yes";
hostname = "192.168.1.x";
topic = "your/topic";
publish_parsed = "yes";
enable_remote = "yes";
}
Available commands are “command”, “beginff”, “beginrew”, “mutetoggle”, “nextitem”, “previtem”, “pause”, “playpause”, “play”, “stop”, “playresume”, “shuffle_songs”, “volumedown”, “volumeup”
Now you can control music playback via Home Assistant using MQTT commands.
Homekit
Now you can manage your speakers via the Homekit component
-
You can add your snapcast clients to the Homekit as MQTT lights. In the Homekit interface, define the room where the speaker is located and give it the name “Speaker” (or specify your own).
-
Add to configuration.yaml file:
light:
- platform: mqtt
name: "Player1" # specify your own name
state_topic: "player1/state"
command_topic: "player1/switch"
brightness_state_topic: "player1/brightness"
brightness_command_topic: "player1/set"
optimistic: true
- Next, you need to create automation so that when you change the brightness value of MQTT light, the volume of the snapcast client also changes.
Add to automations.yaml file:
- id: 'Player1_volume'
alias: Vol_1_1
description: ''
trigger:
- above: '0'
below: '25'
entity_id: light.player1
platform: numeric_state
value_template: "{% if states.light.player1 %} \n {{ state_attr(\"light.player1\"\
,'brightness') }}\n{% else %}\n??\n{% endif %}"
condition: []
action:
- data:
is_volume_muted: false
entity_id: media_player.snapcast_client_74_de_2b_6a_fd_79 # specify your own snapclient
service: media_player.volume_mute
- data:
volume_level: 0.1
entity_id: media_player.snapcast_client_74_de_2b_6a_fd_79 # specify your own snapclient
service: media_player.volume_set
The brightness parameter of MQTT light is set from 0 to 255. Repeat this automation by changing the values above
, below
and volume_level
. You can set the desired step. In my case, I divided this scale into 10 steps for brightness (0-25, 25-50, 50-75, 75-100, 100-125, 125-150, 150-175, 175-200, 200-225, 225-255) and for volume level (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1).
- After creating all the automations, you will get voice control of your speakers. You can adjust the volume in each room. You can also add additional music playback controls to the Homekit via MQTT commands.
Presence detection multiroom
You can install presence or motion sensors in different rooms of the house and make automation so that the music moves with your movement around the house. I haven’t done this yet, but I think it would be interesting.
If someone is implementing something like this, please share exactly how you did it.
Please share your ideas if anyone knows how to improve this configuration.