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 OnoffState
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 if st != OnoffState.active:
50 print("state is %s waiting for signal" % st.name)
51 wait_for_signal(proxy, "changestate", OnoffState.active.value)
52 print("new state is actived")
53 os.execvp(args.command[0], args.command)
55 proxy = onoff.dbusutils.get_dbus_proxy(args)
56 st = OnoffState(proxy.activatetime(args.duration))
57 if st != OnoffState.active:
58 print("state is %s waiting for signal" % st.name)
59 wait_for_signal(proxy, "changestate", OnoffState.active.value)
60 print("new state is active")
62 if __name__ == "__main__":