-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC-Webhook_Git.py
225 lines (199 loc) · 7.54 KB
/
AC-Webhook_Git.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
from flask import Flask, request, Response
import requests
import json
import sys
# Please note that this is still early code and I have not implemented any error handling or coding best practices. Feel free to improve on your own branch :)
app = Flask(__name__)
# URL's
Base_URL = "https://que.actronair.com.au"
Signin_add = "/api/v0/bc/signin"
API_ID_add = "/rest/v0/device/ACONNECTXXXXXXXX_0_2_4?user_access_token="
# Main page for Flask
@app.route('/')
def hello():
return 'Hello, World! - Test you can reach this page'
#API Command to Actron Cloud
def Actron_Set(payload):
print(payload)
user_access_token = Signin()
URL_Actron_Set = (Base_URL+API_ID_add+user_access_token)
#You can get this default information for using Postman - Then copy out python and drop here
# URL: https://que.actronair.com.au/rest/v0/device/ACONNECTXXXReplace_With_Your_Serial_XXXX_0_2_4?user_access_token=XXX_Use_the_Signin_API_to_get_this_XX
# Method: PUT
# Authentication: In the header using access token
# Header:
# Content-Type: application/json
#Payload will depend on the instructions you want to send. Easiest way to get all the different options and identify which zones you have. Use this URL with your local IP.
headers = {'Content-Type': 'application/json','Cookie': 'TiPMix=;XXXXX x-ms-routing-name=self; ARRAffinity=XXXXXX; ARRAffinitySameSite=XXXXXXX'}
response = requests.request("PUT", URL_Actron_Set, headers=headers, data=payload)
print(response.text)
# Authenticate to Actron Cloud, return User Access Token for sending commands.
def Signin():
URL_Signin = Base_URL+Signin_add
payload={}
# These are headers generated by my web browser that I have been using, most of these attributes are not required in order to authenticate.
# you will need to replace this with the output generated by your web browser using inspect in Chrome or Postman.
headers = {
'Connection': 'keep-alive',
'Content-Length': '0',
'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
'Accept': 'application/json, text/plain, */*',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36',
'Origin': 'https://actronair.com.au',
'Sec-Fetch-Site': 'same-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://actronair.com.au/',
'Accept-Language': 'en-US,en;q=0.9',
'Authorization': 'Basic XXX_You will need to generate your own Auth string - This can be done using Postman and basic auth_XXX',
'Cookie': 'xxxxx'
}
response = requests.request("POST", URL_Signin, headers=headers, data=payload)
response = response.json()
data = response["status"]
Access_Token = response["value"]["userAccessToken"]
print("Status: " + str(data))
return(Access_Token)
@app.route('/targetTemperature', methods=['GET'])
def targetTemperature():
Target_Temp = request.args.get('value')
#print(Target_Temp)
Actron_Set("{DA:{tempTarget:"+Target_Temp+"}}")
return('200')
@app.route('/targetHeatingCoolingState', methods=['GET'])
#This is to aline the actron mode variables to the plugin I have been using.
def targetHeatingCoolingState():
Target_State = request.args.get('value')
if Target_State == '0':
Target_Value = "{DA:{amOn: false}}"
elif Target_State == '1':
Target_Value = "{DA:{amOn: true, mode: 1}}"
elif Target_State == '2':
Target_Value = "{DA:{amOn: true, mode: 2}}"
elif Target_State == '3':
Target_Value = "{DA:{amOn: true, mode: 0}}"
else:
print("Error")
Actron_Set(Target_Value)
return('200')
"""
Actron Settings:
* Mode Selection and status
* 0 = Auto
* 1 = Heat
* 2 = Cool
* 3 = Fan
* Fan Speed Selection and status
* 0 = Low
* 1 = Med
* 2 = High
* Compressor Activity - This is just information
* 0 = Heating
* 1 = Cooling
* 2 = Off
Plug In Settings:
0 Off
1 Heat
2 Cool
3 Auto
"""
@app.route('/Get_Status', methods=['GET'])
def Get_Status():
# I have hard coded my Actron Connect IP here, change this to your controller IP.
response = requests.get('http://10.30.0.9/6.json')
status_data = json.loads(response.content)
status_update_list = {}
#print(status_data['isOn'])
# This is the expected response for the homebridge plug in that I'm using, you don't need to use the same pluggin as me. But you will need to change the payloads and web handlers if you do.
# plug in details: https://github.com/Tommrodrigues/homebridge-web-thermostat#readme
# Plug in Settings:
"""
{
"accessory": "Thermostat",
"name": "Actron",
"apiroute": "http://IP_Of_Where_This_Code_IS_Running", #Port 80 is the default
"http_method": "GET",
}
"""
if str(status_data['isOn']) == 'False':
status_update_list['currentHeatingCoolingState'] = 0
status_update_list['currentTemperature'] = status_data['roomTemp_oC']
status_update_list['targetHeatingCoolingState'] = 0
status_update_list['targetTemperature'] = status_data['setPoint']
elif str(status_data['isOn']) == 'True':
status_update_list['currentTemperature'] = status_data['roomTemp_oC']
status_update_list['targetTemperature'] = status_data['setPoint']
if int(status_data['mode']) == 0:
status_update_list['targetHeatingCoolingState'] = "3"
status_update_list['currentHeatingCoolingState'] = "3"
elif int(status_data['mode']) == 1:
status_update_list['targetHeatingCoolingState'] = "1"
status_update_list['currentHeatingCoolingState'] = "1"
elif int(status_data['mode']) == 2:
status_update_list['targetHeatingCoolingState'] = "2"
status_update_list['currentHeatingCoolingState'] = "2"
elif int(status_data['mode']) == 3:
status_update_list['targetHeatingCoolingState'] = "0"
status_update_list['currentHeatingCoolingState'] = "0"
else:
return("Error no valid state")
else:
print("Failed_loop_error 404")
print(status_update_list)
@app.route('/zone_state', methods=['GET'])
def zone_state():
response = requests.get('http://10.30.0.98/6.json')
status_data = json.loads(response.content)
zone = request.args.get('zone')
print(zone)
if zone == "upstairs":
zone_state = status_data['enabledZones'][0]
elif zone == "downstairs":
zone_state = status_data['enabledZones'][1]
else:
return('404')
if zone_state == 0:
return('0')
elif zone_state == 1:
return('1')
else:
return('404')
@app.route('/zone', methods=['GET'])
def zone():
response = requests.get('http://10.30.0.98/6.json')
status_data = json.loads(response.content)
zone = request.args.get('zone')
zone_state = request.args.get('state')
print(zone)
print(zone_state)
if zone == "upstairs":
Downstairs_Zone_State = status_data['enabledZones'][1]
if zone_state == 'on':
Upstairs_Zone_State = '1'
elif zone_state == 'off':
Upstairs_Zone_State = '0'
elif zone == "downstairs":
Upstairs_Zone_State = status_data['enabledZones'][0]
if zone_state == 'on':
Downstairs_Zone_State = '1'
elif zone_state == 'off':
Downstairs_Zone_State = '0'
else:
return('404 fff')
payload = ("{DA:{enabledZones: [%s,%s,0,0,0,0,0,0]}}"%(Upstairs_Zone_State,Downstairs_Zone_State))
print(payload)
Actron_Set(payload)
return('200')
@app.route('/status', methods=['GET'])
def status():
status_data = Get_Status()
data_template = {
"targetHeatingCoolingState": status_data['targetHeatingCoolingState'],
"targetTemperature":status_data['targetTemperature'],
"currentHeatingCoolingState": status_data['currentHeatingCoolingState'],
"currentTemperature": status_data['currentTemperature']
}
return data_template
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=80) #Keep as port 80 otherwise the homebridge plug in will not be able to communicate to this webhook.