-
Notifications
You must be signed in to change notification settings - Fork 6
/
sensors.js
143 lines (123 loc) · 5.09 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
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
"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: 15000,
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);
} else if(sensor.type == "ZHATemperature" || sensor.type == "ZLLTemperature") {
this.temp(previous, sensor);
} else if(sensor.type == "ZHAHumidity") {
this.humidity(previous, sensor);
}
}
setTimeout(this.callback.bind(this), this.timeout);
})
.catch(error => {
console.log(`An error occurred: ${error.message}`);
});
};
sensors.humidity = function(previous, current) {
var currentAttr = current.state.attributes.attributes;
var prevAttr;
if(previous) {
var prevAttr = previous.state.attributes.attributes;
}
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 && (prevAttr["lastupdated"] != currentAttr["lastupdated"])) {
var humidity = current.state.attributes.attributes["humidity"] / 100;
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/humidity`, `${humidity}`, { retain: true });
}
if(!previous) {
var humidity = current.state.attributes.attributes["humidity"] / 100;
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/humidity`, `${humidity}`, { retain: true });
}
}
sensors.temp = function(previous, current) {
var currentAttr = current.state.attributes.attributes;
var prevAttr;
if(previous) {
var prevAttr = previous.state.attributes.attributes;
}
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 && (prevAttr["lastupdated"] != currentAttr["lastupdated"])) {
var temp = current.state.attributes.attributes["temperature"] / 100;
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/temperature`, `${temp}`, { retain: true });
}
if(!previous) {
var temp = current.state.attributes.attributes["temperature"] / 100;
this.mqtt.publish(`sensors/hue/${current.uniqueId}/get/temperature`, `${temp}`, { retain: true });
}
}
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;
}
}
}