Skip to content

Commit

Permalink
add parking position
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverrahner committed Nov 29, 2023
1 parent 13d5df1 commit 72c8e26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 42 deletions.
30 changes: 3 additions & 27 deletions volkswagencarnet/vw_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 9 additions & 15 deletions volkswagencarnet/vw_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {
Expand All @@ -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:
Expand Down

0 comments on commit 72c8e26

Please sign in to comment.