Last active
May 6, 2021 21:23
-
-
Save i3p9/649424fcc8ebebcbcc24f78f9b90f186 to your computer and use it in GitHub Desktop.
Usage: tesla.py -e '[email protected] -p 'hunter2'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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 :)
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
I've taken the approach of splitting this into two programs now. Program #1 can take the password and OTP as inputs and will authenticate with the servers generating the cache file that can then be used repeatedly (for I think up to 30 days) with the second program without needing to reauthenticate):
Tesla_Authenticate.py:
Tesla_Probe.py: