-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
175 lines (143 loc) · 6.14 KB
/
index.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
const NefitEasyClient = require('nefit-easy-commands');
var Service, Characteristic;
var deviceClient;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-nefit-easy', 'NefitEasy', NefitEasyAccessory);
homebridge.registerAccessory('homebridge-nefit-easy', 'NefitEasyOutdoorTemp', NefitEasyAccessoryOutdoorTemp);
};
const nefitEasyServices = function() {
const informationService = new Service.AccessoryInformation()
.setCharacteristic(Characteristic.Manufacturer, 'Nefit')
.setCharacteristic(Characteristic.Model, 'Easy')
.setCharacteristic(Characteristic.SerialNumber, this.serialNumber);
return [informationService, this.service];
};
function NefitEasyAccessory(log, config) {
this.log = log;
this.name = config.name;
// Make sure that the credentials are there.
var creds = config.options || config.authentication;
if (! creds || typeof creds.serialNumber !== 'string' ||
typeof creds.accessKey !== 'string' || typeof creds.password !== 'string') {
throw Error('[homebridge-nefit-easy] Invalid/missing credentials in configuration file.');
}
this.serialNumber = creds.serialNumber;
this.service = new Service.Thermostat(this.name);
if (typeof deviceClient === 'undefined') {
deviceClient = NefitEasyClient(creds);
}
// Establish connection with device.
deviceClient.connect().catch((e) => {
throw Error(e);
});
this.service
.getCharacteristic(Characteristic.TemperatureDisplayUnits)
.on('get', (callback) => callback(null, Characteristic.TemperatureDisplayUnits.CELSIUS))
.setProps({validValues: [Characteristic.TemperatureDisplayUnits.CELSIUS]});;
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this, 'current', 'in house temp', true));
this.service
.getCharacteristic(Characteristic.TargetTemperature)
.on('get', this.getTemperature.bind(this, 'target', 'temp setpoint', true))
.on('set', this.setTemperature.bind(this))
.setProps({minValue: 5, maxValue: 30, minStep: 0.5});
this.service
.getCharacteristic(Characteristic.CurrentHeatingCoolingState)
.on('get', this.getCurrentState.bind(this))
.setProps(
{validValues: [Characteristic.CurrentHeatingCoolingState.OFF,
Characteristic.CurrentHeatingCoolingState.HEAT]
});
this.service
.getCharacteristic(Characteristic.TargetHeatingCoolingState)
.on('get', (callback) => callback(null, Characteristic.TargetHeatingCoolingState.AUTO))
.setProps({validValues: [Characteristic.TargetHeatingCoolingState.AUTO]});
};
const nefitEasyGetTemp = function(type, prop, skipOutdoor, callback) {
this.log.debug('Getting %s temperature...', type);
deviceClient.status(skipOutdoor).then((status) => {
var temp = status[prop];
if (!isNaN(temp) && isFinite(temp)) {
this.log.debug('...%s temperature is %s', type, temp);
return callback(null, temp);
}
else {
this.log.debug('Request for temperature resulted in invalid value: %s', temp);
// Try one more time, this almost always results in a valid value.
deviceClient.status(skipOutdoor).then((newStatus) => {
var newTemp = newStatus[prop];
if (!isNaN(newTemp) && isFinite(newTemp)) {
this.log.debug("Retry request for temperature resulted in valid value: %s", newTemp);
return callback(null, newTemp);
}
else {
this.log.debug("Retry request for temperature resulted in invalid value again: %s", newTemp);
// Return last known value, needed to keep service responsive for Siri.
if (prop == 'in house temp' || prop == 'outdoor temp') {
return callback(null, this.service.getCharacteristic(Characteristic.CurrentTemperature).value);
}
else if (prop == 'temp setpoint') {
return callback(null, this.service.getCharacteristic(Characteristic.TargetTemperature).value);
}
}
});
}
}).catch((e) => {
console.error(e);
return callback(e);
});
};
NefitEasyAccessory.prototype.getTemperature = nefitEasyGetTemp;
NefitEasyAccessory.prototype.setTemperature = function(temp, callback) {
// Round off to nearest half/full.
temp = Math.round(temp * 2) / 2;
this.log.info('Setting temperature to %s', temp);
deviceClient.setTemperature(temp).then(() => {
return callback();
}).catch((e) => {
return callback(e);
});
};
NefitEasyAccessory.prototype.getCurrentState = function(callback) {
this.log.debug('Getting current state..');
deviceClient.status(true).then((status) => {
var state = status['boiler indicator'];
var isHeating = state === 'central heating';
this.log.debug('...current state is', state);
return callback(null,
isHeating ? Characteristic.CurrentHeatingCoolingState.HEAT :
Characteristic.CurrentHeatingCoolingState.OFF
);
}).catch((e) => {
console.error(e);
return callback(e);
});
};
NefitEasyAccessory.prototype.getServices = nefitEasyServices;
function NefitEasyAccessoryOutdoorTemp(log, config) {
this.log = log;
this.name = config.name;
// Make sure that the credentials are there.
var creds = config.options || config.authentication;
if (! creds || typeof creds.serialNumber !== 'string' ||
typeof creds.accessKey !== 'string' || typeof creds.password !== 'string') {
throw Error('[homebridge-nefit-easy] Invalid/missing credentials in configuration file.');
}
this.serialNumber = creds.serialNumber;
this.service = new Service.TemperatureSensor(this.name);
if (typeof deviceClient === 'undefined') {
deviceClient = NefitEasyClient(creds);
}
// Establish connection with device.
deviceClient.connect().catch((e) => {
throw Error(e);
});
this.service
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getTemperature.bind(this, 'outdoor', 'outdoor temp', false));
};
NefitEasyAccessoryOutdoorTemp.prototype.getTemperature = nefitEasyGetTemp;
NefitEasyAccessoryOutdoorTemp.prototype.getServices = nefitEasyServices;