Skip to content

Commit

Permalink
feat(USA): Expose EV Trip Details (#473) (#474)
Browse files Browse the repository at this point in the history
* Feature: [US] Expose EV Trip Details

The US version of the MyHyundai app shows some details about the most recent trips; extract and expose this.

#473

* fix: black action

* Only fetch evTripDetails if the vehicle is an EV.

---------

Co-authored-by: DanielRussell <DanielRussell@users.noreply.github.com>
  • Loading branch information
DanielRussell and DanielRussell committed Jan 6, 2024
1 parent 66b112a commit dbbbb45
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion hyundai_kia_connect_api/HyundaiBlueLinkAPIUSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .utils import get_child_value, get_float
from .ApiImpl import ApiImpl, ClimateRequestOptions
from .Token import Token
from .Vehicle import Vehicle
from .Vehicle import DailyDrivingStats, Vehicle


CIPHERS = "DEFAULT@SECLEVEL=1"
Expand Down Expand Up @@ -176,6 +176,24 @@ def _get_vehicle_status(

return status

def _get_ev_trip_details(self, token: Token, vehicle: Vehicle) -> dict:
if vehicle.engine_type != ENGINE_TYPES.EV:
return {}

url = self.API_URL + "ts/alerts/maintenance/evTripDetails"
headers = self._get_vehicle_headers(token, vehicle)
headers["userId"] = headers["username"]
# This header is sent by the MyHyundai app, but doesn't seem to do anything
# headers["offset"] = "-5"

_LOGGER.debug(f"{DOMAIN} - using API headers: {headers}")

response = self.sessions.get(url, headers=headers)
response = response.json()
_LOGGER.debug(f"{DOMAIN} - get_ev_trip_details response {response}")

return response

def _get_vehicle_location(self, token: Token, vehicle: Vehicle):
"""
Get the location of the vehicle
Expand Down Expand Up @@ -415,12 +433,30 @@ def _update_vehicle_properties(self, vehicle: Vehicle, state: dict) -> None:
)
vehicle.air_control_is_on = get_child_value(state, "vehicleStatus.airCtrlOn")

tripStats = []
tripDetails = get_child_value(state, "evTripDetails.tripdetails") or {}
for trip in tripDetails:
processedTrip = DailyDrivingStats(
date=dt.datetime.strptime(trip["startdate"], "%Y-%m-%d %H:%M:%S.%f"),
total_consumed=get_child_value(trip, "totalused"),
engine_consumption=get_child_value(trip, "drivetrain"),
climate_consumption=get_child_value(trip, "climate"),
onboard_electronics_consumption=get_child_value(trip, "accessories"),
battery_care_consumption=get_child_value(trip, "batterycare"),
regenerated_energy=get_child_value(trip, "regen"),
distance=get_child_value(trip, "distance"),
)
tripStats.append(processedTrip)

vehicle.daily_stats = tripStats

vehicle.data = state

def update_vehicle_with_cached_state(self, token: Token, vehicle: Vehicle) -> None:
state = {}
state["vehicleDetails"] = self._get_vehicle_details(token, vehicle)
state["vehicleStatus"] = self._get_vehicle_status(token, vehicle, False)
state["evTripDetails"] = self._get_ev_trip_details(token, vehicle)

if state["vehicleStatus"] is not None:
vehicle_location_result = None
Expand Down Expand Up @@ -451,6 +487,8 @@ def force_refresh_vehicle_state(self, token: Token, vehicle: Vehicle) -> None:
state = {}
state["vehicleDetails"] = self._get_vehicle_details(token, vehicle)
state["vehicleStatus"] = self._get_vehicle_status(token, vehicle, True)
state["evTripDetails"] = self._get_ev_trip_details(token, vehicle)

if state["vehicleStatus"] is not None:
vehicle_location_result = self._get_vehicle_location(token, vehicle)
if vehicle_location_result is not None:
Expand Down

0 comments on commit dbbbb45

Please sign in to comment.