Skip to content

Commit

Permalink
refactor: save car_state from get_vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Feb 9, 2020
1 parent 990ee3d commit 2445d5b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion teslajsonpy/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ async def async_update(self) -> None:
"""Update the battery state."""
await super().async_update()
self.__online_state = self._controller.car_online[self._vin]
self.attrs["state"] = self._controller.raw_online_state[self._vin]
self.attrs["state"] = self._controller.car_state[self._vin].get("state")

@staticmethod
def has_battery() -> bool:
Expand Down
13 changes: 7 additions & 6 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def __init__(
self.__controller_lock = None
self.__wakeup_conds = {}
self.car_online = {}
self.raw_online_state = {}
self.car_state = {}
self.__id_vin_map = {}
self.__vin_id_map = {}
self.__vin_vehicle_id_map = {}
Expand Down Expand Up @@ -106,7 +106,7 @@ async def connect(self, test_login=False) -> Tuple[Text, Text]:
self._last_wake_up_time[vin] = 0
self.__update[vin] = True
self.__update_state[vin] = "normal"
self.raw_online_state[vin] = car["state"]
self.car_state[vin] = car
self.car_online[vin] = car["state"] == "online"
self.__last_parked_timestamp[vin] = self._last_attempted_update_time
self.__climate[vin] = {}
Expand Down Expand Up @@ -463,9 +463,10 @@ async def _wake_up(self, car_id):
car_id, "wake_up", wake_if_asleep=False
) # avoid wrapper loop
self.car_online[car_vin] = result["response"]["state"] == "online"
self.car_state[car_vin] = result["response"]
self._last_wake_up_time[car_vin] = cur_time
_LOGGER.debug(
"Wakeup %s: %s", car_vin[-5:], result["response"]["state"]
"Wakeup %s: %s", car_vin[-5:], self.car_state[car_vin]["state"]
)
return self.car_online[car_vin]

Expand Down Expand Up @@ -506,7 +507,7 @@ def _calculate_next_interval(vin: int) -> int:
# self.__climate[vin].get("is_climate_on"),
# self.__charging[vin].get("charging_state") == "Charging",
# )
if self.raw_online_state[vin] == "asleep" or self.__driving[vin].get(
if self.car_state[vin].get("state") == "asleep" or self.__driving[vin].get(
"shift_state"
):
_LOGGER.debug(
Expand Down Expand Up @@ -562,7 +563,7 @@ def _calculate_next_interval(vin: int) -> int:
self.__vin_vehicle_id_map[car["vin"]] = car["vehicle_id"]
self.__vehicle_id_vin_map[car["vehicle_id"]] = car["vin"]
self.car_online[car["vin"]] = car["state"] == "online"
self.raw_online_state[car["vin"]] = car["state"]
self.car_state[car["vin"]] = 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
Expand All @@ -576,7 +577,7 @@ def _calculate_next_interval(vin: int) -> int:
if car_vin and car_vin != vin:
continue
async with self.__lock[vin]:
car_state = self.raw_online_state[vin]
car_state = self.car_state[vin].get("state")
if (
(online or (wake_if_asleep and car_state == "asleep"))
and ( # pylint: disable=too-many-boolean-expressions
Expand Down

0 comments on commit 2445d5b

Please sign in to comment.