From 45b83883ba284ee581a07d798ff785e7d27c5eb3 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 26 Dec 2024 13:07:49 +1000 Subject: [PATCH] Limit retries to 3 --- tesla_fleet_api/vehiclesigned.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tesla_fleet_api/vehiclesigned.py b/tesla_fleet_api/vehiclesigned.py index 9981564..820ae6e 100644 --- a/tesla_fleet_api/vehiclesigned.py +++ b/tesla_fleet_api/vehiclesigned.py @@ -234,7 +234,7 @@ async def _sendInfotainment(self, command: Action) -> dict[str, Any]: await self._handshake(DOMAIN_INFOTAINMENT) return await self._send(DOMAIN_INFOTAINMENT, command.SerializeToString()) - async def _send(self, domain: int, command: bytes) -> dict[str, Any]: + async def _send(self, domain: int, command: bytes, attempt: int = 1) -> dict[str, Any]: """Send a signed message to the vehicle.""" LOGGER.debug(f"Sending to domain {Domain.Name(domain)}") msg = RoutableMessage() @@ -276,11 +276,15 @@ async def _send(self, domain: int, command: bytes) -> dict[str, Any]: try: resp = await self._signed_message(msg) - except TeslaFleetMessageFaultIncorrectEpoch: + except TeslaFleetMessageFaultIncorrectEpoch as e: + attempt += 1 + if attempt > 3: + # We tried 3 times, give up, raise the error + raise e LOGGER.info(f"Session expired, starting new handshake with {Domain.Name(domain)}") await self._handshake(domain) LOGGER.info(f"Handshake complete, retrying message to {Domain.Name(domain)}") - return await self._send(domain, command) + return await self._send(domain, command, attempt) if resp.signedMessageStatus.operation_status == OPERATIONSTATUS_WAIT: return {"response": {"result": False}}