gak.dev
Loading...
Loading...
Been doing some work on teslatte
🚗🔋☕. It is a Rust crate to talk to the Tesla API, as well a command-line interface.
It's still very early in development. I will probably change the API significantly in the 0.1.*
releases.
For example:
$ teslatte api vehicle 1234567890 honk-horn
vehicle
.vehicles/{}/data_request/charge_state
(#2)Below is an example of how you would use the CLI.
$ teslatte help
Teslatte
A command line interface for the Tesla API.
Usage: teslatte <COMMAND>
Commands:
auth Authenticate with Tesla via URL, and receive an access token and refresh token
refresh Refresh your tokens
api Run API commands
help Print this message or the help of the given subcommand(s)
Options:
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
You'll need to set up either a known API key in the TESLA_ACCESS_TOKEN environment variable, or use the interactive
authentication flow to get a token. Don't forget to --save
to save the access token and refresh token to cli.json
.
The interactive authentication flow will generate a URL for you to click on. The website will ask for your Tesla credentials (if you're not logged in), and then redirect you to a 404 page. Copy the URL of the 404 page and paste it into the CLI. I've tried to make this all automatic but there are a bunch of anti-automation measures in place that I couldn't get around.
Note that --save
might be removed in the future as it doesn't make too much sense to just print them to the user.
Maybe a teslatte auth save
and teslatte auth print
options would be better.
Also, cli.json
will probably end up moving the the user's home directory, e.g. ~/.config/teslatte/auth.json
.
$ teslatte auth --save
--------------------------------------------------------------------------------
https://auth.tesla.com/oauth2/v3/authorize?client_id=ownerapi&code_challenge=xxx&code_challenge_method=xxx&redirect_uri=https%3A%2F%2Fauth.tesla.com%2Fvoid%2Fcallback&response_type=code&scope=openid+email+offline_access&state=xxx
--------------------------------------------------------------------------------
Visit the URL above, and log in to your Tesla account if not already logged in.
After you log in (or already logged in), it will redirect you to a 404 error
page, where the URL will start with https://auth.tesla.com/void/callback?code=...
Enter the whole URL of the 404 page: https://...
Saved tokens to cli.json
Now that the tokens are saved, you can use the API commands.
$ teslatte api vehicles
{
"count": 1,
"response": [
{
"id": 1234567890,
...
}
]
}
Found a vehicle! Let's remember the ID.
$ teslatte api help
Run API commands
Usage: teslatte api [OPTIONS] <COMMAND>
Commands:
vehicles List of vehicles
vehicle Specific Vehicle
energy-sites List of energy sites
energy-site Specific energy site
powerwall Powerwall queries
help Print this message or the help of the given subcommand(s)
Options:
-a, --access-token <ACCESS_TOKEN> Access token. If not provided, will try to load from the cli.json file [env: TESLA_ACCESS_TOKEN=]
-h, --help Print help
Most of the work is in the vehicle
subcommand. Some stuff in energy-site
and powerwall
which have been tested.
$ teslatte api vehicle 1234567890 help
Specific Vehicle
Usage: teslatte api vehicle <ID> <COMMAND>
Commands:
vehicle-data Get vehicle data
charge-port-door-open Open the charge port door or unlocks the cable
charge-port-door-close For vehicles with a motorized charge port, this closes it
set-charge-limit Set charge limit
set-charging-amps Set charge amps
charge-standard Set the charge limit to the standard %
charge-max-range Set the charge limit to the maximum %
charge-start Start charging
charge-stop Stop charging
set-scheduled-charging Set scheduled charging
set-scheduled-departure Set scheduled departure
honk-horn Honk!
flash-lights Flash the lights
help Print this message or the help of the given subcommand(s)
Honk!
$ teslatte api vehicle 1234567890 honk-horn
{
"response": {
"reason": "",
"result": true
}
}
All of these are just thin wrappers around the API endpoints. I've tried to make the CLI as ergonomic as possible, but there's still a lot of work to do.
Feel free to check out the code and submit a PR or issue!