Trying to get Plasma to log out via DBus but from Python

Wasn’t sure if Help or Development is a better fit, feel free to move the thread.

I posted on StackOverflow but since many of the KDE devs will be familiar with how the KDE DBus interface works, maybe someone here can help me.

Essentially, I am trying to achieve the same outcome as the terminal command:

qdbus6 org.kde.Shutdown /Shutdown org.kde.Shutdown.logout

from Python. Given that Plasma is all built on Qt, I should have thought this is also possible using QtDBus from PySide6 as well as C++.

As such, I have a simple test script which looks like:

from PySide6.QtDBus import QDBusConnection, QDBusMessage

connection = QDBusConnection.sessionBus()
message = QDBusMessage.createTargetedSignal("org.kde.Shutdown", "/Shutdown", "org.kde.Shutdown", "logout")

If this code is run as a script, or entered into a Python interpreter, it returns True, but returns False if the script is run as root using sudo, which kind of implies to me that org.kde.Shutdown is available via the connection made with sessionBus() i.e. the connection is to the user session bus.

However, Plasma doesn’t log out when I run script, unlike when I enter it as a terminal command. Does anyone have any idea why?

Ok I got an answer at StackOverflow. The issue was using a Signal rather than a Method Call, and me not being aware of the existence of the latter.