@@ -147,21 +147,21 @@ def __init__(self, scan_interval, mac=None):
147147 self .scan_interval = scan_interval
148148 self .last_scan = - 1
149149
150-
151150 def _parse_serial_number (self , manufacturer_data ):
152151 try :
153- #print(manufacturer_data)
154152 (ID , SN , _ ) = struct .unpack ("<HLH" , manufacturer_data )
155153 except Exception as e : # Return None for non-Airthings devices
156154 return None
157155 else : # Executes only if try-block succeeds
158156 if ID == 0x0334 :
159157 return SN
160158
161- def find_devices (self , scanns = 10 ):
159+ def find_devices (self , scans = 50 , timeout = 0.1 ):
160+ # Search for devices, scan for BLE devices scans times for timeout seconds
161+ # Get manufacturer data and try to match match it to airthings ID.
162162 scanner = btle .Scanner ()
163- for _count in range (scanns ):
164- advertisements = scanner .scan (0.1 )
163+ for _count in range (scans ):
164+ advertisements = scanner .scan (timeout )
165165 for adv in advertisements :
166166 sn = self ._parse_serial_number (adv .getValue (btle .ScanEntry .MANUFACTURER ))
167167 if sn is not None :
@@ -171,9 +171,9 @@ def find_devices(self, scanns=10):
171171 _LOGGER .debug ("Found {} airthings devices" .format (len (self .airthing_devices )))
172172 return len (self .airthing_devices )
173173
174- def connect (self , mac , retries = 1 ):
174+ def connect (self , mac , retries = 10 ):
175175 tries = 0
176- self ._dev = None
176+ self .disconnect ()
177177 while (tries < retries ):
178178 tries += 1
179179 try :
@@ -183,7 +183,7 @@ def connect(self, mac, retries=1):
183183 if tries == retries :
184184 pass
185185 else :
186- print ("Retrying" )
186+ _LOGGER . debug ("Retrying {}" . format ( mac ) )
187187
188188 def disconnect (self ):
189189 if self ._dev is not None :
@@ -193,7 +193,6 @@ def disconnect(self):
193193 def get_info (self ):
194194 # Try to get some info from the discovered airthings devices
195195 self .devices = {}
196-
197196 for mac in self .airthing_devices :
198197 self .connect (mac )
199198 if self ._dev is not None :
@@ -229,9 +228,9 @@ def get_sensor_data(self):
229228 self .connect (mac )
230229 if self ._dev is not None :
231230 for characteristic in characteristics :
232- char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
233- data = char .read ()
234231 if str (characteristic .uuid ) in sensor_decoders :
232+ char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
233+ data = char .read ()
235234 sensor_data = sensor_decoders [str (characteristic .uuid )].decode_data (data )
236235 _LOGGER .debug ("{} Got sensordata {}" .format (mac , sensor_data ))
237236 if self .sensordata .get (mac ) is None :
0 commit comments