@@ -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 )):
0 commit comments