Created
December 4, 2016 13:48
-
-
Save jamesbulpin/431e729d6161cd7b9130dcabd11c93b2 to your computer and use it in GitHub Desktop.
Alexa smart home skill adapter to send MQTT messages via Octoblu
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 urllib | |
import urllib2 | |
octoblu_trigger = "https://triggers.octoblu.com/v2/flows/01234567-89ab-cdef-0123-456789abcdef/triggers/01234567-89ab-cdef-0123-456789abcdef" | |
def lambda_handler(event, context): | |
access_token = event['payload']['accessToken'] | |
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":"Bedroom1=Desk", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"Study Desk Lamp", | |
"friendlyDescription":"Desk lamp in bedroom 1", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"Light/Bedroom1/Desk" | |
} | |
}, | |
{ | |
"applianceId":"Bedroom2=Lamp", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"Bedroom Lamp", | |
"friendlyDescription":"Bedside lamp in bedroom 2", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"Light/Bedroom2/Lamp" | |
} | |
}, | |
{ | |
"applianceId":"OutsideFront", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"Outside light", | |
"friendlyDescription":"Outside front door light", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"Light/frontdoor" | |
} | |
}, | |
{ | |
"applianceId":"LoungeLights", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"Lounge lights", | |
"friendlyDescription":"All lights in the living room", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"compound/loungelights" | |
} | |
}, | |
{ | |
"applianceId":"DiningRoomLights", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"Dining room lights", | |
"friendlyDescription":"Fireplace lights in the dining room", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"Light/Dining/Fireplace" | |
} | |
}, | |
{ | |
"applianceId":"loungetv", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"TV", | |
"friendlyDescription":"Living room TV and amp", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"KODI/Lounge/TV" | |
} | |
}, | |
{ | |
"applianceId":"loungeeverything", | |
"manufacturerName":"James Bulpin", | |
"modelName":"LWRF", | |
"version":"v0.1", | |
"friendlyName":"living room", | |
"friendlyDescription":"Living room all devices", | |
"isReachable":True, | |
"actions":[ | |
"turnOn", | |
"turnOff" | |
], | |
"additionalApplianceDetails":{ | |
"mqttTopics":"compound/loungeall" | |
} | |
} | |
] | |
} | |
return {"header":header, "payload":payload} | |
def handleControl(context, event): | |
payload = '' | |
device_id = event['payload']['appliance']['applianceId'] | |
message_id = event['header']['messageId'] | |
obmsg = {"debug":event} | |
cfm = "" | |
if event['header']['name'] == 'TurnOnRequest' or event['header']['name'] == 'TurnOffRequest': | |
if event['header']['name'] == 'TurnOnRequest': | |
obmsg["message"] = "on" | |
cfm = "TurnOnConfirmation" | |
else: | |
obmsg["message"] = "off" | |
cfm = "TurnOffConfirmation" | |
for t in event["payload"]["appliance"]["additionalApplianceDetails"]["mqttTopics"].split(): | |
obmsg["topic"] = t | |
url = octoblu_trigger | |
data = urllib.urlencode(obmsg) | |
req = urllib2.Request(url, data) | |
response = urllib2.urlopen(req) | |
the_page = response.read() | |
payload = { } | |
header = { | |
"namespace":"Alexa.ConnectedHome.Control", | |
"name":cfm, | |
"payloadVersion":"2", | |
"messageId": message_id | |
} | |
return {"header":header, "payload":payload} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment