tesla-fleet-api 0.7.1__py3-none-any.whl → 0.7.3__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,6 +15,7 @@ from .vehiclespecific import VehicleSpecific
15
15
  __all__ = [
16
16
  "TeslaFleetApi",
17
17
  "TeslaFleetOAuth",
18
+ "TeslaFleetOpenSource",
18
19
  "Teslemetry",
19
20
  "Tessie",
20
21
  "Charging",
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.1"
6
+ VERSION = "0.7.3"
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)
@@ -95,7 +95,8 @@ class TeslaFleetApi:
95
95
 
96
96
  # Call a pre-request hook if provided
97
97
  if self.refresh_hook is not None:
98
- await self.refresh_hook()
98
+ if access_token := await self.refresh_hook():
99
+ self.access_token = access_token
99
100
 
100
101
  LOGGER.debug("Sending request to %s", path)
101
102
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tesla_fleet_api
3
- Version: 0.7.1
3
+ Version: 0.7.3
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,11 +1,12 @@
1
- tesla_fleet_api/__init__.py,sha256=-OhrvoMVLeo13dDCjsltCacLxPqe2UODvSgHFdjyteA,639
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=fMx1jUvELzTRrgoTN_OlN-eiQX0ets8E5pIQ8K8mPVQ,9277
3
+ tesla_fleet_api/const.py,sha256=U4i4bRs9pZxTtEtOsA7_Azj0uY5bhTRqeNm5VmBrD_Y,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=725WSE3eeHwTvVY0pXqEiZNP4M5n2Op0c3q5cJIkPeY,5421
8
+ tesla_fleet_api/ratecalculator.py,sha256=4lz8yruUeouHXh_3ezsXX-CTpIegp1T1J4VuRV_qdHA,1791
9
+ tesla_fleet_api/teslafleetapi.py,sha256=EJyfUq5X-rx7ruR6hKlSMLXLERLj2SBxT-YemiCIHcs,5490
9
10
  tesla_fleet_api/teslafleetoauth.py,sha256=OY9yRQuokYo3ts0C8Qb6Z-o9NNAGHbX9F5mHfAh50fo,4121
10
11
  tesla_fleet_api/teslafleetopensource.py,sha256=TJfVPcqJlA1b3kMoGuLr-g5Gn8UDyYsTZhjvGY1MtIk,2007
11
12
  tesla_fleet_api/teslemetry.py,sha256=kcZG7O9tsBt0BoUyCUSU9j9yCbN1qJDL1iUUo4DFESs,2167
@@ -13,8 +14,8 @@ tesla_fleet_api/tessie.py,sha256=4dBYxe1G2v9JvJGRbb01wXrAmvWT4jOfV4f_VQE_vkE,230
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.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
- tesla_fleet_api-0.7.1.dist-info/METADATA,sha256=nazwokUo30zFvNPt79tKCs9Kx0nbrEf3LR0Htv_9QNA,3821
18
- tesla_fleet_api-0.7.1.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
19
- tesla_fleet_api-0.7.1.dist-info/top_level.txt,sha256=jeNbog_1saXBFrGpom9WyPWmilxsyP3szL_G7JLWQfM,16
20
- tesla_fleet_api-0.7.1.dist-info/RECORD,,
17
+ tesla_fleet_api-0.7.3.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
18
+ tesla_fleet_api-0.7.3.dist-info/METADATA,sha256=KBkJZD7_o6mq96thoH_ghzr6TCAUfSqlhU-1PzGULEo,3821
19
+ tesla_fleet_api-0.7.3.dist-info/WHEEL,sha256=Wyh-_nZ0DJYolHNn1_hMa4lM7uDedD_RGVwbmTjyItk,91
20
+ tesla_fleet_api-0.7.3.dist-info/top_level.txt,sha256=jeNbog_1saXBFrGpom9WyPWmilxsyP3szL_G7JLWQfM,16
21
+ tesla_fleet_api-0.7.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.3.0)
2
+ Generator: setuptools (71.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5