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

Better check if car is supported before creating vehicle object #101

Merged
merged 1 commit into from
Nov 6, 2021
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
21 changes: 15 additions & 6 deletions mytoyota/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,24 @@ class Vehicle:
sensors: Optional[Sensors] = None
statistics: VehicleStatistics = VehicleStatistics()

def __init__(
def __init__( # pylint: disable=too-many-branches
self,
vehicle_info: dict,
connected_services: Optional[dict],
odometer: Optional[list],
status: Optional[dict],
remote_control: Optional[dict],
connected_services=None,
odometer=None,
status=None,
remote_control=None,
) -> None:

if connected_services is None:
connected_services = {}
if remote_control is None:
remote_control = {}
if status is None:
status = {}
if odometer is None:
odometer = []

# If no vehicle information is provided, abort.
if not vehicle_info:
_LOGGER.error("No vehicle information provided!")
Expand All @@ -67,7 +76,7 @@ def __init__(
# Format vehicle details.
self.details = self._format_details(vehicle_info)

if self.is_connected:
if self.is_connected and self.vin:

remote_control_info = remote_control.get("VehicleInfo", {})

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Python client for Toyota Connected Services."
authors = ["Simon Grud Hansen <[email protected]>"]
license = "MIT"
readme = "README.md"
version = "0.7.2"
version = "0.7.3"
classifiers = [
"Development Status :: 4 - Beta",
"License :: OSI Approved :: MIT License",
Expand Down
7 changes: 7 additions & 0 deletions tests/data/vehicles.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,12 @@
"productionYear": "2016",
"licensePlate": "SP234S",
"modelDescription": "Verso"
},
{
"id": 666666,
"alias": "Aygo",
"owner": true,
"licensePlate": "SP234S",
"productionYear": "2019"
}
]
77 changes: 77 additions & 0 deletions tests/test_vehicle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""pytest tests for mytoyota.vehicle.Vehicle"""
import json
import os

from mytoyota.vehicle import Vehicle

# pylint: disable=no-self-use


class TestVehicle:
"""pytest functions for Vehicle object"""

@staticmethod
def _load_from_file(filename: str):
"""Load a data structure from the specified JSON filename, and
return it."""
with open(filename, encoding="UTF-8") as json_file:
return json.load(json_file)

def test_vehicle_no_data(self):
"""Test vehicle with no initialization data"""
vehicle = Vehicle({})

assert vehicle.id == 0
assert vehicle.vin is None
assert vehicle.alias is None
assert vehicle.details is None
assert vehicle.is_connected is False
assert vehicle.odometer is None
assert vehicle.energy is None
assert vehicle.hvac is None
assert vehicle.parking is None

def test_vehicle_init_no_status(self):
"""Test vehicle initialization with no status"""

data_files = os.path.join(os.path.curdir, "tests", "data")

fixtures = self._load_from_file(os.path.join(data_files, "vehicles.json"))

for veh in fixtures:
vehicle = Vehicle(vehicle_info=veh)

assert vehicle.vin == veh.get("vin")
assert vehicle.is_connected is False
assert vehicle.odometer is None
assert vehicle.energy is None
assert vehicle.hvac is None
assert vehicle.parking is None

def test_vehicle_init(self):
"""Test vehicle initialization with connected services"""

data_files = os.path.join(os.path.curdir, "tests", "data")

fixtures = self._load_from_file(os.path.join(data_files, "vehicles.json"))

for veh in fixtures:
vehicle = Vehicle(
vehicle_info=veh,
connected_services={"connectedService": {"status": "ACTIVE"}},
)

assert vehicle.vin == veh.get("vin")
assert vehicle.is_connected is True
print(vehicle.id)

if vehicle.vin is None:
assert vehicle.odometer is None
assert vehicle.energy is None
assert vehicle.hvac is None
assert vehicle.parking is None
else:
assert vehicle.odometer is not None
assert vehicle.energy is None
assert vehicle.hvac is None
assert vehicle.parking is not None