MAC OS X AND TESLA API (PT. 2)

When it comes to loading and working with with third party APIs for your car, it kind of goes without saying that there are some caveats to be considered. For one thing, Tesla's servers don't take kindly to being spammed into oblivion if your computer decides to be a Chatty Cathy - if you're sending a ton of requests on a constant basis then that'll put a load on the servers, and they'll probably respond by blocking your IP address. Secondly, you're tinkering and tampering with a big, heavy, complicated and expensive machine, so consider what you're doing and don't decide that it'd be neat if you could heat your house by cranking the temperature up on the heater and leaving all the doors open or something equally asinine. You should exercise common sense regarding activities that might reduce the efficacy of the battery or other systems of the car.

Also remember that if you're tinkering with the locks and remote starting the car then you're also practically rolling out the red carpet for someone to steal your car. It's perfectly possible for me to be in my office and remotely unlock the car and start it up so that any passerby could just get into the thing and drive it away. I don't do that because that would be dumb, and I'd hope that nobody else would either.

With that out of the way, let's get on to the good stuff.

You'll need some kind of environment that'll allow you to access the REST API. There are some ways of doing it in Python (which is very tempting), but the simplest method is to use a Javascript runtime like nodejs. That can be downloaded from nodejs.org

Once that's installed you'll use the npm to install the Tesla tools, thus:

Sudo npm install -g teslams

Every time you query Tesla's servers you need some kind of authorization. On the car (and on the official client) that's handled with an oauth token, but the simplest way to handle authentication in this case is to just use the raw credentials. Yes, I know it's a bad idea. 

There are a bunch of commands built into the API and there's good documentation out there about what it all does (see here for a good rundown). Each requires a -u username and a -p password flag. I don't really want to be able to unlock the car or flash the lights, but it might be nice to have a simple way to see how much charge is in the thing, so I added this line to my .profile (credentials changed for obvious reasons):

alias charge='chargebar -u myemailaddress@emailaddress.com -p mysupersecretpassword'

Firing up Terminal and typing in "charge" gets me this:

...which is pretty neat. Now I just have to find the time to get that data periodically pulled down into a .json file so that I could throw it up on something like Panic's StatusBoard...