Advertisement
Guest User

start/stop charging a tesla using python.

a guest
Aug 26th, 2016
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1.  
  2. import os
  3. import sys
  4. import teslajson
  5.  
  6. TESLA_EMAIL = os.environ['TESLA_EMAIL']
  7. TESLA_PASSWORD = os.environ['TESLA_PASSWORD']
  8.  
  9. if len(sys.argv) !=2 or sys.argv[1] not in ("start", "stop"):
  10.     sys.exit("Usage: %s start|stop" % sys.argv[0])
  11.  
  12. startstop = sys.argv[1]
  13. connection = teslajson.Connection(TESLA_EMAIL, TESLA_PASSWORD)
  14. vehicle = connection.vehicles[0]
  15. wake_up = vehicle.wake_up()["response"]
  16. if "state" not in wake_up:
  17.     sys.exit("Could not wake up car.")
  18. if wake_up["state"] != "online":
  19.     sys.exit("Car not online.")
  20. print startstop, "charging."
  21. charge = vehicle.command("charge_" + startstop)
  22. if "response" not in charge or "result" not in charge["response"]:
  23.     sys.exit("Car did not respond.")
  24. if charge["response"]["result"]:
  25.     print "OK."
  26. else:
  27.     sys.exit("Car did not %s charging." % startstop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement