Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return empty data on missing attributes #1389

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/renault_api/kamereon/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,12 @@ class KamereonVehicleDataResponse(KamereonResponse):

data: Optional[KamereonVehicleData]

def get_attributes(self, schema: Schema) -> Optional[KamereonVehicleDataAttributes]:
def get_attributes(self, schema: Schema) -> KamereonVehicleDataAttributes:
"""Return jwt token."""
return (
cast(KamereonVehicleDataAttributes, schema.load(self.data.attributes))
if self.data and self.data.attributes is not None
else None
)
attributes = {}
if self.data and self.data.attributes is not None:
attributes = self.data.attributes
return cast(KamereonVehicleDataAttributes, schema.load(attributes))


@dataclass
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/kamereon/vehicle_data/no_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data": {
"type": "ChargeMode",
"id": "VF1AAAA"
}
}
20 changes: 20 additions & 0 deletions tests/kamereon/test_kamereon_vehicle_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,23 @@ def test_hvac_settings_schedule() -> None:
assert vehicle_data.schedules[i].id == i + 1
for day in DAYS_OF_WEEK:
assert vehicle_data.schedules[i].__dict__.get(day) is None


def test_no_data() -> None:
"""Test missing vehicle data."""
response: models.KamereonVehicleDataResponse = fixtures.get_file_content_as_schema(
f"{fixtures.KAMEREON_FIXTURE_PATH}/vehicle_data/no_data.json",
schemas.KamereonVehicleDataResponseSchema,
)
response.raise_for_error_code()
assert response.data is not None
assert response.data.raw_data == {"id": "VF1AAAA", "type": "ChargeMode"}

vehicle_data = cast(
models.KamereonVehicleCockpitData,
response.get_attributes(schemas.KamereonVehicleCockpitDataSchema),
)

assert vehicle_data.totalMileage is None
assert vehicle_data.fuelAutonomy is None
assert vehicle_data.fuelQuantity is None
Loading