Navigation Menu

Skip to content

Commit

Permalink
Made bluetooth implementation more resilient
Browse files Browse the repository at this point in the history
  • Loading branch information
MartyTremblay committed Aug 27, 2019
1 parent 63a15dc commit 3e35c56
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.5.0 (2019-04-08)

- Made bluetooth implementation slightly more resilient. Thanks to https://community.home-assistant.io/t/airthings-radon-detector/87043/10?u=martytremblay

## v0.4.0 (2019-04-08)

- Upgraded to pygatt 4.0.3 to resolve stability issues
Expand Down
23 changes: 11 additions & 12 deletions custom_components/airthings_wave/sensor.py
Expand Up @@ -33,7 +33,7 @@
EVENT_HOMEASSISTANT_STOP, ILLUMINANCE,
STATE_UNKNOWN)
from homeassistant.helpers.entity import Entity
VERSION = '0.4.0'
VERSION = '0.5.0'

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

Expand Down Expand Up @@ -361,8 +361,8 @@ def run(self):
BLEError, NotConnectedError, NotificationTimeout)

adapter = pygatt.backends.GATTToolBackend()
try:
while self.keep_going:
while self.keep_going:
try:
_LOGGER.debug("Connecting to %s", self.name)

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

if s.name == 'date_time':
val = str(datetime(val[0], val[1], val[2], val[3],
val[4], val[5]).isoformat())
val[4], val[5]).isoformat())
self.data[s.name] = val
elif s.name == 'illuminance_accelerometer':
self.data['illuminance'] = str(val[0] * s.scale)
self.data['accelerometer'] = str(val[1] * s.scale)
else:
self.data[s.name] = str(round(val[0] * s.scale, 1))

except (BLEError, NotConnectedError, NotificationTimeout) as ex:
_LOGGER.error("Exception: %s ", str(ex))
finally:
adapter.stop()
self.event.wait(self.scan_interval.total_seconds())

except (BLEError, NotConnectedError, NotificationTimeout) as ex:
_LOGGER.error("Exception: %s ", str(ex))
finally:
adapter.stop()

def terminate(self):
"""Signal runner to stop and join thread."""
_LOGGER.debug("Terminating the thread")
Expand Down

0 comments on commit 3e35c56

Please sign in to comment.