-
Notifications
You must be signed in to change notification settings - Fork 2
/
hass_proxy.py
42 lines (35 loc) · 1.37 KB
/
hass_proxy.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
import os
import ssl
import json
import traceback
from urllib import request
from functools import reduce
HASS_URL = os.environ.get('HASS_URL')
BEARER_TOKEN = os.environ.get('BEARER_TOKEN')
VERIFY_SSL = os.environ.get('VERIFY_SSL') != 'false'
def dict_get(dictionary, dotted_key):
keys = dotted_key.split('.')
return reduce(lambda d, key: d.get(key) if d else None, keys, dictionary)
def lambda_handler(event, context):
try:
assert HASS_URL, 'HASS_URL environment variable is not set'
bearer_token = BEARER_TOKEN or \
dict_get(event, 'directive.endpoint.scope.token') or \
dict_get(event, 'directive.payload.grantee.token') or \
dict_get(event, 'directive.payload.scope.token')
assert bearer_token, 'Bearer token missing'
url = HASS_URL.rstrip('/') + '/api/alexa/smart_home'
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {bearer_token}'
}
req = request.Request(url, data=json.dumps(event).encode('utf-8'), headers=headers)
context = ssl.create_default_context()
if VERIFY_SSL is False:
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
r = request.urlopen(req, context=context)
return json.load(r)
except Exception as e:
traceback.print_exc()
raise e