@@ -84,20 +84,34 @@ async def discover(self, scanner: BleakScanner = BleakScanner()) -> BleakClient:
8484 raise ValueError (f"Device { self .ble_name } not found" )
8585 self ._device = device
8686 self .client = BleakClient (self ._device , services = [SERVICE_UUID ])
87- LOGGER .info (f"Discovered device { self ._device .name } { self ._device .address } " )
87+ LOGGER .debug (f"Discovered device { self ._device .name } { self ._device .address } " )
88+ return self .client
89+
90+ def create_client (self , mac :str ):
91+ """Create a client with a MAC."""
92+ self .client = BleakClient (mac , services = [SERVICE_UUID ])
8893 return self .client
8994
9095 async def connect (self , mac :str | None = None ) -> None :
9196 """Connect to the Tesla BLE device."""
9297 if mac is not None :
93- self .client = BleakClient (mac , services = [ SERVICE_UUID ] )
98+ self .create_client (mac )
9499 await self .client .connect ()
95100 await self .client .start_notify (READ_UUID , self ._on_notify )
96101
97102 async def disconnect (self ) -> bool :
98103 """Disconnect from the Tesla BLE device."""
99104 return await self .client .disconnect ()
100105
106+ async def __aenter__ (self ) -> VehicleBluetooth :
107+ """Enter the async context."""
108+ await self .connect ()
109+ return self
110+
111+ async def __aexit__ (self , exc_type , exc_val , exc_tb ):
112+ """Exit the async context."""
113+ await self .disconnect ()
114+
101115 def _on_notify (self ,sender : BleakGATTCharacteristic ,data : bytearray ):
102116 """Receive data from the Tesla BLE device."""
103117 if self ._recv_len :
@@ -157,15 +171,14 @@ async def _send(self, msg: RoutableMessage) -> RoutableMessage:
157171 """Serialize a message and send to the vehicle and wait for a response."""
158172 domain = msg .to_destination .domain
159173 async with self ._sessions [domain ].lock :
160- LOGGER .info (f"Sending message { msg } " )
174+ LOGGER .debug (f"Sending message { msg } " )
161175 future = await self ._create_future (domain )
162176 payload = prependLength (msg .SerializeToString ())
163- LOGGER .info (f"Payload: { payload } " )
164177
165178 await self .client .write_gatt_char (WRITE_UUID , payload , True )
166179
167180 resp = await future
168- LOGGER .info (f"Received message { resp } " )
181+ LOGGER .debug (f"Received message { resp } " )
169182
170183 if resp .signedMessageStatus .signed_message_fault :
171184 raise MESSAGE_FAULTS [resp .signedMessageStatus .signed_message_fault ]
0 commit comments