Skip to content

Commit 74f91ae

Browse files
committed
v0.8.2
1 parent 1129ddf commit 74f91ae

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="tesla_fleet_api",
8-
version="0.8.1",
8+
version="0.8.2",
99
author="Brett Adams",
1010
author_email="[email protected]",
1111
description="Tesla Fleet API library for Python",

tesla_fleet_api/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from enum import Enum
44
import logging
55

6-
VERSION = "0.8.1"
6+
VERSION = "0.8.2"
77
LOGGER = logging.getLogger(__package__)
88
SERVERS = {
99
"na": "https://fleet-api.prd.na.vn.cloud.tesla.com",

tesla_fleet_api/teslafleetapi.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from typing import Any, Awaitable
55
from os.path import exists
66
import aiohttp
7+
import aiofiles
78

89
# cryptography
9-
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
1010
from cryptography.hazmat.primitives.asymmetric import ec
11-
from cryptography.hazmat.primitives import hashes, serialization
11+
from cryptography.hazmat.primitives import serialization
1212
from cryptography.hazmat.backends import default_backend
1313

1414
from .exceptions import raise_for_status, InvalidRegion, LibraryError, ResponseError
@@ -30,7 +30,7 @@ class TeslaFleetApi:
3030
session: aiohttp.ClientSession
3131
headers: dict[str, str]
3232
refresh_hook: Awaitable | None = None
33-
_private_key: ec.EllipticCurvePrivateKey
33+
_private_key: ec.EllipticCurvePrivateKey | None = None
3434

3535
def __init__(
3636
self,
@@ -161,8 +161,8 @@ async def products(self) -> dict[str, Any]:
161161
"api/1/products",
162162
)
163163

164-
def private_key(self, path: str = "private_key.pem") -> ec.EllipticCurvePrivateKey:
165-
"""Create or load the private key."""
164+
async def get_private_key(self, path: str = "private_key.pem") -> ec.EllipticCurvePrivateKey:
165+
"""Get or create the private key."""
166166
if not exists(path):
167167
self._private_key = ec.generate_private_key(
168168
ec.SECP256R1(), default_backend()
@@ -173,16 +173,15 @@ def private_key(self, path: str = "private_key.pem") -> ec.EllipticCurvePrivateK
173173
format=serialization.PrivateFormat.TraditionalOpenSSL,
174174
encryption_algorithm=serialization.NoEncryption(),
175175
)
176-
with open(path, "wb") as key_file:
177-
key_file.write(pem)
178-
return self._private_key
176+
async with aiofiles.open(path, "wb") as key_file:
177+
await key_file.write(pem)
179178
else:
180179
try:
181-
with open(path, "rb") as key_file:
182-
key_data = key_file.read()
183-
value = serialization.load_pem_private_key(
184-
key_data, password=None, backend=default_backend()
185-
)
180+
async with aiofiles.open(path, "rb") as key_file:
181+
key_data = await key_file.read()
182+
value = serialization.load_pem_private_key(
183+
key_data, password=None, backend=default_backend()
184+
)
186185
except FileNotFoundError:
187186
raise FileNotFoundError(f"Private key file not found at {path}")
188187
except PermissionError:
@@ -191,4 +190,4 @@ def private_key(self, path: str = "private_key.pem") -> ec.EllipticCurvePrivateK
191190
if not isinstance(value, ec.EllipticCurvePrivateKey):
192191
raise AssertionError("Loaded key is not an EllipticCurvePrivateKey")
193192
self._private_key = value
194-
return self._private_key
193+
return self._private_key

tesla_fleet_api/vehiclesigned.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22
import base64
3-
from dataclasses import dataclass
43
from random import randbytes
54
from typing import Any, TYPE_CHECKING
65
import time
@@ -155,7 +154,7 @@ def tag(
155154
class VehicleSigned(VehicleSpecific):
156155
"""Class describing the Tesla Fleet API vehicle endpoints and commands for a specific vehicle with command signing."""
157156

158-
_key: ec.EllipticCurvePrivateKey
157+
_private_key: ec.EllipticCurvePrivateKey
159158
_public_key: bytes
160159
_from_destination: bytes
161160
_sessions: dict[int, Session]
@@ -165,13 +164,13 @@ def __init__(
165164
):
166165
super().__init__(parent, vin)
167166
if key:
168-
self._key = key
167+
self._private_key = key
169168
elif parent._parent._private_key:
170-
self._key = parent._parent._private_key
169+
self._private_key = parent._parent._private_key
171170
else:
172171
raise ValueError("No private key.")
173172

174-
self._public_key = self._key.public_key().public_bytes(
173+
self._public_key = self._private_key.public_key().public_bytes(
175174
encoding=Encoding.X962, format=PublicFormat.UncompressedPoint
176175
)
177176
self._from_destination = randbytes(16)
@@ -201,7 +200,7 @@ async def _handshake(self, domain: int) -> None:
201200
vehicle_public_key = info.publicKey
202201

203202
# Derive shared key from private key _key and vehicle public key
204-
shared = self._key.exchange(
203+
shared = self._private_key.exchange(
205204
ec.ECDH(),
206205
ec.EllipticCurvePublicKey.from_encoded_point(
207206
ec.SECP256R1(), vehicle_public_key
@@ -215,8 +214,6 @@ async def _handshake(self, domain: int) -> None:
215214
delta=int(time.time()) - info.clock_time,
216215
)
217216

218-
print(self._sessions[domain])
219-
220217
async def _sendVehicleSecurity(self, command: UnsignedMessage) -> dict[str, Any]:
221218
"""Sign and send a message to Infotainment computer."""
222219
if DOMAIN_VEHICLE_SECURITY not in self._sessions:

0 commit comments

Comments
 (0)