alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
601 stars 100 forks source link

Implement the Streaming API for improved data accuracy while driving #539

Open chicagoandy opened 1 year ago

chicagoandy commented 1 year ago

Currently data-updates while driving appear to be limited to a 60-second refresh timeout. There is a streaming API that is available while the vehicle is in motion, this will give near-real-time updates to the following: speed,odometer, soc, elevation, est_heading est_lat, est_lng, power, shift_state, range, est_range, heading

A reference implementation of this is available (via TeslaMate, in Ruby). https://github.com/adriankumpf/teslamate/commit/a691784926eed4c6ac08b98b2d8c60f1b7950d89#diff-9f70483b27cf0755e732038378b0d7682e404eca39594eddd76036c3b0d7a772

Note this appears to mirror the approach taken by the official Tesla app.

alandtse commented 1 year ago

We've explored this. Issue is only one application can access the streaming API at a time. This means this may block the official app. HA doesn't need information this often. Also, we're not using a websocket library so we'd have to import another dependency to even handle this. If someone wants to try a PR they can.

Megabytemb commented 1 year ago

Hey Team,

I was thinking about how i can get more-realtime updates on my tesla, specifically its location so I can automate things like opening the garage door when i enter my driveway.

Rather then trying to implement the Websocket API in this Component, Could we make use of MQTT from Teslamate https://docs.teslamate.org/docs/integrations/mqtt/

The idea is that we could subscribe to the MQTT topics from Teslamate, and update the backend data in the _car variable in the TeslaCar class.

I just wanted to table the idea before i write any code. But if writing code and doing a PoC helps get the idea across, let me know

alandtse commented 1 year ago

I'd be ok with the PR but you'd have to make it optional. Also, HA natively supports MQTT, so you can already make MQTT sensors to do this. MQTT allows autodiscovery, so you could make it on the Teslamate side without even touching this component. So you may want to consider where it makes sense to do it.

Megabytemb commented 1 year ago

Yeah, so i did have MQTT sensors set up initially,. The TeslaMate docs even specically call this out https://docs.teslamate.org/docs/integrations/home_assistant

But it means every "Value" is duplicated. Once from the MQTT sensor, and once from this integration.

Ideally, I'd still have 1 entity per value, and that entity just always has the latest info from either this components own refresh cycle, or from the MQTT publish.

And yeah, I'd make it optional in the configFlow options.

I'll work on a PR and see how far I get. It will be a bit gross, because i'd have to update variables that are "owned" by teslajsonpy, OR, do a bit of an overhaul on the Car data storage. I'll see where I end up.

Megabytemb commented 1 year ago

Early days, not prod ready. But gives you an idea of how i expected the connection to work.

https://github.com/Megabytemb/tesla/tree/teslamate

it backs of the home assistant MQTT integration. I've yet to add logic that ignores everything if you don't have the mqtt integration configured.

Megabytemb commented 1 year ago

view pull request here

https://github.com/alandtse/tesla/pull/564

Megabytemb commented 1 year ago

Alright, @alandtse has been awesome and merged my code into Dev.

You can try out the new connection by following this guide!

https://github.com/alandtse/tesla/wiki/Teslamate-MQTT-Intergration

Please let me know if you run into any issues or have feature requests.