Skip to content

Commit

Permalink
style: black
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Feb 16, 2020
1 parent 500968a commit 468df5a
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ def valid_result(result):
result = await wrapped(*args, **kwargs)
except TeslaException as ex:
_LOGGER.debug(
"Exception: %s\n%s(%s %s)",
ex.message,
wrapped.__name__,
args,
kwargs,
"Exception: %s\n%s(%s %s)", ex.message, wrapped.__name__, args, kwargs
)
raise
if valid_result(result) or is_wake_command:
Expand Down Expand Up @@ -175,12 +171,7 @@ def valid_result(result):
break
instance.car_online[instance._id_to_vin(car_id)] = True
# retry function
_LOGGER.debug(
"Retrying %s(%s %s)",
wrapped.__name__,
args,
kwargs,
)
_LOGGER.debug("Retrying %s(%s %s)", wrapped.__name__, args, kwargs)
try:
result = await wrapped(*args, **kwargs)
_LOGGER.debug(
Expand All @@ -189,11 +180,7 @@ def valid_result(result):
)
except TeslaException as ex:
_LOGGER.debug(
"Exception: %s\n%s(%s %s)",
ex.message,
wrapped.__name__,
args,
kwargs,
"Exception: %s\n%s(%s %s)", ex.message, wrapped.__name__, args, kwargs
)
raise
if valid_result(result):
Expand Down Expand Up @@ -257,7 +244,9 @@ def __init__(
self.__update_state = {}
self.enable_websocket = enable_websocket

async def connect(self, test_login=False, wake_if_asleep=False) -> Tuple[Text, Text]:
async def connect(
self, test_login=False, wake_if_asleep=False
) -> Tuple[Text, Text]:
"""Connect controller to Tesla.
Args
Expand All @@ -270,8 +259,6 @@ async def connect(self, test_login=False, wake_if_asleep=False) -> Tuple[Text, T
"""

cars = await self.get_vehicles()
if test_login:
return (self.__connection.refresh_token, self.__connection.access_token)
self._last_attempted_update_time = time.time()
self.__controller_lock = asyncio.Lock()

Expand Down Expand Up @@ -312,11 +299,14 @@ async def connect(self, test_login=False, wake_if_asleep=False) -> Tuple[Text, T
self.__components.append(Odometer(car, self))
self.__components.append(OnlineSensor(car, self))

tasks = [self.update(car["id"], wake_if_asleep=wake_if_asleep) for car in cars]
try:
await asyncio.gather(*tasks)
except (TeslaException, RetryLimitError):
pass
if not test_login:
tasks = [
self.update(car["id"], wake_if_asleep=wake_if_asleep) for car in cars
]
try:
await asyncio.gather(*tasks)
except (TeslaException, RetryLimitError):
pass
return (self.__connection.refresh_token, self.__connection.access_token)

def is_token_refreshed(self) -> bool:
Expand Down Expand Up @@ -446,7 +436,9 @@ async def data_request(self, car_id, name, wake_if_asleep=False):
)
)["response"]

@backoff.on_exception(min_expo, TeslaException, max_time=60, logger=__name__, min_value=15)
@backoff.on_exception(
min_expo, TeslaException, max_time=60, logger=__name__, min_value=15
)
async def command(self, car_id, name, data=None, wake_if_asleep=True):
"""Post name command to the car_id.
Expand Down Expand Up @@ -576,7 +568,7 @@ def _calculate_next_interval(vin: int) -> int:
if self.__update_state[vin] != "normal":
self.__update_state[vin] = "normal"
_LOGGER.debug(
"%s scanning every %s seconds", vin[-5:], self.update_interval,
"%s scanning every %s seconds", vin[-5:], self.update_interval
)
return self.update_interval

Expand Down Expand Up @@ -655,11 +647,13 @@ def _calculate_next_interval(vin: int) -> int:
and self.get_drive_params(car_id).get("shift_state")
and self.get_drive_params(car_id).get("shift_state") != "P"
):
await self.__connection.websocket_connect(
vin[-5:],
self.__vin_vehicle_id_map[vin],
on_message=self._process_websocket_message,
on_disconnect=self._process_websocket_disconnect,
asyncio.create_task(
self.__connection.websocket_connect(
vin[-5:],
self.__vin_vehicle_id_map[vin],
on_message=self._process_websocket_message,
on_disconnect=self._process_websocket_disconnect,
)
)
return update_succeeded

Expand Down

0 comments on commit 468df5a

Please sign in to comment.