Getting UPower events through gjs's DBusProxy

I’m working on a small gnome extension that will perform an action whenever a laptop’s AC adapter is plugged in or unplugged.

To do so in javascript, I’m using a Gio DBusProxy for the org.freedesktop.UPower interface. However, the properties of the proxy are never filled (they are null), and as soon as I call a method on the proxy, I’m getting the following error:

ERROR: DBus: Error org.freedesktop.DBus.Error.ServiceUnknown - The name org.freedesktop.UPower was not provided by any .service files

I can successfully create a proxy for another interface,org.gnome.SettingsDaemon.Power's Screen, using the same approach. The proxy’s Brightness property then has a valid non-null value that I can access. (That one is on the session bus, while the UPower one is on the system bus, I don’t know whether that matters.) This leads me to believe that the general approach is OK.

I can successfully monitor the AC adapter being unplugged / plugged in from the commandline by running gdbus monitor --system --dest org.freedesktop.UPower --object-path /org/freedesktop/UPower

What could be the issue with the UPower proxy?

I’m on Ubuntu 21.10.

Well, it would probably help if you showed us some code :slightly_smiling_face:

Something like this should get you a working proxy:

function getPowerProxy() {
    return new Promise((resolve, reject) => {
            (proxy_, result) => {
                try {
                } catch (e) {

You can watch for property changes with the g-properties-changed signal and get properties with Gio.DBusProxy.get_cached_property().

This is basically what the proxy wrapper code does, just without the automagic property getters/setters. If you’d like help with that, you’ll have to show some code.

Thank you for the example. I’ll give that a try.

The (working) code for the Screen interface, using makeProxyWrapper, is as follows:

const ScreenInterface = `<node>
    <interface name="org.gnome.SettingsDaemon.Power.Screen">
        <property type="i" name="Brightness" access="readwrite">
const ScreenProxyClass = Gio.DBusProxy.makeProxyWrapper(ScreenInterface);
let screenProxy = new ScreenProxyClass(
log('Brightness: ' + screenProxy.Brightness + '%')


Brightness: 28%

So this proxy works as expected.

The code for the UPower interface, using the same approach:

const DBusUPowerInterface = `<node>
    <interface name="org.freedesktop.UPower">
        <method name="EnumerateDevices">
            <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
            <arg type="ao" name="devices" direction="out"/>
        <method name="GetDisplayDevice">
            <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
            <arg type="o" name="device" direction="out"/>
        <method name="GetCriticalAction">
            <annotation name="org.freedesktop.DBus.GLib.Async" value=""/>
            <arg type="s" name="action" direction="out"/>
        <signal name="DeviceAdded">
            <arg type="o" name="device"/>
        <signal name="DeviceRemoved">
            <arg type="o" name="device"/>
        <property type="s" name="DaemonVersion" access="read"/>
        <property type="b" name="OnBattery" access="read"/>
        <property type="b" name="LidIsClosed" access="read"/>
        <property type="b" name="LidIsPresent" access="read"/>
const DBusUPowerProxyClass = Gio.DBusProxy.makeProxyWrapper(
let uPowerProxy = new DBusUPowerProxyClass(
log('OnBattery: ' + uPowerProxy.OnBattery)
uPowerProxy.EnumerateDevicesSync(); // throws


OnBattery: null
JS ERROR: Extension Gio.DBusError: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.UPower was not provided by any .service files

Here you are passing the session bus singleton, instead of the system bus. You should pass Gio.DBus.system here instead and that should work.

That solved it, thank you!

Helped me understand the meaning of that parameter as well.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.