3 A simpe client for an onoff device. If no command is given, the device is
4 temporarily activated. When a command is given, the device is activated, then
5 the command is run. Some time after the command finishes, the device is
12 from dbus.mainloop.glib import DBusGMainLoop
13 from gi.repository import GObject
15 from onoff.common import ST_ACTIVE
16 import onoff.dbusutils
18 def wait_for_signal(proxy, signal, desired_state):
19 loop = GObject.MainLoop()
21 if st == desired_state:
23 proxy.connect_to_signal(signal, callback)
27 parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
28 parser.add_argument("--duration", type=int, default=10,
29 help="how long to activate the device in seconds " +
30 "(default: %(default)d")
31 parser.add_argument("command", nargs=argparse.REMAINDER,
32 help="a command to be executed with the device being" +
33 "activated for the duration of the execution")
34 parser.add_argument("--list", action="store_true",
35 help="list available devices and exit")
36 args = parser.parse_args()
37 DBusGMainLoop(set_as_default=True)
39 bus = onoff.dbusutils.get_dbus(args)
40 for elem in onoff.dbusutils.list_objects(bus, args.busname):
43 proxy = onoff.dbusutils.get_dbus_proxy(args)
44 st, fd = proxy.activatefd()
49 print("state is %d waiting for signal" % st)
50 wait_for_signal(proxy, "changestate", ST_ACTIVE)
51 print("new state is actived")
52 os.execvp(args.command[0], args.command)
54 proxy = onoff.dbusutils.get_dbus_proxy(args)
55 st = proxy.activatetime(args.duration)
57 print("state is %d waiting for signal" % st)
58 wait_for_signal(proxy, "changestate", ST_ACTIVE)
59 print("new state is active")
61 if __name__ == "__main__":