Skip to content

Commit

Permalink
hande null values
Browse files Browse the repository at this point in the history
  • Loading branch information
CM000n committed Jan 10, 2024
1 parent fe16bbf commit 5b68804
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions mytoyota/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
from datetime import timedelta
from typing import Any, List, Optional

from mytoyota.models.endpoints.electric import ElectricResponseModel
from mytoyota.models.endpoints.telemetry import TelemetryResponseModel
from mytoyota.models.endpoints.vehicle_health import VehicleHealthResponseModel
from mytoyota.models.endpoints.electric import (
ElectricResponseModel,
ElectricStatusModel,
)
from mytoyota.models.endpoints.telemetry import TelemetryModel, TelemetryResponseModel
from mytoyota.models.endpoints.vehicle_health import (
VehicleHealthModel,
VehicleHealthResponseModel,
)
from mytoyota.utils.conversions import convert_distance


Expand All @@ -26,10 +32,10 @@ def __init__( # noqa: D417
----------
metric: bool: Report distances in metric(or imperial)
"""
self._electric = electric.payload if electric else None
self._telemetry = telemetry.payload if telemetry else None
self._health = health.payload if health else None
self._metric = "km" if metric else "mi"
self._electric: Optional[ElectricStatusModel] = electric.payload if electric else None
self._telemetry: Optional[TelemetryModel] = telemetry.payload if telemetry else None
self._health: Optional[VehicleHealthModel] = health.payload if health else None
self._distance_unit: str = "km" if metric else "mi"

Check warning on line 38 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L35-L38

Added lines #L35 - L38 were not covered by tests

def __repr__(self):
"""Representation of the Dashboard model."""
Expand All @@ -42,26 +48,30 @@ def __repr__(self):
)

@property
def odometer(self) -> float:
def odometer(self) -> Optional[float]:
"""Odometer distance.
Returns
-------
The latest odometer reading in the current selected units
"""
return convert_distance(
self._metric, self._telemetry.odometer.unit, self._telemetry.odometer.value
)
if self._telemetry:
return convert_distance(

Check warning on line 59 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L58-L59

Added lines #L58 - L59 were not covered by tests
self._distance_unit,
self._telemetry.odometer.unit,
self._telemetry.odometer.value,
)
return None

Check warning on line 64 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L64

Added line #L64 was not covered by tests

@property
def fuel_level(self) -> int:
def fuel_level(self) -> Optional[int]:
"""Fuel level.
Returns
-------
A value as percentage
"""
return self._telemetry.fuel_level
return self._telemetry.fuel_level if self._telemetry else None

Check warning on line 74 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L74

Added line #L74 was not covered by tests

@property
def battery_level(self) -> Optional[float]:
Expand All @@ -86,13 +96,13 @@ def fuel_range(self) -> Optional[float]:
"""
if self._electric and self._electric.fuel_range:

Check warning on line 97 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L97

Added line #L97 was not covered by tests
return convert_distance(
self._metric,
self._distance_unit,
self._electric.fuel_range.unit,
self._electric.fuel_range.value,
)
if self._telemetry and self._telemetry.distance_to_empty:

Check warning on line 103 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L103

Added line #L103 was not covered by tests
return convert_distance(
self._metric,
self._distance_unit,
self._telemetry.distance_to_empty.unit,
self._telemetry.distance_to_empty.value,
)
Expand All @@ -112,7 +122,7 @@ def battery_range(self) -> Optional[float]:
"""
if self._electric:
return convert_distance(
self._metric,
self._distance_unit,
self._electric.ev_range.unit,
self._electric.ev_range.value,
)
Expand All @@ -132,7 +142,7 @@ def battery_range_with_ac(self) -> Optional[float]:
"""
if self._electric:
return convert_distance(
self._metric,
self._distance_unit,
self._electric.ev_range_with_ac.unit,
self._electric.ev_range_with_ac.value,
)
Expand All @@ -152,9 +162,9 @@ def range(self) -> Optional[float]:
hybrid == fuel_range + battery_range_with_ac
None if not supported
"""
if self._telemetry.distance_to_empty:
if self._telemetry and self._telemetry.distance_to_empty:

Check warning on line 165 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L165

Added line #L165 was not covered by tests
return convert_distance(
self._metric,
self._distance_unit,
self._telemetry.distance_to_empty.unit,
self._telemetry.distance_to_empty.value,
)
Expand Down Expand Up @@ -182,7 +192,11 @@ def remaining_charge_time(self) -> Optional[timedelta]:
None if vehicle is not currently charging.
None if vehicle doesn't support charging
"""
return self._electric.remaining_charge_time if self._electric else None
return (

Check warning on line 195 in mytoyota/models/dashboard.py

View check run for this annotation

Codecov / codecov/patch

mytoyota/models/dashboard.py#L195

Added line #L195 was not covered by tests
timedelta(minutes=self._electric.remaining_charge_time)
if self._electric and self._electric.remaining_charge_time
else None
)

@property
def warning_lights(self) -> Optional[List[Any]]:
Expand Down

0 comments on commit 5b68804

Please sign in to comment.