-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda-function.py
80 lines (65 loc) · 2.71 KB
/
lambda-function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import logging
import urllib2
import json
########################################
# Enter here your IP Symcon Connect ID #
########################################
myips = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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):
logger.info('handleDiscovery')
payload = ''
header = {
"namespace": "Alexa.ConnectedHome.Discovery",
"name": "DiscoverAppliancesResponse",
"payloadVersion": "2"
}
if event['header']['name'] == 'DiscoverAppliancesRequest':
devices = urllib2.urlopen("https://"+myips+".ipmagic.de/hook/alexa/discover").read()
payload = { "discoveredAppliances":
json.loads(devices)
}
return { 'header': header, 'payload': payload }
def handleControl(context, event):
payload = {}
device_id = event['payload']['appliance']['applianceId']
message_id = event['header']['messageId']
request_type = event['header']['name']
logger.info(event['header']['name'] + ": " + device_id)
logger.info(event)
confirmation = ''
if request_type == 'TurnOnRequest':
#payload = { }
value = 100;
confirmation = 'TurnOnConfirmation'
elif request_type == 'TurnOffRequest':
#payload = { }
value = 0;
confirmation = 'TurnOffConfirmation'
elif request_type == 'SetPercentageRequest':
value = event['payload']['percentageState']['value']
logger.info('SetPercentageRequest ' + str(value))
confirmation = 'SetPercentageConfirmation'
elif request_type == 'SetTargetTemperatureRequest':
value = event['payload']['targetTemperature']['value']
payload = {"targetTemperature":{
"value":value}
}
logger.info('SetTargetTemperatureRequest ' + str(value))
confirmation = 'SetTargetTemperatureConfirmation'
urllib2.urlopen("https://"+myips+".ipmagic.de/hook/alexa/control/dimm?id="+device_id+"&value="+str(value)).read()
logger.info('-->' + confirmation)
header = {
"namespace":"Alexa.ConnectedHome.Control",
"name":confirmation,
"payloadVersion":"2",
"messageId": message_id
}
return { 'header': header, 'payload': payload }