-
Notifications
You must be signed in to change notification settings - Fork 64
/
vehicle.py
123 lines (102 loc) · 3.84 KB
/
vehicle.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
# 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 logging
from typing import Dict, Text
_LOGGER = logging.getLogger(__name__)
class VehicleDevice:
"""Home-assistant class of Tesla vehicles.
This is intended to be partially inherited by a Home-Assitant entity.
"""
def __init__(self, data, controller):
"""Initialize the Vehicle.
Parameters
----------
data : dict
The base state for a Tesla vehicle.
https://tesla-api.timdorr.com/vehicle/state/data
controller : teslajsonpy.Controller
The controller that controls updates to the Tesla API.
Returns
-------
None
"""
self._id = data["id"]
self._vehicle_id = data["vehicle_id"]
self._display_name = data["display_name"]
self._vin = data["vin"]
self._state = data["state"]
self._car_type = f"Model {str(self._vin[3]).upper()}"
self._car_version = ""
self._sentry_mode_available = (
"vehicle_state" in data
and "sentry_mode_available" in data["vehicle_state"]
and data["vehicle_state"]["sentry_mode_available"]
)
self._controller = controller
self.should_poll = True
self.type = "device"
self.attrs: Dict[Text, Text] = {}
def _name(self):
return (
"{} {}".format(self._display_name, self.type)
if self._display_name is not None and self._display_name != self._vin[-6:]
else "Tesla Model {} {}".format(str(self._vin[3]).upper(), self.type)
)
def _uniq_name(self):
return "Tesla Model {} {} {}".format(
str(self._vin[3]).upper(), self._vin[-6:], self.type
)
def id(self):
# pylint: disable=invalid-name
"""Return the id of this Vehicle."""
return self._id
def vehicle_id(self):
"""Return the vehicle_id of this Vehicle."""
return self._vehicle_id
def car_name(self):
"""Return the car name of this Vehicle."""
return (
self._display_name
if self._display_name is not None and self._display_name != self._vin[-6:]
else f"Tesla Model {str(self._vin[3]).upper()}"
)
@property
def car_version(self):
"""Return the software version of this Vehicle."""
return self._car_version
@property
def car_type(self):
"""Return the type of this Vehicle."""
return self._car_type
@property
def sentry_mode_available(self):
"""Return True if sentry mode is available on this Vehicle."""
return self._sentry_mode_available
def assumed_state(self):
# pylint: disable=protected-access
"""Return whether the data is from an online vehicle."""
return not self._controller.car_online[self.id()] and (
self._controller._last_update_time[self.id()]
- self._controller._last_wake_up_time[self.id()]
> self._controller.update_interval
)
async def async_update(self, wake_if_asleep=False):
"""Update the vehicle data."""
await self._controller.update(self.id(), wake_if_asleep=wake_if_asleep)
state = self._controller.get_state_params(self.id())
if state and "car_version" in state:
self._car_version = state["car_version"]
if state and "sentry_mode_available" in state:
self._sentry_mode_available = state["sentry_mode_available"]
@staticmethod
def is_armable():
"""Return whether the data is from an online vehicle."""
return False
@staticmethod
def is_armed():
"""Return whether the vehicle is armed."""
return False