Skip to content

Commit ccde953

Browse files
committed
Add name methods
1 parent aa11353 commit ccde953

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

tesla_fleet_api/tesla/vehicle/bluetooth.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
WRITE_UUID = "00000212-b2d1-43f0-9b88-960cebf8b91e"
6666
READ_UUID = "00000213-b2d1-43f0-9b88-960cebf8b91e"
6767
VERSION_UUID = "00000214-b2d1-43f0-9b88-960cebf8b91e"
68+
NAME_UUID = "00002a00-0000-1000-8000-00805f9b34fb"
69+
APPEARANCE_UUID = "00002a01-0000-1000-8000-00805f9b34fb"
6870

6971
if TYPE_CHECKING:
7072
from tesla_fleet_api.tesla.tesla import Tesla
@@ -216,6 +218,39 @@ async def _send(self, msg: RoutableMessage, requires: str, timeout: int = 2) ->
216218
if resp.HasField(requires):
217219
return resp
218220

221+
async def query_name(self, strip_key=True):
222+
"""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:
235+
"""Read the device appearance via GATT characteristic if available"""
236+
try:
237+
# Standard GATT Appearance characteristic (0x2A01)
238+
return await self.client.read_gatt_char(APPEARANCE_UUID)
239+
except Exception as e:
240+
LOGGER.error(f"Failed to read device appearance: {e}")
241+
return None
242+
243+
async def query_version(self) -> int | None:
244+
"""Read the device version via GATT characteristic if available"""
245+
try:
246+
# Custom GATT Version characteristic (0x2A02)
247+
device_version = await self.client.read_gatt_char(VERSION_UUID)
248+
# Convert the bytes to an integer
249+
if device_version and len(device_version) > 0:
250+
return int.from_bytes(device_version, byteorder='big')
251+
except Exception as e:
252+
LOGGER.error(f"Failed to read device version: {e}")
253+
return None
219254

220255
async def pair(self, role: Role = Role.ROLE_OWNER, form: KeyFormFactor = KeyFormFactor.KEY_FORM_FACTOR_CLOUD_KEY, timeout: int = 60):
221256
"""Pair the key."""

tesla_fleet_api/tesla/vehicle/vehicle.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ class Vehicle:
1818
"""Base class describing a Tesla vehicle."""
1919

2020
vin: str
21+
parent: Tesla
2122

2223
def __init__(self, parent: Tesla, vin: str):
2324
self.vin = vin
25+
self.parent = parent
2426

2527
@property
2628
def pre2021(self) -> bool:

tesla_fleet_api/tesla/vehicle/vehicles.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
3+
from bleak import BleakClient
34
from bleak.backends.device import BLEDevice
5+
from bleak_retry_connector import establish_connection
46
from cryptography.hazmat.primitives.asymmetric import ec
57

68
from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned
7-
from tesla_fleet_api.tesla.vehicle.bluetooth import VehicleBluetooth
9+
from tesla_fleet_api.tesla.vehicle.bluetooth import NAME_UUID, VehicleBluetooth
810
from tesla_fleet_api.tesla.vehicle.fleet import VehicleFleet
911
from tesla_fleet_api.tesla.vehicle.vehicle import Vehicle
12+
from tesla_fleet_api.const import LOGGER
1013

1114
if TYPE_CHECKING:
1215
from tesla_fleet_api.tesla.fleet import TeslaFleetApi
@@ -69,3 +72,19 @@ def createBluetooth(self, vin: str, key: ec.EllipticCurvePrivateKey | None = Non
6972
vehicle = self.Bluetooth(self._parent, vin, key, device)
7073
self[vin] = vehicle
7174
return vehicle
75+
76+
async def query_device_name(self, device: BLEDevice, max_attempts=3) -> str:
77+
"""Queries the name of a bluetooth vehicle."""
78+
async with await establish_connection(
79+
BleakClient,
80+
device,
81+
device.name or "Unknown",
82+
max_attempts=max_attempts
83+
) as client:
84+
try:
85+
# Standard GATT Device Name characteristic (0x2A00)
86+
device_name = await client.read_gatt_char(NAME_UUID)
87+
return device_name.decode('utf-8').replace("🔑 ","")
88+
except Exception as e:
89+
LOGGER.error(f"Failed to read device name: {e}")
90+
return None

0 commit comments

Comments
 (0)