MIDI Add-On

Hello everyone,

Hope someone can help me. I’m currently trying to use a python-application I wrote to run as an add-on for Home Assistant.
Sadly, I can’t get MIDI to work for the add-on. I’m using the python-rtmidi library.
I enabled full access on the hardware for the add-on and the library is imported correctly, but as soon as I try to open a MIDI port with python the add-on crashes.

Heres the full console output:

[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.
Python app started
Collecting wheel
  Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Collecting python-osc
  Downloading python_osc-1.8.0-py3-none-any.whl (32 kB)
Collecting python-rtmidi
  Downloading python-rtmidi-1.4.9.tar.gz (251 kB)
Using legacy 'setup.py install' for python-rtmidi, since package 'wheel' is not installed.
Installing collected packages: wheel, python-rtmidi, python-osc
    Running setup.py install for python-rtmidi: started
    Running setup.py install for python-rtmidi: still running...
    Running setup.py install for python-rtmidi: finished with status 'done'
Successfully installed python-osc-1.8.0 python-rtmidi-1.4.9 wheel-0.37.1
Installed packages
ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Operation not permitted
Traceback (most recent call last):
  File "src/_rtmidi.pyx", line 815, in rtmidi._rtmidi.MidiIn.__cinit__
RuntimeError: MidiInAlsa::initialize: error creating ALSA sequencer client object.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "//app/main.py", line 43, in <module>
    midi_client = MidiHandler(ls_bridge)
  File "/app/MidiHandler.py", line 15, in __init__
    self.connect_midi_console()
  File "/app/MidiHandler.py", line 22, in connect_midi_console
    self.midi_in_port, port_name = open_midiinput(1)
  File "/usr/lib/python3.9/site-packages/rtmidi/midiutil.py", line 270, in open_midiinput
    return open_midiport(port, "input", api, use_virtual, interactive,
  File "/usr/lib/python3.9/site-packages/rtmidi/midiutil.py", line 201, in open_midiport
    midiobj = midiclass_(api, name=client_name)
  File "src/_rtmidi.pyx", line 817, in rtmidi._rtmidi.MidiIn.__cinit__
rtmidi._rtmidi.SystemError: MidiInAlsa::initialize: error creating ALSA sequencer client object.
[cmd] /run.sh exited 1
[cont-finish.d] executing container finish scripts...
[cont-finish.d] done.
[s6-finish] waiting for services.
[s6-finish] sending all processes the TERM signal.

Here’s my Dockerfile:
(There are so many alpine requirements because the python-rtmidi library must compile the c++ code first)

ARG BUILD_FROM
FROM $BUILD_FROM

# Install requirements for add-on
RUN \
  apk add --no-cache \
    python3 \
	py3-pip \
	libc-dev \
	python3-dev \
	gcc \
	g++ \
	alsa-lib-dev \
	jack-dev

# Python 3 HTTP Server serves the current working dir
# So let's set it to our add-on persistent data directory.
WORKDIR /

# Copy data for add-on
COPY run.sh /
COPY app /app
RUN chmod a+x /run.sh

CMD [ "/run.sh" ]

And the config.yaml:

name: "LightShark Bridge"
description: "This python-project enables to control the Work LightShark DMX-Console via HTTP and MIDI"
version: "1.1.0"
slug: "ls_bridge"
arch:
  - aarch64
  - amd64
  - armhf
  - armv7
  - i386
startup: before
ports:
  8000/tcp: 8000
full_access: true

the run.sh just installs the python libraries and executes the python application.

The code for the python_application can be found in github

check if your Midi port is blocked or the .asoundrc is corrupted by running amidi -l in the consol. If something is broken it should look lie this