Skip to content

Commit fccd015

Browse files
committed
feat: fetch vehicle profiles inside get_vehicles (flag-gated), drop VehicleManager wiring
1 parent 811b173 commit fccd015

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

hyundai_kia_connect_api/ApiImplType1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def get_vehicles(self, token: Token) -> list[Vehicle]:
195195
ccu_ccs2_protocol_support=entry["ccuCCS2ProtocolSupport"],
196196
)
197197
result.append(vehicle)
198+
if self.supports_vehicle_profile:
199+
self._fetch_vehicle_profiles(token, result)
198200
return result
199201

200202
def _fetch_vehicle_profiles(self, token: Token, vehicles: list[Vehicle]) -> None:

hyundai_kia_connect_api/VehicleManager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ def initialize_vehicles(self):
127127
"Vehicles already initialized, this will re-initialize and cause data loss mapping errors"
128128
)
129129
vehicles = self.api.get_vehicles(self.token)
130-
self.api._fetch_vehicle_profiles(self.token, vehicles)
131130
for vehicle in vehicles:
132131
vehicle.supports_window_control = self.api.supports_window_control
133132
self.vehicles[vehicle.id] = vehicle

tests/test_vehicle_profile.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,50 @@ def test_capability_properties_work_with_mapped_profile(self, eu_api):
186186
assert v.air_purifier_supported is False
187187
assert v.is_left_hand_drive is True
188188
assert v.ev_alarm_supported is False
189+
190+
191+
class TestGetVehiclesProfileFetch:
192+
"""get_vehicles fetches profiles only when supports_vehicle_profile is set."""
193+
194+
def _make_api(self, supported: bool):
195+
from hyundai_kia_connect_api.ApiImplType1 import ApiImplType1
196+
197+
api = object.__new__(ApiImplType1)
198+
api.supports_vehicle_profile = supported
199+
api.SPA_API_URL = "https://example.test/api/v1/spa/"
200+
return api
201+
202+
def test_get_vehicles_fetches_profiles_when_supported(self):
203+
import hyundai_kia_connect_api.ApiImplType1 as type1_mod
204+
from hyundai_kia_connect_api.ApiImplType1 import ApiImplType1
205+
from unittest.mock import patch, MagicMock
206+
207+
api = self._make_api(supported=True)
208+
resp = MagicMock()
209+
resp.json.return_value = {"resMsg": {"vehicles": []}}
210+
with (
211+
patch.object(ApiImplType1, "_get_authenticated_headers", return_value={}),
212+
patch.object(type1_mod, "_check_response_for_errors", return_value=None),
213+
patch.object(type1_mod.requests, "get", return_value=resp),
214+
patch.object(ApiImplType1, "_fetch_vehicle_profiles") as fetch,
215+
):
216+
result = ApiImplType1.get_vehicles(api, token=MagicMock())
217+
assert result == []
218+
fetch.assert_called_once()
219+
220+
def test_get_vehicles_skips_profiles_when_not_supported(self):
221+
import hyundai_kia_connect_api.ApiImplType1 as type1_mod
222+
from hyundai_kia_connect_api.ApiImplType1 import ApiImplType1
223+
from unittest.mock import patch, MagicMock
224+
225+
api = self._make_api(supported=False)
226+
resp = MagicMock()
227+
resp.json.return_value = {"resMsg": {"vehicles": []}}
228+
with (
229+
patch.object(ApiImplType1, "_get_authenticated_headers", return_value={}),
230+
patch.object(type1_mod, "_check_response_for_errors", return_value=None),
231+
patch.object(type1_mod.requests, "get", return_value=resp),
232+
patch.object(ApiImplType1, "_fetch_vehicle_profiles") as fetch,
233+
):
234+
ApiImplType1.get_vehicles(api, token=MagicMock())
235+
fetch.assert_not_called()

0 commit comments

Comments
 (0)