-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheatpump.py
267 lines (197 loc) · 5.37 KB
/
heatpump.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import requests
from config import config
base_url = 'https://api.melview.net/'
username = config["account"]["username"]
password = config["account"]["password"]
appversion = '3.2.673'
class HeatpumpStatus:
def __init__(self, json):
self.id = json['id']
self.power = json['power']
self.standby = json["standby"]
self.setmode = json["setmode"]
self.automode = json["automode"]
self.setfan = json['setfan']
self.settemp = json["settemp"]
self.roomtemp = json['roomtemp']
self.airdir = json["airdir"]
self.airdirh = json["airdirh"]
self.sendcount = json["sendcount"]
self.fault = json["fault"]
self.error = json["error"]
def get_headers(cookie):
return { 'Cookie': cookie }
def post(cookie, api, data):
req = requests.post(base_url + api, json=data, headers = get_headers(cookie))
print(f"{api} result", req.status_code, req.reason)
if req.status_code == 200:
return req.json()
return None
def login():
data = {
"user": username,
"pass": password,
"appversion": appversion
}
req = requests.post(base_url + 'api/login.aspx', json=data)
print('login result', req.status_code, req.reason)
if req.status_code == 200:
return req.headers['Set-Cookie']
return None
def logout():
req = requests.post(base_url + 'api/logout.aspx')
print('logout result', req.status_code, req.reason)
def list_rooms(cookie):
req = requests.get(base_url + 'api/rooms.aspx', headers = get_headers(cookie))
print('list_rooms result', req.status_code, req.reason)
if req.status_code != 200:
return []
# don't care about buildings, just get the list of units.
units = req.json()[0]['units']
heatpumps = []
for unit in units:
room = unit['room']
unitid = unit['unitid']
print('found room', room, unitid)
status = get_unit_status(cookie, unitid)
get_unit_capabilities(cookie, unitid)
if status != None:
heatpump = Heatpump(room, unitid, status)
heatpumps.append(heatpump)
return heatpumps
def get_unit_capabilities(cookie, unitid):
return post(cookie, "api/unitcapabilities.aspx", { 'unitid': unitid })
def get_unit_status(cookie, unitid):
data = {
'unitid': unitid,
'v': 2
}
res = post(cookie, "api/unitcommand.aspx", data)
if res != None:
return HeatpumpStatus(res)
return None
def send_cmd(cookie, unitid, cmd):
data = {
'unitid': unitid,
'v': 2,
'commands': cmd
}
return post(cookie, "api/unitcommand.aspx", data)
def send_set_power(cookie, unitid, power):
return send_cmd(cookie, unitid, f"PW{power}")
def send_set_temp(cookie, unitid, temp):
return send_cmd(cookie, unitid, f"TS{temp}")
def send_set_fan(cookie, unitid, fan):
return send_cmd(cookie, unitid, f"FS{fan}")
def send_set_mode(cookie, unitid, mode):
return send_cmd(cookie, unitid, f"MD{mode}")
def get_room(name):
for room in config["rooms"]:
if room["name"].lower() == name.lower():
return room
return None
def set_power(unitid, power):
cookie = login()
if cookie == None:
return False
if send_set_power(cookie, unitid, power):
logout()
return True
logout()
return False
def turn_on(unitid):
return set_power(unitid, 1)
def turn_off(unitid):
return set_power(unitid, 0)
def get_temp(name):
cookie = login()
if cookie == None:
return "Failed to Login"
room = get_room(name)
if room == None:
logout()
return f"Failed to find {name}"
unitid = room["unitid"]
status = get_unit_status(cookie, unitid)
if status == None:
logout()
return "Failed to get the current heatpump temperature"
logout()
return f"The current heatpump temperature is {status.settemp}"
def get_room_temp(name):
cookie = login()
if cookie == None:
return "Failed to Login"
room = get_room(name)
if room == None:
logout()
return f"Failed to find {name}"
unitid = room["unitid"]
status = get_unit_status(cookie, unitid)
if status == None:
logout()
return "Failed to get the current room temperature"
logout()
return f"The current room temperature is {status.roomtemp}"
def get_status(unitid):
cookie = login()
if cookie == None:
return "Failed to Login"
status = get_unit_status(cookie, unitid)
if status == None:
logout()
return "Failed to get the current status"
logout()
return status
def set_temp(name, temp):
cookie = login()
if cookie == None:
return "Failed to Login"
room = get_room(name)
if room == None:
logout()
return f"Failed to find {name}"
unitid = room["unitid"]
if send_set_temp(cookie, unitid, temp):
logout()
return f'Set temperature on the {name} heatpump to {temp} degrees'
logout()
return f'Failed to set temperature on the {name} heatpump'
def set_fan(unitid, fan):
cookie = login()
if cookie == None:
return "Failed to Login"
if send_set_fan(cookie, unitid, fan):
logout()
return True
logout()
return False
def set_mode(name, mode):
mode_num = None
if mode == "heat" or mode == "heating":
mode = "heat"
mode_num = 1
elif mode == "dry":
mode_num = 2
elif mode == "cool" or mode == "cooling":
mode = "cool"
mode_num = 3
elif mode == "fan":
mode_num = 7
elif mode == "auto":
mode_num = 8
else:
return "I don't know what mode {mode} is"
cookie = login()
if cookie == None:
return "Failed to Login"
room = get_room(name)
if room == None:
logout()
return f"Failed to find {name}"
unitid = room["unitid"]
if send_set_mode(cookie, unitid, mode_num):
logout()
return f'Set mode on the {name} heatpump to {mode}'
logout()
return f'Failed to set mode on the {name} heatpump'