77
88import bluepy .btle as btle
99
10-
1110from uuid import UUID
1211
1312_LOGGER = logging .getLogger (__name__ )
@@ -199,9 +198,13 @@ def get_info(self):
199198 if self ._dev is not None :
200199 device = AirthingsDeviceInfo (serial_nr = mac )
201200 for characteristic in device_info_characteristics :
202- char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
203- data = char .read ()
204- setattr (device , characteristic .name , data .decode (characteristic .format ))
201+ try :
202+ char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
203+ data = char .read ()
204+ setattr (device , characteristic .name , data .decode (characteristic .format ))
205+ except btle .BTLEDisconnectError :
206+ _LOGGER .exception ("Disconnected" )
207+ self ._dev = None
205208
206209 self .devices [mac ] = device
207210 self .disconnect ()
@@ -212,13 +215,17 @@ def get_sensors(self):
212215 for mac in self .airthing_devices :
213216 self .connect (mac )
214217 if self ._dev is not None :
215- characteristics = self ._dev .getCharacteristics ()
216- sensor_characteristics = []
217- for characteristic in characteristics :
218- _LOGGER .debug (characteristic )
219- if characteristic .uuid in sensors_characteristics_uuid_str :
220- sensor_characteristics .append (characteristic )
221- self .sensors [mac ] = sensor_characteristics
218+ try :
219+ characteristics = self ._dev .getCharacteristics ()
220+ sensor_characteristics = []
221+ for characteristic in characteristics :
222+ _LOGGER .debug (characteristic )
223+ if characteristic .uuid in sensors_characteristics_uuid_str :
224+ sensor_characteristics .append (characteristic )
225+ self .sensors [mac ] = sensor_characteristics
226+ except btle .BTLEDisconnectError :
227+ _LOGGER .exception ("Disconnected" )
228+ self ._dev = None
222229 self .disconnect ()
223230 return self .sensors
224231
@@ -228,16 +235,20 @@ def get_sensor_data(self):
228235 for mac , characteristics in self .sensors .items ():
229236 self .connect (mac )
230237 if self ._dev is not None :
231- for characteristic in characteristics :
232- if str (characteristic .uuid ) in sensor_decoders :
233- char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
234- data = char .read ()
235- sensor_data = sensor_decoders [str (characteristic .uuid )].decode_data (data )
236- _LOGGER .debug ("{} Got sensordata {}" .format (mac , sensor_data ))
237- if self .sensordata .get (mac ) is None :
238- self .sensordata [mac ] = sensor_data
239- else :
240- self .sensordata [mac ].update (sensor_data )
238+ try :
239+ for characteristic in characteristics :
240+ if str (characteristic .uuid ) in sensor_decoders :
241+ char = self ._dev .getCharacteristics (uuid = characteristic .uuid )[0 ]
242+ data = char .read ()
243+ sensor_data = sensor_decoders [str (characteristic .uuid )].decode_data (data )
244+ _LOGGER .debug ("{} Got sensordata {}" .format (mac , sensor_data ))
245+ if self .sensordata .get (mac ) is None :
246+ self .sensordata [mac ] = sensor_data
247+ else :
248+ self .sensordata [mac ].update (sensor_data )
249+ except btle .BTLEDisconnectError :
250+ _LOGGER .exception ("Disconnected" )
251+ self ._dev = None
241252 self .disconnect ()
242253
243254 return self .sensordata
0 commit comments