forked from Darksecond/mqtt-hue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.js
90 lines (80 loc) · 2.89 KB
/
sensors.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
"use strict";
// TODO: capabilities
// sensors/hue/{unique_id}/get/battery
// sensors/hue/{unique_id}/get/name
// sensors/hue/{unique_id}/get/buttons/on
// sensors/hue/{unique_id}/get/buttons/off
// sensors/hue/{unique_id}/get/buttons/up
// sensors/hue/{unique_id}/get/buttons/down
let sensors = {
timeout: 50,
sensors: {}
};
module.exports = sensors;
sensors.init = function(options) {
this.hue = options.hue;
this.mqtt = options.mqtt;
this.callback();
};
sensors.callback = function() {
this.hue.sensors.getAll()
.then(sensors => {
for (let sensor of sensors) {
let previous = this.sensors[sensor.uniqueId];
this.sensors[sensor.uniqueId] = sensor;
if(sensor.type == "ZLLSwitch") {
this.zll(previous, sensor);
}
}
setTimeout(this.callback.bind(this), this.timeout);
})
.catch(error => {
console.log(`An error occurred: ${error.message}`);
});
};
sensors.zll = function(previous, current) {
if(!previous || previous.name != current.name)
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/name`, `${current.name}`, { retain: true });
if(!previous || previous.config["battery"] != current.config["battery"])
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/battery`, `${current.config["battery"]}`, { retain: true });
if(previous && (previous.state["lastupdated"] != current.state["lastupdated"] || previous.state["buttonevent"] != current.state["buttonevent"])) {
switch(current.state["buttonevent"]) {
case 1001:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/on`, "hold");
break;
case 2001:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/up`, "hold");
break;
case 3001:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/down`, "hold");
break;
case 4001:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/off`, "hold");
break;
case 1002:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/on`, "short");
break;
case 2002:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/up`, "short");
break;
case 3002:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/down`, "short");
break;
case 4002:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/off`, "short");
break;
case 1003:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/on`, "long");
break;
case 2003:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/up`, "long");
break;
case 3003:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/down`, "long");
break;
case 4003:
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/buttons/off`, "long");
break;
}
}
}