from PySide import QtCore
from neoman.device import open_first_device


class YubiKeyNeo(object):

    def __init__(self, device):
        self._dev = device
        self._name = "YubiKey %s" % device.serial
        self._mode = device.mode
        self._serial = device.serial
        self._version = device.version

    def _set_device(self, device):
        assert self.serial == device.serial
        assert self.version == device.version
        self._dev = device
        self._mode = device.mode

    @property
    def device(self):
        return self._dev

    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, new_name):
        self._name = new_name

    @property
    def mode(self):
        return self._mode

    @property
    def serial(self):
        return self._serial

    @property
    def version(self):
        return self._version

    def __str__(self):
        return "%s[%d]" % (self.name, self.mode)


class AvailableNeos(QtCore.QObject):
    changed = QtCore.Signal(list)

    def __init__(self):
        super(AvailableNeos, self).__init__()

        self._neos = []

        self.discover_devices()
        self.startTimer(1000)

    @property
    def neos(self):
        return self._neos

    def discover_devices(self):
        single = open_first_device()
        discovered = [single] if single else []

        attached = []
        new_neos = []
        dead_neos = []

        for neo in self._neos:
            del neo._dev

            for dev in [dev for dev in discovered if dev not in attached]:
                if dev.serial == neo.serial:
                    neo._dev = dev
                    attached.append(dev)
                    break
            else:
                dead_neos.append(neo)

        new_neos = []
        for dev in discovered:
            if dev not in attached:
                new_neos.append(YubiKeyNeo(dev))

        if len(dead_neos) != 0 or len(new_neos) != 0:
            self._neos = [neo for neo in self._neos if neo not in dead_neos] \
                + new_neos
            self.changed.emit(self._neos)

    def timerEvent(self, event):
        self.discover_devices()

@@ -1124,33 +1124,32 @@
 self._neos = %5B%5D%0A
-%0A
         self.dis
@@ -1280,32 +1280,123 @@
 _devices(self):%0A
+        for neo in self._neos:%0A            del neo._dev%0A        dead_neos = self._neos%5B:%5D%0A%0A
         single =
@@ -1477,67 +1477,21 @@
     
-attached = %5B%5D%0A        new_neos = %5B%5D%0A        dead_neos = %5B%5D%0A
+new_neos = %5B%5D
 %0A   
@@ -1503,126 +1503,60 @@
 for 
-neo
+dev
  in 
-self._neos:%0A            del neo._dev%0A%0A            for dev in %5Bdev for dev in discovered if dev not in attached%5D
+discovered:%0A            for neo in self._neos
 :%0A  
@@ -1637,49 +1637,8 @@
 dev%0A
-                    attached.append(dev)%0A
     
@@ -1689,28 +1689,27 @@
             
-dead
+new
 _neos.append
@@ -1709,19 +1709,31 @@
 .append(
-neo
+YubiKeyNeo(dev)
 )%0A%0A     
@@ -1727,35 +1727,36 @@
 (dev))%0A%0A        
-new
+dead
 _neos = %5B%5D%0A     
@@ -1752,179 +1752,89 @@
  = %5B
-%5D%0A       
+x
  for 
-dev
+x
  in 
-discovered:%0A            if dev not in attached:%0A                new_neos.append(YubiKeyNeo(dev))%0A%0A        if len(dead_neos) != 0 or len(new_neos) != 0
+self._neos if not hasattr(x, '_dev')%5D%0A        if new_neos or dead_neos
 :%0A  
@@ -1861,19 +1861,15 @@
  = %5B
-neo
+x
  for 
-neo
+x
  in 
@@ -1882,19 +1882,17 @@
 neos if 
-neo
+x
  not in 

Fix mode switching.