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

Fix: NoneType object has no attribute unit in self._electric.fuel_range.unit #293

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
58 changes: 36 additions & 22 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 @@
----------
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 @@
)

@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 @@ -84,15 +94,15 @@
If vehicle is electric returns 0
If vehicle doesn't support fuel range returns None
"""
if self._electric:
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.distance_to_empty:
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 @@
"""
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 @@
"""
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 @@
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 @@
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