Creating an addon that plays sound

Hello,

I’m very new to writing addons for home assistant and am running into some difficulties.
There is bound to be a lot I don’t understand, any help would be great.

Long term goal

Setup my raspberry pi 4 as both a hassio server and a bluetooth speaker so I can connect my phone to the raspberry pi 4 via the built in bluetooth and play music from my phone to a set of connected speakers on the raspberry pi.

I figured the way to get both of these working on the one device is to get the bluetooth speaker part setup as a hassio plugin as I already have hassio setup.

Short term goal

Play local audio generated from within a hassio addon to speakers connected to the raspberry pi 4 running hassio.
To do this, I’m starting with gTTS to generate a sound clip saying “hello”.

What I’ve got so far (for the short term goal)

I started by following the addon tutorial with a few minor additions, the main one being to add the "audio": true config which I read from here.

I ended up with my addon installed and running, here are the 3 files that make it up:

Dockerfile

ARG BUILD_FROM
FROM $BUILD_FROM

ENV LANG C.UTF-8

# Copy data for add-on
COPY run.sh /

# Install requirements for add-on
RUN apk add --no-cache python3 mpg123 alsa-plugins-pulse
RUN pip3 install gTTS

WORKDIR /data

RUN chmod a+x /run.sh

CMD [ "/run.sh" ]

config.json

{
  "name": "Hello world",
  "version": "12",
  "slug": "hello_world",
  "description": "Says hello to the world, literally.",
  "panel_icon": "mdi:bluetooth",
  "arch": ["armhf", "armv7", "aarch64", "amd64", "i386"],
  "startup": "before",
  "boot": "auto",
  "options": {},
  "schema": {},
  "audio": true
}

run.sh

#!/usr/bin/with-contenv bashio

echo "Saying hello"
gtts-cli "Hello" | mpg123 -
echo "All done"

The result

The script executes ok but does not play any sound.
Here is the output from the log tab of the addon page:

[s6-init] making user provided files available at /var/run/s6/etc...exited 0.
[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] done.
[services.d] starting services
[services.d] done.
Saying hello
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
	version 1.25.13; written and copyright by Michael Hipp and others
	free software (LGPL) without any warranty but with best wishes
Playing MPEG stream 1 of 1: - ...
MPEG 2.0 L III cbr32 24000 mono
[0:03] Decoding of - finished.
All done
[cmd] /run.sh exited 0
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal.

Hi @occhiso
I hate to disturb the peace of the deceased community posts, but did you have any success with this project? I’m looking to do the same. :slight_smile:

Nope, sadly. I gave up :pensive:
Hope you have better luck than me!

1 Like