diff --git a/mytoyota/vehicle.py b/mytoyota/vehicle.py index fb0a2d63..ee95f220 100644 --- a/mytoyota/vehicle.py +++ b/mytoyota/vehicle.py @@ -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!") @@ -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", {}) diff --git a/pyproject.toml b/pyproject.toml index e2f70f6d..ed9219f5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ description = "Python client for Toyota Connected Services." authors = ["Simon Grud Hansen "] license = "MIT" readme = "README.md" -version = "0.7.2" +version = "0.7.3" classifiers = [ "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License", diff --git a/tests/data/vehicles.json b/tests/data/vehicles.json index 273e1af4..a461831d 100644 --- a/tests/data/vehicles.json +++ b/tests/data/vehicles.json @@ -127,5 +127,12 @@ "productionYear": "2016", "licensePlate": "SP234S", "modelDescription": "Verso" + }, + { + "id": 666666, + "alias": "Aygo", + "owner": true, + "licensePlate": "SP234S", + "productionYear": "2019" } ] diff --git a/tests/test_vehicle.py b/tests/test_vehicle.py new file mode 100644 index 00000000..b685db66 --- /dev/null +++ b/tests/test_vehicle.py @@ -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