Skip to content

Commit

Permalink
feat: Add ability to call check_and_force_update_vehicles function fo…
Browse files Browse the repository at this point in the history
…r just one specific vehicle. (#688)

Hello,
This PR adds the ability to call the check_and_force_update_vehicles
function for just one specific vehicle.
The reason this is a neccessary feature is that if you have multiple
vehicles connected to your account and one of them goes into energy
saver mode so that it does not respond to force update requests anymore,
these requests fail with an APIError which breaks the vehicles loop so
that later vehicles are not queried at all even though they would
respond. With the new funtion it is possible to query the vehicles from
isolated try blocks so that one failing vehicle does not cause all
others to fail too.
Best regards,
Triple-S

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
Triple-S and pre-commit-ci[bot] authored Dec 24, 2024
1 parent aa1cdb2 commit a073ecf
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions hyundai_kia_connect_api/VehicleManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,28 @@ def update_vehicle_with_cached_state(self, vehicle_id: str) -> None:
_LOGGER.debug(f"{DOMAIN} - Vehicle Disabled, skipping.")

def check_and_force_update_vehicles(self, force_refresh_interval: int) -> None:
for vehicle_id in self.vehicles.keys():
self.check_and_force_update_vehicle(force_refresh_interval, vehicle_id)

def check_and_force_update_vehicle(
self, force_refresh_interval: int, vehicle_id: str
) -> None:
# Force refresh only if current data is older than the value bassed in seconds.
# Otherwise runs a cached update.
started_at_utc: dt = dt.datetime.now(pytz.utc)
for vehicle_id in self.vehicles.keys():
vehicle = self.get_vehicle(vehicle_id)
if vehicle.last_updated_at is not None:
_LOGGER.debug(
f"{DOMAIN} - Time differential in seconds: {(started_at_utc - vehicle.last_updated_at).total_seconds()}" # noqa
)
if (
started_at_utc - vehicle.last_updated_at
).total_seconds() > force_refresh_interval:
self.force_refresh_vehicle_state(vehicle_id)
else:
self.update_vehicle_with_cached_state(vehicle_id)
vehicle = self.get_vehicle(vehicle_id)
if vehicle.last_updated_at is not None:
_LOGGER.debug(
f"{DOMAIN} - Time differential in seconds: {(started_at_utc - vehicle.last_updated_at).total_seconds()}" # noqa
)
if (
started_at_utc - vehicle.last_updated_at
).total_seconds() > force_refresh_interval:
self.force_refresh_vehicle_state(vehicle_id)
else:
self.update_vehicle_with_cached_state(vehicle_id)
else:
self.update_vehicle_with_cached_state(vehicle_id)

def force_refresh_all_vehicles_states(self) -> None:
for vehicle_id in self.vehicles.keys():
Expand Down

0 comments on commit a073ecf

Please sign in to comment.