Skip to content

Instantly share code, notes, and snippets.

@i3p9
Last active May 6, 2021 21:23
Show Gist options
  • Save i3p9/649424fcc8ebebcbcc24f78f9b90f186 to your computer and use it in GitHub Desktop.
Save i3p9/649424fcc8ebebcbcc24f78f9b90f186 to your computer and use it in GitHub Desktop.
Usage: tesla.py -e '[email protected] -p 'hunter2'
import argparse
import teslapy
import requests
from twilio.rest import Client
def parse():
parser = argparse.ArgumentParser(description='Tesla Charge Reminder via Twilio')
requredParser =parser.add_argument_group('Required arguments')
requredParser.add_argument('-e', '--email', help='Email for authentication.',required=True)
requredParser.add_argument('-p', '--password', help='Password for authentication.',required=True)
parser.add_argument('-o', '--otp', help='OTP for 2FA (OPTIONAL).', default='') #not yet implemented
args = parser.parse_args()
email = args.email
password = args.password
otp = args.otp
return email, password, otp
def checkStatus(email,password,otp):
acceptedCharge = 75
with teslapy.Tesla(email, password) as tesla: # add lambda:'passcode' if mfa is enabled, it's not yet supported
tesla.fetch_token()
vehicles = tesla.vehicle_list()
vehicles[0].sync_wake_up()
chargePercentage = vehicles[0].get_vehicle_data()['charge_state']['battery_level']
if (vehicles[0].get_vehicle_data()['charge_state']['charging_state'] != "Charging"):
if(chargePercentage > acceptedCharge):
txt = "Car not plugged in and has "+str(chargePercentage)+"% Charge! Plug it in"
sendTextMessage(txt)
else:
txt = "Car is plugged in and has "+str(chargePercentage)+"% Charge."
sendTextMessage(txt)
def sendTextMessage(txt):
client = Client("ACxxxxxxxxxxxxxx", "zzzzzzzzzzzzz") #Get API and secret from Twilio
yourNumber = "+15552221422"
client.messages.create(to=yourNumber, from_="+FromNumber", body=txt)
if __name__ == '__main__':
email, password,otp = parse()
checkStatus(email, password,otp)
@michaelgreed
Copy link

This lets me run the first script, prompts me for my password, and then I can pull the current OTP number from my phone to do the authentication. Using pyotp would be a bad idea IMHO, almost as bad as hardcoding passwords (probably worse in this case if you hardcoded BOTH the password and the OTP secret :)

@i3p9
Copy link
Author

i3p9 commented May 6, 2021

Oh definitely DO NOT want to hardcore otp secret. So okay if the toket lasts for 30 days then it's fine, I was wondering if you need a token frequently, which would cause manual input, thus not really making it "automation". Thanks for you script, I'll try to incorporate into mine. Thank you for all the pointers as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment