-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubaru_link_service.py
119 lines (103 loc) · 4.04 KB
/
subaru_link_service.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from subarulink import Controller
from subarulink import SubaruException
from aiohttp import ClientSession
import os
import asyncio
import logging
SUBARU_USERNAME=""
SUBARU_PASSWORD=""
SUBARU_DEVICE_ID=""
SUBARU_DEVICE_NAME="subarulink"
SUBARU_VIN=""
SUBARU_PIN=""
LOGGER = logging.getLogger("subarulink")
STREAMHANDLER = logging.StreamHandler()
STREAMHANDLER.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
LOGGER.addHandler(STREAMHANDLER)
LOOP = asyncio.get_event_loop()
class SubaruLinkService():
def __init__(self):
self._current_vin = SUBARU_VIN
self.__ctrl = None
self.attributes = None
self._pin = None
self._car_data = None
self._car_info = None
@property
def _ctrl(self):
if self.__ctrl is None:
self._session = ClientSession()
self._cars = []
self.__ctrl = Controller(
self._session, # aiohttp
SUBARU_USERNAME,
SUBARU_PASSWORD,
SUBARU_DEVICE_ID,
self._pin,
SUBARU_DEVICE_NAME,
)
return self.__ctrl
async def connect(self, pin):
LOGGER.info("Connecting to Subaru Remote Services API")
self._pin = pin
try:
if await self._ctrl.connect():
self._current_hasEV = self._ctrl.get_ev_status(self._current_vin)
self._current_hasRES = self._ctrl.get_res_status(self._current_vin)
self._current_hasRemote = self._ctrl.get_remote_status(self._current_vin)
self._current_api_gen = self._ctrl.get_api_gen(self._current_vin)
LOGGER.info("Successfully connected")
if self._current_api_gen == "g2":
await self._fetch()
else:
LOGGER.error("Unsupprted telematics version: %s" % self._current_api_gen)
return False
except SubaruException:
LOGGER.error("Unable to connect. Check Username/Password.")
await self._session.close()
return False
return True
async def disconnect(self):
await self._session.close()
async def _fetch(self):
LOGGER.info("Fetching data for %s..." % self._ctrl.vin_to_name(self._current_vin))
self._car_data = await self._ctrl.get_data(self._current_vin)
return True
async def update(self):
LOGGER.info("Requesting update for %s..." % self._ctrl.vin_to_name(self._current_vin))
await self._ctrl.update(self._current_vin)
await self._fetch()
return True
async def unlock(self):
LOGGER.info("Requesting door unlock for %s..." % self._ctrl.vin_to_name(self._current_vin))
if await self._ctrl.unlock(self._current_vin):
self._car_data['doors'] = 'unlocked'
return True
else:
return False
async def lock(self):
LOGGER.info("Requesting door lock for %s..." % self._ctrl.vin_to_name(self._current_vin))
if await self._ctrl.lock(self._current_vin):
self._car_data['doors'] = 'locked'
return True
else:
return False
async def start_engine(self):
LOGGER.info("Requesting engine START for %s..." % self._ctrl.vin_to_name(self._current_vin))
if self._car_data.get("climate") is None:
await self._ctrl.get_climate_settings(self._current_vin)
self._car_data = await self._ctrl.get_data(self._current_vin)
await self._ctrl.remote_start(self._current_vin, self._car_data["climate"])
async def stop_engine(self):
LOGGER.info("Requesting engine STOP for %s..." % self._ctrl.vin_to_name(self._current_vin))
await self._ctrl.remote_stop(self._current_vin)
@property
def car_data(self):
return self._car_data
async def test():
sls = SubaruLinkService()
if await sls.connect(SUBARU_PIN):
print(sls._car_data)
await sls.disconnect()
if __name__ == '__main__':
LOOP.run_until_complete(test())