Skip to content

Commit a4fc56a

Browse files
committed
Move improvements
1 parent d3a27bf commit a4fc56a

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

tesla_fleet_api/tesla/bluetooth.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
import hashlib
44
import re
55
from google.protobuf.json_format import MessageToJson, MessageToDict
6-
from bleak.backends.device import BLEDevice
7-
from cryptography.hazmat.primitives.asymmetric import ec
86

97
from tesla_fleet_api.tesla.tesla import Tesla
10-
from tesla_fleet_api.tesla.vehicle.bluetooth import VehicleBluetooth
8+
from tesla_fleet_api.tesla.vehicle.vehicles import VehiclesBluetooth
119

1210
class TeslaBluetooth(Tesla):
1311
"""Class describing a Tesla Bluetooth connection."""
1412

13+
Vehicles = VehiclesBluetooth
14+
1515
def __init__(
1616
self,
1717
):
1818
"""Initialize the Tesla Fleet API."""
1919

20-
self.vehicles = Vehicles(self)
20+
self.vehicles = self.Vehicles(self)
2121

2222
def valid_name(self, name: str) -> bool:
2323
"""Check if a BLE device name is a valid Tesla vehicle."""
@@ -27,23 +27,8 @@ def get_name(self, vin: str) -> str:
2727
"""Get the name of a vehicle."""
2828
return "S" + hashlib.sha1(vin.encode('utf-8')).hexdigest()[:16] + "C"
2929

30-
class Vehicles(dict[str, VehicleBluetooth]):
31-
"""Class containing and creating vehicles."""
32-
33-
_parent: TeslaBluetooth
34-
35-
def __init__(self, parent: TeslaBluetooth):
36-
self._parent = parent
37-
38-
def create(self, vin: str) -> VehicleBluetooth:
39-
"""Creates a specific vehicle."""
40-
return self.createBluetooth(vin)
4130

42-
def createBluetooth(self, vin: str, key: ec.EllipticCurvePrivateKey | None = None, device: None | str | BLEDevice = None) -> VehicleBluetooth:
43-
"""Creates a specific vehicle."""
44-
vehicle = VehicleBluetooth(self._parent, vin, key, device)
45-
self[vin] = vehicle
46-
return vehicle
31+
# Helpers
4732

4833
def toJson(message) -> str:
4934
"""Convert a protobuf message to JSON."""

tesla_fleet_api/tesla/fleet.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
from tesla_fleet_api.tesla.tesla import Tesla
88
from tesla_fleet_api.exceptions import raise_for_status, InvalidRegion, LibraryError, ResponseError
99
from tesla_fleet_api.const import SERVERS, Method, LOGGER, VERSION
10-
from tesla_fleet_api.tesla.charging import Charging
11-
from tesla_fleet_api.tesla.energysite import EnergySites
12-
from tesla_fleet_api.tesla.partner import Partner
13-
from tesla_fleet_api.tesla.user import User
14-
from tesla_fleet_api.tesla.vehicle.vehicles import Vehicles
1510

1611

1712
# Based on https://developer.tesla.com/docs/fleet-api
@@ -54,15 +49,15 @@ def __init__(
5449
LOGGER.debug("Using server %s", self.server)
5550

5651
if charging_scope:
57-
self.charging = Charging(self)
52+
self.charging = self.Charging(self)
5853
if energy_scope:
59-
self.energySites = EnergySites(self)
54+
self.energySites = self.EnergySites(self)
6055
if user_scope:
61-
self.user = User(self)
56+
self.user = self.User(self)
6257
if partner_scope:
63-
self.partner = Partner(self)
58+
self.partner = self.Partner(self)
6459
if vehicle_scope:
65-
self.vehicles = Vehicles(self)
60+
self.vehicles = self.Vehicles(self)
6661

6762
async def find_server(self) -> str:
6863
"""Find the server URL for the Tesla Fleet API."""

tesla_fleet_api/tesla/tesla.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
from os.path import exists
44
import aiofiles
55

6+
from tesla_fleet_api.tesla.charging import Charging
7+
from tesla_fleet_api.tesla.energysite import EnergySites
8+
from tesla_fleet_api.tesla.partner import Partner
9+
from tesla_fleet_api.tesla.user import User
10+
from tesla_fleet_api.tesla.vehicle.vehicles import Vehicles
11+
612
# cryptography
713
from cryptography.hazmat.primitives.asymmetric import ec
814
from cryptography.hazmat.primitives import serialization
@@ -11,6 +17,12 @@
1117
class Tesla:
1218
"""Base class describing interactions with Tesla products."""
1319

20+
Charging = Charging
21+
EnergySites = EnergySites
22+
Partner = Partner
23+
User = User
24+
Vehicles = Vehicles
25+
1426
private_key: ec.EllipticCurvePrivateKey | None = None
1527

1628
async def get_private_key(

tesla_fleet_api/tesla/vehicle/vehicles.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
if TYPE_CHECKING:
1010
from tesla_fleet_api.tesla.fleet import TeslaFleetApi
11-
11+
from tesla_fleet_api.tesla.bluetooth import TeslaBluetooth
1212

1313

1414
class Vehicles(dict[str, Vehicle]):
@@ -47,3 +47,19 @@ def specific(self, vin: str) -> Vehicle:
4747
def specificSigned(self, vin: str) -> VehicleSigned:
4848
"""Legacy method for creating a Fleet API vehicle that uses command protocol."""
4949
return self.createSigned(vin)
50+
51+
52+
class VehiclesBluetooth(dict[str, Vehicle]):
53+
"""Class containing and creating bluetooth vehicles."""
54+
55+
_parent: TeslaBluetooth
56+
Bluetooth = VehicleBluetooth
57+
58+
def __init__(self, parent: TeslaBluetooth):
59+
self._parent = parent
60+
61+
def create(self, vin: str) -> VehicleBluetooth:
62+
"""Creates a bluetooth vehicle that uses command protocol."""
63+
vehicle = self.Bluetooth(self._parent, vin)
64+
self[vin] = vehicle
65+
return vehicle

0 commit comments

Comments
 (0)