-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTessie.py
87 lines (67 loc) · 2.66 KB
/
Tessie.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# pylint: disable=invalid-name
from loguru import logger
from timezonefinder import TimezoneFinder
import arrow, requests
class Tessie:
"""
Class for interacting with the Tessie API
"""
# Internal state
tessie_token = state = vin = session = None
def __init__(self, tessie_token, vin=None):
self.tessie_token = tessie_token
self.vin = vin
def get_vehicle_state(self):
if self.state:
return self.state
state = self.request("state", self.vin)
state_date = arrow.get(state["drive_state"]["timestamp"])
logger.info(f"Retrieved state for {state['display_name']} (#{state['vin']}) as of {state_date.humanize()}")
self.state = state
return state
def check_state(self, key, sub_key, func, message):
# Convenience method to check the value of a state against an expected value
state = self.get_vehicle_state()
data = state[key].get(sub_key)
if func(data):
return True
else:
logger.debug(f"{key}:{sub_key} did not pass test with value: {data}")
raise ValueError(message)
def get_sleep_status(self):
return self.request("status", self.vin)["status"]
def wake_up(self):
sleep_status = self.get_sleep_status()
logger.debug(f"Sleep status is {sleep_status}")
if sleep_status != "awake":
logger.info("Sending wake up")
self.request("wake", self.vin)
logger.success("Awake")
def get_car_time(self):
# For the supplied time, shift from UTC via the lat/long
timezone = TimezoneFinder().timezone_at(
lng=self.state["drive_state"]["longitude"], lat=self.state["drive_state"]["latitude"]
)
return arrow.utcnow().to(timezone)
def are_windows_open(self):
vehicle_state = self.get_vehicle_state()["vehicle_state"]
return any(
[
vehicle_state["fd_window"],
vehicle_state["rd_window"],
vehicle_state["fp_window"],
vehicle_state["rp_window"],
]
)
def request(self, path, vin=None):
# Set up a requests session, which enables keep alive and allows for retries
if self.session is None:
self.session = requests.Session()
base_url = "https://api.tessie.com"
if vin:
base_url = f"{base_url}/{vin}"
url = f"{base_url}/{path}"
logger.trace(f"Requesting {url}")
response = self.session.get(url, headers={"Authorization": f"Bearer {self.tessie_token}"}, timeout=30)
response.raise_for_status()
return response.json()