88from typing import Any , Type
99
1010from bleak import BaseBleakScanner , BleakClient , BleakError , BleakScanner , BLEDevice
11+ from bleak .backends .characteristic import BleakGATTCharacteristic
1112from bleak_retry_connector import BleakClientWithServiceCache , establish_connection
1213
1314from pylamarzocco .const import (
3637class LaMarzoccoBluetoothClient :
3738 """Class to interact with machine via Bluetooth."""
3839
39- _client : BleakClient
40+ _client : BleakClientWithServiceCache
4041
4142 def __init__ (
4243 self ,
@@ -176,14 +177,7 @@ async def set_temp(self, boiler: BoilerType, temperature: float) -> None:
176177
177178 async def _authenticate (self ) -> None :
178179 """Build authentication string and send it to the machine."""
179-
180- auth_characteristic = self ._client .services .get_characteristic (
181- AUTH_CHARACTERISTIC
182- )
183- if auth_characteristic is None :
184- raise BluetoothConnectionFailed (
185- f"Could not find auth characteristic { AUTH_CHARACTERISTIC } on machine."
186- )
180+ auth_characteristic = await self ._resolve_characteristic (AUTH_CHARACTERISTIC )
187181
188182 try :
189183 await self ._client .write_gatt_char (
@@ -205,12 +199,7 @@ async def _read_bluetooth_message(
205199 ) -> str :
206200 """Read a bluetooth message."""
207201
208- read_characteristic = self ._client .services .get_characteristic (characteristic )
209- if read_characteristic is None :
210- raise BluetoothConnectionFailed (
211- f"Could not find auth characteristic { characteristic } on machine."
212- )
213-
202+ read_characteristic = await self ._resolve_characteristic (characteristic )
214203 result = await self ._client .read_gatt_char (read_characteristic )
215204 return result .decode ()
216205
@@ -230,13 +219,7 @@ async def __write_bluetooth_message(
230219
231220 _logger .debug ("Sending bluetooth message: %s to %s" , message , characteristic )
232221
233- settings_characteristic = self ._client .services .get_characteristic (
234- characteristic
235- )
236- if settings_characteristic is None :
237- raise BluetoothConnectionFailed (
238- f"Could not find characteristic { characteristic } on machine."
239- )
222+ settings_characteristic = await self ._resolve_characteristic (characteristic )
240223
241224 await self ._client .write_gatt_char (
242225 char_specifier = settings_characteristic ,
@@ -255,3 +238,29 @@ async def __write_bluetooth_json_message(
255238 characteristic = characteristic ,
256239 message = json .dumps (data , separators = ("," , ":" )),
257240 )
241+
242+ async def _resolve_characteristic (
243+ self , characteristic : str
244+ ) -> BleakGATTCharacteristic :
245+ """Resolve characteristic UUID from machine services."""
246+ resolved_characteristic = self ._client .services .get_characteristic (
247+ characteristic
248+ )
249+ if resolved_characteristic is not None :
250+ return resolved_characteristic
251+
252+ _logger .debug (
253+ "Characteristic %s not found in cache, clearing cache and retrying." ,
254+ characteristic ,
255+ )
256+ await self ._client .clear_cache ()
257+
258+ resolved_characteristic = self ._client .services .get_characteristic (
259+ characteristic
260+ )
261+ if resolved_characteristic is not None :
262+ return resolved_characteristic
263+
264+ raise BluetoothConnectionFailed (
265+ f"Could not find characteristic { characteristic } on machine."
266+ )
0 commit comments