7
7
8
8
import bluepy .btle as btle
9
9
10
-
11
10
from uuid import UUID
12
11
13
12
_LOGGER = logging .getLogger (__name__ )
@@ -199,9 +198,13 @@ def get_info(self):
199
198
if self ._dev is not None :
200
199
device = AirthingsDeviceInfo (serial_nr = mac )
201
200
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
205
208
206
209
self .devices [mac ] = device
207
210
self .disconnect ()
@@ -212,13 +215,17 @@ def get_sensors(self):
212
215
for mac in self .airthing_devices :
213
216
self .connect (mac )
214
217
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
222
229
self .disconnect ()
223
230
return self .sensors
224
231
@@ -228,16 +235,20 @@ def get_sensor_data(self):
228
235
for mac , characteristics in self .sensors .items ():
229
236
self .connect (mac )
230
237
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
241
252
self .disconnect ()
242
253
243
254
return self .sensordata
0 commit comments