-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcircuitAccessory.js
executable file
·67 lines (52 loc) · 2.47 KB
/
circuitAccessory.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
var Accessory, Service, Characteristic, UUIDGen;
var debug = false;
var PoolCircuitAccessory = function(log, accessory, circuit, circuitState, homebridge, socket) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
Homebridge = homebridge;
this.accessory = accessory;
this.log = log;
this.circuit = circuit;
this.circuitState = circuitState;
this.service = accessory.getService(Service.Switch);
this.socket = socket;
if (this.service) {
this.service
.getCharacteristic(Characteristic.On)
.on('set', this.setCircuitState.bind(this))
.on('get', this.getCircuitState.bind(this));
}
// not needed/used with latest HomeKit API's
// accessory.updateReachability(true);
}
PoolCircuitAccessory.prototype.setCircuitState = function(circuitState, callback) {
if (this.circuitState !== circuitState) {
this.log("Setting Circuit", this.accessory.displayName, "to", circuitState);
this.socket.emit("toggleCircuit", this.circuit);
//this.updateCircuitState(circuitState);
//this following line will update the value without the internal callback to getCircuitState
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).updateValue(circuitState);
}
callback();
};
PoolCircuitAccessory.prototype.getCircuitState = function(callback) {
callback(null, this.circuitState);
};
// For when state is changed elsewhere.
PoolCircuitAccessory.prototype.updateCircuitState = function(circuitState) {
if (this.circuitState !== circuitState) {
this.log("Update Circuit State for %s (state: %s-->%s)", this.accessory.displayName, this.circuitState, circuitState)
this.circuitState = circuitState;
// since this is being called internally (via the socket initiation), call the function that will call the callback
//this.accessory.getService(Service.Switch).setCharacteristic(Characteristic.On, circuitState) // DO NOT USE - creates an infinite loop
// this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).setValue(circuitState) // works
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).updateValue(circuitState); // works
//this.service.getCharacteristic(Characteristic.On).setValue(this.circuitState); // works
} else {
//console.log("No change in state for %s", this.accessory.displayName)
}
return
};
module.exports = PoolCircuitAccessory;