From 72c8e261404ff0df1cb2338ed46ec8a99562bcf7 Mon Sep 17 00:00:00 2001 From: Oliver Rahner Date: Wed, 29 Nov 2023 09:51:07 +0100 Subject: [PATCH] add parking position --- volkswagencarnet/vw_connection.py | 30 +++--------------------------- volkswagencarnet/vw_vehicle.py | 24 +++++++++--------------- 2 files changed, 12 insertions(+), 42 deletions(-) diff --git a/volkswagencarnet/vw_connection.py b/volkswagencarnet/vw_connection.py index 86764b31..79e6c37d 100644 --- a/volkswagencarnet/vw_connection.py +++ b/volkswagencarnet/vw_connection.py @@ -536,6 +536,7 @@ async def post(self, url, vin="", tries=0, **data): # Construct URL from request, home region and variables def _make_url(self, ref, vin=""): + # TODO after verifying that we don't need home region handling anymore, this method should be completely removed return ref replacedUrl = re.sub("\\$vin", vin, ref) if "://" in replacedUrl: @@ -663,8 +664,8 @@ async def getParkingPosition(self, vin): if "data" in response: return {"parkingposition": response["data"]} - - _LOGGER.warning(f"Could not fetch parkingposition for vin {vin}") + else: + return {"parkingposition": {}} except Exception as error: _LOGGER.warning(f"Could not fetch parkingposition, error: {error}") @@ -697,31 +698,6 @@ async def getRealCarData(self, vin): _LOGGER.warning(f"Could not fetch realCarData, error: {error}") return False - async def getCarportData(self, vin): - """Get carport data for vehicle, model, model year etc.""" - if not await self.validate_tokens: - return False - try: - self._session_headers["Accept"] = ( - "application/vnd.vwg.mbb.vehicleDataDetail_v2_1_0+json," - " application/vnd.vwg.mbb.genericError_v1_0_2+json" - ) - response = await self.get( - f"fs-car/vehicleMgmt/vehicledata/v2/{BRAND}/{self._session_country}/vehicles/$vin", vin=vin - ) - self._session_headers["Accept"] = "application/json" - - if response.get("vehicleDataDetail", {}).get("carportData", {}): - data = {"carportData": response.get("vehicleDataDetail", {}).get("carportData", {})} - return data - elif response.get("status_code", {}): - _LOGGER.warning(f'Could not fetch carportdata, HTTP status code: {response.get("status_code")}') - else: - _LOGGER.info("Unhandled error while trying to fetch carport data") - except Exception as error: - _LOGGER.warning(f"Could not fetch carportData, error: {error}") - return False - async def getVehicleStatusData(self, vin): """Get stored vehicle data response.""" try: diff --git a/volkswagencarnet/vw_vehicle.py b/volkswagencarnet/vw_vehicle.py index eeb72c45..dbb0bfc7 100644 --- a/volkswagencarnet/vw_vehicle.py +++ b/volkswagencarnet/vw_vehicle.py @@ -213,12 +213,6 @@ async def get_realcardata(self): if data: self._states.update(data) - async def get_carportdata(self): - """Fetch carport data.""" - data = await self._connection.getCarportData(self.vin) - if data: - self._states.update(data) - async def get_preheater(self): """Fetch pre-heater data if function is enabled.""" if self._services.get("rheating_v1", {}).get("active", False): @@ -1049,10 +1043,9 @@ def position(self) -> dict[str, str | float | None]: if self.vehicle_moving: output = {"lat": None, "lng": None, "timestamp": None} else: - pos_obj = self.attrs.get("findCarResponse", {}) - lat = int(pos_obj.get("Position").get("carCoordinate").get("latitude")) / 1000000 - lng = int(pos_obj.get("Position").get("carCoordinate").get("longitude")) / 1000000 - parking_time = pos_obj.get("parkingTimeUTC") + lat = float(find_path(self.attrs, "parkingposition.lat")) + lng = float(find_path(self.attrs, "parkingposition.lon")) + parking_time = find_path(self.attrs, "parkingposition.carCapturedTimestamp") output = {"lat": lat, "lng": lng, "timestamp": parking_time} except Exception: output = { @@ -1064,22 +1057,23 @@ def position(self) -> dict[str, str | float | None]: @property def position_last_updated(self) -> datetime: """Return position last updated.""" - return self.attrs.get("findCarResponse", {}).get("Position", {}).get("timestampTssReceived") + return find_path(self.attrs, "parkingposition.carCapturedTimestamp") @property def is_position_supported(self) -> bool: - """Return true if carfinder_v1 service is active.""" - return self._services.get("carfinder_v1", {}).get("active", False) or self.attrs.get("isMoving", False) + """Return true if position is available.""" + return is_valid_path(self.attrs, "parkingposition.carCapturedTimestamp") @property def vehicle_moving(self) -> bool: """Return true if vehicle is moving.""" - return self.attrs.get("isMoving", False) + # there is not "isMoving" property anymore in VW's API, so we just take the absence of position data as the indicator + return not is_valid_path(self.attrs, "parkingposition.lat") @property def vehicle_moving_last_updated(self) -> datetime: """Return attribute last updated timestamp.""" - return self.attrs.get("findCarResponse", {}).get("Position", {}).get("timestampTssReceived") + return find_path(self.attrs, "parkingposition.carCapturedTimestamp") @property def is_vehicle_moving_supported(self) -> bool: