-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
108 lines (98 loc) · 3.55 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
'use strict';
const Homey = require('homey');
const constants = require('./lib/constants');
module.exports = class TelldusLiveApp extends Homey.App {
onInit() {
try {
this.homey.on('unload', () => this._onUninstall());
this.log('TelldusLiveApp is running...');
} catch (err) {
this.log('onInit error', err);
}
}
sensorValues(telldusSensors) {
const drivers = this.homey.drivers.getDrivers();
if (drivers) {
for (let key in drivers) {
if (drivers.hasOwnProperty(key)) {
const driver = drivers[key];
const devices = driver.getDevices();
for (let device of devices) {
if (device.getId && device.onSensorValue) {
const telldusSensor = telldusSensors.find(s => s.id === device.getId());
device.onSensorValue(telldusSensor);
}
}
}
}
}
}
deviceValues(telldusDevices) {
const drivers = this.homey.drivers.getDrivers();
if (drivers) {
for (let key in drivers) {
if (drivers.hasOwnProperty(key)) {
const driver = drivers[key];
const devices = driver.getDevices();
for (let device of devices) {
if (device.getId) {
const telldusDevice = telldusDevices.find(d => d.id === device.getId());
if (device.hasCapability('onoff') && device.onSwitchValue) {
device.onSwitchValue(telldusDevice);
}
if (device.hasCapability('dim') && device.onDimValue) {
device.onDimValue(telldusDevice);
}
}
}
}
}
}
}
getApi(clientId) {
const apis = [];
for (let driverId of constants.DRIVER_CLIENTS) {
const driver = this.homey.drivers.getDriver(driverId);
if (driver) {
const devices = driver.getDevices();
for (let device of devices) {
if (device.getId && device.getApi) {
if (clientId && clientId === device.getId()) {
return device.getApi();
}
apis.push({
shortName: device.getShortName(),
api: device.getApi(),
clientId: device.getId(),
driverId: driverId
});
}
}
}
}
return apis;
}
_onUninstall() {
try {
this._clearTimers();
} catch (err) {
this.log('_onUninstall error', err);
}
}
_clearTimers() {
const drivers = this.homey.drivers.getDrivers();
if (drivers) {
for (let key in drivers) {
if (drivers.hasOwnProperty(key)) {
const driver = drivers[key];
const devices = driver.getDevices();
for (let device of devices) {
if (device.clearCheckData) {
device.clearCheckData();
}
}
}
}
}
}
};