-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
executable file
·258 lines (223 loc) · 8.74 KB
/
app.js
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
"use strict";
const Homey = require('homey');
var jsonPath = require('jsonpath-plus')
var http = require('http.min')
var evohomey = require('./lib/evohomey.js')
var login = require('./lib/login.js')
global.global_zones_read = []
global.global_list_devices = []
//var moment = require('./lib/moments.js');
class Evohome extends Homey.App {
log() {
console.log.bind(this, "[log]").apply(this, arguments);
}
error() {
console.error.bind(this, "[error]").apply(this, arguments);
}
onInit() {
// this.log(`${this.homey.manifest.id} - ${this.homey.manifest.version} started..`);
this.log("Evohome version 3.1.3 started")
Homey.ManagerSettings.set('account_info','None');
Homey.ManagerSettings.set('installation','None');
Homey.ManagerSettings.set('zones_read','None');
Homey.ManagerSettings.set('access_token_expires',Date()); // make sure new is used at start-up
// register qa-status-token
let QA_Token = new Homey.FlowToken( 'evohome_qa_status', {
type: 'string',
title: 'Evohome quickaction'
});
QA_Token.register()
.then(() => {
return QA_Token.setValue( 'not available' );
})
.catch( err => {
this.error( err );
});
// set_quickaction
let set_quickaction = new Homey.FlowCardAction('set_quickaction');
set_quickaction
.register()
.registerRunListener(( args, state ) => {
this.log(args['qa'])
let qa_set = evohomey.quickaction_set(args['qa'],'True',''); // true or false
Homey.ManagerSettings.set('quickAction',args['qa']);
QA_Token.setValue( args['qa'] );
return Promise.resolve( qa_set );
})
// set_quickaction
let set_temporary_quickaction = new Homey.FlowCardAction('set_temporary_quickaction');
set_temporary_quickaction
.register()
.registerRunListener(( args, state ) => {
this.log(args['qa'])
const tmpTime = new Date();
tmpTime.setHours(tmpTime.getHours() + args['temp_hours']);
tmpTime.setSeconds(0,0);
this.log(args['temp_hours']);
this.log(tmpTime.toISOString().replace(/\.\d+Z/,'Z'));
//this.log(args['qa_date']);
//this.log(args['qa_time']);
//var TimezoneDate = new Date().getTimezoneOffset()
//this.log(TimezoneDate);
let qa_temp_set = evohomey.quickaction_set(args['qa'],'False',tmpTime.toISOString().replace(/\.\d+Z/,'Z')); // true or false
Homey.ManagerSettings.set('quickAction',args['qa']);
QA_Token.setValue( args['qa'] );
//return Promise.resolve( qa_temp_set );
return Promise.resolve (' TMP ok');
})
// set_quickaction_manual_entry
let set_quickaction_manual_entry = new Homey.FlowCardAction('set_quickaction_manual_entry');
set_quickaction_manual_entry
.register()
.registerRunListener(( args, state ) => {
this.log('quickaction manual entry')
this.log(args['qa'])
switch(args['qa']) {
case "HeatingOff":
case "Auto":
case "AutoWithEco":
case "Away":
case "Custom":
case "DayOff":
let qa_set = evohomey.quickaction_set(args['qa'],'True',''); // true or false
Homey.ManagerSettings.set('quickAction',args['qa']);
return Promise.resolve( qa_set );
break
default:
return Promise.reject ('invalidSettings')
}
})
// set_temperature_manual (device)
let set_temperature_manual = new Homey.FlowCardAction('set_temperature_manual');
set_temperature_manual
.register()
.registerRunListener(( args, state ) => {
this.log('temperature manual entry')
var id = args.device.getID();
this.log(id);
let temp_set = evohomey.temperature_set(id,args['temp_manual'],1)
return Promise.resolve( 'temp_set' );
})
// set_temperature_temporary (device)
let set_temperature_temporary = new Homey.FlowCardAction('set_temperature_temporary');
set_temperature_temporary
.register()
.registerRunListener(( args, state ) => {
this.log('temperature temporary manual entry')
var id = args.device.getID();
this.log(id);
const tmpTime = new Date();
this.log(tmpTime);
tmpTime.setHours(tmpTime.getHours() + args['temp_hours']);
tmpTime.setSeconds(0,0);
this.log(args['temp_hours']);
this.log(tmpTime.toISOString().replace(/\.\d+Z/,'Z'));
var setcode = "TemporaryOverride";
let temp_set = evohomey.temperature_set(id,args['temp_manual'],setcode,tmpTime.toISOString().replace(/\.\d+Z/,'Z'))
return Promise.resolve( 'temp_temorary_set' );
})
// reset_temperature (device)
let reset_temperature = new Homey.FlowCardAction('reset_temperature');
reset_temperature
.register()
.registerRunListener(( args, state ) => {
this.log('temperature reset')
var id = args.device.getID();
this.log(id);
let temp_reset = evohomey.temperature_set(id,'',0)
return Promise.resolve( 'temp_reset' );
})
// reset_all_zones (device)
let reset_all_zones = new Homey.FlowCardAction('reset_all_zones');
reset_all_zones
.register()
.registerRunListener(( args, state ) => {
this.log('reset all zones');
// first we need a list of all IDs
var zonePromise = evohomey.zones_read();
zonePromise.then(function(result) {
var data = result;
console.log('test')
data.forEach(function(value){
console.log('+++reset all temp: +++')
//console.log(value)
//console.log('--')
//console.log(value.zoneId);
//console.log(value.setpointStatus.setpointMode)
if (value.setpointStatus.setpointMode != 'FollowSchedule') {
console.log(' cancel needed for: ', value.zoneId);
let temp_reset = evohomey.temperature_set(value.zoneId,'',0,'')
}
})
return Promise.resolve( 'ok' );
})
.catch('catch reset_all_zones');
return Promise.resolve( 'ok' );
})
//// MAIN
console.log('-----')
//console.log(userid);
// first, some checks and logging to timeline is something is not right
var evohomeUser = Homey.ManagerSettings.get('username');
var evohomePassword= Homey.ManagerSettings.get('password');
if ( !evohomeUser || !evohomePassword ) {
new Homey.Notification({excerpt:'Evohome: missing username or password; enter your Honeywell credentials in app settings'}) // username + password set?
.register();
} else {
// login correct ?
var appid="91db1612-73fd-4500-91b2-e63b069b185c";
var evohomeloginPromise = login.evohomelogin(evohomeUser,evohomePassword,appid);
evohomeloginPromise.then(function(result) {
console.log ('app start login check: login successfull')
regular_update(); // kick-off during start-up
})
.catch(function(reject) {
console.log('app start: login failed: ', reject);
new Homey.Notification({excerpt:'Evohome: invalid username or password; please adjust in app settings and restart app'}) // username + password set?
.register();
})
}
// all checks completed
// uncomment after checks complete
setInterval(regular_update,3 * 60 * 1000);
//
function regular_update() {
console.log('3 minute update routine')
// 1 - quickaction status uitlezen
console.log('quickaction read')
var quickactionPromise = evohomey.quickaction_read();
quickactionPromise.then(function(result) {
var qa_new = result;
console.log('QA retrieved: ', qa_new);
var qa_old = Homey.ManagerSettings.get('qa')
console.log('QA Stored: ',qa_old);
QA_Token.setValue( qa_new );
if (qa_old != qa_new) {
// Trigger quickaction_changed_externally
console.log ('quickaction changed')
Homey.ManagerSettings.set('qa',qa_new);
let quickaction_changed_externally = new Homey.FlowCardTrigger('quickaction_changed_externally');
let tokens = {
'qa_name': qa_new
}
quickaction_changed_externally
.register()
.trigger(tokens)
.catch('qa changed externally catch')
.then(console.log('new qa set'))
}
// 2 - zone status uitlezen
console.log('zone status read')
var zonePromise = evohomey.zones_read();
zonePromise.then(function(result) {
var data = result;
console.log('hier gebeurt niets volgens mij');
//Homey.ManagerSettings.set('zones_read','test');
// hier dingen uitvoeren
})
});
} // 5 minute update
//// END MAIN
} // end oninit
}
module.exports = Evohome;