Skip to content
Open
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
34 changes: 27 additions & 7 deletions custom_components/toyota_na/patch_seventeen_cy_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SeventeenCYPlusToyotaVehicle(ToyotaVehicle):
_has_remote_subscription = False
_has_electric = False
_last_vehicle_status = None # persist last successful status across polls
_last_vehicle_status_timestamp = None # parsed from "occuranceDate"

_command_map = {
RemoteRequestCommand.DoorLock: "door-lock",
Expand Down Expand Up @@ -95,6 +96,12 @@ def __init__(
ApiVehicleGeneration.CY17PLUS,
)

@staticmethod
def _parse_timestamp(value: str | None) -> datetime.datetime | None:
if not value:
return None
return datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%S%z")

_last_graphql_status = None # persist last successful GraphQL status

async def update(self):
Expand Down Expand Up @@ -255,6 +262,8 @@ def _parse_vehicle_status(self, vehicle_status: dict) -> None:
if "vehicleStatus" not in vehicle_status or vehicle_status["vehicleStatus"] is None:
return

self._last_vehicle_status_timestamp = self._parse_timestamp(vehicle_status.get("occurrenceDate"))

for category in vehicle_status["vehicleStatus"]:
if not category or "sections" not in category:
continue
Expand Down Expand Up @@ -413,19 +422,28 @@ def _parse_graphql_vehicle_status(self, status: dict) -> None:
def _parse_telemetry(self, telemetry: dict) -> None:
if not telemetry:
return


telemetry_timestamp = self._parse_timestamp(telemetry.get("lastTimestamp"))
if telemetry_timestamp:
self._features[VehicleFeatures.LastTimeStamp] = ToyotaNumeric(telemetry_timestamp.timestamp(), "")

telemetry_is_newer = (
self._last_vehicle_status_timestamp is None
or (
telemetry_timestamp is not None
and telemetry_timestamp > self._last_vehicle_status_timestamp
)
)

for key, value in telemetry.items():
if value is None:
continue

# last time stamp is a primitive
if key == "lastTimestamp":
self._features[VehicleFeatures.LastTimeStamp] = ToyotaNumeric(datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc).timestamp(), "")
continue

# tire pressure time stamp is a primitive
if key == "tirePressureTimestamp":
self._features[VehicleFeatures.LastTirePressureTimeStamp] = ToyotaNumeric(datetime.datetime.strptime(value, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=datetime.timezone.utc).timestamp(), "")
self._features[VehicleFeatures.LastTirePressureTimeStamp] = ToyotaNumeric(
self._parse_timestamp(value).timestamp(), ""
)
continue

# fuel level is a primitive
Expand All @@ -443,6 +461,8 @@ def _parse_telemetry(self, telemetry: dict) -> None:
if "Window" in key or "Roof" in key:
if value not in (1, 2):
continue
if not telemetry_is_newer:
continue
self._features[
self._vehicle_telemetry_map.get(key, key)
] = ToyotaOpening(closed=(value == 2))
Expand Down