homebridge-lib 5.1.24-6 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,6 @@
7
7
 
8
8
  const homebridgeLib = require('../index')
9
9
 
10
- const maxLogLevel = 4
11
10
  const startsWithUuid = /^[0-9A-F]{8}-[0-9A-F]{4}-[1-5][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/
12
11
 
13
12
  /** Delegate of a HomeKit accessory.
@@ -35,9 +34,6 @@ class AccessoryDelegate extends homebridgeLib.Delegate {
35
34
  * @param {!string} params.firmware - The accessory firmware revision.
36
35
  * @param {?string} params.hardware - The accessory hardware revision.
37
36
  * @param {?string} params.software - The accessory software revision.
38
- * @param {integer} params.logLevel - The log level for the accessory.
39
- * @param {?boolean} [params.inheritLogLevel] - Inherit `logLevel` from
40
- * `platform` instead of maintaining it oneself.
41
37
  */
42
38
  constructor (platform, params = {}) {
43
39
  if (params.name == null) {
@@ -53,18 +49,16 @@ class AccessoryDelegate extends homebridgeLib.Delegate {
53
49
 
54
50
  // Link or create associated PlatformAccessory.
55
51
  this._accessory = this._platform._getAccessory(this, params)
56
- this._context = this._accessory.context
57
52
 
58
53
  if (params.logLevel != null) {
59
- this._context.logLevel = Math.max(0, Math.min(params.logLevel, maxLogLevel))
60
- } else if (params.inheritLogLevel != null) {
61
- if (params.inheritLogLevel) {
62
- Object.defineProperty(this, 'logLevel', {
63
- get () { return platform.logLevel }
64
- })
65
- }
66
- delete params.inheritLogLevel
54
+ this.warn('params.logLevel: deprecated')
67
55
  }
56
+ if (params.inheritLogLevel != null) {
57
+ this.warn('params.inheritLogLevel: deprecated')
58
+ }
59
+ this._context = this._accessory.context
60
+
61
+ delete this._context.logLevel
68
62
 
69
63
  // Setup shortcut for property values and values of the characteristics
70
64
  // of the _Accessory Information_ service.
@@ -270,16 +264,7 @@ class AccessoryDelegate extends homebridgeLib.Delegate {
270
264
  * @type {!integer}
271
265
  */
272
266
  get logLevel () {
273
- return this._context.logLevel
274
- }
275
-
276
- set logLevel (value) {
277
- const oldLogLevel = this._context.logLevel
278
- this._context.logLevel = Math.max(0, Math.min(value, maxLogLevel))
279
- this._platform._message(
280
- 'log', 2, this.name + ': ',
281
- 'set Log Level from %d to %d', oldLogLevel, this.logLevel
282
- )
267
+ return this.platform.logLevel
283
268
  }
284
269
 
285
270
  /** Inherit `logLevel` from another accessory delegate.
@@ -298,6 +283,19 @@ class AccessoryDelegate extends homebridgeLib.Delegate {
298
283
  })
299
284
  }
300
285
 
286
+ /** Manage `logLevel` from characteristic delegate.
287
+ * @param {CharacteristicDelegate} delegate - The delegate of the `logLevel`
288
+ * characteristic.
289
+ */
290
+ manageLogLevel (delegate) {
291
+ if (!(delegate instanceof homebridgeLib.CharacteristicDelegate)) {
292
+ throw new TypeError('delegate: not an CharacteristicDelegate')
293
+ }
294
+ Object.defineProperty(this, 'logLevel', {
295
+ get () { return delegate.value }
296
+ })
297
+ }
298
+
301
299
  // Called by homebridge when Identify is selected.
302
300
  _identify () {
303
301
  this.emit('identify')
@@ -188,10 +188,11 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
188
188
  }, 'Current Consumption')
189
189
 
190
190
  this.createCharacteristicClass('AirPressure', uuid('10F'), {
191
- format: this.Formats.UINT16,
191
+ format: this.Formats.FLOAT,
192
192
  unit: 'hPa',
193
193
  minValue: 700,
194
194
  maxValue: 1100,
195
+ minStep: 0.1,
195
196
  perms: [this.Perms.READ, this.Perms.NOTIFY]
196
197
  }, 'Air Pressure')
197
198
 
@@ -589,7 +589,7 @@ class MyHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
589
589
  this.createCharacteristicClass('LogLevel', uuid('065'), {
590
590
  format: this.Formats.UINT8,
591
591
  minValue: 0,
592
- maxValue: 3,
592
+ maxValue: 3, // 4 for homebridge-zp
593
593
  minStep: 1, // Force Down|Up control in Eve
594
594
  perms: [this.Perms.READ, this.Perms.NOTIFY, this.Perms.WRITE],
595
595
  adminOnlyAccess: [this.Access.WRITE]
@@ -598,6 +598,7 @@ class MyHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
598
598
  this.Characteristics.LogLevel.LOG = 1
599
599
  this.Characteristics.LogLevel.DEBUG = 2
600
600
  this.Characteristics.LogLevel.VDEBUG = 3
601
+ this.Characteristics.LogLevel.VVDEBUG = 4
601
602
 
602
603
  this.createCharacteristicClass('Repeat', uuid('066'), {
603
604
  format: this.Formats.UINT8,
package/lib/Platform.js CHANGED
@@ -27,8 +27,6 @@ const context = {
27
27
  checkInterval: 7 * 24 * 3600
28
28
  }
29
29
 
30
- const globalKey = context.libName // + '@' + context.libVersion
31
-
32
30
  /** Homebridge dynamic platform plugin.
33
31
  *
34
32
  * `Platform` provides the following features to a platform plugin:
@@ -59,6 +57,8 @@ class Platform extends homebridgeLib.Delegate {
59
57
  static loadPlatform (homebridge, packageJson, platformName, Platform) {
60
58
  if (context.homebridge == null) {
61
59
  context.homebridge = homebridge
60
+ context.packageJson = packageJson
61
+ context.platformName = platformName
62
62
  context.homebridgeVersion = homebridge.serverVersion
63
63
  context.PlatformAccessory = homebridge.platformAccessory
64
64
  const hap = {
@@ -98,25 +98,6 @@ class Platform extends homebridgeLib.Delegate {
98
98
  my: Object.freeze(my.Characteristics)
99
99
  })
100
100
  }
101
- if (global[globalKey] == null) {
102
- global[globalKey] = {
103
- Platform: {
104
- platformName: context.libName,
105
- packageJson: libPackageJson,
106
- UpnpClient: homebridgeLib.UpnpClient
107
- }
108
- }
109
- homebridge.registerPlatform(
110
- // libPackageJson.name, 'Lib', homebridgeLib.Platform, false
111
- packageJson.name, 'Lib', homebridgeLib.Platform, false
112
- )
113
- // } else {
114
- // TODO: check compatible homebridge-lib version.
115
- }
116
- global[globalKey][Platform.name] = {
117
- platformName: platformName,
118
- packageJson: packageJson
119
- }
120
101
  homebridge.registerPlatform(
121
102
  packageJson.name, platformName, Platform, true
122
103
  )
@@ -148,50 +129,19 @@ class Platform extends homebridgeLib.Delegate {
148
129
  this._log = log
149
130
  this._configJson = configJson
150
131
  this._homebridge = homebridge
151
- const myContext = global[globalKey][this.className]
152
- this._platformName = myContext.platformName
153
- this._pluginName = myContext.packageJson.name
154
- this._pluginVersion = myContext.packageJson.version
155
- if (myContext.packageJson.name === context.libName) {
156
- this._isHomebridgeLib = true
157
- myContext.heartbeat = this
158
- // Delay start of Homebridge's HAP server until all plugins have
159
- // initialised.
160
- this.accessories = (f) => {
161
- this.on('initialised', () => { f([]) })
162
- }
163
- // this.debug('Categories: %j', this.Accessory.Categories)
164
- // for (const type of ['Access', 'Formats', 'Perms', 'Units']) {
165
- // this.debug('%s: %j', type, this.Characteristic[type])
166
- // }
167
- // for (const module of ['hap', 'eve', 'my']) {
168
- // this.debug('Services.%s: %j', module, Object.keys(this.Services[module]))
169
- // this.debug('Characteristics.%s: %j', module, Object.keys(this.Characteristics[module]))
170
- // }
171
- } else {
172
- /** Configure an accessory, after it has been restored from peristent
173
- * storage.
174
- *
175
- * Called by homebridge when restoring peristed accessories, typically from
176
- * `~/.homebridge/accessories/cachedAccessories`.
177
- * @method
178
- * @param {!PlatformAccessory} accessory - The restored Homebridge
179
- * [PlatformAccessory](https://github.com/nfarina/homebridge/blob/master/lib/platformAccessory.js).
180
- */
181
- this.configureAccessory = this._configureAccessory
182
- }
132
+ this._platformName = context.platformName
133
+ this._pluginName = context.packageJson.name
134
+ this._pluginVersion = context.packageJson.version
135
+
183
136
  this._accessories = {}
184
137
  this._accessoryDelegates = {}
185
138
 
186
- if (myContext.platform != null) {
139
+ if (context.platform != null) {
187
140
  this.fatal(
188
- 'config.json: duplicate entry for %s platform', myContext.platformName
141
+ 'config.json: duplicate entry for %s platform', context.platformName
189
142
  )
190
143
  }
191
- myContext.platform = this
192
- if (global[globalKey].Platform == null) {
193
- this.fatal('%s platform not registered', context.libName)
194
- }
144
+ context.platform = this
195
145
  this._identify()
196
146
 
197
147
  this._homebridge
@@ -205,54 +155,30 @@ class Platform extends homebridgeLib.Delegate {
205
155
  // Main platform function.
206
156
  // Called by homebridge after restoring accessories from cache.
207
157
  async _main () {
208
- if (this._isHomebridgeLib || global[globalKey].Platform.heartbeat == null) {
209
- // process.on('unhandledRejection', (error) => {
210
- // this.error('unhandled rejection: %s', error.stack)
211
- // })
212
- global[globalKey].Platform.heartbeat = this
213
- /** System information.
214
- * @type {SystemInfo}
215
- * @readonly
216
- */
217
- this.systemInfo = new homebridgeLib.SystemInfo()
218
- this.systemInfo
219
- .on('error', (error) => { this.warn(error) })
220
- .on('exec', (command) => { this.debug('exec: %s', command) })
221
- .on('readFile', (filename) => { this.debug('read file: %s', filename) })
222
- await this.systemInfo.init()
223
- this.log('hardware: %s', this.systemInfo.hwInfo.prettyName)
224
- this.log('os: %s', this.systemInfo.osInfo.prettyName)
225
- this.heartbeatClients = Object.keys(global[globalKey]).filter((key) => {
226
- return global[globalKey][key].platform != null
227
- })
228
- this.debug('starting heartbeat for %j', this.heartbeatClients)
229
- this._heartbeatStart = new Date()
230
- setTimeout(() => { this._beat(-1) }, 1000)
231
- this.on('exit', () => {
232
- this.debug('flush cachedAccessories')
233
- this._homebridge.updatePlatformAccessories()
234
- this.log('goodbye')
235
- })
236
- } else {
237
- this.systemInfo = global[globalKey].Platform.heartbeat.systemInfo
238
- }
239
- if (this._isHomebridgeLib) {
240
- const jobs = []
241
- for (const plugin in global[globalKey]) {
242
- const platform = global[globalKey][plugin].platform
243
- if (
244
- platform != null && platform !== this &&
245
- Object.keys(platform._accessories).length === 0
246
- ) {
247
- this.warn('waiting on %s', plugin)
248
- jobs.push(events.once(platform, 'initialised'))
249
- }
250
- }
251
- for (const job of jobs) {
252
- await job
253
- }
254
- this.emit('initialised')
255
- }
158
+ // process.on('unhandledRejection', (error) => {
159
+ // this.error('unhandled rejection: %s', error.stack)
160
+ // })
161
+ /** System information.
162
+ * @type {SystemInfo}
163
+ * @readonly
164
+ */
165
+ this.systemInfo = new homebridgeLib.SystemInfo()
166
+ this.systemInfo
167
+ .on('error', (error) => { this.warn(error) })
168
+ .on('exec', (command) => { this.debug('exec: %s', command) })
169
+ .on('readFile', (filename) => { this.debug('read file: %s', filename) })
170
+ await this.systemInfo.init()
171
+ this.log('hardware: %s', this.systemInfo.hwInfo.prettyName)
172
+ this.log('os: %s', this.systemInfo.osInfo.prettyName)
173
+ this.debug('starting heartbeat for %j', this.heartbeatClients)
174
+ this._heartbeatStart = new Date()
175
+ setTimeout(() => { this._beat(-1) }, 1000)
176
+ this.on('exit', () => {
177
+ this.debug('flush cachedAccessories')
178
+ this._homebridge.updatePlatformAccessories()
179
+ this.log('goodbye')
180
+ })
181
+
256
182
  const n = Object.keys(this._accessories).length
257
183
  if (n > 0) {
258
184
  this.log('restored %d accessories from cache', n)
@@ -301,15 +227,11 @@ class Platform extends homebridgeLib.Delegate {
301
227
  this._homebridge.updatePlatformAccessories()
302
228
  }
303
229
 
304
- for (const plugin of this.heartbeatClients) {
305
- global[globalKey][plugin].platform._heartbeat(beat)
306
- }
307
- }
308
-
309
- _heartbeat (beat) {
310
230
  if (beat % context.checkInterval === 0) {
311
- this._checkLatest()
231
+ this._checkLatest(this._pluginName, this._pluginVersion)
232
+ this._checkLatest(context.libName, context.libVersion)
312
233
  }
234
+
313
235
  /** Emitted every second.
314
236
  * @event Platform#heartbeat
315
237
  * @param {number} beat - The sequence number of this heartbeat.
@@ -364,13 +286,10 @@ class Platform extends homebridgeLib.Delegate {
364
286
 
365
287
  // Issue an identity message.
366
288
  _identify () {
367
- const s = this._isHomebridgeLib
368
- ? ''
369
- : ', ' + context.libName + ' v' + context.libVersion
370
289
  this.log(
371
- '%s v%s, node v%s, homebridge v%s%s',
372
- this._pluginName, this._pluginVersion,
373
- context.nodeVersion, context.homebridgeVersion, s
290
+ '%s v%s, node v%s, homebridge v%s, %s v%s',
291
+ this._pluginName, this._pluginVersion, context.nodeVersion,
292
+ context.homebridgeVersion, context.libName, context.libVersion
374
293
  )
375
294
  if (context.nodeVersion !== context.recommendedNodeVersion) {
376
295
  this.warn(
@@ -391,16 +310,16 @@ class Platform extends homebridgeLib.Delegate {
391
310
  }
392
311
 
393
312
  // Check the NPM registry for the latest version of this plugin.
394
- async _checkLatest () {
313
+ async _checkLatest (name, version) {
395
314
  try {
396
- if (global[globalKey].Platform.npmRegistry == null) {
397
- const npmRegistry = new homebridgeLib.HttpClient({
315
+ if (this.npmRegistry == null) {
316
+ this.npmRegistry = new homebridgeLib.HttpClient({
398
317
  https: true,
399
318
  host: 'registry.npmjs.org',
400
319
  json: true,
401
320
  maxSockets: 1
402
321
  })
403
- npmRegistry
322
+ this.npmRegistry
404
323
  .on('error', (error) => {
405
324
  this.log(
406
325
  'npm registry: request %d: %s %s', error.request.id,
@@ -428,15 +347,14 @@ class Platform extends homebridgeLib.Delegate {
428
347
  response.statusCode, response.statusMessage
429
348
  )
430
349
  })
431
- global[globalKey].Platform.npmRegistry = npmRegistry
432
350
  }
433
- const { body } = await global[globalKey].Platform.npmRegistry.get(
434
- '/' + this._pluginName + '/latest', { Accept: 'application/json' })
351
+ const { body } = await this.npmRegistry.get(
352
+ '/' + name + '/latest', { Accept: 'application/json' })
435
353
  if (body != null && body.version != null) {
436
- if (body.version !== this._pluginVersion) {
437
- this.warn('latest version: %s v%s', this._pluginName, body.version)
354
+ if (body.version !== version) {
355
+ this.warn('latest version: %s v%s', name, body.version)
438
356
  } else {
439
- this.debug('latest version: %s v%s', this._pluginName, body.version)
357
+ this.debug('latest version: %s v%s', name, body.version)
440
358
  }
441
359
  }
442
360
  } catch (error) {
@@ -448,7 +366,16 @@ class Platform extends homebridgeLib.Delegate {
448
366
 
449
367
  // ===== Handle Accessories ==================================================
450
368
 
451
- _configureAccessory (accessory) {
369
+ /** Configure an accessory, after it has been restored from peristent
370
+ * storage.
371
+ *
372
+ * Called by homebridge when restoring peristed accessories, typically from
373
+ * `~/.homebridge/accessories/cachedAccessories`.
374
+ * @method
375
+ * @param {!PlatformAccessory} accessory - The restored Homebridge
376
+ * [PlatformAccessory](https://github.com/nfarina/homebridge/blob/master/lib/platformAccessory.js).
377
+ */
378
+ configureAccessory (accessory) {
452
379
  const className = accessory.context.className
453
380
  const version = accessory.context.version
454
381
  const id = accessory.context.id
@@ -584,7 +511,7 @@ class Platform extends homebridgeLib.Delegate {
584
511
  if (this._upnpMonitor != null) {
585
512
  throw new SyntaxError('upnpConfig(): already called')
586
513
  }
587
- this._upnpMonitor = new global[globalKey].Platform.UpnpClient(config)
514
+ this._upnpMonitor = new homebridgeLib.UpnpClient(config)
588
515
  this._upnpMonitor
589
516
  .on('error', (error) => {
590
517
  this.error('upnp: error')
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Library for homebridge plugins",
4
4
  "author": "Erik Baauw",
5
5
  "license": "Apache-2.0",
6
- "version": "5.1.24-6",
6
+ "version": "5.2.0",
7
7
  "keywords": [
8
8
  "homekit",
9
9
  "homebridge"
@@ -21,7 +21,7 @@
21
21
  "upnp": "cli/upnp.js"
22
22
  },
23
23
  "engines": {
24
- "homebridge": "^1.3.9",
24
+ "homebridge": "^1.4.0",
25
25
  "node": "^16.13.2"
26
26
  },
27
27
  "dependencies": {