Skip to content

Commit

Permalink
feat: Bluelink USA better distance accuracy using odometer, one-tenth…
Browse files Browse the repository at this point in the history
… of mile (#662)
  • Loading branch information
ZuinigeRijder authored Oct 29, 2024
1 parent a7d63b4 commit d21667b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
31 changes: 30 additions & 1 deletion hyundai_kia_connect_api/HyundaiBlueLinkAPIUSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from requests.adapters import HTTPAdapter
from urllib3.util.ssl_ import create_urllib3_context

from hyundai_kia_connect_api.exceptions import APIError

from .const import (
DOMAIN,
VEHICLE_LOCK_ACTION,
Expand Down Expand Up @@ -459,6 +461,31 @@ def _update_vehicle_properties(self, vehicle: Vehicle, state: dict) -> None:
# fill vehicle.daily_stats
tripStats = []
tripDetails = get_child_value(state, "evTripDetails.tripdetails") or {}

# compute more digits for distance mileage using odometer and overrule distance
previous_odometer = None
for trip in reversed(tripDetails):
odometer = get_child_value(trip, "odometer.value")
if previous_odometer and odometer:
delta_odometer = odometer - previous_odometer
if delta_odometer >= 0.0:
trip["distance"] = delta_odometer
previous_odometer = odometer

# overrule odometer with more accuracy from last trip
if (
previous_odometer
and vehicle.odometer
and previous_odometer > vehicle.odometer
):
_LOGGER.debug(
f"Overruling odometer: {previous_odometer:.1f} old: {vehicle.odometer:.1f}" # noqa
)
vehicle.odometer = (
previous_odometer,
DISTANCE_UNITS[3],
)

for trip in tripDetails:
processedTrip = DailyDrivingStats(
date=dt.datetime.strptime(trip["startdate"], "%Y-%m-%d %H:%M:%S.%f"),
Expand All @@ -485,7 +512,7 @@ def _update_vehicle_properties(self, vehicle: Vehicle, state: dict) -> None:
hhmmss=yyyymmdd_hhmmss,
drive_time=int(drive_time / 60), # convert seconds to minutes
idle_time=int(idle_time / 60), # convert seconds to minutes
distance=int(trip["distance"]),
distance=float(trip["distance"]),
avg_speed=get_child_value(trip["avgspeed"], "value"),
max_speed=int(get_child_value(trip["maxspeed"], "value")),
)
Expand Down Expand Up @@ -733,6 +760,8 @@ def lock_action(self, token: Token, vehicle: Vehicle, action) -> None:
elif action == VEHICLE_LOCK_ACTION.UNLOCK:
url = self.API_URL + "rcs/rdo/on"
_LOGGER.debug(f"{DOMAIN} - Calling unlock")
else:
raise APIError(f"Invalid action value: {action}")

headers = self._get_vehicle_headers(token, vehicle)
headers["APPCLOUD-VIN"] = vehicle.VIN
Expand Down
6 changes: 2 additions & 4 deletions hyundai_kia_connect_api/Vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TripInfo:
hhmmss: str = None # will not be filled by summary
drive_time: int = None # minutes
idle_time: int = None # minutes
distance: int = None
distance: float = None
avg_speed: float = None
max_speed: int = None

Expand Down Expand Up @@ -60,9 +60,7 @@ class DailyDrivingStats:
onboard_electronics_consumption: int = None
battery_care_consumption: int = None
regenerated_energy: int = None
# distance is expressed in (I assume) whatever unit the vehicle is
# configured in. KMs (rounded) in my case
distance: int = None
distance: float = None
distance_unit: str = DISTANCE_UNITS[1] # set to kms by default


Expand Down

0 comments on commit d21667b

Please sign in to comment.