stijnstijn / pyaranet4

A cross-platform Python interface for the Aranet4 CO₂ meter
MIT License
47 stars 8 forks source link

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

Open MaxAnderson95 opened 10 months ago

MaxAnderson95 commented 10 months ago

When instantiating the Aranet4 client class, there is the option to specify a mac_address in the constructor. This means the class will connect to this address rather than running auto-disovery to find the aranet4 device.

This currently doesn't work, because the _client property is never instantiated when specifying the address manually. You simply receive an error that says _client of None type does not have a method read_gatt_char().

Digging into this, it looks like a simple fix. In the _read_value() method, simply always call self._discover() regardless of if self.address is specified or not. The _discover() method already has logic to check for the presence of self.address and will skip auto discovery accordingly. It then properly instantiates the BleakClient and calls connect().

I've tested this and it works perfect for me.

MaxAnderson95 commented 10 months ago

My first attempt at this worked but was a naive approach. It will cause a re-connection on every call to _read_value(). I moved the connection logic to its own method, and then we call that method in _discover() or if _client is none, or if _client.is_connected is False.

I also threw in some additional error messaging into the logging call.