Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Fixed a bug where specifying a MAC Address does not work #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions pyaranet4/pyaranet4.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ async def _get_history(self, sensors=None, start=0x0001, end=0xFFFF):
readings.sensors = included_keys
return readings

async def _connect(self, address):
"""
Initialize BleakClient object to a given address and connect to device

:param str address: MAC address of the device to connect to
"""
logging.info("Connecting to device %s" % self._address)
self._client = BleakClient(address)
await self._client.connect(timeout=15)

async def _discover(self):
"""
Discover Aranet4 device and initialise client
Expand All @@ -458,9 +468,7 @@ async def _discover(self):
if not self._address:
raise Aranet4NotFoundException("No Aranet4 device found. Try moving it closer to the Bluetooth receiver.")

logging.info("Connecting to device %s" % self._address)
self._client = BleakClient(self._address)
await self._client.connect(timeout=15)
await self._connect(self._address)
return self._address

async def _read_value(self, uuid):
Expand All @@ -473,9 +481,12 @@ async def _read_value(self, uuid):
if not self._address:
await self._discover()

if not self._client or not self._client.is_connected:
await self._connect(self._address)

try:
value = await self._client.read_gatt_char(uuid)
except BleakError as e:
raise Aranet4UnpairedException("Error reading from device. Check if it is properly paired.")
raise Aranet4UnpairedException(f"Error reading from device. Check if it is properly paired: {e}")

return value