Skip to content

Commit cc47a96

Browse files
committed
Better query_display_name
1 parent ccde953 commit cc47a96

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

tesla_fleet_api/tesla/vehicle/bluetooth.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,20 @@ async def _send(self, msg: RoutableMessage, requires: str, timeout: int = 2) ->
218218
if resp.HasField(requires):
219219
return resp
220220

221-
async def query_name(self, strip_key=True):
221+
async def query_display_name(self, max_attempts=5) -> str | None:
222222
"""Read the device name via GATT characteristic if available"""
223-
try:
224-
# Standard GATT Device Name characteristic (0x2A00)
225-
device_name = await self.client.read_gatt_char(NAME_UUID)
226-
device_name = device_name.decode('utf-8')
227-
if(strip_key):
228-
device_name = device_name.replace("🔑 ","")
229-
return device_name
230-
except Exception as e:
231-
LOGGER.error(f"Failed to read device name: {e}")
232-
return None
233-
234-
async def query_appearance(self) -> bytearray:
223+
for i in range(max_attempts):
224+
try:
225+
# Standard GATT Device Name characteristic (0x2A00)
226+
device_name = (await self.client.read_gatt_char(NAME_UUID)).decode('utf-8')
227+
if device_name.startswith("🔑 "):
228+
return device_name.replace("🔑 ","")
229+
await asyncio.sleep(1)
230+
LOGGER.debug(f"Attempt {i+1} to query display name failed, {device_name}")
231+
except Exception as e:
232+
LOGGER.error(f"Failed to read device name: {e}")
233+
234+
async def query_appearance(self) -> bytearray | None:
235235
"""Read the device appearance via GATT characteristic if available"""
236236
try:
237237
# Standard GATT Appearance characteristic (0x2A01)

tesla_fleet_api/tesla/vehicle/vehicles.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import asyncio
23
from typing import TYPE_CHECKING
34
from bleak import BleakClient
45
from bleak.backends.device import BLEDevice
@@ -73,18 +74,26 @@ def createBluetooth(self, vin: str, key: ec.EllipticCurvePrivateKey | None = Non
7374
self[vin] = vehicle
7475
return vehicle
7576

76-
async def query_device_name(self, device: BLEDevice, max_attempts=3) -> str:
77+
async def query_display_name(self, device: BLEDevice, max_attempts=5) -> str | None:
7778
"""Queries the name of a bluetooth vehicle."""
78-
async with await establish_connection(
79+
client = await establish_connection(
7980
BleakClient,
8081
device,
8182
device.name or "Unknown",
8283
max_attempts=max_attempts
83-
) as client:
84+
)
85+
name: str | None = None
86+
for i in range(max_attempts):
8487
try:
8588
# Standard GATT Device Name characteristic (0x2A00)
86-
device_name = await client.read_gatt_char(NAME_UUID)
87-
return device_name.decode('utf-8').replace("🔑 ","")
89+
device_name = (await client.read_gatt_char(NAME_UUID)).decode('utf-8')
90+
if device_name.startswith("🔑 "):
91+
name = device_name.replace("🔑 ","")
92+
break
93+
await asyncio.sleep(1)
94+
LOGGER.debug(f"Attempt {i+1} to query display name failed, {device_name}")
8895
except Exception as e:
8996
LOGGER.error(f"Failed to read device name: {e}")
90-
return None
97+
98+
await client.disconnect()
99+
return name

0 commit comments

Comments
 (0)