@JurajNyiri There is probably a smarter way of doing this, but I ended up installing an icecast server alongside Home Assistant and I have a few scripts to control that.
start_stream.sh
#!/bin/sh
URL=$1
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
PID=$SCRIPTPATH/pid
if [ -z "$URL" ]
then
exit
else
/usr/bin/ffmpeg -re -i "$URL" -loglevel error -f webm -content_type video/webm -r 30 -g 90 -s 1280x720 -quality realtime -speed 5 -threads 8 -row-mt 1 -tile-columns 2 -frame-parallel 1 -qmin 4 -qmax 48 -b:v 3000k icecast://source:username@icecast_servier_ip:port/live & echo $! > $PID &
fi
sleep 10
$SCRIPTPATH/ice_status.sh
stop_stream.sh
#!/bin/sh
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
PID=$(cat $SCRIPTPATH/pid)
kill -9 $PID
rm pid
sleep 5
$SCRIPTPATH/ice_status.sh
ice_status.sh
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
DATA=$(curl -s --user admin:password http://icecast_server_ip:port/admin/stats.xsl)
#echo $DATA
listeners=$(echo $DATA | tr -d \\r | sed 's/.*>listeners<\/td> <td>\([[:digit:]]\).*/\1/')
sources=$(echo $DATA | tr -d \\r | sed 's/.*sources<\/td> <td>\([[:digit:]]\).*/\1/')
echo "{\"listeners\":\"$listeners\",\"sources\":\"$sources\"}" > $SCRIPTPATH/icecast_status.json
I defined those scripts as shell_commands in HA:
shell_command:
start_icestream: '/config/custom_components/ice/start_stream.sh {{states.input_text.streaming_url.state}}'
stop_icestream: '/config/custom_components/ice/stop_stream.sh'
status_icestream: '/config/custom_components/ice/ice_status.sh'
I defined a input_text called streaming_url which means I can use any URL in the HA dashboard. This could be hardcoded if you wanted, but I wanted to be able to change the URL.
Then used this example here (Chromecast Radio with station and player selection) to cast to radio. I have a hard coded URL for the icecast server, so the final part of the data_template has:
media_content_id: 'http://icecast_server_ip:port/live'
media_content_type: 'audio/mpeg'
It is working well for me.