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):
19 loop = GObject.MainLoop()
24 proxy.connect_to_signal(signal, callback)
29 parser = argparse.ArgumentParser(parents=[onoff.dbusutils.dbus_options])
30 parser.add_argument("--duration", type=int, default=10,
31 help="how long to activate the device in seconds " +
32 "(default: %(default)d")
33 parser.add_argument("command", nargs=argparse.REMAINDER,
34 help="a command to be executed with the device being" +
35 "activated for the duration of the execution")
36 args = parser.parse_args()
37 DBusGMainLoop(set_as_default=True)
38 proxy = onoff.dbusutils.get_dbus_proxy(args)
40 st, fd = proxy.activatefd(args.duration)
45 print("state is %d waiting for signal" % st)
46 st = wait_for_signal(proxy, "changestate")
47 print("new state is %d" % st)
48 os.execvp(args.command[0], args.command)
50 st = proxy.activatetime(args.duration)
52 print("state is %d waiting for signal" % st)
53 st = wait_for_signal(proxy, "changestate")
54 print("new state is %d" % st)
56 if __name__ == "__main__":