Created
January 15, 2017 20:30
-
-
Save Westlad/7111f414727d8ddc15475acb29ac0408 to your computer and use it in GitHub Desktop.
Lambda function for controlling a Raspberry Pi enabled Alexa Smart Home Skill
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 requests | |
| def lambda_handler(event, context): | |
| if event['header']['namespace'] == 'Alexa.ConnectedHome.Discovery': | |
| return handleDiscovery(context, event) | |
| elif event['header']['namespace'] == 'Alexa.ConnectedHome.Control': | |
| return handleControl(context, event) | |
| def handleDiscovery(context, event): | |
| payload = '' | |
| header = { | |
| "namespace": "Alexa.ConnectedHome.Discovery", | |
| "name": "DiscoverAppliancesResponse", | |
| "payloadVersion": "2" | |
| } | |
| if event['header']['name'] == 'DiscoverAppliancesRequest': | |
| payload = { | |
| "discoveredAppliances":[ | |
| { | |
| "applianceId":"device001", | |
| "manufacturerName":"Westland Enterprises", | |
| "modelName":"model 01", | |
| "version":"0.1", | |
| "friendlyName":"Shack Heater", | |
| "friendlyDescription":"Turns the Shack heater on or off", | |
| "isReachable":True, | |
| "actions":[ | |
| "turnOn", | |
| "turnOff" | |
| ], | |
| "additionalApplianceDetails":{ | |
| "extraDetail1":"optionalDetailForSkillAdapterToReferenceThisDevice", | |
| "extraDetail2":"There can be multiple entries", | |
| "extraDetail3":"but they should only be used for reference purposes.", | |
| "extraDetail4":"This is not a suitable place to maintain current device state" | |
| } | |
| } | |
| ] | |
| } | |
| return { 'header': header, 'payload': payload } | |
| def handleControl(context, event): | |
| payload = '' | |
| device_id = event['payload']['appliance']['applianceId'] | |
| message_id = event['header']['messageId'] | |
| payload = { } | |
| if event['header']['name'] == 'TurnOnRequest': | |
| cmd = {"request":"switch","power":"on"} | |
| header = { | |
| "namespace":"Alexa.ConnectedHome.Control", | |
| "name":"TurnOnConfirmation", | |
| "payloadVersion":"2", | |
| "messageId": message_id | |
| } | |
| else: | |
| cmd = {"request":"switch","power":"off"} | |
| header = { | |
| "namespace":"Alexa.ConnectedHome.Control", | |
| "name":"TurnOffConfirmation", | |
| "payloadVersion":"2", | |
| "messageId": message_id | |
| } | |
| print "putting request ",cmd | |
| URL = "https://www.example.com/heater/heater.php" #change this | |
| access_token = event['payload']['accessToken'] | |
| bearer = "Bearer " + access_token | |
| auth = {"Authorization": bearer} | |
| print auth | |
| requests.put(URL, verify="server.pem", json=cmd, headers=auth) | |
| return { 'header': header, 'payload': payload } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment