Determine when monitor is turned on or off via python dbus

The method needs to be a separate argument in callDBus e.g

callDBus("org.kde.krunner", "/org/kde/krunner", "org.kde.KDBusService", "CommandLine", args, "", {});

But that doesn’t work from kwin for some reason, and from python-dbus it just opens krunner with the command in the input but doesn’t actually run it until you press enter.

Also freedesktop notifications fails string "No such method 'Notify' in interface 'org.freedesktop.Notifications' at object path '/org/freedesktop/Notifications' (signature 'sisssava{sv}i')" but works fine from python?? I was calling it like this:

callDBus("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "Notify", "App", replace_id, icon, heading, content, [], hints, 3000);

What you could do instead is have a dbus service that runs any command (possibly unsafe??) when you call it or have it hardcoded in a method:

"""dbus run command example service
"""

import subprocess
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib


class Service(dbus.service.Object):
    def __init__(self):
        self._loop = GLib.MainLoop()

    def run(self):
        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
        bus_name = dbus.service.BusName("com.example.runcommand", dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, "/com/example/runcommand")

        print("Service running...")
        self._loop.run()
        print("Service stopped")

    @dbus.service.method("com.example.runcommand", in_signature="s", out_signature="i")
    def run_command(self, m):
        print(f"Running command '{m}'")
        command = m.split()
        result = subprocess.run(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
            check=True,
        )

        print(f"exit code: '{result.returncode}'")
        print(f"stdout: '{result.stdout.strip()}'")
        return result.returncode

    @dbus.service.method("com.example.runcommand", in_signature="", out_signature="i")
    def notify_send(self):
        command = ["notify-send", "Hello", "World"]
        command_str = " ".join(command)
        print(f"Running command '{command_str}'")
        result = subprocess.run(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
            text=True,
            check=True,
        )

        print(f"exit code: '{result.returncode}'")
        print(f"stdout: '{result.stdout.strip()}'")
        return result.returncode

    @dbus.service.method("com.example.runcommand", in_signature="", out_signature="")
    def quit(self):
        print("Shutting down")
        self._loop.quit()


if __name__ == "__main__":
    Service().run()

Then from KWin call it like this:

callDBus("com.example.runcommand", "/com/example/runcommand", "com.example.runcommand", "run_command", "notify-send Hello World");