0

How to monitor and read Ubuntu's "Night Light" status via D-Bus using Python with dasbus? I can't figure out the API docs on how to read a property or subscribe to a signal.
Likely candidates:

The following is adapted from the basic examples and prints the interfaces and properties/signals of the object:

#!/usr/bin/env python3

from dasbus.connection import SessionMessageBus
bus = SessionMessageBus()

# dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
    "org.gnome.SettingsDaemon.Color",  # bus name
    "/org/gnome/SettingsDaemon/Color",  # object path
)

print(proxy.Introspect())

# read and print properties "NightLightActive" and "Temperature" from interface "org.gnome.SettingsDaemon.Color" in (callback) function

# subscribe to signal "PropertiesChanged" in interface "org.freedesktop.DBus.Properties" / register callback function

Resources
handle
  • 5,859
  • 3
  • 54
  • 82

1 Answers1

1

Looking at the dasbus examples and the Introspection data it looks like to get the property the dasbus is pythonic so proxy.<property name> works. For your example of NightLightActive it would be:

print("Night light active?", proxy.NightLightActive)

For the signal you need to connect to the signal on the proxy so that seems to take the form of proxy.<signal name>.connect so for example:

proxy.PropertiesChanged.connect(callback)

And this will need to have an EventLoop running.

My entire test was:

from dasbus.connection import SessionMessageBus
from dasbus.loop import EventLoop

bus = SessionMessageBus()
loop = EventLoop()

# dasbus.client.proxy.ObjectProxy
proxy = bus.get_proxy(
    "org.gnome.SettingsDaemon.Color",  # bus name
    "/org/gnome/SettingsDaemon/Color",  # object path
)

print("Night light active?", proxy.NightLightActive)
print("Temperature is set to:", proxy.Temperature)


def callback(iface, prop_changed, prop_invalidated):
    print("The notification:",
          iface, prop_changed, prop_invalidated)


proxy.PropertiesChanged.connect(callback)

loop.run()

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • The fade to a different Color Temperature emits many signals with `prop_changed` = `{'Temperature': GLib.Variant('u', 6472)}` for individual steps. – handle Feb 08 '23 at 11:34
  • Get the value with `prop_changed['Temperature'].get_uint32()` – handle Feb 08 '23 at 11:48
  • 1
    Better use [`unpack()`](https://github.com/GNOME/pygobject/blob/master/gi/overrides/GLib.py#L237), it returns native Python type. – handle Feb 08 '23 at 11:56