Skip to content

Commit 3e35c56

Browse files
committed
Made bluetooth implementation more resilient
1 parent 63a15dc commit 3e35c56

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## v0.5.0 (2019-04-08)
4+
5+
- Made bluetooth implementation slightly more resilient. Thanks to https://community.home-assistant.io/t/airthings-radon-detector/87043/10?u=martytremblay
6+
37
## v0.4.0 (2019-04-08)
48

59
- Upgraded to pygatt 4.0.3 to resolve stability issues

custom_components/airthings_wave/sensor.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
EVENT_HOMEASSISTANT_STOP, ILLUMINANCE,
3434
STATE_UNKNOWN)
3535
from homeassistant.helpers.entity import Entity
36-
VERSION = '0.4.0'
36+
VERSION = '0.5.0'
3737

3838
REQUIREMENTS = ['pygatt[GATTTOOL]==4.0.3']
3939

@@ -361,8 +361,8 @@ def run(self):
361361
BLEError, NotConnectedError, NotificationTimeout)
362362

363363
adapter = pygatt.backends.GATTToolBackend()
364-
try:
365-
while self.keep_going:
364+
while self.keep_going:
365+
try:
366366
_LOGGER.debug("Connecting to %s", self.name)
367367

368368
# We need concurrent connect, so lets not reset the device
@@ -372,28 +372,27 @@ def run(self):
372372
# Give the adaptor a breather
373373
self.event.wait(1)
374374
for s in sensors:
375-
val = struct.unpack(s.format_type,
376-
device.char_read(s.uuid, timeout=CONNECT_TIMEOUT))
375+
val = struct.unpack(
376+
s.format_type,
377+
device.char_read(
378+
s.uuid, timeout=CONNECT_TIMEOUT))
377379
_LOGGER.debug("Sensor %s: %s", s.name, val)
378380

379381
if s.name == 'date_time':
380382
val = str(datetime(val[0], val[1], val[2], val[3],
381-
val[4], val[5]).isoformat())
383+
val[4], val[5]).isoformat())
382384
self.data[s.name] = val
383385
elif s.name == 'illuminance_accelerometer':
384386
self.data['illuminance'] = str(val[0] * s.scale)
385387
self.data['accelerometer'] = str(val[1] * s.scale)
386388
else:
387389
self.data[s.name] = str(round(val[0] * s.scale, 1))
388-
390+
except (BLEError, NotConnectedError, NotificationTimeout) as ex:
391+
_LOGGER.error("Exception: %s ", str(ex))
392+
finally:
389393
adapter.stop()
390394
self.event.wait(self.scan_interval.total_seconds())
391395

392-
except (BLEError, NotConnectedError, NotificationTimeout) as ex:
393-
_LOGGER.error("Exception: %s ", str(ex))
394-
finally:
395-
adapter.stop()
396-
397396
def terminate(self):
398397
"""Signal runner to stop and join thread."""
399398
_LOGGER.debug("Terminating the thread")

0 commit comments

Comments
 (0)