-
Notifications
You must be signed in to change notification settings - Fork 2
/
home.py
120 lines (93 loc) · 3.06 KB
/
home.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
import os
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from requests.auth import HTTPDigestAuth
from rooms import Room
from constants import REST_URL
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
DEBUG = True
def log(msg):
if DEBUG:
print("Ambipy :: {0}".format(msg))
class Home(object):
def __init__(self, userObj):
self.__userObj = userObj
self.__setup()
def __setup(self):
queryStr = "{0}/UserCredential?email={1}&pwd={2}".format(REST_URL, self.__userObj.email, self.__userObj.password)
log(queryStr)
response = requests.get(queryStr, verify=False)
if response.json() != {}:
self.__userObj.userId = response.json()["user_id"]
self.__userObj.tokenId = response.json()["token_id"]
self.__userObj.createdOn = response.json()["created_on"]
self.__headers = {"Authorization":"Bearer {0}".format(response.json()["token_id"])}
else:
raise Exception("User failed to login.")
@property
def rooms(self):
"""
Returns a list of room device objects.
Args:
None
Returns:
`list` of rooms.Room objects.
"""
roomsList = []
queryStr = "{0}/User?expand=appliance%2Cdevice&user_id={1}".format(REST_URL, self.__userObj.userId)
log(queryStr)
response = requests.get(queryStr, headers=self.__headers, verify=False)
for device in response.json()["devices"]:
roomObj = Room(device, self.__headers)
roomsList.append(roomObj)
return roomsList
class User(object):
AUTH_JSON = "auth.json"
def __init__(self):
self.__password = None
self.__email = None
self.__tokenId = None
self.__userId = None
self.__createdOn = None
self.__setup()
def __setup(self):
authFile = os.path.join(os.path.split(__file__)[0], self.AUTH_JSON)
if os.path.exists(authFile):
fh = open(authFile, "r")
data = json.loads(fh.read())
if data != {}:
log("Loading Auth file")
self.__email = data["email"]
self.__password = data["password"]
fh.close()
@property
def email(self):
return self.__email
@email.setter
def email(self, emailIn):
self.__email = emailIn
@property
def password(self):
return self.__password
@password.setter
def password(self, passwordIn):
self.__password = passwordIn
@property
def createdOn(self):
return self.__createdOn
@createdOn.setter
def createdOn(self, createdOnIn):
self.__createdOn = createdOnIn
@property
def userId(self):
return self.__userId
@userId.setter
def userId(self, userIdIn):
self.__userId = userIdIn
@property
def tokenId(self):
return self.__tokenId
@tokenId.setter
def tokenId(self, tokenIdIn):
self.__tokenId = tokenIdIn