-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
platform.js
330 lines (274 loc) · 8.31 KB
/
platform.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
const os = require('os')
const shellies = require('shellies')
const AdminServer = require('./admin')
module.exports = homebridge => {
const AccessoryFactory = require('./accessories/factory')(homebridge)
const DeviceWrapper = require('./device-wrapper')(homebridge)
class ShellyPlatform {
constructor(log, config) {
this.log = log
this.config = config || {}
this.deviceConfigs = new Map()
this.deviceWrappers = new Map()
this.stalePlatformAccessories = new Set()
this.adminServer = null
this.configure()
shellies
.on('discover', this.discoverDeviceHandler, this)
.on('stale', this.deviceStaleHandler, this)
homebridge.on('didFinishLaunching', () => {
const num = Array.from(this.deviceWrappers.values()).reduce(
(n, dw) => n + dw.platformAccessories.length,
0
)
this.log.info(
`${num} ${num === 1 ? 'accessory' : 'accessories'} loaded from cache`
)
this.handleStalePlatformAccessories()
shellies.start(this.getNetworkInterface())
if (!this.config.admin || this.config.admin.enabled !== false) {
// start the admin server
this.adminServer = new AdminServer(this, this.config.admin || {}, log)
this.adminServer.listen()
.then(port => {
this.log.info(`Admin server is running on port ${port}`)
})
.catch(e => {
this.log.error('Failed to launch the admin server:', e.message)
})
}
})
}
configure() {
const config = this.config
if (config.username && config.password) {
shellies.setAuthCredentials(config.username, config.password)
}
if (typeof config.requestTimeout === 'number') {
shellies.request.timeout(config.requestTimeout)
}
if (typeof config.staleTimeout === 'number') {
shellies.staleTimeout = config.staleTimeout
}
if (Array.isArray(config.devices)) {
for (const c of config.devices) {
let id = c.id
if (!id) {
continue
}
id = id.toUpperCase().trim()
this.deviceConfigs.set(id, c)
}
}
}
/**
* Returns the configured network interface do listen for CoAP messages on,
* if one has been configured.
*/
getNetworkInterface() {
const iface = this.config.networkInterface
if (!iface) {
return null
}
const ifaces = os.networkInterfaces()
// if an interface name has been specified, return its address
if (ifaces[iface] && ifaces[iface].length > 0) {
// return the first address
return ifaces[iface][0].address
}
// otherwise, go through each interface and see if there is one with the
// specified address
for (const i in ifaces) {
for (const ii of ifaces[i]) {
if (ii.address === iface) {
// address found, so it's valid
return ii.address
}
}
}
// the configured value doesn't match any interface name or address, so
// ignore it
this.log.warn(
`Ignoring unknown network interface name or address ${iface}`
)
return null
}
getDeviceConfig(deviceId) {
return this.deviceConfigs.get(deviceId) || {}
}
discoverDeviceHandler(device, unknown) {
if (this.getDeviceConfig(device.id).exclude) {
this.log.info(
'Excluding device',
device.type,
device.id,
'at',
device.host,
)
return
}
if (!unknown) {
this.log.info(
'New device discovered:',
device.type,
device.id,
'at',
device.host
)
try {
if (!this.addDevice(device)) {
this.log.info('Unknown device, so skipping it')
}
} catch (e) {
this.log.error('Failed to add device')
if (e.stack) {
this.log.error(e.stack)
}
}
} else {
this.log.info(
'Unknown device',
device.type,
device.id,
'at',
device.host,
'discovered'
)
}
}
deviceStaleHandler(device) {
this.log.info(
'Device',
device.type,
device.id,
'is stale. Unregistering its accessories.'
)
this.removeDevice(device)
}
addDevice(device) {
const deviceConfig = this.getDeviceConfig(device.id)
const accessories = AccessoryFactory.createAccessories(
device,
deviceConfig,
this.log
)
if (accessories && accessories.length > 0) {
const deviceWrapper = new DeviceWrapper(
this,
device,
deviceConfig,
...accessories
)
this.deviceWrappers.set(device, deviceWrapper)
homebridge.registerPlatformAccessories(
'homebridge-shelly',
'Shelly',
deviceWrapper.platformAccessories
)
return deviceWrapper
}
return null
}
removeDevice(device) {
const deviceWrapper = this.deviceWrappers.get(device)
if (!deviceWrapper) {
return
}
homebridge.unregisterPlatformAccessories(
'homebridge-shelly',
'Shelly',
deviceWrapper.platformAccessories
)
deviceWrapper.destroy()
this.deviceWrappers.delete(device)
}
configureAccessory(platformAccessory) {
const ctx = platformAccessory.context
const deviceConfig = this.getDeviceConfig(ctx.id)
if (deviceConfig.exclude ||
this.accessoryTypeHasChanged(ctx, deviceConfig)) {
this.stalePlatformAccessories.add(platformAccessory)
return
}
this.log.debug(
'Configuring cached accessory',
'#' + (ctx.index || 0),
'for device',
ctx.type,
ctx.id
)
this.createAccessoryFromContext(ctx, platformAccessory)
}
accessoryTypeHasChanged(ctx, deviceConfig) {
const accessoryConfig = AccessoryFactory.getAccessoryConfig(
deviceConfig,
ctx.index || 0
)
const defaultAccessoryType = AccessoryFactory.getDefaultAccessoryType(
ctx.type,
ctx.mode
)
const oldAccessoryType = ctx.accessoryType || defaultAccessoryType
const newAccessoryType = accessoryConfig.type || defaultAccessoryType
return oldAccessoryType !== newAccessoryType
}
createAccessoryFromContext(ctx, platformAccessory = null) {
const deviceConfig = this.getDeviceConfig(ctx.id)
let device = shellies.getDevice(ctx.type, ctx.id)
if (!device) {
device = shellies.createDevice(ctx.type, ctx.id, ctx.host, ctx.mode)
shellies.addDevice(device)
}
let deviceWrapper = this.deviceWrappers.get(device)
if (!deviceWrapper) {
deviceWrapper = new DeviceWrapper(this, device, deviceConfig)
this.deviceWrappers.set(device, deviceWrapper)
}
const accessory = AccessoryFactory.createAccessory(
device,
ctx.index,
deviceConfig,
this.log,
platformAccessory
)
if (accessory) {
deviceWrapper.accessories.push(accessory)
}
return accessory
}
handleStalePlatformAccessories() {
if (this.stalePlatformAccessories.size > 0) {
homebridge.unregisterPlatformAccessories(
'homebridge-shelly',
'Shelly',
Array.from(this.stalePlatformAccessories)
)
}
for (const platformAccessory of this.stalePlatformAccessories) {
const ctx = platformAccessory.context
const deviceConfig = this.getDeviceConfig(ctx.id)
if (deviceConfig.exclude) {
continue
}
this.log.debug(
'Recreating accessory',
'#' + (ctx.index || 0),
'for device',
ctx.type,
ctx.id,
'since its configured type has changed'
)
const accessory = this.createAccessoryFromContext(ctx)
if (accessory) {
homebridge.registerPlatformAccessories(
'homebridge-shelly',
'Shelly',
[accessory.platformAccessory]
)
}
}
delete this.stalePlatformAccessories
}
}
return ShellyPlatform
}