WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 77e015f

Browse files
committed
clear char cache
1 parent 1790346 commit 77e015f

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

pylamarzocco/clients/_bluetooth.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, Type
99

1010
from bleak import BaseBleakScanner, BleakClient, BleakError, BleakScanner, BLEDevice
11+
from bleak.backends.characteristic import BleakGATTCharacteristic
1112
from bleak_retry_connector import BleakClientWithServiceCache, establish_connection
1213

1314
from pylamarzocco.const import (
@@ -36,7 +37,7 @@
3637
class 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+
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "pylamarzocco"
3-
version = "2.2.0b0"
3+
version = "2.2.0b1"
44
license = { text = "MIT" }
55
description = "A Python implementation of the La Marzocco API"
66
readme = "README.md"

0 commit comments

Comments
 (0)