Skip to content

Commit

Permalink
Updates from flecmart to ensure disconnect is called.
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmccans committed Jul 27, 2022
1 parent ec205f4 commit 23e102a
Showing 1 changed file with 42 additions and 25 deletions.
67 changes: 42 additions & 25 deletions src/airthings.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,15 @@ async def disconnect(self):
if self._dev is not None:
await self._dev.disconnect()
self._dev = None
_LOGGER.debug("Disconnected.")

async def get_info(self):
# Try to get some info from the discovered airthings devices
self.devices = {}
for mac in self.airthing_devices:
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
_LOGGER.debug("Getting device info for {}".format(mac))
try:
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
device = AirthingsDeviceInfo(serial_nr=mac)
for characteristic in device_info_characteristics:
Expand All @@ -266,35 +268,48 @@ async def get_info(self):
setattr(device, characteristic.name, data.decode(characteristic.format))
except:
_LOGGER.warning("Error getting {}".format(characteristic.name))
self.devices[mac] = device
self.devices[mac] = device
else:
raise "Could not connect to {}".format(mac)
except Exception as e:
_LOGGER.exception("Error getting device info for {}: {}".format(mac, e))
finally:
await self.disconnect()
else:
_LOGGER.error("Not getting device info because failed to connect to device.")

return self.devices

async def get_sensors(self):
self.sensors = {}
for mac in self.airthing_devices:
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
sensor_characteristics = []
svcs = await self._dev.get_services()
for service in svcs:
for characteristic in service.characteristics:
_LOGGER.debug(characteristic)
if characteristic.uuid in sensors_characteristics_uuid_str:
sensor_characteristics.append(characteristic)
self.sensors[mac] = sensor_characteristics
await self.disconnect()
_LOGGER.debug("Getting sensors for {}".format(mac))
try:
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
sensor_characteristics = []
svcs = await self._dev.get_services()
for service in svcs:
for characteristic in service.characteristics:
_LOGGER.debug(characteristic)
if characteristic.uuid in sensors_characteristics_uuid_str:
sensor_characteristics.append(characteristic)
self.sensors[mac] = sensor_characteristics
else:
raise "Could not connect to {}".format(mac)
except Exception as e:
_LOGGER.exception("Error getting sensors for {}: {}".format(mac, e))
finally:
await self.disconnect()

return self.sensors

async def get_sensor_data(self):
if time.monotonic() - self.last_scan > self.scan_interval or self.last_scan == -1:
self.last_scan = time.monotonic()
for mac, characteristics in self.sensors.items():
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
try:
_LOGGER.debug("Getting sensor data for {}".format(mac))
try:
await self.connect(mac)
if self._dev is not None and self._dev.is_connected:
for characteristic in characteristics:
sensor_data = None
if str(characteristic.uuid) in sensor_decoders:
Expand Down Expand Up @@ -325,12 +340,14 @@ async def get_sensor_data(self):
if self.sensordata.get(mac) is None:
self.sensordata[mac] = sensor_data
else:
self.sensordata[mac].update(sensor_data)
except:
_LOGGER.exception("Error getting sensor data.")
self._dev = None

await self.disconnect()
self.sensordata[mac].update(sensor_data)
else:
raise "Could not connect to {}".format(mac)
except Exception as e:
_LOGGER.exception("Error getting sensor data for '{}': {}".format(mac, e))

finally:
await self.disconnect()

return self.sensordata

Expand Down

0 comments on commit 23e102a

Please sign in to comment.