Skip to content

Commit becee20

Browse files
committed
Fixes
1 parent c71dda6 commit becee20

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

tesla_fleet_api/exceptions.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,6 @@ class SignedMessageInformationFaultCouldNotHashMetadata(SignedMessageInformation
875875
class WhitelistOperationStatus(TeslaFleetError):
876876
message = "Whitelist operation failed"
877877

878-
def __init__(self, message):
879-
self.message = message
880-
881878
class WhitelistOperationUndocumentedError(WhitelistOperationStatus):
882879
message = "Undocumented whitelist operation error"
883880
code = 1

tesla_fleet_api/tesla/vehicle/bluetooth.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ def __init__(
9999

100100
async def find_vehicle(self, name: str | None = None, address: str | None = None, scanner: BleakScanner = BleakScanner()) -> BLEDevice:
101101
"""Find the Tesla BLE device."""
102-
if name is not None:
103-
device = await scanner.find_device_by_name(name)
104-
elif address is not None:
102+
103+
if address is not None:
105104
device = await scanner.find_device_by_address(address)
105+
elif name is not None:
106+
device = await scanner.find_device_by_name(name)
106107
else:
107108
device = await scanner.find_device_by_name(self.ble_name)
108109
if not device:
@@ -116,12 +117,10 @@ def set_device(self, device: BLEDevice) -> None:
116117
def get_device(self) -> BLEDevice:
117118
return self.device
118119

119-
async def connect(self, device: BLEDevice | None = None, max_attempts: int = MAX_CONNECT_ATTEMPTS) -> None:
120+
async def connect(self, max_attempts: int = MAX_CONNECT_ATTEMPTS) -> None:
120121
"""Connect to the Tesla BLE device."""
121-
if device:
122-
self.device = device
123-
if not self.device:
124-
raise ValueError(f"Device {self.ble_name} not found or provided")
122+
if not hasattr(self, 'device'):
123+
raise ValueError(f"BLEDevice {self.ble_name} has not been found or set")
125124
self.client = await establish_connection(
126125
BleakClient,
127126
self.device,
@@ -189,7 +188,7 @@ async def _on_message(self, msg: RoutableMessage) -> None:
189188
LOGGER.debug(f"Received response: {msg}")
190189
await self._queues[msg.from_destination.domain].put(msg)
191190

192-
async def _send(self, msg: RoutableMessage, requires: str) -> RoutableMessage:
191+
async def _send(self, msg: RoutableMessage, requires: str, timeout: int = 2) -> RoutableMessage:
193192
"""Serialize a message and send to the vehicle and wait for a response."""
194193
domain = msg.to_destination.domain
195194
async with self._sessions[domain].lock:
@@ -203,7 +202,7 @@ async def _send(self, msg: RoutableMessage, requires: str) -> RoutableMessage:
203202
await self.client.write_gatt_char(WRITE_UUID, payload, True)
204203

205204
# Process the response
206-
async with asyncio.timeout(2):
205+
async with asyncio.timeout(timeout):
207206
while True:
208207
resp = await self._queues[domain].get()
209208
LOGGER.debug(f"Received message {resp}")
@@ -213,7 +212,7 @@ async def _send(self, msg: RoutableMessage, requires: str) -> RoutableMessage:
213212
if resp.HasField(requires):
214213
return resp
215214

216-
async def pair(self, role: Role = Role.ROLE_OWNER, form: KeyFormFactor = KeyFormFactor.KEY_FORM_FACTOR_CLOUD_KEY):
215+
async def pair(self, role: Role = Role.ROLE_OWNER, form: KeyFormFactor = KeyFormFactor.KEY_FORM_FACTOR_CLOUD_KEY, timeout: int = 60):
217216
"""Pair the key."""
218217

219218
request = UnsignedMessage(
@@ -236,7 +235,7 @@ async def pair(self, role: Role = Role.ROLE_OWNER, form: KeyFormFactor = KeyForm
236235
),
237236
protobuf_message_as_bytes=request.SerializeToString(),
238237
)
239-
resp = await self._send(msg, "protobuf_message_as_bytes")
238+
resp = await self._send(msg, "protobuf_message_as_bytes", timeout)
240239
respMsg = FromVCSECMessage.FromString(resp.protobuf_message_as_bytes)
241240
if(respMsg.commandStatus.whitelistOperationStatus.whitelistOperationInformation):
242241
if(respMsg.commandStatus.whitelistOperationStatus.whitelistOperationInformation < len(WHITELIST_OPERATION_STATUS)):

tesla_fleet_api/tesla/vehicle/vehicles.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
3+
from bleak.backends.device import BLEDevice
4+
from cryptography.hazmat.primitives.asymmetric import ec
35

46
from tesla_fleet_api.tesla.vehicle.signed import VehicleSigned
57
from tesla_fleet_api.tesla.vehicle.bluetooth import VehicleBluetooth
@@ -58,8 +60,12 @@ class VehiclesBluetooth(dict[str, Vehicle]):
5860
def __init__(self, parent: TeslaBluetooth):
5961
self._parent = parent
6062

61-
def create(self, vin: str) -> VehicleBluetooth:
63+
def create(self, vin: str, key: ec.EllipticCurvePrivateKey | None = None, device: BLEDevice | None = None) -> VehicleBluetooth:
6264
"""Creates a bluetooth vehicle that uses command protocol."""
63-
vehicle = self.Bluetooth(self._parent, vin)
65+
return self.createBluetooth(vin, key, device)
66+
67+
def createBluetooth(self, vin: str, key: ec.EllipticCurvePrivateKey | None = None, device: BLEDevice | None = None) -> VehicleBluetooth:
68+
"""Creates a bluetooth vehicle that uses command protocol."""
69+
vehicle = self.Bluetooth(self._parent, vin, key, device)
6470
self[vin] = vehicle
6571
return vehicle

0 commit comments

Comments
 (0)