forked from leftyfl1p/homebridge-poolcontroller
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bodyAccessory.js
executable file
·178 lines (128 loc) · 6.89 KB
/
bodyAccessory.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var Accessory, Service, Characteristic, UUIDGen;
var utils = require('./utils.js')
var PoolBodyAccessory = function (log, accessory, bodyData, homebridge, platform) {
Accessory = homebridge.platformAccessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
Homebridge = homebridge;
this.accessory = accessory;
this.log = log;
this.accessory.log = log;
this.bodyData = bodyData
this.platform = platform
var customtypes = require('./customTypes.js')
var CustomTypes = new customtypes(Homebridge)
var FakeGatoHistoryService = require('fakegato-history')(homebridge);
this.loggingService = new FakeGatoHistoryService("thermo", this.accessory, { size: 11520, disableTimer: true, storage: 'fs' });
/*
this.service = accessory.getService(Service.Switch);
if (this.service) {
this.service
.getCharacteristic(Characteristic.On)
.on('set', this.setCircuitState.bind(this))
.on('get', this.getCircuitState.bind(this));
}
this.service = accessory.getService(Service.TemperatureSensor);
if (this.service) {
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getCurrentTemp.bind(this));
}
*/
this.service = accessory.getService(Service.Thermostat);
if (this.service) {
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getThermoCurrTemp.bind(this));
this.service
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getThermoState.bind(this));
this.service
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('set', this.setThermoTargetState.bind(this))
.on('get', this.getThermoTargetState.bind(this));
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.on('set', this.setThermoTargetTemp.bind(this))
.on('get', this.getThermoTargetTemp.bind(this));
}
this.updateState(bodyData)
// not needed/used with latest HomeKit API's
// accessory.updateReachability(true);
}
/*
PoolBodyAccessory.prototype.setCircuitState = async function (newCircuitState, callback) {
if (this.bodyData.isOn !== newCircuitState) {
if (this.platform.LogLevel >= 3) this.log("Setting Body", this.accessory.displayName, "to", newCircuitState);
await this.platform.execute("toggleCircuit", { id: this.bodyData.circuit })
this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On).updateValue(newCircuitState);
this.loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), status: this.bodyData.isOn });
}
callback();
};
PoolBodyAccessory.prototype.getCircuitState = function (callback) {
callback(null, this.bodyData.isOn);
};
*/
PoolBodyAccessory.prototype.getCurrentTemp = function (callback) {
callback(null, utils.F2C(this.bodyData.temp));
};
// Thermostat
PoolBodyAccessory.prototype.getThermoCurrTemp = function (callback) {
callback(null, utils.F2C(this.bodyData.temp));
};
PoolBodyAccessory.prototype.getThermoTargetTemp = function (callback) {
callback(null, utils.F2C(this.bodyData.setPoint));
};
PoolBodyAccessory.prototype.setThermoTargetTemp = function (newSetPoint, callback) {
if (this.bodyData.setPoint !== utils.C2F(newSetPoint)) {
if (this.platform.LogLevel >= 3) this.log("Setting Body Setpoint", this.accessory.displayName, "to", Math.round(utils.C2F(newSetPoint)));
this.platform.execute("setHeatSetPoint", { id: this.bodyData.id, heatSetpoint: utils.C2F(newSetPoint) })
// this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.TargetTemperature).updateValue(newSetPoint);
}
callback();
};
PoolBodyAccessory.prototype.getThermoState = function (callback) {
callback(null, utils.HeatingState(this.bodyData.heatStatus, Characteristic));
};
PoolBodyAccessory.prototype.getThermoTargetState = function (callback) {
callback(null, utils.HeatingMode(this.bodyData.heatMode, Characteristic));
};
PoolBodyAccessory.prototype.setThermoTargetState = async function (newTargetState, callback) {
if (this.platform.LogLevel >= 3) this.log("Setting Body Target State", this.accessory.displayName, "to", utils.HK_mode(newTargetState, Characteristic));
await this.platform.execute("setHeatMode", { id: this.bodyData.id, mode: utils.HK_Mode(newTargetState, Characteristic) })
// this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.TargetTemperature).updateValue(newTargetState);
callback();
};
// For when state is changed elsewhere.
PoolBodyAccessory.prototype.updateState = function (newbodyData) {
var customtypes = require('./customTypes.js')
var CustomTypes = new customtypes(Homebridge)
this.bodyData = newbodyData;
if (this.platform.LogLevel >= 3)
this.log('Updating data for %s body: state: %s', newbodyData.name, newbodyData.isOn)
if (this.platform.LogLevel >= 4)
this.log('Additional data for %s body: Curr temp: %s, target temp: %s, heat state: %s, target heat state: %s', newbodyData.name, newbodyData.temp, newbodyData.setPoint, newbodyData.heatStatus.desc, newbodyData.heatMode.desc)
// this.accessory.getService(Service.Switch).getCharacteristic(Characteristic.On)
// .updateValue(this.bodyData.isOn);
// this.accessory.getService(Service.TemperatureSensor).getCharacteristic(Characteristic.CurrentTemperature)
// .updateValue(utils.F2C(this.bodyData.temp))
this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.CurrentTemperature)
.updateValue(utils.F2C(this.bodyData.temp))
this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.TargetTemperature)
.updateValue(utils.F2C(this.bodyData.setPoint))
this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.updateValue(utils.HeatingState(this.bodyData.heatStatus, Characteristic))
this.accessory.getService(Service.Thermostat).getCharacteristic(Characteristic.TargetHeatingCoolingState)
.updateValue(utils.HeatingMode(this.bodyData.heatMode, Characteristic))
this.loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), currentTemp: utils.F2C(this.bodyData.temp), setTemp: utils.F2C(this.bodyData.setPoint), valvePosition: this.bodyData.heatStatus.val });
var interval = 8 * 60 * 1000
clearTimeout(this.bodyTempTimer)
this.bodyTempTimer = setInterval(function (platform, loggingService, tempData, setPoint, heatState) {
if (platform.LogLevel >= 4) platform.log('Adding body temp log entry %s %s %s', tempData, setPoint, heatState * 100)
loggingService.addEntry({ time: Math.round(new Date().valueOf() / 1000), currentTemp: tempData, setTemp: setPoint, valvePosition: heatState * 100 });
}, interval, this.platform, this.loggingService, utils.F2C(this.bodyData.temp), utils.F2C(this.bodyData.setPoint), this.bodyData.heatStatus.val)
return
}
module.exports = PoolBodyAccessory;