-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.js
54 lines (48 loc) · 1.34 KB
/
main2.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
var TargetMqtt = 'mqt://<target>';
var TargetPrefix = 'target/prefix';
var HassUpdaterOptions = {
'host' : '<localmqtt>'
,'connection' : 'homeassistant/connection'
,'eventstream' : 'homeassistant/eventstream'
};
const HassStateUpdater = require('./lib/HassStateUpdater.js');
const HassRemoteDevice = require('./lib/HassRemoteDevice.js');
var Devices={};
Hass = new HassStateUpdater(HassUpdaterOptions);
Hass.events.on('MqttConnected',function(){
console.info('Connected!');
});
Hass.events.on('UpdateState',function(id,name,value){
setTimeout(function(){
ProcessData(id,name,value);
});
});
Hass.events.on('UpdateStateStart',function(id){
ProcessStart(id);
});
Hass.events.on('UpdateStateEnd',function(id){
ProcessEnd(id);
});
Hass.events.on('ConnectionStatus',function(status){
DeviceDefaultStatus=status;
for(i in Devices){
Devices[i].updateConnection(status);
}
});
function DeviceIdExists(id){
if(typeof(Devices[id])=='undefined'){
Devices[id] = new HassRemoteDevice(id,TargetMqtt,TargetPrefix);
}
}
function ProcessStart(id){
DeviceIdExists(id);
Devices[id].updateStart();
}
function ProcessEnd(id){
DeviceIdExists(id);
Devices[id].updateEnd();
}
function ProcessData(id,name,value){
DeviceIdExists(id);
Devices[id].update(name,value);
}