Skip to content

Instantly share code, notes, and snippets.

@jwilf
Created January 7, 2020 23:38
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 jwilf/ef8fb04dbfd5446415f8222ccce02826 to your computer and use it in GitHub Desktop.
Save jwilf/ef8fb04dbfd5446415f8222ccce02826 to your computer and use it in GitHub Desktop.
AWS Lambda function to control Nissan Leaf climate control
import json
import pycarwings2
import time
config = {}
def param(event,key):
try:
return event['queryStringParameters'][key]
except:
body = json.loads(event['body'])
return body[key]
def lambda_handler(event, context):
for p in ['username','password','region','command']:
try:
config[p] = param(event,p)
except Exception:
return {
'statusCode': 500,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps('Could not get ' + p)
}
try:
s = pycarwings2.Session(config['username'], config['password'], config['region'])
leaf = s.get_leaf()
except Exception as e:
return {
'statusCode': 500,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(str(e))
}
for i in range(0, 5):
try:
if config['command'] == 'startclimate':
msg = "Starting climate control"
leaf.start_climate_control()
if config['command'] == 'stopclimate':
msg = "Stopping climate control"
leaf.stop_climate_control()
except Exception:
time.sleep(2)
continue
break
return {
'statusCode': 200,
'body': json.dumps(msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment