I am unable to re-create a dbus-send
command in Python. dbus-send
does the expected, running:
#!/bin/bash
dbus-send \
--type=method_call \
--dest=org.mpris.MediaPlayer2.spotify \
/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.OpenUri \
string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO
When re-creating in Python, the oddest thing happens - it half works. It changes the Spotify player to the playlist but doesn't play the first song (and sort of breaks the player). This also happens when manually running the call from the QDbusViewer debug application.
In a minimal python example, that looks like:
#!/usr/bin/python3
import dbus
session_bus = dbus.SessionBus()
spotify = session_bus.get_object("org.mpris.MediaPlayer2.spotify", "/org/mpris/MediaPlayer2")
spotify_iface = dbus.Interface(spotify, dbus_interface='org.mpris.MediaPlayer2.Player')
spotify_iface.OpenUri("string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO")
PlayPause()
seem to work as expectedThis is super strange to me because I would have thought the messages sent to DBus would have been identical. Does anyone know where the discrepancy might come from?
Solved!
The issue came down to a small syntax error:
dbus-send [...] string:spotify:playlist:74sUjcvpGfdOvCHvgzNEDO
That prefix string
is required in dbus-send so it knows the data type of the argument.
In Python however, it's adding taking care of that for us using some reflection on the dbus provider:
spotify.OpenUri('spotify:playlist:74sUjcvpGfdOvCHvgzNEDO')
We can drop the leading string:
. Darn!
I setup my own listener by using https://github.com/LEW21/pydbus/blob/master/doc/tutorial.rst#exporting-own-objects to inspect what data was getting sent back.