-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomekit_to_mqtt.js
299 lines (252 loc) · 8.52 KB
/
homekit_to_mqtt.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
const debugModule = require('debug');
debugModule.enable('*');
const debug = debugModule('homekit_to_mqtt');
const _ = require('lodash');
const mqtt = require('mqtt');
const config = require('config');
// Apparently this is a dep of hap-nodejs? Not sure what it needs to store.
debug('Initing node-persist...');
var storage = require('node-persist');
storage.initSync();
debug('Done initing node-persist');
const {Service, Characteristic, Accessory, Bridge, uuid} = require('hap-nodejs');
const mqttConfig = config.get('mqtt');
const homekitConfig = config.get('homekit');
const deviceConfig = config.get('devices');
let bridge;
let mqttClient;
let mqttTopics = {};
function defaultStateForDeviceType(deviceType) {
switch (deviceType) {
case 'switch':
return {on: false};
case 'colored_light':
return {on: false, hue: 0, saturation: 1.0, brightness: 50};
case 'temp_meter':
return {temp: 0.0, humidity: 0.0};
default:
throw new Error('invalid device type');
}
}
function isDeviceTypeSupported(deviceType) {
switch (deviceType) {
case 'switch':
return true;
case 'temp_meter':
return true;
default:
return false;
}
}
const state = _
.chain(deviceConfig)
.filter(device => {
const supported = isDeviceTypeSupported(device.type);
if (!supported) {
debug('Ignoring device %s of unsupported type %s', device.displayName, device.type);
}
return supported;
})
.keyBy('id')
.mapValues(val => ({
...val,
state: defaultStateForDeviceType(val.type),
service: null,
secondaryService: null,
accessory: null,
ignoreSets: false,
}))
.value();
function onMqttMessage(topic, message) {
if (topic in mqttTopics) {
const fn = mqttTopics[topic];
fn(message);
} else {
debug('received message on unknown mqtt topic', topic);
}
}
async function mqttSubscribe(topic, fn) {
debug("Subscribing to MQTT topic %s", topic);
return new Promise((resolve, reject) => {
mqttTopics[topic] = fn;
mqttClient.subscribe(topic, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function refreshDevice(device) {
if (device.topics.refresh) {
debug("Asking for refresh of device %s.", device.displayName);
const refreshTopic = device.topics.refresh;
mqttClient.publish(refreshTopic, '{}');
} else {
debug("Device %s doesn't have a refresh topic.", device.displayName);
}
}
async function getHomekitValue(device, key) {
const value = device.state[key];
debug('HomeKit asked for property %s of device %s. Returning %s then refreshing.', key, device.displayName, value);
refreshDevice(device);
return value;
}
async function setValueFromHomekit(device, key, val) {
if (device.ignoreSets) {
return;
}
debug('HomeKit set device %s\'s property: %s=%s', device.displayName, key, val);
device.state[key] = val;
switch (key) {
case 'on':
const onTopic = device.topics.set_on;
mqttClient.publish(onTopic, JSON.stringify(val));
break;
}
}
async function setValueFromMQTT(device, key, val) {
debug('Received updated property value over MQTT for device %s: %s=%s', device.displayName, key, val);
device.state[key] = val;
switch (key) {
case 'on':
const onChar = device.service.getCharacteristic(Characteristic.On);
// The ignoreSets flag is to avoid an infinite loop where setting in homekit causes a homekit
// set event, that triggers an MQTT set call, that then triggers a state event that triggers
// setting in HomeKit.
device.ignoreSets = true;
onChar.setValue(val, () => device.ignoreSets = false);
break;
case 'temp':
const tempChar = device.service.getCharacteristic(Characteristic.CurrentTemperature);
tempChar.setValue(val);
break;
case 'humidity':
const humService = device.secondaryService || device.service;
const humChar = humService.getCharacteristic(Characteristic.CurrentRelativeHumidity);
humChar.setValue(val);
break;
}
}
function setupMQTTLogging() {
const events = ['connect', 'reconnect', 'close', 'disconnect', 'offline', 'error', 'end'];
events.forEach(eventName => {
mqttClient.on(eventName, (value) => {
debug(`MQTT ${eventName} event: `, value);
});
});
}
async function connectToMQTT() {
debug("Connecting to MQTT...");
mqttClient = mqtt.connect(mqttConfig.brokerAddress, {clientId: mqttConfig.clientId, keepalive: 5});
setupMQTTLogging();
mqttClient.on('message', onMqttMessage);
await new Promise((resolve, reject) => {
mqttClient.on('error', reject);
mqttClient.on('connect', () => resolve());
});
debug('Connected to MQTT.');
}
function getUuid(type, key) {
const namespace = homekitConfig.uuidNamespace;
const suffix = type + (key ? ':' + key : '');
const prefix = 'homekit_to_mqtt:' + (namespace ? namespace + ':' : '');
return uuid.generate(prefix + suffix);
}
async function main() {
await connectToMQTT();
debug("Setting up Homekit devices");
const bridgeUuid = getUuid('bridge');
bridge = new Bridge(homekitConfig.bridgeDisplayName, bridgeUuid);
const mqttSubscribePromises = _.map(state, async device => {
const deviceUuid = getUuid('device', device.id);
const accessory = new Accessory(device.displayName, deviceUuid);
device.accessory = accessory;
switch (device.type) {
case 'switch':
// Setup HomeKit listeners
const switchService = accessory.addService(Service.Switch, device.displayName);
device.service = switchService;
const on = switchService.getCharacteristic(Characteristic.On);
on.on('get', async cb => {
const val = await getHomekitValue(device, 'on');
// TODO: Should this wait for a device response and error if it times out?
cb(null /* error */, val);
});
on.on('set', async (val, cb) => {
await setValueFromHomekit(device, 'on', val);
cb();
});
// Setup MQTT listeners
await mqttSubscribe(device.topics.on, async message => {
let val;
try {
val = JSON.parse(message);
} catch (e) {
debug("Received invalid on value for device %s from MQTT, ignoring", device.displayName);
return;
}
await setValueFromMQTT(device, 'on', val);
});
break;
case 'temp_meter':
// Setup HomeKit listeners
const tempService = accessory.addService(Service.TemperatureSensor, device.displayName);
device.service = tempService;
const temp = tempService.getCharacteristic(Characteristic.CurrentTemperature);
temp.on('get', async cb => {
const val = await getHomekitValue(device, 'temp');
cb(null /* error */, val);
});
const humidityService = accessory.addService(Service.HumiditySensor, device.displayName);
device.secondaryService = humidityService;
const humidity = humidityService.getCharacteristic(Characteristic.CurrentRelativeHumidity);
humidity.on('get', async cb => {
const val = await getHomekitValue(device, 'humidity');
cb(null /* error */, val);
});
// Setup MQTT listeners
await Promise.all(['temp', 'humidity'].map(name =>
mqttSubscribe(device.topics[name], async message => {
let val;
try {
val = JSON.parse(message);
} catch (e) {
debug("Received invalid %s value for device %s from MQTT, ignoring", name, device.displayName);
return;
}
await setValueFromMQTT(device, name, val);
})
));
break;
default:
throw new Error('unsupported device type');
}
bridge.addBridgedAccessory(accessory);
// Do an initial refresh request so we have correct initial state.
refreshDevice(device);
debug('Registered HomeKit device %s (type=%s)', device.displayName, device.type);
});
await Promise.all(mqttSubscribePromises);
debug('Publishing HomeKit bridge. PIN is %s', homekitConfig.pincode);
bridge.publish({
..._.pick(homekitConfig, [
'username',
'port',
'pincode',
]),
category: Accessory.Categories.BRIDGE
});
debug('Setting up unpublish hook');
var signals = { 'SIGINT': 2, 'SIGTERM': 15 };
Object.keys(signals).forEach(function (signal) {
process.on(signal, function () {
bridge.unpublish();
setTimeout(function (){
process.exit(128 + signals[signal]);
}, 1000);
});
});
}
main();