use the enum module to represent states
[~helmut/onoff.git] / onoff / dbusutils.py
index c248ed5..31b51cf 100644 (file)
@@ -7,6 +7,8 @@ import dbus
 import dbus.service
 from gi.repository import GObject
 
+from .gobject import ScheduledFunction
+
 logger = logging.getLogger("onoff.dbusutils")
 
 object_prefix = "/de/subdivi/onoff0"
@@ -101,28 +103,32 @@ class OnoffControl(dbus.service.Object):
         dbus.service.Object.__init__(self, busname, "%s/%s" % (self.path, name))
         self.device = device
         self.usecount = 0
-        device.notify.add(self.changestate)
+        device.notify.add(self._changestate_notifier)
+
+    def _changestate_notifier(self, state):
+        logger.debug("emitting state %s", state.name)
+        self.changestate(state.value)
 
     @dbus.service.signal(domain, signature="q")
     def changestate(self, state):
-        logger.debug("emitting state %d", state)
+        pass
 
     @dbus.service.method(domain, out_signature="q")
     def state(self):
-        return self.device.state
+        return self.device.state.value
 
     @dbus.service.method(domain, in_signature="q", out_signature="q")
     def activatetime(self, duration):
         """Activate the device for a given number of seconds."""
         logger.info("activatetime %d", duration)
-        GObject.timeout_add(duration * 1000, self.unuse)
+        ScheduledFunction(duration, self.unuse)
         return self.use()
 
     @dbus.service.method(domain, in_signature="", out_signature="qh")
     def activatefd(self):
         """Activate a device until the returned file descriptor is closed."""
-        logger.info("activatefd")
         notifyfd, retfd = socketpair()
+        logger.info("activatefd returning fd %d", notifyfd.fileno())
         def callback(fd, _):
             logger.info("fd %d completed", fd.fileno())
             fd.close()
@@ -135,7 +141,7 @@ class OnoffControl(dbus.service.Object):
         self.usecount += 1
         if self.usecount <= 1:
             self.device.activate()
-        return self.device.state
+        return self.device.state.value
 
     def unuse(self):
         self.usecount -= 1