tesla-fleet-api 0.7.2__py3-none-any.whl → 0.7.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tesla_fleet_api/const.py CHANGED
@@ -3,7 +3,7 @@
3
3
  from enum import Enum
4
4
  import logging
5
5
 
6
- VERSION = "0.7.2"
6
+ VERSION = "0.7.4"
7
7
  LOGGER = logging.getLogger(__package__)
8
8
  SERVERS = {
9
9
  "na": "https://fleet-api.prd.na.vn.cloud.tesla.com",
@@ -0,0 +1,60 @@
1
+ """Rate Calculator helper"""
2
+
3
+ import time
4
+
5
+ DAY = 24 * 60 * 60
6
+
7
+
8
+ class RateCalculator:
9
+ """Calculate the consumption and remaining rate of a rate limit."""
10
+
11
+ def __init__(
12
+ self,
13
+ limit: int,
14
+ period: int = 86400,
15
+ min_wait: int = 0,
16
+ max_wait: int | None = None,
17
+ factor: int = 5,
18
+ ) -> None:
19
+ """Initialize the rate calculator."""
20
+ self.limit: int = limit
21
+ self.period: int = period
22
+ self.history: list[int] = []
23
+ self.start = time.time()
24
+ self.min_wait = min_wait
25
+ self.max_wait = max_wait if max_wait is not None else period
26
+ self.factor = factor
27
+
28
+ def constrain(self, value: float) -> float:
29
+ """Constrain a value between min and max."""
30
+ return max(self.min_wait, min(self.max_wait, value))
31
+
32
+ def consume(self, timestamp: int | None = None) -> None:
33
+ """Consume a unit of the rate limit."""
34
+ now = timestamp or int(time.time() + 1)
35
+ self.history.append(now)
36
+
37
+ def calculate(self, timestamp: int | None = None) -> float:
38
+ """Return the ideal delay to avoid rate limiting."""
39
+
40
+ count = len(self.history)
41
+ if count == 0:
42
+ return self.min_wait
43
+
44
+ now = timestamp or int(time.time())
45
+
46
+ while self.history and self.history[0] < now - self.period:
47
+ self.history.pop(0)
48
+
49
+ remaining = self.limit - count
50
+
51
+ if remaining <= 0:
52
+ # The wait until a request is available
53
+ return self.constrain(self.history[abs(remaining)] + self.period - now)
54
+
55
+ return self.constrain(self.period / remaining / self.factor)
56
+
57
+ @property
58
+ def count(self) -> int:
59
+ """Return the number of requests in the current period."""
60
+ return len(self.history)
@@ -22,7 +22,7 @@ class TeslaFleetApi:
22
22
  server: str | None = None
23
23
  session: aiohttp.ClientSession
24
24
  headers: dict[str, str]
25
- refresh_hook: Awaitable | None
25
+ refresh_hook: Awaitable | None = None
26
26
 
27
27
  def __init__(
28
28
  self,
@@ -114,7 +114,7 @@ class TeslaFleetApi:
114
114
  headers={
115
115
  "Authorization": f"Bearer {self.access_token}",
116
116
  "Content-Type": "application/json",
117
- "X-Library": f"python tesla_fleet_api ${VERSION}",
117
+ "X-Library": f"python tesla_fleet_api {VERSION}",
118
118
  },
119
119
  json=json,
120
120
  params=params,
@@ -2,12 +2,18 @@ import aiohttp
2
2
  from aiolimiter import AsyncLimiter
3
3
  from typing import Any
4
4
  from .teslafleetapi import TeslaFleetApi
5
+ from .charging import Charging
6
+ from .energy import Energy
7
+ from .partner import Partner
8
+ from .user import User
9
+ from .vehicle import Vehicle
10
+ from .vehiclespecific import VehicleSpecific
11
+
5
12
  from .const import Method, LOGGER, Scope
6
13
 
7
14
  # Rate limit should be global, even if multiple instances are created
8
15
  rate_limit = AsyncLimiter(5, 10)
9
16
 
10
-
11
17
  class Teslemetry(TeslaFleetApi):
12
18
  def __init__(
13
19
  self,
@@ -16,11 +22,11 @@ class Teslemetry(TeslaFleetApi):
16
22
  ):
17
23
  """Initialize the Teslemetry API."""
18
24
  super().__init__(
19
- session,
20
- access_token,
25
+ session=session,
26
+ access_token=access_token,
21
27
  server="https://api.teslemetry.com",
22
- partner_scope=False,
23
28
  user_scope=False,
29
+ partner_scope=False
24
30
  )
25
31
  self.rate_limit = rate_limit
26
32
 
@@ -38,6 +44,14 @@ class Teslemetry(TeslaFleetApi):
38
44
  "api/test",
39
45
  )
40
46
 
47
+ async def userdata(self) -> dict[str, Any]:
48
+ """Get userdata."""
49
+ resp = await self._request(
50
+ Method.GET,
51
+ "api/userdata",
52
+ )
53
+ return resp
54
+
41
55
  async def metadata(self, update_region=True) -> dict[str, Any]:
42
56
  """Get user metadata including scopes."""
43
57
  resp = await self._request(
@@ -49,7 +63,7 @@ class Teslemetry(TeslaFleetApi):
49
63
  self.server = f"https://{self.region}.teslemetry.com"
50
64
  LOGGER.debug("Using server %s", self.server)
51
65
  return resp
52
-
66
+
53
67
  async def scopes(self) -> list[str]:
54
68
  """Get user scopes."""
55
69
  resp = await self.metadata(False)
@@ -61,6 +75,23 @@ class Teslemetry(TeslaFleetApi):
61
75
  assert self.region
62
76
  return self.region
63
77
 
78
+ async def server_side_polling(self, vin: str, value: bool | None = None) -> bool | None:
79
+ """Get or set Auto mode."""
80
+ if value is True:
81
+ return (await self._request(
82
+ Method.POST,
83
+ f"api/auto/{vin}",
84
+ )).get("response")
85
+ if value is False:
86
+ return (await self._request(
87
+ Method.DELETE,
88
+ f"api/auto/{vin}",
89
+ )).get("response")
90
+ return (await self._request(
91
+ Method.GET,
92
+ f"api/auto/{vin}",
93
+ )).get("response")
94
+
64
95
  async def _request(
65
96
  self,
66
97
  method: Method,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tesla_fleet_api
3
- Version: 0.7.2
3
+ Version: 0.7.4
4
4
  Summary: Tesla Fleet API library for Python
5
5
  Home-page: https://github.com/Teslemetry/tesla_fleet_api
6
6
  Author: Brett Adams
@@ -1,20 +1,21 @@
1
1
  tesla_fleet_api/__init__.py,sha256=qGS4Qfp0uHaeK8UXOQVYjZ9rs_iR-CJ6rfCMLswphKA,667
2
2
  tesla_fleet_api/charging.py,sha256=N_mc8axrXj3iduqLj_jCt4Vx86tHqe3xqQT4R1R7HvU,1689
3
- tesla_fleet_api/const.py,sha256=oMxY97yVTIRA94E2AejYiZ-Xe8WCEB9NPXaT2ufjXLc,9277
3
+ tesla_fleet_api/const.py,sha256=ASJgrHCP2DWy3GL9RgQ_ZZ5fVrkF12WTcyIHlpOxdxI,9277
4
4
  tesla_fleet_api/energy.py,sha256=yOKNPyXWKMT7Z9a5RqdNksHokLRo657SWjly1bP2_Qs,5827
5
5
  tesla_fleet_api/energyspecific.py,sha256=zggN-q0tf6EH_57bM4EuQ-IdcemKP0o-xv__LArRNnA,4147
6
6
  tesla_fleet_api/exceptions.py,sha256=0-qeJUfyUGUcm2R3_W4OuGImgshY92ApSixl_CRM8ak,11171
7
7
  tesla_fleet_api/partner.py,sha256=1vIBUaxKLIfqcC0X6VXZN0dMAzj_CLNPUMjA6QVqZ1k,1223
8
- tesla_fleet_api/teslafleetapi.py,sha256=EJyfUq5X-rx7ruR6hKlSMLXLERLj2SBxT-YemiCIHcs,5490
8
+ tesla_fleet_api/ratecalculator.py,sha256=4lz8yruUeouHXh_3ezsXX-CTpIegp1T1J4VuRV_qdHA,1791
9
+ tesla_fleet_api/teslafleetapi.py,sha256=U6kRKS0yYztQuXPcslhZxH15-G1mPMXoFKEcwKtuVY4,5496
9
10
  tesla_fleet_api/teslafleetoauth.py,sha256=OY9yRQuokYo3ts0C8Qb6Z-o9NNAGHbX9F5mHfAh50fo,4121
10
11
  tesla_fleet_api/teslafleetopensource.py,sha256=TJfVPcqJlA1b3kMoGuLr-g5Gn8UDyYsTZhjvGY1MtIk,2007
11
- tesla_fleet_api/teslemetry.py,sha256=kcZG7O9tsBt0BoUyCUSU9j9yCbN1qJDL1iUUo4DFESs,2167
12
+ tesla_fleet_api/teslemetry.py,sha256=tXGfZmF0lO3fdgdoR2T8mXlWuF4dPKZebGijkwQN5Mk,3138
12
13
  tesla_fleet_api/tessie.py,sha256=4dBYxe1G2v9JvJGRbb01wXrAmvWT4jOfV4f_VQE_vkE,2302
13
14
  tesla_fleet_api/user.py,sha256=TZE2oh-n5zrhKXmGRuiNL9voKVODD7rBhGE_IObYVGA,1179
14
15
  tesla_fleet_api/vehicle.py,sha256=KFFotHSmzaC4MhIlU8hoG7SVvPiV3_FC__uJf8BG_G0,31412
15
16
  tesla_fleet_api/vehiclespecific.py,sha256=Nr4zZzfmIuw3RFYjQEX6c_xtYZgztMsN5ohVn-YEH0I,20600
16
- tesla_fleet_api-0.7.2.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
- tesla_fleet_api-0.7.2.dist-info/METADATA,sha256=yBS2vyPI8A7jgI2XNqDUsDangdsuAB60-0ZCO5KhzZE,3821
18
- tesla_fleet_api-0.7.2.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
19
- tesla_fleet_api-0.7.2.dist-info/top_level.txt,sha256=jeNbog_1saXBFrGpom9WyPWmilxsyP3szL_G7JLWQfM,16
20
- tesla_fleet_api-0.7.2.dist-info/RECORD,,
17
+ tesla_fleet_api-0.7.4.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
18
+ tesla_fleet_api-0.7.4.dist-info/METADATA,sha256=86anwGAk_8eZvWjidXnuQaWR6Kv293SE0vBL2G9LY3k,3821
19
+ tesla_fleet_api-0.7.4.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
20
+ tesla_fleet_api-0.7.4.dist-info/top_level.txt,sha256=jeNbog_1saXBFrGpom9WyPWmilxsyP3szL_G7JLWQfM,16
21
+ tesla_fleet_api-0.7.4.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.3.0)
2
+ Generator: setuptools (74.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5