-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
203 lines (174 loc) · 7.03 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
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
'use strict';
let Accessory, Service, Characteristic, UUIDGen;
const Homee = require('homee-api');
const nodeTypes = require('./lib/node_types');
let HomeeAccessory, WindowCoveringAccessory, HomeegramAccessory, SecuritySystemAccessory;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
UUIDGen = homebridge.hap.uuid;
Accessory = homebridge.platformAccessory;
HomeeAccessory = require('./accessories/HomeeAccessory.js')(Service, Characteristic);
WindowCoveringAccessory = require('./accessories/WindowCoveringAccessory.js')(Service, Characteristic);
HomeegramAccessory = require('./accessories/HomeegramAccessory.js')(Service, Characteristic);
SecuritySystemAccessory = require('./accessories/SecuritySystemAccessory')(Service, Characteristic);
homebridge.registerPlatform('homebridge-homee', 'homee', HomeePlatform, false);
};
class HomeePlatform {
/**
* create a new instance
* @param log
* @param config
* @param api
*/
constructor(log, config, api) {
this.log = log;
this.homee = new Homee(
config.host,
config.user,
config.pass,
{ device: config.deviceId || 'homebridge' },
);
this.nodes = [];
this.homeegrams = [];
this.foundAccessories = [];
this.attempts = 0;
this.connected = false;
this.groupName = config.groupName || 'homebridge';
this.alarmGroup = config.alarmGroup || null;
if (api) this.api = api;
this.homee.on('message', message => this.handleMessage(message));
this.homee.on('error', err => this.log.error(err));
this.homee.on('disconnect', () => {
this.log('disconnected from homee');
this.connected = false;
});
this.homee
.connect()
.then(() => {
this.log('connected to homee');
this.connected = true;
this.homee.send('GET:all');
})
.catch(err => this.log.error(err));
}
/**
* create accessories
* @param callback
*/
accessories(callback) {
if (this.attempts > 10) {
throw new Error("Can't get devices or homeegrams. Please check that homee is online and your config is ok");
}
this.attempts++;
if (!this.connected) {
this.log('Not connected to homee. Retrying...');
setTimeout(() => this.accessories(callback), 2000);
return;
}
if (!this.nodes.length && !this.homeegrams.length) {
this.log("Didn't receive any devices or homeegrams. Retrying...");
setTimeout(() => this.accessories(callback), 2000);
return;
}
for (let node of this.nodes) {
if (node.id < 0) continue;
let name = decodeURI(node.name);
let uuid = UUIDGen.generate('homee-' + node.id);
let newAccessory;
let nodeType = nodeTypes.getAccessoryTypeByNodeProfile(node.profile);
if (nodeType === 'WindowCovering') {
this.log.debug(name + ': ' + nodeType);
newAccessory = new WindowCoveringAccessory(name, uuid, nodeType, node, this);
} else if (nodeType === 'DoubleSwitch') {
this.log.debug(name + ': ' + nodeType);
this.foundAccessories.push(new HomeeAccessory(name + '-1', uuid, 'Switch', node, this, 1));
let uuid2 = UUIDGen.generate('homee-' + node.id + '2');
newAccessory = new HomeeAccessory(name + '-2', uuid2, 'Switch', node, this, 2);
} else if (nodeType) {
this.log.debug(name + ': ' + nodeType);
newAccessory = new HomeeAccessory(name, uuid, nodeType, node, this);
} else {
this.log.debug(name + ': unknown Accessory Type');
}
if (newAccessory) this.foundAccessories.push(newAccessory);
}
// Security System (only when alarm group is set)
if (this.alarmGroup) {
this.foundAccessories.push(
new SecuritySystemAccessory('Alarm', UUIDGen.generate('homee'), this.alarmGroup, this)
);
}
for (let homeegram of this.homeegrams) {
let name = decodeURI(homeegram.name);
let uuid = UUIDGen.generate('homee-hg-' + homeegram.id);
let newAccessory = '';
this.log.debug(name + ': Homeegram');
newAccessory = new HomeegramAccessory(name, uuid, homeegram, this);
this.foundAccessories.push(newAccessory);
}
callback(this.foundAccessories);
}
/**
* filter nodes if group 'homebridge' exists
* @param all
* @returns {*[]}
*/
filterDevices(all) {
let groupId;
let nodeIds = [];
let homeegramIds = [];
let filtered = { nodes: [], homeegrams: [] };
for (let group of all.groups) {
if (group.name.match(new RegExp('^' + this.groupName + '$', 'i'))) {
groupId = group.id;
}
}
if (!groupId) {
if (this.groupName !== 'homebridge') {
throw new Error(
'Specified group not found. Aborting Homebridge startup to prevent lost of accessories'
);
} else {
return [all.nodes, all.homeegrams];
}
}
for (let relationship of all.relationships) {
if (relationship.group_id === groupId) {
nodeIds.push(relationship.node_id);
homeegramIds.push(relationship.homeegram_id);
}
}
for (let node of all.nodes) {
if (nodeIds.indexOf(node.id) !== -1) {
filtered.nodes.push(node);
}
}
for (let homeegram of all.homeegrams) {
if (homeegramIds.indexOf(homeegram.id) !== -1) {
filtered.homeegrams.push(homeegram);
}
}
return [filtered.nodes, filtered.homeegrams];
}
/**
* handle incoming messages
* @param message
*/
handleMessage(message) {
if (message.all && !this.foundAccessories.length) {
[this.nodes, this.homeegrams] = this.filterDevices(message.all);
} else if (message.attribute || message.node) {
let attributes = message.node ? message.node.attributes : [message.attribute];
for (let attribute of attributes) {
let node = this.nodes.find(n => n.id === attribute.node_id);
let nodeType = node && nodeTypes.getAccessoryTypeByNodeProfile(node.profile);
let accessory = this.foundAccessories.find(a => a.nodeId === attribute.node_id && (nodeType !== 'DoubleSwitch' || a.instance === attribute.instance));
if (accessory) {
accessory.updateValue(attribute);
this.log.info('Updated accessory %s', accessory.name);
}
}
}
}
}