Skip to content

Instantly share code, notes, and snippets.

@normalfaults
Created July 7, 2019 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save normalfaults/a1a7b59d68f2f36aab76052677ebc015 to your computer and use it in GitHub Desktop.
Save normalfaults/a1a7b59d68f2f36aab76052677ebc015 to your computer and use it in GitHub Desktop.
from __future__ import print_function
from pprint import pprint
import time
import math
import datetime
import json
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file as oauth_file, client, tools
import mytesla
#from here: https://github.com/zmsp/python-my-tesla/blob/master/myTesla.py
# If modifying these scopes, delete the file token.json.
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
# The ID and range of a sample spreadsheet.
SAMPLE_SPREADSHEET_ID = '<spreadsheetid>'
SAMPLE_RANGE_NAME = 'Class Data!A2:E'
def main():
thecar = mytesla.connect('<useremail>', "<password>")
# pprint(thecar.vehicles())
thecar_vehicle_state = thecar.vehicle_state()
pprint(thecar_vehicle_state)
thecar_charge_state = thecar.charge_state()
pprint(thecar_charge_state)
#pprint(thecar.climate_state())
#pprint(thecar.charge_state())
thecar_drive_state = thecar.drive_state()
pprint(thecar_drive_state)
store = oauth_file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('sheets', 'v4', http=creds.authorize(Http()))
# Call the Sheets API
values = [
[
time.time(),
datetime.datetime.now().strftime("%B %d, %Y %H:%M:%S"),
thecar_vehicle_state['response']['car_version'],
thecar_vehicle_state['response']['odometer'],
thecar_vehicle_state['response']['locked'],
thecar_vehicle_state['response']['timestamp'],
thecar_charge_state['response']['battery_level'],
thecar_charge_state['response']['battery_range'],
thecar_charge_state['response']['charge_energy_added'],
thecar_charge_state['response']['charge_miles_added_ideal'],
thecar_charge_state['response']['charge_miles_added_rated'],
thecar_charge_state['response']['charge_rate'],
thecar_charge_state['response']['charger_actual_current'],
thecar_charge_state['response']['charger_power'],
thecar_charge_state['response']['charger_voltage'],
thecar_charge_state['response']['charging_state'],
thecar_charge_state['response']['est_battery_range'],
thecar_charge_state['response']['max_range_charge_counter'],
thecar_charge_state['response']['time_to_full_charge'],
thecar_charge_state['response']['usable_battery_level'],
thecar_drive_state['response']['gps_as_of'],
thecar_drive_state['response']['heading'],
thecar_drive_state['response']['latitude'],
thecar_drive_state['response']['longitude'],
thecar_drive_state['response']['native_latitude'],
thecar_drive_state['response']['native_longitude'],
thecar_drive_state['response']['power'],
thecar_drive_state['response']['shift_state'],
thecar_drive_state['response']['speed'],
thecar_drive_state['response']['native_type'],
thecar_vehicle_state['response']['homelink_nearby'],
],
# Additional rows ...
]
body = {
'values': values
}
result = service.spreadsheets().values().append(
spreadsheetId=SAMPLE_SPREADSHEET_ID, range="Sheet1",
valueInputOption="USER_ENTERED", body=body).execute()
print('{0} cells appended.'.format(result \
.get('updates') \
.get('updatedCells')));
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment