Control your Home Assistant media players from the desktop! (*nix only, requires dbus)

Hi everyone,

I got tired of opening my homeassistant homepage to change the current song or to see what is currently playing, so I created a small python project to bridge the media player entities with the desktop.

Each active media player instance is exposed to the desktop using the MPRIS interface. MPRIS is widely implemented way to control your media players on Linux desktops (tested only on KDE Plasma for now), so I hope some of you will find this also useful!

Check out more details at https://github.com/rytilahti/homeassistant-mpris-bridge (including a short-ish video on how it functions) and please let me know what you think!

1 Like

Excellent. And here I was expecting (from the subject line) another hack for windows users…

Oopsie, I updated the title to contain *nix and dbus, afaik there is not such common media player interface for windows at all :slight_smile:

I know, let’s stick to proper OSes.

I just gave it a try - but the instructions are a little unclear to me. (first time I ever heard of poetry)
When I follow the instructions hassbridge ll be installed into a venv. I can reatch it with poetry shell

[underknowledge@rizen-fed homeassistant-mpris-bridge]$ poetry shell
Spawning shell within /home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7
. /home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/bin/activate
[underknowledge@rizen-fed homeassistant-mpris-bridge]$ . /home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/bin/activate
(python-mpris-hassbridge-e0CMv6Nb-py3.7) [underknowledge@rizen-fed homeassistant-mpris-bridge]$ export HASSBRIDGE_TOKEN="eyJ[...]AT3vnL8"
(python-mpris-hassbridge-e0CMv6Nb-py3.7) [underknowledge@rizen-fed homeassistant-mpris-bridge]$ export HASSBRIDGE_ENDPOINT="http://10.0.1.1:8123"
(python-mpris-hassbridge-e0CMv6Nb-py3.7) [underknowledge@rizen-fed homeassistant-mpris-bridge]$ hassbridge
Traceback (most recent call last):
  File "/home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/bin/hassbridge", line 11, in <module>
    load_entry_point('python-mpris-hassbridge', 'console_scripts', 'hassbridge')()
  File "/home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/lib64/python3.7/site-packages/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/lib64/python3.7/site-packages/pkg_resources/__init__.py", line 2852, in load_entry_point
    return ep.load()
  File "/home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/lib64/python3.7/site-packages/pkg_resources/__init__.py", line 2443, in load
    return self.resolve()
  File "/home/underknowledge/.cache/pypoetry/virtualenvs/python-mpris-hassbridge-e0CMv6Nb-py3.7/lib64/python3.7/site-packages/pkg_resources/__init__.py", line 2449, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/home/underknowledge/homeassistant-mpris-bridge/hassbridge/cli.py", line 5, in <module>
    import asyncclick as click
ModuleNotFoundError: No module named 'asyncclick'

but good thing is - gnome/fedora defenitly got the MPRIS interface.

Hi, sorry, I noticed myself also that I declared the dependencies incorrectly, now it should work again for everyone who didn’t have asyncclick installed before like I did. You can also simply execute pip install asyncclick in your poetry shell to install the missing module.

About poetry, it is just a way to manage python packages (and their dependencies), poetry install creates a virtual environment (much like virtualenv venv and installs the dependencies much like pip install -r requirements.txt or pip install <directory> do in some python packages).

:woman_facepalming: aah I would have known that one. Just seen that you also included dbus_next but i had to install it with pip again. (layer 8?) I see the beauty in this poetry.
Well here my next clumsy try.
https://pastebin.com/J5dTY11Z
I might have issues running it because of my offline media players.(?)
The fire stick gets for example his power from the TV who is connected to a smart plug.

Ah, dbus-next was indeed missing, I added that, thanks for testing! (about “layer8”, poetry creates a new virtualenv during install, so that’s why you had to install it separately inside poetry shell)

Now to the next part, for some reason the media_duration as reported by homeassitant does not seem to be an integer as expected. I have no idea why that would happen, but I modified the source to cast the received value to an integer (and created a PR to dbus-next to be more informative on errors: https://github.com/altdesktop/python-dbus-next/pull/46). It may very well be that for some reason homeassistant is reporting something completely else than an integer, so I also added a debug printout (if you run with -d you will get debug logs) to print out the value of that attribute. It would be great if you could test and report back what value is causing the problem.

The offline players should not be a problem. During the startup only the players which are online are being added to the list of controllable devices, and new players are created as they start playing.

(btw, you are exposing your token in that pastebin :slight_smile:)

Hi! I’ve published — as a HACS addon — an alternative to this project that does not require hassbridge, setting up system services, or configuration files… and is pretty straightforward to setup.

Check it out here: Control your Linux media players from Home Assistant!