tesla-fleet-api 0.6.1__tar.gz → 0.7.0__tar.gz
Sign up to get free protection for your applications and to get access to all the features.
- {tesla_fleet_api-0.6.1/tesla_fleet_api.egg-info → tesla_fleet_api-0.7.0}/PKG-INFO +1 -1
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/setup.py +1 -1
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/__init__.py +1 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/const.py +1 -1
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/teslafleetapi.py +2 -2
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/teslafleetoauth.py +25 -9
- tesla_fleet_api-0.7.0/tesla_fleet_api/teslafleetopensource.py +61 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/teslemetry.py +7 -2
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/tessie.py +8 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/vehicle.py +1 -1
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/vehiclespecific.py +8 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0/tesla_fleet_api.egg-info}/PKG-INFO +1 -1
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api.egg-info/SOURCES.txt +1 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/LICENSE +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/README.md +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/setup.cfg +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/charging.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/energy.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/energyspecific.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/exceptions.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/partner.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api/user.py +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api.egg-info/dependency_links.txt +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api.egg-info/requires.txt +0 -0
- {tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api.egg-info/top_level.txt +0 -0
@@ -46,8 +46,6 @@ class TeslaFleetApi:
|
|
46
46
|
if region not in SERVERS:
|
47
47
|
raise ValueError(f"Region must be one of {', '.join(SERVERS.keys())}")
|
48
48
|
self.server = SERVERS.get(region)
|
49
|
-
else:
|
50
|
-
raise ValueError("Either server or region must be provided.")
|
51
49
|
|
52
50
|
LOGGER.debug("Using server %s", self.server)
|
53
51
|
|
@@ -114,6 +112,8 @@ class TeslaFleetApi:
|
|
114
112
|
params=params,
|
115
113
|
) as resp:
|
116
114
|
LOGGER.debug("Response Status: %s", resp.status)
|
115
|
+
if "x-txid" in resp.headers:
|
116
|
+
LOGGER.debug("Response TXID: %s", resp.headers["x-txid"])
|
117
117
|
if not resp.ok:
|
118
118
|
await raise_for_status(resp)
|
119
119
|
if not resp.content_type.lower().startswith("application/json"):
|
@@ -11,11 +11,16 @@ class TeslaFleetOAuth(TeslaFleetApi):
|
|
11
11
|
"""Tesla Fleet OAuth API."""
|
12
12
|
|
13
13
|
expires: int
|
14
|
+
refresh_token: str
|
15
|
+
redirect_uri: str | None
|
16
|
+
_client_secret: str | None
|
14
17
|
|
15
18
|
def __init__(
|
16
19
|
self,
|
17
20
|
session: aiohttp.ClientSession,
|
18
21
|
client_id: str,
|
22
|
+
client_secret: str | None = None,
|
23
|
+
redirect_uri: str | None = None,
|
19
24
|
access_token: str | None = None,
|
20
25
|
refresh_token: str | None = None,
|
21
26
|
expires: int = 0,
|
@@ -23,6 +28,8 @@ class TeslaFleetOAuth(TeslaFleetApi):
|
|
23
28
|
server: str | None = None,
|
24
29
|
):
|
25
30
|
self.client_id = client_id
|
31
|
+
self._client_secret = client_secret
|
32
|
+
self.redirect_uri = redirect_uri
|
26
33
|
self.access_token = access_token
|
27
34
|
self.refresh_token = refresh_token
|
28
35
|
self.expires = expires
|
@@ -34,25 +41,34 @@ class TeslaFleetOAuth(TeslaFleetApi):
|
|
34
41
|
server=server,
|
35
42
|
)
|
36
43
|
|
37
|
-
def get_login_url(
|
38
|
-
self, redirect_uri: str, scopes: list[Scope], state: str = "login"
|
39
|
-
) -> str:
|
44
|
+
def get_login_url(self, scopes: list[Scope], state: str = "login") -> str:
|
40
45
|
"""Get the login URL."""
|
41
|
-
|
46
|
+
if self.redirect_uri is None:
|
47
|
+
raise ValueError("Redirect URI is missing")
|
48
|
+
return f"https://auth.tesla.com/oauth2/v3/authorize?response_type=code&prompt=login&client_id={self.client_id}&redirect_uri={self.redirect_uri}&scope={' '.join(scopes)}&state={state}"
|
42
49
|
|
43
|
-
async def get_refresh_token(
|
44
|
-
self, client_secret: str, code: str, redirect_uri: str
|
45
|
-
) -> None:
|
50
|
+
async def get_refresh_token(self, code: str) -> None:
|
46
51
|
"""Get the refresh token."""
|
52
|
+
|
53
|
+
if self._client_secret is None:
|
54
|
+
raise ValueError("Client secret is missing")
|
55
|
+
|
56
|
+
if self.redirect_uri is None:
|
57
|
+
raise ValueError("Redirect URI is missing")
|
58
|
+
|
59
|
+
if self.server is None:
|
60
|
+
self.region = code.split("_")[0].lower()
|
61
|
+
self.server = SERVERS.get(self.region)
|
62
|
+
|
47
63
|
async with self.session.post(
|
48
64
|
"https://auth.tesla.com/oauth2/v3/token",
|
49
65
|
data={
|
50
66
|
"grant_type": "authorization_code",
|
51
67
|
"client_id": self.client_id,
|
52
|
-
"client_secret":
|
68
|
+
"client_secret": self._client_secret,
|
53
69
|
"code": code,
|
54
70
|
"audience": self.server,
|
55
|
-
"redirect_uri": redirect_uri,
|
71
|
+
"redirect_uri": self.redirect_uri,
|
56
72
|
},
|
57
73
|
) as resp:
|
58
74
|
if resp.ok:
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import aiohttp
|
2
|
+
import time
|
3
|
+
import secrets
|
4
|
+
import hashlib
|
5
|
+
import base64
|
6
|
+
|
7
|
+
from .teslafleetoauth import TeslaFleetOAuth
|
8
|
+
from .const import Scope, SERVERS
|
9
|
+
|
10
|
+
|
11
|
+
class TeslaFleetOpenSource(TeslaFleetOAuth):
|
12
|
+
"""Tesla Fleet Open Source OAuth API."""
|
13
|
+
|
14
|
+
code_verifier: str
|
15
|
+
code_challenge: str
|
16
|
+
|
17
|
+
def __init__(
|
18
|
+
self,
|
19
|
+
session: aiohttp.ClientSession,
|
20
|
+
client_id: str,
|
21
|
+
redirect_uri: str,
|
22
|
+
):
|
23
|
+
self.code_verifier = secrets.token_urlsafe(32)
|
24
|
+
|
25
|
+
# Hash the code_verifier using SHA-256
|
26
|
+
hashed_verifier = hashlib.sha256(self.code_verifier.encode()).digest()
|
27
|
+
# Encode the hash using URL-safe Base64 encoding, without padding
|
28
|
+
self.code_challenge = (
|
29
|
+
base64.urlsafe_b64encode(hashed_verifier).decode().replace("=", "")
|
30
|
+
)
|
31
|
+
|
32
|
+
super().__init__(session, client_id, redirect_uri=redirect_uri)
|
33
|
+
|
34
|
+
def get_login_url(self, scopes: list[Scope], state: str = "login") -> str:
|
35
|
+
"""Get the login URL without a client secret."""
|
36
|
+
|
37
|
+
return (
|
38
|
+
super().get_login_url(scopes, state)
|
39
|
+
+ f"&code_challenge={self.code_challenge}"
|
40
|
+
)
|
41
|
+
|
42
|
+
async def get_refresh_token(self, code: str) -> None:
|
43
|
+
"""Get the refresh token."""
|
44
|
+
async with self.session.post(
|
45
|
+
"https://auth.tesla.com/oauth2/v3/token",
|
46
|
+
data={
|
47
|
+
"grant_type": "authorization_code",
|
48
|
+
"client_id": self.client_id,
|
49
|
+
"code": code,
|
50
|
+
"audience": self.server,
|
51
|
+
"redirect_uri": self.redirect_uri,
|
52
|
+
"code_verifier": self.code_verifier,
|
53
|
+
},
|
54
|
+
) as resp:
|
55
|
+
if resp.ok:
|
56
|
+
data = await resp.json()
|
57
|
+
self.refresh_token = data["refresh_token"]
|
58
|
+
self.access_token = data["access_token"]
|
59
|
+
self.expires = int(time.time()) + data["expires_in"]
|
60
|
+
region = code.split("_")[0].lower()
|
61
|
+
self.server = SERVERS.get(region)
|
@@ -2,7 +2,7 @@ import aiohttp
|
|
2
2
|
from aiolimiter import AsyncLimiter
|
3
3
|
from typing import Any
|
4
4
|
from .teslafleetapi import TeslaFleetApi
|
5
|
-
from .const import Method, LOGGER
|
5
|
+
from .const import Method, LOGGER, Scope
|
6
6
|
|
7
7
|
# Rate limit should be global, even if multiple instances are created
|
8
8
|
rate_limit = AsyncLimiter(5, 10)
|
@@ -39,7 +39,7 @@ class Teslemetry(TeslaFleetApi):
|
|
39
39
|
)
|
40
40
|
|
41
41
|
async def metadata(self, update_region=True) -> dict[str, Any]:
|
42
|
-
"""
|
42
|
+
"""Get user metadata including scopes."""
|
43
43
|
resp = await self._request(
|
44
44
|
Method.GET,
|
45
45
|
"api/metadata",
|
@@ -49,6 +49,11 @@ class Teslemetry(TeslaFleetApi):
|
|
49
49
|
self.server = f"https://{self.region}.teslemetry.com"
|
50
50
|
LOGGER.debug("Using server %s", self.server)
|
51
51
|
return resp
|
52
|
+
|
53
|
+
async def scopes(self) -> list[str]:
|
54
|
+
"""Get user scopes."""
|
55
|
+
resp = await self.metadata(False)
|
56
|
+
return resp["scopes"]
|
52
57
|
|
53
58
|
async def find_server(self) -> str:
|
54
59
|
"""Find the server URL for the Tesla Fleet API."""
|
@@ -19,6 +19,14 @@ class Tessie(TeslaFleetApi):
|
|
19
19
|
user_scope=False,
|
20
20
|
)
|
21
21
|
|
22
|
+
async def scopes(self) -> list[str]:
|
23
|
+
"""Get user scopes."""
|
24
|
+
resp = await self._request(
|
25
|
+
Method.GET,
|
26
|
+
"auth/tesla_scopes",
|
27
|
+
)
|
28
|
+
return resp["scopes"]
|
29
|
+
|
22
30
|
async def find_server(self) -> str:
|
23
31
|
"""Find the server URL for the Tesla Fleet API."""
|
24
32
|
raise NotImplementedError("Do not use this function for Tessie.")
|
@@ -752,7 +752,7 @@ class Vehicle:
|
|
752
752
|
async def fleet_status(self, vins: List[str]) -> dict[str, Any]:
|
753
753
|
"""Checks whether vehicles can accept Tesla commands protocol for the partner's public key"""
|
754
754
|
return await self._request(
|
755
|
-
Method.
|
755
|
+
Method.POST, "api/1/vehicles/fleet_status", json={"vins": vins}
|
756
756
|
)
|
757
757
|
|
758
758
|
async def fleet_telemetry_config_create(
|
@@ -436,6 +436,14 @@ class VehicleSpecific:
|
|
436
436
|
"""Checks whether vehicles can accept Tesla commands protocol for the partner's public key"""
|
437
437
|
return await self._parent.fleet_status([self.vin])
|
438
438
|
|
439
|
+
async def fleet_telemetry_config_create(
|
440
|
+
self, config: dict[str, Any]
|
441
|
+
) -> dict[str, Any]:
|
442
|
+
"""Configures fleet telemetry."""
|
443
|
+
return await self._parent.fleet_telemetry_config_create(
|
444
|
+
{"vins": [self.vin], "config": config}
|
445
|
+
)
|
446
|
+
|
439
447
|
async def fleet_telemetry_config_get(self) -> dict[str, Any]:
|
440
448
|
"""Configures fleet telemetry."""
|
441
449
|
return await self._parent.fleet_telemetry_config_get(self.vin)
|
@@ -10,6 +10,7 @@ tesla_fleet_api/exceptions.py
|
|
10
10
|
tesla_fleet_api/partner.py
|
11
11
|
tesla_fleet_api/teslafleetapi.py
|
12
12
|
tesla_fleet_api/teslafleetoauth.py
|
13
|
+
tesla_fleet_api/teslafleetopensource.py
|
13
14
|
tesla_fleet_api/teslemetry.py
|
14
15
|
tesla_fleet_api/tessie.py
|
15
16
|
tesla_fleet_api/user.py
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{tesla_fleet_api-0.6.1 → tesla_fleet_api-0.7.0}/tesla_fleet_api.egg-info/dependency_links.txt
RENAMED
File without changes
|
File without changes
|
File without changes
|