Created
January 7, 2020 23:38
-
-
Save jwilf/ef8fb04dbfd5446415f8222ccce02826 to your computer and use it in GitHub Desktop.
AWS Lambda function to control Nissan Leaf climate control
This file contains 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 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