forked from hahn-th/homematicip-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
62 lines (54 loc) · 1.85 KB
/
auth.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
# coding=utf-8
import json
import uuid
import requests
import homematicip
from homematicip.home import Home
class Auth(object):
def __init__(self, home: Home):
self.uuid = str(uuid.uuid4())
self.headers = {
"content-type": "application/json",
"accept": "application/json",
"VERSION": "12",
"CLIENTAUTH": home._connection.clientauth_token,
}
self.url_rest = home._connection.urlREST
self.pin = None
def connectionRequest(
self, access_point, devicename="homematicip-python"
) -> requests.Response:
data = {"deviceId": self.uuid, "deviceName": devicename, "sgtin": access_point}
headers = self.headers
if self.pin != None:
headers["PIN"] = self.pin
response = requests.post(
"{}/hmip/auth/connectionRequest".format(self.url_rest),
json=data,
headers=headers,
)
return response
def isRequestAcknowledged(self):
data = {"deviceId": self.uuid}
response = requests.post(
"{}/hmip/auth/isRequestAcknowledged".format(self.url_rest),
json=data,
headers=self.headers,
)
return response.status_code == 200
def requestAuthToken(self):
data = {"deviceId": self.uuid}
response = requests.post(
"{}/hmip/auth/requestAuthToken".format(self.url_rest),
json=data,
headers=self.headers,
)
return json.loads(response.text)["authToken"]
def confirmAuthToken(self, authToken):
data = {"deviceId": self.uuid, "authToken": authToken}
response = requests.post(
"{}/hmip/auth/confirmAuthToken".format(self.url_rest),
json=data,
headers=self.headers,
)
return json.loads(response.text)["clientId"]