forked from zabuldon/teslajsonpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
1350 lines (1181 loc) · 51.7 KB
/
controller.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-License-Identifier: Apache-2.0
"""
Python Package for controlling Tesla API.
For more details about this api, please refer to the documentation at
https://github.com/zabuldon/teslajsonpy
"""
import asyncio
import logging
import pkgutil
import time
from json import JSONDecodeError
from typing import Dict, List, Optional, Set, Text
import httpx
import orjson
from tenacity import retry, stop_after_delay
from yarl import URL
from teslajsonpy.car import TeslaCar
from teslajsonpy.connection import Connection
from teslajsonpy.const import (
AUTH_DOMAIN,
DRIVING_INTERVAL,
IDLE_INTERVAL,
MAX_API_RETRY_TIME,
ONLINE_INTERVAL,
RESOURCE_TYPE,
RESOURCE_TYPE_BATTERY,
RESOURCE_TYPE_SOLAR,
SLEEP_INTERVAL,
UPDATE_INTERVAL,
WAKE_CHECK_INTERVAL,
WAKE_TIMEOUT,
)
from teslajsonpy.energy import EnergySite, PowerwallSite, SolarPowerwallSite, SolarSite
from teslajsonpy.exceptions import (
TeslaException,
custom_retry,
custom_retry_except_unavailable,
custom_wait,
)
_LOGGER = logging.getLogger(__name__)
def valid_result(result):
"""Check if TeslaAPI result successful.
Parameters
----------
result : tesla API result
This is the result of a Tesla Rest API call.
Returns
-------
bool
Tesla API failure can be checked in a dict with a bool in
['response']['result'], a bool, or None or
['response']['reason'] == 'could_not_wake_buses'
Returns true when a failure state not detected.
"""
try:
return (
result is not None
and result is not False
and (
result is True
or (
isinstance(result, dict)
and (
(
isinstance(result["response"], dict)
and (
result["response"].get("result") is True
or result["response"].get("reason")
!= "could_not_wake_buses"
)
)
or isinstance(result.get("response"), list)
)
)
)
)
except TypeError as exception:
_LOGGER.error("Result: %s, %s", result, exception)
return False
class Controller:
# pylint: disable=too-many-public-methods
"""Controller for connections to Tesla Motors API."""
def __init__(
self,
websession: Optional[httpx.AsyncClient] = None,
email: Text = None,
password: Text = None,
access_token: Text = None,
refresh_token: Text = None,
expiration: int = 0,
update_interval: int = UPDATE_INTERVAL,
enable_websocket: bool = False,
polling_policy: Text = None,
auth_domain: str = AUTH_DOMAIN,
) -> None:
"""Initialize controller.
Args:
websession (aiohttp.ClientSession): Websession for aiohttp.
email (Text, optional): Email account. Defaults to None.
password (Text, optional): Password. Defaults to None.
access_token (Text, optional): Access token. Defaults to None.
refresh_token (Text, optional): Refresh token. Defaults to None.
expiration (int, optional): Timestamp when access_token expires. Defaults to 0
update_interval (int, optional): Seconds between allowed updates to the API. This is to prevent
being blocked by Tesla. Defaults to UPDATE_INTERVAL.
enable_websocket (bool, optional): Whether to connect with websockets. Defaults to False.
polling_policy (Text, optional): How aggressively will we poll the car. Possible values:
Not set - Only keep the car awake while it is actively charging or driving, and while sentry
mode is enabled (default).
'connected' - Also keep the car awake while it is connected to a charger, even if the charging
session is complete.
'always' - Keep polling the car at all times. Will possibly never allow the car to sleep.
auth_domain (str, optional): The authentication domain. Defaults to const.AUTH_DOMAIN
"""
self.__connection = Connection(
websession=websession
if websession and isinstance(websession, httpx.AsyncClient)
else httpx.AsyncClient(timeout=60),
email=email,
password=password,
access_token=access_token,
refresh_token=refresh_token,
expiration=expiration,
auth_domain=auth_domain,
)
self._update_interval: int = update_interval
self._update_interval_vin = {}
self.__update = {}
self.__driving = {} # for websocket timestamp only
self._last_update_time = {} # succesful update attempts by car
self._last_wake_up_time = {} # succesful wake_ups by car
self._last_attempted_update_time = 0 # all attempts by controller
self.__lock = {}
self.__update_lock = None # controls access to update function
self.__wakeup_lock = {}
self.car_online = {}
self.__id_vin_map = {}
self.__vin_id_map = {}
self.__vin_vehicle_id_map = {}
self.__vehicle_id_vin_map = {}
self.__websocket_listeners = []
self.__last_parked_timestamp = {}
self.__update_state = {}
self.enable_websocket = enable_websocket
self.endpoints = {}
self.polling_policy = polling_policy
self._include_vehicles: bool = True
self._include_energysites: bool = True
self._product_list: List[dict] = []
self._vehicle_list: List[dict] = []
self._vehicle_data: Dict[str, dict] = {}
self._energysite_list: List[dict] = []
self._site_config: Dict[int, dict] = {}
self._site_data: Dict[int, dict] = {}
self._site_summary: Dict[int, dict] = {}
self._grid_status_unknown: Dict[int, bool] = {}
self.cars: Dict[str, TeslaCar] = {}
self.energysites: Dict[int, EnergySite] = {}
async def connect(
self,
test_login: bool = False,
include_vehicles: bool = True,
include_energysites: bool = True,
mfa_code: Text = "",
) -> Dict[Text, Text]:
"""Connect controller to Tesla.
Args
test_login (bool, optional): Whether to test credentials only. Defaults to False.
include_vehicles (bool, optional): Whether to include vehicles. Defaults to True.
include_energysites(bool, optional): Whether to include energysites. Defaults to True.
mfa_code (Text, optional): MFA code to use for connection
Returns
Dict[Text, Text]: Returns the refresh_token, access_token, id_token and expires_in time
"""
if mfa_code:
self.__connection.mfa_code = mfa_code
self._last_attempted_update_time = round(time.time())
self.__update_lock = asyncio.Lock()
if not test_login:
self._product_list = await self.get_product_list()
if include_vehicles:
self._vehicle_list = [
cars for cars in self._product_list if "vehicle_id" in cars
]
if include_energysites:
self._energysite_list = [
p
for p in self._product_list
if p.get(RESOURCE_TYPE) == RESOURCE_TYPE_SOLAR
or p.get(RESOURCE_TYPE) == RESOURCE_TYPE_BATTERY
]
return {
"refresh_token": self.__connection.refresh_token,
"access_token": self.__connection.access_token,
"expiration": self.__connection.expiration,
"id_token": self.__connection.id_token,
}
async def disconnect(self) -> None:
"""Disconnect from Tesla api."""
_LOGGER.debug("Disconnecting controller.")
await self.__connection.close()
def is_token_refreshed(self) -> bool:
"""Return whether token has been changed and not retrieved.
Returns
bool: Whether token has been changed since the last return
"""
return self.__connection.token_refreshed
def get_tokens(self) -> Dict[Text, Text]:
"""Return oauth data including refresh and access tokens, and expires time.
This will set the the self.__connection token_refreshed to False.
Returns
Dict[Text, Text]: Returns the refresh_token, access_token, id_token and expires time
"""
self.__connection.token_refreshed = False
return {
"refresh_token": self.__connection.refresh_token,
"access_token": self.__connection.access_token,
"expiration": self.__connection.expiration,
"id_token": self.__connection.id_token,
}
def get_expiration(self) -> int:
"""Return expiration for oauth.
Returns
int: Returns timestamp when oauth expires
"""
return self.__connection.expiration
def get_oauth_url(self) -> URL:
"""Return oauth url."""
return self.__connection.get_authorization_code_link(new=True)
def set_authorization_code(self, code: Text) -> None:
"""Set authorization code in Connection."""
self.__connection.code = code
def set_authorization_domain(self, domain: Text) -> None:
"""Set authorization domain in Connection."""
if not domain:
return
if self.__connection.auth_domain.host != domain:
self.__connection.auth_domain = self.__connection.auth_domain.with_host(
domain
)
def register_websocket_callback(self, callback) -> int:
"""Register callback for websocket messages.
Args
callback (function): function to call with json data
Returns
int: Return index of entry
"""
self.__websocket_listeners.append(callback)
return len(self.__websocket_listeners) - 1
async def get_product_list(self) -> list:
"""Get product list from Tesla."""
return (await self.api("PRODUCT_LIST"))["response"]
async def get_site_config(self, energysite_id: int) -> dict:
"""Get site config json from TeslaAPI for a given energysite_id."""
return (await self.api("SITE_CONFIG", path_vars={"site_id": energysite_id}))[
"response"
]
async def get_vehicle_data(self, vin: str, wake_if_asleep: bool = False) -> dict:
"""Get vehicle data json from TeslaAPI for a given vin."""
try:
response = (
await self.api(
"VEHICLE_DATA",
path_vars={"vehicle_id": self._vin_to_id(vin)},
wake_if_asleep=wake_if_asleep,
)
)["response"]
except TeslaException as ex:
if ex.message == "VEHICLE_UNAVAILABLE":
_LOGGER.debug("Vehicle offline - data unavailable.")
return {}
raise ex
return response
async def get_vehicle_summary(self, vin: str) -> dict:
"""Get vehicle summary json from TeslaAPI for a given vin."""
return (
await self.api(
"VEHICLE_SUMMARY",
path_vars={"vehicle_id": self._vin_to_id(vin)},
wake_if_asleep=False,
)
)["response"]
async def get_site_data(self, energysite_id: int) -> dict:
"""Get site data json from TeslaAPI for a given energysite_id."""
return (await self.api("SITE_DATA", path_vars={"site_id": energysite_id}))[
"response"
]
async def get_site_summary(self, energysite_id: int) -> dict:
"""Get site data json from TeslaAPI for a given energysite_id."""
return (await self.api("SITE_SUMMARY", path_vars={"site_id": energysite_id}))[
"response"
]
async def generate_car_objects(
self,
wake_if_asleep: bool = False,
filtered_vins: Optional[List[Text]] = None,
) -> Dict[str, TeslaCar]:
"""Generate car objects.
Args
wake_if_asleep (bool, optional): Wake up vehicles if asleep.
filtered_vins (list, optional): If not empty, filters the cars by the provided VINs.
"""
for car in self._vehicle_list:
vin = car["vin"]
if filtered_vins and vin not in filtered_vins:
_LOGGER.debug("Skipping car with VIN: %s", vin)
continue
self.set_id_vin(car_id=car["id"], vin=vin)
self.set_vehicle_id_vin(vehicle_id=car["vehicle_id"], vin=vin)
self.__lock[vin] = asyncio.Lock()
self.__wakeup_lock[vin] = asyncio.Lock()
self._last_update_time[vin] = 0
self._last_wake_up_time[vin] = 0
self.__update[vin] = True
self.__update_state[vin] = "normal"
self.set_car_online(vin=vin, online_status=car["state"] == "online")
self.set_last_park_time(vin=vin, timestamp=self._last_attempted_update_time)
self.__driving[vin] = {}
self._vehicle_data[vin] = {}
try:
self._vehicle_data[vin] = await self.get_vehicle_data(
vin, wake_if_asleep=wake_if_asleep
)
except TeslaException as ex:
_LOGGER.warning(
"Unable to get vehicle data during setup, car will still be added. %s: %s",
ex.code,
ex.message,
)
self.cars[vin] = TeslaCar(car, self, self._vehicle_data[vin])
return self.cars
async def generate_energysite_objects(self) -> Dict[int, EnergySite]:
"""Generate energy site objects."""
for energysite in self._energysite_list:
energysite_id = energysite["energy_site_id"]
self._site_config[energysite_id] = await self.get_site_config(energysite_id)
# For dealing with sites that always report "Unknown"
# Default to True and check during updates
self._grid_status_unknown = {energysite_id: True}
# Solar only systems (no Powerwalls) are listed as "solar"
try:
self._site_data[energysite_id] = await self.get_site_data(
energysite_id
)
except TeslaException as ex:
_LOGGER.warning(
"Unable to get site data during setup, site will still be added. %s: %s",
ex.code,
ex.message,
)
self._site_data[energysite_id] = {}
if energysite[RESOURCE_TYPE] == RESOURCE_TYPE_SOLAR:
self.energysites[energysite_id] = SolarSite(
self.api,
energysite,
self._site_config[energysite_id],
self._site_data[energysite_id],
)
# Powerwall systems listed as "battery"
if energysite[RESOURCE_TYPE] == RESOURCE_TYPE_BATTERY:
self._site_summary[energysite_id] = await self.get_site_summary(
energysite_id
)
if energysite["components"]["solar"]:
self.energysites[energysite_id] = SolarPowerwallSite(
self.api,
energysite,
self._site_config[energysite_id],
self._site_data[energysite_id],
self._site_summary[energysite_id],
)
else:
self.energysites[energysite_id] = PowerwallSite(
self.api,
energysite,
self._site_config[energysite_id],
self._site_data[energysite_id],
self._site_summary[energysite_id],
)
return self.energysites
async def wake_up(self, car_id) -> bool:
"""Attempt to wake the car, returns True if successfully awakened."""
car_vin = self._id_to_vin(car_id)
car_id = self._update_id(car_id)
async with self.__wakeup_lock[car_vin]:
wake_start_time = time.time()
wake_deadline = wake_start_time + WAKE_TIMEOUT
_LOGGER.debug(
"%s: Sending wake request with deadline of: %d",
car_vin[-5:],
wake_deadline,
)
result = await self.api(
"WAKE_UP", path_vars={"vehicle_id": car_id}, wake_if_asleep=False
)
state = result.get("response", {}).get("state")
self.set_car_online(
car_id=car_id,
online_status=state == "online",
)
while not self.is_car_online(vin=car_vin) and time.time() < wake_deadline:
await asyncio.sleep(WAKE_CHECK_INTERVAL)
response = await self.get_vehicle_summary(vin=car_vin)
state = response.get("state")
self.set_car_online(
car_id=car_id,
online_status=state == "online",
)
_LOGGER.debug(
"%s: Wakeup took %d seconds, state: %s",
car_vin[-5:],
time.time() - wake_start_time,
state,
)
return self.is_car_online(vin=car_vin)
def _calculate_next_interval(self, vin: Text) -> int:
cur_time = round(time.time())
_LOGGER.debug(
"%s: %s. Polling policy: %s. Update state: %s. Since last park: %s. Since last wake up: %s. Idle interval: %s. shift_state: %s sentry: %s climate: %s, charging: %s ",
vin[-5:],
self.cars[vin].state,
self.polling_policy,
self.__update_state.get(vin),
cur_time - self.get_last_park_time(vin=vin),
cur_time - self.get_last_wake_up_time(vin=vin),
IDLE_INTERVAL,
self.cars[vin].shift_state,
self.cars[vin].sentry_mode,
self.cars[vin].is_climate_on,
self.cars[vin].charging_state,
)
if vin not in self.__update_state:
self.__update_state[vin] = "normal"
if self.cars[vin].state == "asleep" or self.cars[vin].shift_state:
self.set_last_park_time(
vin=vin, timestamp=cur_time, shift_state=self.cars[vin].shift_state
)
if self.cars[vin].is_in_gear:
driving_interval = min(
DRIVING_INTERVAL, self.get_update_interval_vin(vin=vin)
)
if self.__update_state[vin] != "driving":
self.__update_state[vin] = "driving"
_LOGGER.debug(
"%s driving; increasing scan rate to every %s seconds",
vin[-5:],
driving_interval,
)
return driving_interval
if self.polling_policy == "always":
_LOGGER.debug(
"%s: %s; Polling policy set to '%s'. Scanning every %s seconds",
vin[-5:],
self.cars[vin].state,
self.polling_policy,
self.get_update_interval_vin(vin=vin),
)
self.__update_state[vin] = "normal"
return self.get_update_interval_vin(vin=vin)
if self.polling_policy == "connected" and (
self.cars[vin].sentry_mode
or self.cars[vin].is_climate_on
or (
self.cars[vin].charging_state
and self.cars[vin].charging_state != "Disconnected"
and self.cars[vin].charging_state != ""
)
or cur_time - self.get_last_wake_up_time(vin=vin) <= IDLE_INTERVAL
):
_LOGGER.debug(
"%s: %s; Polling policy set to '%s' and car is connected. "
"or last_wake_up_time < IDLE_INTERVAL "
"Polling every %s seconds",
vin[-5:],
self.cars[vin].state,
self.polling_policy,
self.get_update_interval_vin(vin=vin),
)
self.__update_state[vin] = "normal"
return self.get_update_interval_vin(vin=vin)
if (cur_time - self.get_last_park_time(vin=vin) > IDLE_INTERVAL) and not (
self.cars[vin].sentry_mode
or self.cars[vin].is_climate_on
or self.cars[vin].charging_state == "Charging"
):
sleep_interval = max(SLEEP_INTERVAL, self.get_update_interval_vin(vin=vin))
if self.__update_state[vin] != "trying_to_sleep":
self.__update_state[vin] = "trying_to_sleep"
_LOGGER.debug(
"%s: %s; Polling policy set to '%s', trying to sleep; scan throttled to %s seconds and will ignore updates for %s seconds",
vin[-5:],
self.cars[vin].state,
self.polling_policy,
sleep_interval,
sleep_interval + self._last_update_time[vin] - cur_time,
)
return sleep_interval
if self.__update_state[vin] != "normal":
self.__update_state[vin] = "normal"
_LOGGER.debug(
"%s: %s; Polling policy set to '%s', scanning every %s seconds",
vin[-5:],
self.cars[vin].state,
self.polling_policy,
self.get_update_interval_vin(vin=vin),
)
return self.get_update_interval_vin(vin=vin)
async def update(
self,
car_id: Optional[Text] = None,
wake_if_asleep: bool = False,
force: bool = False,
update_vehicles: bool = True,
vins: Optional[Set[str]] = None,
energy_site_ids: Optional[Set[str]] = None,
) -> bool:
# pylint: disable=too-many-locals,too-many-statements
"""Update all vehicle and energy site attributes in the cache.
This command will connect to the Tesla API and first update the list of
online vehicles assuming no attempt for at least the [update_interval].
It will then update all the cached values for cars that are awake
assuming no update has occurred for at least the [update_interval].
For energy sites, they will only be updated if car_id is blank.
Args
car_id (Text, optional): The vehicle to update. If None, all cars are updated. Defaults to None.
wake_if_asleep (bool, optional): force a vehicle awake. Defaults to False.
force (bool, optional): force a vehicle update regardless of the update_interval. Defaults to False.
vins: (Set[str], optional): The cars to update. If None, all cars are updated. Defaults to None.
energy_site_ids: (Set[str], optional): The energy sites to update. If None, all sites are updated. Defaults to None.
Returns
Whether update was successful.
"""
tasks = []
async def _get_and_process_car_data(vin: str) -> None:
async with self.__lock[vin]:
_LOGGER.debug("%s: Updating VEHICLE_DATA", vin[-5:])
try:
response = await self.get_vehicle_data(
vin, wake_if_asleep=wake_if_asleep
)
except TeslaException as ex:
# VEHICLE_UNAVAILABLE is handled in get_vehicle_data as debug and ignore
# Anything else would be caught here and logged as a warning
_LOGGER.warning(
"Unable to get vehicle data during poll. %s: %s",
ex.code,
ex.message,
)
response = None
if response:
if (
self.cars[vin].is_climate_on
and self.cars[vin].is_climate_on
!= response["drive_state"]["shift_state"]
and (
response["drive_state"]["shift_state"] is None
or response["drive_state"]["shift_state"] == "P"
)
):
self.set_last_park_time(
vin=vin,
timestamp=response["drive_state"]["timestamp"] / 1000,
shift_state=response["drive_state"]["shift_state"],
)
self._last_update_time[vin] = round(time.time())
if self.enable_websocket and self.cars[vin].is_in_gear:
asyncio.create_task(
self.__connection.websocket_connect(
vin[-5:],
self.vin_to_vehicle_id(vin=vin),
on_message=self._process_websocket_message,
on_disconnect=self._process_websocket_disconnect,
)
)
self._vehicle_data[vin].update(response)
async def _get_and_process_site_data(energysite_id: int) -> None:
_LOGGER.debug("Updating SITE_DATA for energysite: %s", energysite_id)
try:
response = await self.get_site_data(energysite_id)
except TeslaException:
response = None
if response:
# Some setups always report grid_status of "Unknown" regardless
# of the actual grid status. Others only report grid_status "Unknown"
# when the actual grid status is unknown. These setups also sometimes
# report an incorrect solar_power value of 0.
if (
"grid_status" not in response
or response.get("grid_status") != "Unknown"
):
self._grid_status_unknown[energysite_id] = False
if (
energysite_id in self._grid_status_unknown
and not self._grid_status_unknown[energysite_id]
and (
response.get("grid_status") == "Unknown"
and response.get("solar_power") == 0
)
):
_LOGGER.debug(
"Ignoring possible spurious energy site solar power read."
)
del response["solar_power"]
self._site_data[energysite_id].update(response)
async def _get_and_process_site_summary(energysite_id: int) -> None:
_LOGGER.debug("Updating SITE_SUMMARY for energysite: %s", energysite_id)
try:
response = await self.get_site_summary(energysite_id)
except TeslaException:
response = None
if response:
self._site_summary[energysite_id].update(response)
async def _get_and_process_site_config(energysite_id: int) -> None:
_LOGGER.debug("Updating SITE_CONFIG for energysite: %s", energysite_id)
try:
response = await self.get_site_config(energysite_id)
except TeslaException:
response = None
if response:
self._site_config[energysite_id].update(response)
async with self.__update_lock:
if self._vehicle_list:
cur_time = round(time.time())
# Update the online cars using get_product_list()
last_update = self._last_attempted_update_time
_LOGGER.debug(
"Get vehicles. Force: %s Time: %s Interval %s",
force,
cur_time - last_update,
ONLINE_INTERVAL,
)
if (
force
or cur_time - last_update >= ONLINE_INTERVAL
and update_vehicles
):
self._product_list = await self.get_product_list()
self._vehicle_list = [
cars for cars in self._product_list if "vehicle_id" in cars
]
for car in self._vehicle_list:
self.set_id_vin(car_id=car["id"], vin=car["vin"])
self.set_vehicle_id_vin(
vehicle_id=car["vehicle_id"], vin=car["vin"]
)
self.set_car_online(
vin=car["vin"], online_status=car["state"] == "online"
)
self.cars[car["vin"]].update_car_info(car)
self._last_attempted_update_time = cur_time
# Only update online vehicles that haven't been updated recently
# The throttling is per car's last succesful update
# Note: This separate check is because there may be individual cars
# to update.
car_id = self._update_id(car_id)
car_vin = self._id_to_vin(car_id)
for vin, online in self.get_car_online().items():
# If specific car_id provided, only update match
if (
(car_vin and car_vin != vin)
or vin not in self.__lock
or (vin and self.cars[vin].in_service)
):
continue
if isinstance(vins, set) and vin not in vins:
continue
async with self.__lock[vin]:
if (
(
online
or (
wake_if_asleep
and self.cars[vin].state in ["asleep", "offline"]
)
)
and ( # pylint: disable=too-many-boolean-expressions
self.__update.get(vin)
) # Only update cars with update flag on
and (
force
or vin not in self._last_update_time
or (
cur_time - self._last_update_time[vin]
>= self._calculate_next_interval(vin)
)
)
):
tasks.append(_get_and_process_car_data(vin))
else:
_LOGGER.debug(
(
"%s: Skipping update with state %s. Polling: %s. "
"Last update: %s ago. Last parked: %s ago. "
"Last wake up %s ago. "
),
vin[-5:],
self.cars[vin].state,
self.__update.get(vin),
cur_time - self._last_update_time[vin],
cur_time - self.get_last_park_time(vin=vin),
cur_time - self.get_last_wake_up_time(vin=vin),
)
if self._energysite_list and not car_id:
# do not update energy sites if car_id was a parameter.
for energysite in self._energysite_list:
energysite_id = energysite["energy_site_id"]
if (
isinstance(energy_site_ids, set)
and energysite_id not in energy_site_ids
):
continue
tasks.append(_get_and_process_site_data(energysite_id))
tasks.append(_get_and_process_site_config(energysite_id))
if energysite[RESOURCE_TYPE] == RESOURCE_TYPE_BATTERY:
tasks.append(_get_and_process_site_summary(energysite_id))
return any(await asyncio.gather(*tasks))
def get_updates(self, car_id: Text = None, vin: Text = None):
"""Get updates dictionary.
Parameters
----------
car_id : string
Identifier for the car on the owner-api endpoint. It is the id
field for identifying the car across the owner-api endpoint.
https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id
vin : string
VIN number.
If both car_id and vin is provided. VIN overrides car_id.
Returns
-------
bool or dict of booleans
If car_id or vin exists, a bool indicating whether updates should be
processed. Othewise, the entire updates dictionary.
"""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin and vin in self.__update:
return self.__update[vin]
return self.__update
def set_updates(
self, car_id: Text = None, vin: Text = None, value: bool = False
) -> None:
"""Set updates dictionary.
If a vehicle is enabled, the vehicle will force an update on next poll.
Parameters
----------
car_id : string
Identifier for the car on the owner-api endpoint. Confusingly it
is not the vehicle_id field for identifying the car across
different endpoints.
https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id
vin : string
Vin number
value : bool
Whether the specific car_id should be updated.
Returns
-------
None
"""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin:
self.__update[vin] = value
if self.__update[vin]:
self.set_last_update_time(vin=vin)
_LOGGER.debug(
"%s: Set Updates enabled; forcing update on next poll by resetting last_update_time",
vin[-5:],
)
def get_last_update_time(self, car_id: Text = None, vin: Text = None):
"""Get last_update time dictionary.
Parameters
----------
car_id : string
Identifier for the car on the owner-api endpoint. It is the id
field for identifying the car across the owner-api endpoint.
https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id
If no car_id, returns the complete dictionary.
vin : string
Vin number
Returns
-------
int or dict of ints
If car_id exists, a int (time.time()) indicating when updates last
processed. Othewise, the entire updates dictionary.
"""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin and vin in self._last_update_time:
return self._last_update_time[vin]
return self._last_update_time
def set_last_update_time(
self, car_id: Text = None, vin: Text = None, timestamp: float = 0
) -> None:
"""Set updated_time for car_id."""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin:
self._last_update_time[vin] = timestamp
def get_last_park_time(self, car_id: Text = None, vin: Text = None):
"""Get park_time.
Parameters
----------
car_id : string
Identifier for the car on the owner-api endpoint. It is the id
field for identifying the car across the owner-api endpoint.
https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id
If no car_id, returns the complete dictionary.
vin : string
Vin number
Returns
-------
int or dict of ints
If car_id exists, a int (time.time()) indicating when car was last
parked. Othewise, the entire updates dictionary.
"""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin and vin in self.__last_parked_timestamp:
return self.__last_parked_timestamp[vin]
return self.__last_parked_timestamp
def set_last_park_time(
self,
car_id: Text = None,
vin: Text = None,
timestamp: float = 0,
shift_state: Text = None,
) -> None:
"""Set park_time for car_id."""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin:
_LOGGER.debug(
"%s: Resetting last_parked_timestamp to: %s shift_state %s",
vin[-5:],
timestamp,
shift_state,
)
self.__last_parked_timestamp[vin] = timestamp
def get_last_wake_up_time(self, car_id: Text = None, vin: Text = None):
"""Get wakeup_time.
Parameters
----------
car_id : string
Identifier for the car on the owner-api endpoint. It is the id
field for identifying the car across the owner-api endpoint.
https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id
If no car_id, returns the complete dictionary.
vin : string
VIN number
Returns
-------
int or dict of ints
If car_id exists, a int (time.time()) indicating when car was last
waken up. Othewise, the entire updates dictionary.
"""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin and vin in self._last_wake_up_time:
return self._last_wake_up_time[vin]
return self._last_wake_up_time
def set_last_wake_up_time(
self, car_id: Text = None, vin: Text = None, timestamp: float = 0
) -> None:
"""Set wakeup_time for car_id."""
if car_id and not vin:
vin = self._id_to_vin(car_id)
if vin:
_LOGGER.debug("%s: Resetting last_wake_up_time to: %s", vin[-5:], timestamp)
self._last_wake_up_time[vin] = timestamp
def set_car_online(
self, car_id: Text = None, vin: Text = None, online_status: bool = True
) -> None:
"""Set online status for car_id.