-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (86 loc) · 2.4 KB
/
index.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
import {Plugin} from 'lisa-plugin';
import {createRequire} from 'module';
import config from './config/index.js';
import drivers from './drivers/index.js';
import BridgesManager from './lib/BridgesManager.js';
const require = createRequire(import.meta.url);
const pkg = require('./package.json');
export default class HuePlugin extends Plugin {
/**
* Initialisation of your plugin
* Called once, when plugin is loaded
* @return Promise
*/
init() {
this.log.info('HuePlugin');
if (!this.bridgesManager) {
this.bridgesManager = new BridgesManager(this.lisa);
}
return super.init().then(() => this.bridgesManager.search())
.catch((err) => {
this.log.error(err.toString());
});
}
/**
* Called when
* @param action to execute
* @param infos context of the action
* @return Promise
*/
interact(action, infos) {
let room = infos.fields.room || infos.context.room;
const device = infos.fields.device;
if (device && device.pluginName !== this.fullName) {
return Promise.resolve();
}
const options = {};
switch (action) {
case 'LIGHT_ALL_TURN_ON':
options['powered'] = true;
room = null;
break;
case 'LIGHT_ALL_TURN_OFF':
options['powered'] = false;
room = null;
break;
case 'LIGHT_TURN_ON':
case 'DEVICE_TURN_ON':
options['powered'] = true;
if (infos.fields.number) {
options['intensity'] = infos.fields.number;
}
if (infos.fields.color) {
options['color'] = infos.fields.color.value;
}
break;
case 'LIGHT_TURN_OFF':
case 'DEVICE_TURN_OFF':
options['powered'] = false;
break;
case 'LIGHT_BRIGHTNESS':
options['intensity'] = infos.fields.number;
break;
default:
return Promise.resolve();
}
const criteria = {};
if (room) {
criteria.roomId = room.id;
return this.lisa.findDevices(criteria).then((devices) => {
return this.drivers['light'].setDevicesValues(devices, options);
});
} else if (device) {
return this.drivers['light'].setDevicesValues([device], options);
} else {
return this.drivers['light'].setDevicesValues(null, options);
}
}
constructor(app) {
super(app, {
drivers: drivers,
config: config,
pkg: pkg,
});
this.bridgesManager = null;
}
}