Skip to content

Commit

Permalink
Fix parsing vehicles without external colour (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasddn committed Feb 10, 2025
1 parent 2ae0426 commit 6ea501d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion custom_components/volvo_cars/volvo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class VolvoCarsVehicle(VolvoCarsApiBaseModel):
model_year: int
gearbox: str
fuel_type: str
external_colour: str
images: VolvoCarsImages
description: VolvoCarsModel
external_colour: str | None = None
battery_capacity_kwh: float | None = None

def has_battery_engine(self) -> bool:
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/ex30_bev_no_colour/vehicle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"vin": "YV1ABCDEFG1234567",
"modelYear": 2024,
"gearbox": "AUTOMATIC",
"fuelType": "NONE",
"batteryCapacityKWH": 69.0,
"images": {
"exteriorImageUrl": "https://wizz.volvocars.com/images/2024/416/v2/exterior/studio/right/transparent_exterior-studio-right_87de52fdb39d5303af0b9accb2ffa02ffb1d640e.png?client=public-api-engineering&w=1920",
"internalImageUrl": "https://wizz.volvocars.com/images/2024/416/v2/interior/studio/side/interior-studio-side_0c5b82913a907ff4ba517506060b29f883c10062.png?client=public-api-engineering&w=1920"
},
"descriptions": {
"model": "EX30",
"upholstery": "R540",
"steering": "LEFT"
}
}
24 changes: 24 additions & 0 deletions tests/test_api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from custom_components.volvo_cars.volvo.models import (
VolvoCarsLocation,
VolvoCarsValueField,
VolvoCarsVehicle,
)

from .common import load_json_object_fixture
Expand Down Expand Up @@ -56,3 +57,26 @@ def test_create_location(has_timestamp: bool) -> None:
assert location.properties.timestamp == date
else:
assert location.properties.timestamp is None


@pytest.mark.parametrize(("has_colour"), [(True), (False)])
def test_create_vehicle(has_colour: bool) -> None:
"""Test deserialization of VolvoCarsVehicle."""

data = (
load_json_object_fixture("ex30_bev/vehicle")
if has_colour
else load_json_object_fixture("ex30_bev_no_colour/vehicle")
)

vehicle = VolvoCarsVehicle.from_dict(data)

assert vehicle
assert vehicle.vin == "YV1ABCDEFG1234567"
assert vehicle.model_year == 2024
assert vehicle.gearbox == "AUTOMATIC"

if has_colour:
assert vehicle.external_colour == "Crystal White Pearl"
else:
assert vehicle.external_colour is None

0 comments on commit 6ea501d

Please sign in to comment.