Skip to content

Commit

Permalink
perf: convert unnecessary async calls to sync
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Nov 6, 2019
1 parent 8eaef85 commit a827301
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 38 deletions.
6 changes: 3 additions & 3 deletions teslajsonpy/battery_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self) -> None:
"""Update the battery state."""
await super().async_update()
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_level = data["battery_level"]
self.__charging_state = data["charging_state"]
Expand Down Expand Up @@ -85,12 +85,12 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self):
"""Update the battery range state."""
await super().async_update()
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_range = data["battery_range"]
self.__est_battery_range = data["est_battery_range"]
self.__ideal_battery_range = data["ideal_battery_range"]
data = await self._controller.get_gui_params(self._id)
data = self._controller.get_gui_params(self._id)
if data:
if data["gui_distance_units"] == "mi/hr":
self.measurement = "LENGTH_MILES"
Expand Down
4 changes: 2 additions & 2 deletions teslajsonpy/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the parking brake sensor."""
await super().async_update()
data = await self._controller.get_drive_params(self._id)
data = self._controller.get_drive_params(self._id)
if data:
if not data["shift_state"] or data["shift_state"] == "P":
self.__state = True
Expand Down Expand Up @@ -98,7 +98,7 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the charger connection sensor."""
await super().async_update()
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data:
if data["charging_state"] in ["Disconnected"]:
self.__state = False
Expand Down
12 changes: 6 additions & 6 deletions teslajsonpy/charger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the charging state of the Tesla Vehicle."""
await super().async_update()
last_update = await self._controller.get_last_update_time(self._id)
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data and data["charging_state"] != "Charging":
self.__charger_state = False
else:
Expand Down Expand Up @@ -97,9 +97,9 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the status of the range setting."""
await super().async_update()
last_update = await self._controller.get_last_update_time(self._id)
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data:
self.__maxrange_state = data["charge_to_max_range"]

Expand Down Expand Up @@ -160,11 +160,11 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self) -> None:
"""Update the battery state."""
await super().async_update()
data = await self._controller.get_gui_params(self._id)
data = self._controller.get_gui_params(self._id)
if data:
self.measurement = data["gui_distance_units"]
self.__rated = data["gui_range_display"] == "Rated"
data = await self._controller.get_charging_params(self._id)
data = self._controller.get_charging_params(self._id)
if data:
self.__added_range = (
data["charge_miles_added_rated"]
Expand Down
6 changes: 3 additions & 3 deletions teslajsonpy/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def get_fan_status(self):
async def async_update(self):
"""Update the HVAC state."""
await super().async_update()
data = await self._controller.get_climate_params(self._id)
data = self._controller.get_climate_params(self._id)
if data:
last_update = await self._controller.get_last_update_time(self._id)
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
self.__is_auto_conditioning_on = data["is_auto_conditioning_on"]
self.__is_climate_on = data["is_climate_on"]
Expand Down Expand Up @@ -181,7 +181,7 @@ def get_outside_temp(self):
async def async_update(self):
"""Update the temperature."""
await super().async_update()
data = await self._controller.get_climate_params(self._id)
data = self._controller.get_climate_params(self._id)
if data:
self.__inside_temp = (
data["inside_temp"] if data["inside_temp"] else self.__inside_temp
Expand Down
24 changes: 12 additions & 12 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async def connect(self, test_login=False) -> Tuple[Text, Text]:
pass
return (self.__connection.refresh_token, self.__connection.access_token)

async def is_token_refreshed(self) -> bool:
def is_token_refreshed(self) -> bool:
"""Return whether token has been changed and not retrieved.
Returns
Expand All @@ -117,7 +117,7 @@ async def is_token_refreshed(self) -> bool:
"""
return self.__connection.token_refreshed

async def get_tokens(self) -> Tuple[Text, Text]:
def get_tokens(self) -> Tuple[Text, Text]:
"""Return refresh and access tokens.
This will set the the self.__connection token_refreshed to False.
Expand Down Expand Up @@ -409,7 +409,7 @@ async def command(self, vehicle_id, name, data=None, wake_if_asleep=True):
vehicle_id, f"command/{name}", data=data, wake_if_asleep=wake_if_asleep
)

async def get_homeassistant_components(self):
def get_homeassistant_components(self):
"""Return list of Tesla components for Home Assistant setup.
Use get_vehicles() for general API use.
Expand Down Expand Up @@ -505,31 +505,31 @@ async def update(self, car_id=None, wake_if_asleep=False, force=False):
update_succeeded = True
return update_succeeded

async def get_climate_params(self, car_id):
def get_climate_params(self, car_id):
"""Return cached copy of climate_params for car_id."""
return self.__climate[car_id]

async def get_charging_params(self, car_id):
def get_charging_params(self, car_id):
"""Return cached copy of charging_params for car_id."""
return self.__charging[car_id]

async def get_state_params(self, car_id):
def get_state_params(self, car_id):
"""Return cached copy of state_params for car_id."""
return self.__state[car_id]

async def get_config_params(self, car_id):
def get_config_params(self, car_id):
"""Return cached copy of state_params for car_id."""
return self.__config[car_id]

async def get_drive_params(self, car_id):
def get_drive_params(self, car_id):
"""Return cached copy of drive_params for car_id."""
return self.__driving[car_id]

async def get_gui_params(self, car_id):
def get_gui_params(self, car_id):
"""Return cached copy of gui_params for car_id."""
return self.__gui[car_id]

async def get_updates(self, car_id=None):
def get_updates(self, car_id=None):
"""Get updates dictionary.
Parameters
Expand All @@ -552,7 +552,7 @@ async def get_updates(self, car_id=None):
return self.__update[car_id]
return self.__update

async def set_updates(self, car_id, value):
def set_updates(self, car_id, value):
"""Set updates dictionary.
Parameters
Expand All @@ -572,7 +572,7 @@ async def set_updates(self, car_id, value):
"""
self.__update[car_id] = value

async def get_last_update_time(self, car_id=None):
def get_last_update_time(self, car_id=None):
"""Get last_update time dictionary.
Parameters
Expand Down
6 changes: 3 additions & 3 deletions teslajsonpy/gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_location(self):
async def async_update(self):
"""Update the current GPS location."""
await super().async_update()
data = await self._controller.get_drive_params(self._id)
data = self._controller.get_drive_params(self._id)
if data:
self.__longitude = data["longitude"]
self.__latitude = data["latitude"]
Expand Down Expand Up @@ -103,10 +103,10 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the odometer and the unit of measurement based on GUI."""
await super().async_update()
data = await self._controller.get_state_params(self._id)
data = self._controller.get_state_params(self._id)
if data:
self.__odometer = data["odometer"]
data = await self._controller.get_gui_params(self._id)
data = self._controller.get_gui_params(self._id)
if data:
if data["gui_distance_units"] == "mi/hr":
self.measurement = "LENGTH_MILES"
Expand Down
20 changes: 12 additions & 8 deletions teslajsonpy/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update the lock state."""
await super().async_update()
last_update = await self._controller.get_last_update_time(self._id)
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = await self._controller.get_state_params(self._id)
self.__lock_state = data["locked"]
data = self._controller.get_state_params(self._id)
self.__lock_state = data["locked"] if data else None

async def lock(self):
"""Lock the doors."""
Expand Down Expand Up @@ -119,12 +119,16 @@ def __init__(self, data, controller):
async def async_update(self):
"""Update state of the charger lock."""
await super().async_update()
last_update = await self._controller.get_last_update_time(self._id)
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = await self._controller.get_charging_params(self._id)
self.__lock_state = not (
(data["charge_port_door_open"])
and (data["charge_port_latch"] != "Engaged")
data = self._controller.get_charging_params(self._id)
self.__lock_state = (
not (
(data["charge_port_door_open"])
and (data["charge_port_latch"] != "Engaged")
)
if data
else None
)

async def lock(self):
Expand Down
2 changes: 1 addition & 1 deletion teslajsonpy/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def assumed_state(self):
async def async_update(self):
"""Update the car version."""
await self._controller.update(self.id(), wake_if_asleep=False)
state = await self._controller.get_state_params(self.id())
state = self._controller.get_state_params(self.id())
if state and "car_version" in state:
self._car_version = state["car_version"]

Expand Down

0 comments on commit a827301

Please sign in to comment.