Skip to content
Merged

Tesla #2746

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/modules/vehicles/tesla/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@

def post_wake_up_command(vehicle: int, token: TeslaSocToken) -> str:
vehicle_id = __get_vehicle_id(vehicle, token)
command = "vehicles/"+str(vehicle_id)+"/wake_up"
log.debug("Sending command: \"%s\"" % (command))
command = f"vehicles/{vehicle_id}/wake_up"
log.debug(f"Sending command: '{command}'")
headers = {
"user-agent": UA,
"x-tesla-user-agent": X_TESLA_USER_AGENT,
"authorization": "bearer " + token.access_token
}
session = req.get_http_session()
response = session.post("https://owner-api.teslamotors.com/api/1/" + command, headers=headers, timeout=50).json()
response = session.post(f"https://owner-api.teslamotors.com/api/1/{command}", headers=headers, timeout=50).json()
return response["response"]["state"]


def request_soc_range(vehicle: int, token: TeslaSocToken) -> Tuple[float, float, float]:
vehicle_id = __get_vehicle_id(vehicle, token)
data_part = "vehicles/"+str(vehicle_id)+"/vehicle_data"
data_part = f"vehicles/{vehicle_id}/vehicle_data"
response = __request_data(data_part, token)
response = json.loads(response)
soc = float(response["response"]["charge_state"]["battery_level"])
# convert miles to km
range = float(response["response"]["charge_state"]["battery_range"]) * 1.60934
soc_timestamp = float(response["response"]["charge_state"]["timestamp"])
range = int(float(response["response"]["charge_state"]["battery_range"]) * 1.60934)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welche Bedeutung hat 1.60934? -> Konstante mit sprechendem Namen bitte...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Steht in Zeile 48.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Steht in Zeile 48.

ja.... als Kommentar... ;o)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Genau. Und da das die einzige Stelle ist, an der die Zahl verwendet wird, sehe ich keinen Bedarf für eine Konstante, die dann irgendwo losgelöst vom Kontext definiert wird.

soc_timestamp = float(response["response"]["charge_state"]["timestamp"]) / 1000
return soc, range, soc_timestamp


Expand Down Expand Up @@ -88,20 +88,20 @@ def __get_vehicle_id(index: int, token: TeslaSocToken) -> str:
products = __request_data('products', token)
try:
vehicle_id = str(json.loads(products)["response"][index]["id"])
log.debug("vehicle_id for entry %d: %s" % (index, vehicle_id))
log.debug(f"vehicle_id for entry {index}: {vehicle_id}")
except IndexError:
raise Exception("Zur Tesla-ID "+str(index)+" konnte kein Fahrzeug im Account gefunden werden.")
raise Exception(f"Zur Tesla-ID {index} konnte kein Fahrzeug im Account gefunden werden.")
return vehicle_id


def __request_data(data_part: str, token: TeslaSocToken) -> str:
log.debug("Requesting data: \"%s\"" % (data_part))
log.debug(f"Requesting data: '{data_part}'")
headers = {
"user-agent": UA,
"x-tesla-user-agent": X_TESLA_USER_AGENT,
"authorization": "bearer " + token.access_token
}
response = req.get_http_session().get("https://owner-api.teslamotors.com/api/1/" + data_part,
response = req.get_http_session().get(f"https://owner-api.teslamotors.com/api/1/{data_part}",
headers=headers,
timeout=50)
return response.text
22 changes: 14 additions & 8 deletions packages/modules/vehicles/tesla/soc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
def fetch(vehicle_config: TeslaSoc, vehicle_update_data: VehicleUpdateData) -> CarState:
vehicle_config.configuration.token = api.validate_token(vehicle_config.configuration.token)
if vehicle_update_data.charge_state is False:
_wake_up_car(vehicle_config)
try:
_wake_up_car(vehicle_config)
except Exception as e:
log.warning(
f"Fehler beim Aufwecken des Fahrzeugs: {e}\n"
"Der abgerufene SoC-Wert ist möglicherweise veraltet."
)
soc, range, soc_timestamp = api.request_soc_range(
vehicle=vehicle_config.configuration.tesla_ev_num, token=vehicle_config.configuration.token)
return CarState(soc=soc, range=range, soc_timestamp=soc_timestamp)
Expand All @@ -36,10 +42,10 @@ def _wake_up_car(vehicle_config: TeslaSoc):
break
counter = counter+1
time.sleep(5)
log.debug("Loop: "+str(counter)+", State: "+str(state))
log.info("Status nach Aufwecken: "+str(state))
log.debug(f"Loop: {counter}, State: {state}")
log.info(f"Status nach Aufwecken: {state}")
if state != "online":
raise Exception("EV konnte nicht geweckt werden.")
raise Exception(f"EV konnte nicht geweckt werden. Status: {state}")


def create_vehicle(vehicle_config: TeslaSoc, vehicle: int):
Expand All @@ -55,10 +61,10 @@ def read_legacy(id: int,
tesla_ev_num: int,
charge_state: bool):

log.debug('SoC-Module tesla num: ' + str(id))
log.debug('SoC-Module tesla token_file: ' + str(token_file))
log.debug('SoC-Module tesla tesla_ev_num: ' + str(tesla_ev_num))
log.debug('SoC-Module tesla charge_state: ' + str(charge_state))
log.debug(f"SoC-Module tesla num: {id}")
log.debug(f"SoC-Module tesla token_file: {token_file}")
log.debug(f"SoC-Module tesla tesla_ev_num: {tesla_ev_num}")
log.debug(f"SoC-Module tesla charge_state: {charge_state}")

with open(token_file, "r") as f:
token = json.load(f)
Expand Down