-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.py
executable file
·250 lines (208 loc) · 9.53 KB
/
action.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#
# https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/smart-home-skill-api-reference
#
import uuid
from datetime import datetime
import sys
action_func_registry = []
# action-func decorator
def alexa(action_name, directive_type, response_type, namespace, properties,payload_version ):
def store_metadata(func):
func.alexa_action_name = action_name
func.alexa_directive_type = directive_type
func.alexa_response_type = response_type
func.alexa_namespace = namespace
func.alexa_properties = properties
func.alexa_payload_version = payload_version
action_func_registry.append( func )
return func
return store_metadata
class AlexaActions(object):
def __init__(self, sh, logger, devices, proto):
self.actions = {}
self.actions_by_directive = {}
for func in action_func_registry:
logger.debug("Alexa: initializing action {}".format(func.alexa_action_name))
action = AlexaAction(sh, logger, devices, func, func.alexa_action_name, func.alexa_directive_type, func.alexa_response_type,func.alexa_namespace, func.alexa_properties,func.alexa_payload_version,proto)
self.actions[action.name] = action
self.actions_by_directive[action.directive_type] = action
def by_name(self, name):
return self.actions[name] if name in self.actions else None
def for_directive(self, directive):
return self.actions_by_directive[directive] if directive in self.actions_by_directive else None
class AlexaAction(object):
def __init__(self, sh, logger, devices, func, action_name, directive_type, response_type, namespace,properties,payload_version,proto):
self.sh = sh
self.logger = logger
self.devices = devices
self.func = func
self.name = action_name
self.directive_type = directive_type
self.response_type = response_type
#P3 Properties
self.namespace = namespace
self.response_Value = None
self.properties = properties
self.payload_version = payload_version
self._proto = proto
def __call__(self, payload):
return self.func(self, payload)
def items(self, device_id):
device = self.devices.get(device_id)
return device.items_for_action(self.name) if device else []
def item_range(self, item, default=None):
return item.alexa_range if hasattr(item, 'alexa_range') else default
def header(self, name=None):
return {
'messageId': uuid.uuid4().hex,
'name': name if name else self.response_type,
'namespace': 'Alexa.ConnectedHome.Control',
'payloadVersion': '2'
}
def respond(self, payload={}):
return {
'header': self.header(),
'payload': payload
}
# find Value for Key in Json-structure
# needed for Alexa Payload V3
def search(self,p, strsearch):
if type(p) is dict: # im Dictionary nach 'language' suchen
if strsearch in p:
tokenvalue = p[strsearch]
if not tokenvalue is None:
return tokenvalue
else:
for i in p:
tokenvalue = self.search(p[i], strsearch) # in den anderen Elementen weiter suchen
if not tokenvalue is None:
return tokenvalue
def replace(self,p, strsearch, newValue):
if type(p) is dict:
if strsearch in p:
tokenvalue = p[strsearch]
p[strsearch] = newValue
if not tokenvalue is None:
return tokenvalue
else:
for i in p:
tokenvalue = self.replace(p[i], strsearch,newValue)
if not tokenvalue is None:
return tokenvalue
def GenerateThermoList(self, myModes, listType):
mylist = myModes.split(' ')
myValueList = {}
myModeList = {}
for i in mylist:
key=i.split(':')
myValueList[key[0]]=key[1]
myModeList[key[1]] = key[0]
if listType == 1:
return myValueList
elif listType == 2:
return myModeList
def p3_AddDependencies(self, orgDirective, dependency, myEndPointID):
now = datetime.now().isoformat()
myTimeStamp = now[0:22]+'Z'
for addDependencies in dependency:
AlexaItem = self.devices.get(myEndPointID)
#myItems = AlexaItem.action_items()
# walk over all Items examp..: Item: OG.Flur.Spots.dimmen / Item: OG.Flur.Spots
for myAction in AlexaItem.action_items:
if myAction == addDependencies: # Action für dieses Device gefunden
myitem = AlexaItem.action_items[myAction][0]
myAddValue = myitem()
myAddName = None
if myAction == 'SetThermostatMode':
myModes = AlexaItem.thermo_config
myValueList = self.GenerateThermoList(myModes,1)
myMode = self.search(myValueList, str(int(myAddValue)))
myAddName = {
"namespace": "Alexa.ThermostatController",
"name": "thermostatMode",
"value": myMode,
"timeOfSample": myTimeStamp,
"uncertaintyInMilliseconds": 5000
}
elif myAction == 'SetTargetTemperature':
myAddName = {
"namespace": "Alexa.ThermostatController",
"name": "targetSetpoint",
"value": {
"value": myAddValue,
"scale": "CELSIUS"
},
"timeOfSample": myTimeStamp,
"uncertaintyInMilliseconds": 5000
}
if myAddName != None:
orgDirective['context']['properties'].append(myAddName)
return orgDirective
def p3_respond(self, Request):
myEndpoint = self.search(Request,'endpoint')
myScope = self.search(Request,'scope')
myEndPointID = self.search(Request,'endpointId')
myHeader = self.search(Request,'header')
now = datetime.now().isoformat()
myTimeStamp = now[0:22]+'Z'
self.replace(myHeader,'messageId',uuid.uuid4().hex)
self.replace(myHeader,'name','Response')
self.replace(myHeader,'namespace','Alexa')
myReponse = {
"context": {
"properties": [ {
"namespace": self.namespace,
"name": self.response_type,
"value": self.response_Value,
"timeOfSample": myTimeStamp,
"uncertaintyInMilliseconds": 5000
} ]
},
"event": {
"header": myHeader
,
"endpoint" : {
"scope": myScope,
"endpointId": myEndPointID
},
"payload": {}
}
}
# Check for Special Response-Type
if self.namespace == 'Alexa.SceneController':
self.replace(myReponse,'context',{})
self.replace(myReponse,'payload',{
"cause" : {
"type" : "VOICE_INTERACTION"
},
"timestamp" : myTimeStamp
})
self.replace(myReponse,'namespace',self.namespace)
self.replace(myReponse,'name',self.response_type,)
elif self.namespace == 'Alexa.CameraStreamController':
myContext = {
"properties": [
{
"namespace": "Alexa.EndpointHealth",
"name": "connectivity",
"value": {
"value": "OK"
},
"timeOfSample": myTimeStamp,
"uncertaintyInMilliseconds": 200
}
]
}
self.replace(myReponse,'namespace',self.namespace)
self.replace(myReponse,'context',myContext)
self.replace(myReponse,'payload',self.response_Value)
# Check for special needs of dependencies
if len(self.properties) != 0:
myReponse = self.p3_AddDependencies(myReponse, self.properties,myEndPointID)
return myReponse
# https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/smart-home-skill-api-reference#error-messages
def error(self, error_type, payload={}):
return {
'header': self.header(error_type),
'payload': payload
}