This repository was archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.js
More file actions
195 lines (161 loc) · 6.24 KB
/
app.js
File metadata and controls
195 lines (161 loc) · 6.24 KB
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
'use strict';
const Homey = require('homey');
const api = require('./lib/Api.js');
const events = require('events');
class App extends Homey.App {
log() {
console.log.bind(this, '[log]').apply(this, arguments);
}
error() {
console.error.bind(this, '[error]').apply(this, arguments);
}
onInit() {
console.log(`${Homey.manifest.id} running...`);
this.lastLocationModes = [];
this._api = new api();
this._api.on('refresh_device', this._syncDevice.bind(this));
this._api.on('refresh_devices', this._syncDevices.bind(this));
this._api.on('refresh_locationMode', this._syncLocationMode.bind(this));
this._api.init();
this._setLocationMode = new Homey.FlowCardAction('change_location_mode');
this.setLocationMode();
this._triggerLocationModeChangedTo = new Homey.FlowCardTrigger('ring_location_mode_changed_generic');
this.registerLocationModeChanged();
this._conditionLocationMode = new Homey.FlowCardCondition('ring_location_mode_active');
this.conditionLocationMode();
}
_syncDevice(data) {
Homey.emit('refresh_device', data);
}
_syncDevices(data) {
Homey.emit('refresh_devices', data);
}
getRingDevices(callback) {
this._api.getDevices(callback);
}
lightOn(data, callback) {
this._api.lightOn(data, callback);
}
lightOff(data, callback) {
this._api.lightOff(data, callback);
}
sirenOn(data, callback) {
this._api.sirenOn(data, callback);
}
sirenOff(data, callback) {
this._api.sirenOff(data, callback);
}
ringChime(data, callback) {
this._api.ringChime(data, callback);
}
grabImage(data, callback) {
this._api.grabImage(data, callback);
}
enableMotion(data, callback) {
this._api.enableMotion(data, callback);
}
disableMotion(data, callback) {
this._api.disableMotion(data, callback);
}
logRealtime(event, details)
{
Homey.ManagerApi.realtime(event, details)
console.log('Realtime event emitted for ', event, details);
}
writeToTimeline(message) {
var notification = new Homey.Notification({ excerpt: message });
notification.register().catch(() => {});
}
_syncLocationMode(newLocationMode)
{
if(this.lastLocationModes.length>0)
{
var matchedLastLocationMode = this.lastLocationModes.find(lastLocationMode =>{
return lastLocationMode.id==newLocationMode.id;
});
if(matchedLastLocationMode!=undefined)
{
//console.log('Check location mode for remembered location '+matchedLastLocationMode.name+' was in mode '+matchedLastLocationMode.mode+' and now is in mode '+newLocationMode.mode);
if(matchedLastLocationMode.mode!=newLocationMode.mode)
{
//console.log('location mode changed, raise the flow trigger!');
this.triggerLocationModeChanged({oldmode: matchedLastLocationMode.mode, mode: newLocationMode.mode},{location: newLocationMode});
}
matchedLastLocationMode.mode = newLocationMode.mode;
}
else {
//console.log('recevied new location mode for location '+newLocationMode.name+', there is no old state known for this location');
this.lastLocationModes.push(newLocationMode);
}
} else{
//console.log('recevied new location mode for location '+newLocationMode.name+', there is no old state known for this location');
this.lastLocationModes.push(newLocationMode);
}
}
triggerLocationModeChanged(tokens, state) {
this._triggerLocationModeChangedTo.trigger(tokens, state);
}
registerLocationModeChanged() {
this._triggerLocationModeChangedTo
.registerRunListener((args, state) => {
return Promise.resolve( args.location.name === state.location.name );
})
.register()
.getArgument('location')
.registerAutocompleteListener((query, args) => {
return new Promise(async (resolve) => {
const locations = await this._api.userLocations();
console.log(locations);
resolve(locations);
});
});
}
setLocationMode() {
this._setLocationMode
.registerRunListener(async (args, state) => {
console.log('attempt to switch location ('+args.location.name+') to new state: '+args.mode);
return new Promise((resolve, reject) => {
this._api.setLocationMode(args.location.id,args.mode).then(() => {
resolve(true);
}, (_error) => {
resolve(false);
});
});
})
.register()
.getArgument('location')
.registerAutocompleteListener((query, args) => {
return new Promise(async (resolve) => {
const locations = await this._api.userLocations();
console.log(locations);
resolve(locations);
});
});
}
conditionLocationMode() {
this._conditionLocationMode
.registerRunListener((args, state) => {
return new Promise((resolve, reject) => {
var matchedLocationMode = this.lastLocationModes.find(lastLocationMode =>{
return lastLocationMode.id==args.location.id;
});
if(matchedLocationMode!=undefined) {
console.log('stored location mode found for location '+matchedLocationMode.name);
resolve(matchedLocationMode.mode === args.mode);
} else {
reject('unknown location');
}
});
})
.register()
.getArgument('location')
.registerAutocompleteListener((query, args) => {
return new Promise(async (resolve) => {
const locations = await this._api.userLocations();
console.log(locations);
resolve(locations);
});
});
}
}
module.exports = App;