-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyson.js
74 lines (61 loc) · 1.94 KB
/
dyson.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
let DysonPureLink = require('dyson-purelink')
class AssistantDyson {
constructor(configuration, plugins) {
this._plugins = plugins;
this._client = new DysonPureLink(configuration.username, configuration.password, configuration.country);
}
action(args) {
let [cmd, arg, device] = args.split(" ")
switch(cmd) {
case "power":
return this._doOnDevices(d => d.setFan(arg === "on"), device)
case "setauto":
let auto = arg === "on"
return this._doOnDevices(d => d.setAuto(auto), device)
case "setfan":
if (arg === "auto") {
return this._doOnDevices(d => {
d.turnOn()
d.setAuto(true)
}, device)
} else if (arg === "off") {
return this._doOnDevices(d => d.turnOff(), device)
} else {
let value = parseInt(arg) * 10
return this._doOnDevices(d => {
d.turnOn()
d.setAuto(false)
d.setFanSpeed(value)
}, device)
}
default:
console.log("[assistant-dyson] Commande inconnue: ", args)
return Promise.resolve()
}
}
_doOnDevices(fun, name) {
let devices = this._devices.filter(d => name == null
|| (this._getDeviceName(d).toUpperCase() === name.toUpperCase()))
let answer = Promise.resolve()
devices.forEach(d => {
answer = answer.then(r => fun(d))
})
return answer
}
_getDeviceName(device) {
return device._deviceInfo ? device._deviceInfo.Name : device.name
}
_initDevices() {
return this._client.getDevices().then(devices => {
devices.forEach(d => console.log("[assistant-dyson] Device: " + this._getDeviceName(d)));
this._devices = devices;
return this;
})
}
}
exports.init = function(configuration, plugins) {
return new AssistantDyson(configuration, plugins)._initDevices().then(p => {
console.log("[assistant-dyson] Plugin chargé");
return p
})
}