bretterer / rivian-python-client

Unofficial Rivian Client for Python
25 stars 7 forks source link

example for how to use subscribe_for_vehicle_updates #93

Open jkh911208 opened 2 months ago

jkh911208 commented 2 months ago

i want to use websocket to get state of the vehicle, but i just can't make it work can you provide me a short example how to get vehicle state something like every 30 seconds? with web socket using subscribe_for_vehicle_updates?

natekspencer commented 2 months ago

Your statements are somewhat contradictory. Polling and subscribing are two different approaches for receiving the data. You can use them in combination with each other, but the subscribe_for_vehicle_updates should not be used every 30 seconds.

If you want to poll every 30 seconds, do something like:

while True:
  response = await api.get_vehicle_state(vehicle_id)
  await asyncio.sleep(30)

If you want to receive data by subscribing, you can do something like:

def handle_update(updated_data: dict) -> None:
  # do something with the updated data
  logging.debug("%s", updated_data)

await api.subscribe_for_vehicle_updates(vehicle_id, callback=handle_update)
jkh911208 commented 2 months ago

Your statements are somewhat contradictory. Polling and subscribing are two different approaches for receiving the data. You can use them in combination with each other, but the subscribe_for_vehicle_updates should not be used every 30 seconds.

If you want to poll every 30 seconds, do something like:

while True:
  response = await api.get_vehicle_state(vehicle_id)
  await asyncio.sleep(30)

If you want to receive data by subscribing, you can do something like:

def handle_update(updated_data: dict) -> None:
  # do something with the updated data
  logging.debug("%s", updated_data)

await api.subscribe_for_vehicle_updates(vehicle_id, callback=handle_update)

ah...got it thanks! I will give it a try, is it possible to get live charging data from subscribe?

tmack8001 commented 2 months ago

Live charging data over the subscription?

Meaning what specifically?

Charge rate and such from the onboard vehicle based charger, yeah. Though not for say your wall box unfortunately we haven't been able to find evidence of that information over a subscription.

jkh911208 commented 2 months ago

@tmack8001 is it possible to make call back function a async function?