homebridge-lib 6.0.2 → 6.1.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.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/cli/hap.js +1 -1
  3. package/cli/json.js +1 -1
  4. package/cli/sysinfo.js +1 -1
  5. package/cli/upnp.js +1 -1
  6. package/index.js +1 -1
  7. package/lib/AccessoryDelegate.js +30 -27
  8. package/lib/AdaptiveLighting.js +1 -1
  9. package/lib/CharacteristicDelegate.js +22 -36
  10. package/lib/Colour.js +1 -1
  11. package/lib/CommandLineParser.js +1 -1
  12. package/lib/CommandLineTool.js +1 -1
  13. package/lib/CustomHomeKitTypes.js +1 -1
  14. package/lib/Delegate.js +31 -33
  15. package/lib/EveHomeKitTypes.js +62 -52
  16. package/lib/HttpClient.js +1 -1
  17. package/lib/JsonFormatter.js +1 -1
  18. package/lib/MyHomeKitTypes.js +88 -97
  19. package/lib/OptionParser.js +27 -2
  20. package/lib/Platform.js +3 -17
  21. package/lib/PropertyDelegate.js +8 -11
  22. package/lib/ServiceDelegate/AccessoryInformation.js +1 -1
  23. package/lib/ServiceDelegate/Battery.js +1 -1
  24. package/lib/ServiceDelegate/Dummy.js +1 -1
  25. package/lib/ServiceDelegate/History/Consumption.js +31 -49
  26. package/lib/ServiceDelegate/History/Contact.js +14 -22
  27. package/lib/ServiceDelegate/History/Light.js +15 -23
  28. package/lib/ServiceDelegate/History/LightLevel.js +113 -0
  29. package/lib/ServiceDelegate/History/Motion.js +67 -80
  30. package/lib/ServiceDelegate/History/On.js +27 -33
  31. package/lib/ServiceDelegate/History/Power.js +46 -52
  32. package/lib/ServiceDelegate/History/Room.js +28 -50
  33. package/lib/ServiceDelegate/History/Thermo.js +19 -33
  34. package/lib/ServiceDelegate/History/Weather.js +33 -56
  35. package/lib/ServiceDelegate/History/index.js +155 -182
  36. package/lib/ServiceDelegate/ServiceLabel.js +1 -1
  37. package/lib/ServiceDelegate/index.js +13 -10
  38. package/lib/SystemInfo.js +3 -2
  39. package/lib/UiServer.js +1 -1
  40. package/lib/UpnpClient.js +1 -1
  41. package/package.json +2 -2
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/OptionParser.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2018-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2018-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -769,6 +769,31 @@ class OptionParser extends events.EventEmitter {
769
769
  return this
770
770
  }
771
771
 
772
+ /** Defines a key that takes an number value,
773
+ * optionally clamped between min and max.
774
+ *
775
+ * @param {!string} key - The key.
776
+ * @param {?number} min - Minimum value returned.
777
+ * @param {?number} max - Maximum value returned.
778
+ * @return {OptionParser} this - For chaining.
779
+ * @throws {TypeError} When key is not a string.
780
+ * @throws {RangeError} When key is empty string.
781
+ * @throws {SyntaxError} On duplicate key.
782
+ */
783
+ numberKey (key, min, max) {
784
+ key = this._toKey(key)
785
+ min = min == null ? -Infinity : OptionParser.toNumber('min', min)
786
+ max = max == null ? Infinity : OptionParser.toNumber('max', max)
787
+ if (max < min) {
788
+ throw newRangeError('max: smaller than min')
789
+ }
790
+
791
+ this._callbacks[key] = (value) => {
792
+ this._object[key] = OptionParser.toNumber(key, value, min, max, this._userInput)
793
+ }
794
+ return this
795
+ }
796
+
772
797
  /** Defines a key that takes an object as value.
773
798
  *
774
799
  * @param {!string} key - The key.
@@ -832,7 +857,7 @@ class OptionParser extends events.EventEmitter {
832
857
  * @throws {TypeError} When option has wrong type.
833
858
  * @throws {RangeError} When option has wrong value.
834
859
  * @throws {SyntaxError} Unknown option.
835
- * @throws {UserError} On error, when value was input by user.
860
+ * @throws {UserInputError} On error, when value was input by user.
836
861
  */
837
862
  parse (options) {
838
863
  options = OptionParser.toObject('options', options)
package/lib/Platform.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/Platform.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -139,7 +139,7 @@ class Platform extends homebridgeLib.Delegate {
139
139
  this._log = log
140
140
  this._configJson = configJson
141
141
  this._homebridge = homebridge
142
- this._myContext = context[this.className]
142
+ this._myContext = context[this.constructor.name]
143
143
  this._platformName = this._myContext.platformName
144
144
  this._pluginName = this._myContext.packageJson.name
145
145
  this._pluginVersion = this._myContext.packageJson.version
@@ -460,8 +460,7 @@ class Platform extends homebridgeLib.Delegate {
460
460
 
461
461
  // Get or create accessory.
462
462
  _getAccessory (delegate, params) {
463
- const className = delegate.className
464
- const version = this._pluginVersion
463
+ const className = delegate.constructor.name
465
464
  const id = params.id
466
465
  const name = params.name
467
466
  let accessory = this._accessories[id]
@@ -473,11 +472,6 @@ class Platform extends homebridgeLib.Delegate {
473
472
  this._accessories[id] = accessory
474
473
  accessory.displayName = name
475
474
  accessory.context = {
476
- className,
477
- version,
478
- id,
479
- name,
480
- logLevel: this.logLevel,
481
475
  context: {}
482
476
  }
483
477
  delegate.once('initialised', () => {
@@ -507,14 +501,6 @@ class Platform extends homebridgeLib.Delegate {
507
501
  delegate.emit('exposeError', error)
508
502
  }
509
503
  })
510
- } else {
511
- // Allow for plugin to change delegate class, version, and name.
512
- accessory.context.className = className
513
- accessory.context.version = version
514
- accessory.context.name = name
515
- if (accessory.context.logLevel == null) {
516
- accessory.context.logLevel = this.logLevel
517
- }
518
504
  }
519
505
  this._accessoryDelegates[id] = delegate
520
506
  return accessory
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/PropertyDelegate.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -22,8 +22,7 @@ class PropertyDelegate extends homebridgeLib.Delegate {
22
22
  /** Instantiate a property delegate.
23
23
  *
24
24
  * Note that instances are normally created by invoking
25
- * {@link AccessoryDelegate#addPropertyDelegate addPropertyDelegate()} or
26
- * {@link ServiceDelegate#addPropertyDelegate addPropertyDelegate()}.
25
+ * {@link AccessoryDelegate#addPropertyDelegate addPropertyDelegate()}.
27
26
  * @param {!AccessoryDelegate|ServiceDelegate} delegate - Reference to the
28
27
  * delegate of the corresponding HomeKit accessory or service.
29
28
  * @param {!object} params - Parameters of the property delegate.
@@ -41,10 +40,7 @@ class PropertyDelegate extends homebridgeLib.Delegate {
41
40
  * optional parameter is not applicable.
42
41
  */
43
42
  constructor (parent, params = {}) {
44
- if (!(
45
- parent instanceof homebridgeLib.AccessoryDelegate ||
46
- parent instanceof homebridgeLib.ServiceDelegate
47
- )) {
43
+ if (!(parent instanceof homebridgeLib.AccessoryDelegate)) {
48
44
  throw new TypeError('parent: not an AccessoryDelegate')
49
45
  }
50
46
  super(parent.platform, parent.name + ': ' + params.key)
@@ -61,8 +57,6 @@ class PropertyDelegate extends homebridgeLib.Delegate {
61
57
  if (this.value == null && params.value != null) {
62
58
  this.value = params.value
63
59
  }
64
-
65
- this.vdebug('created')
66
60
  }
67
61
 
68
62
  /** Destroy the propery delegate.
@@ -92,6 +86,10 @@ class PropertyDelegate extends homebridgeLib.Delegate {
92
86
  return this._parent.logLevel
93
87
  }
94
88
 
89
+ get _namePrefix () {
90
+ return this._parent._namePrefix + this._key + ': '
91
+ }
92
+
95
93
  validate (value) {
96
94
  // Todo: check value against type.
97
95
  return { value, s: '' }
@@ -116,8 +114,7 @@ class PropertyDelegate extends homebridgeLib.Delegate {
116
114
  this._log('set to %j%s%s', value, this._unit, s)
117
115
  } else {
118
116
  this._log(
119
- 'set to %j%s%s (from %j%s)', value, this._unit, s,
120
- this.value, this._unit
117
+ 'set to %j%s%s (from %j%s)', value, this._unit, s, this.value, this._unit
121
118
  )
122
119
  }
123
120
 
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/AccessoryInformation.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/Battery.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -1,7 +1,7 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/Dummy.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
5
 
6
6
  'use strict'
7
7
 
@@ -1,20 +1,13 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/History/Consumption.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
- //
6
- // The logic for handling Eve history was copied from Simone Tisa's
7
- // fakagato-history repository, copyright © 2017 simont77.
8
- // See https://github.com/simont77/fakegato-history.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
9
5
 
10
6
  'use strict'
11
7
 
12
8
  const homebridgeLib = require('../../../index')
13
- const util = require('util')
14
9
 
15
- const { ServiceDelegate } = homebridgeLib
16
- const { History } = ServiceDelegate
17
- const { swap16, swap32, numToHex } = History
10
+ const { History } = homebridgeLib.ServiceDelegate
18
11
 
19
12
  /** Class for an Eve Energy _History_ service delegate.
20
13
  *
@@ -53,9 +46,9 @@ class Consumption extends History {
53
46
  * @param {?CharacteristicDelegate} params.powerDelegate - A reference to the
54
47
  * delegate of the associated `Characteristics.eve.CurrentConsumption`
55
48
  * characteristic.
56
- * @param {!CharacteristicDelegate} params.onDelegate - A reference to the
49
+ * @param {?CharacteristicDelegate} params.onDelegate - A reference to the
57
50
  * delegate of the associated `Characteristics.hap.On` characteristic.
58
- * @param {!CharacteristicDelegate} params.lastActivationDelegate - A
51
+ * @param {?CharacteristicDelegate} params.lastActivationDelegate - A
59
52
  * reference to the delegate of the associated
60
53
  * `Characteristics.eve.LastActivation` characteristic.
61
54
  */
@@ -85,66 +78,55 @@ class Consumption extends History {
85
78
  this._consumptionDelegate = params.consumptionDelegate
86
79
  this._powerDelegate = params.powerDelegate
87
80
 
88
- this._entry = { time: 0, power: 0 }
81
+ this.entry = { p: 0 }
89
82
  if (params.onDelegate != null) {
90
- this._entry.on = params.onDelegate.value ? 1 : 0
83
+ this.entry.o = params.onDelegate.value ? 1 : 0
91
84
  params.onDelegate.on('didSet', (value) => {
92
- const now = Math.round(new Date().valueOf() / 1000)
85
+ const now = History.now()
93
86
  if (params.lastActivationDelegate != null) {
94
- params.lastActivationDelegate.value = now - this._h.initialTime
87
+ params.lastActivationDelegate.value = this.lastActivationValue(now)
95
88
  }
96
- this._entry.on = value ? 1 : 0
97
- const power = this._entry.power
98
- this._entry.power = null
99
- super._addEntry(now)
100
- this._entry.power = power
89
+ this.entry.o = value ? 1 : 0
90
+ super.addEntry({ time: now, o: this.entry.o })
101
91
  })
102
92
  }
103
93
  }
104
94
 
105
- _addEntry () {
106
- const now = Math.round(new Date().valueOf() / 1000)
95
+ addEntry (entry) {
107
96
  // Sensor deliveres totalConsumption, optionally compute currentConsumption
108
97
  if (this._consumption != null && this._time != null) {
109
98
  const delta = this._consumptionDelegate.value - this._consumption // kWh
110
- const period = now - this._time // s
99
+ const period = entry.time - this._time // s
111
100
  const power = 1000 * 3600 * delta / period // W
112
101
  if (this._powerDelegate != null) {
113
102
  this._powerDelegate.value = Math.round(power) // W
114
103
  }
115
- this._entry.power = Math.round(power * 10) // 0.1 W * 10 min
116
- super._addEntry(now)
104
+ entry.p = Math.round(power * 10) // 0.1 W * 10 min
117
105
  }
106
+ super.addEntry(entry)
118
107
  this._consumption = this._consumptionDelegate.value
119
- this._time = now
108
+ this._time = entry.time
120
109
  }
121
110
 
122
- get _fingerPrint () { return '02 0702 0E01' }
111
+ get fingerPrint () { return '02 0702 0E01' }
123
112
 
124
- _entryStream (entry) {
125
- if (entry.on == null) {
126
- return util.format(
127
- '|0c %s %s 01 %s',
128
- numToHex(swap32(this._h.currentEntry), 8),
129
- numToHex(swap32(entry.time - this._h.initialTime), 8),
130
- numToHex(swap16(entry.power), 4)
131
- )
113
+ entryToBuffer (entry) {
114
+ const buffer = Buffer.alloc(4)
115
+ let o = 0
116
+ if (entry.o == null) {
117
+ buffer.writeUInt8(0x01, o); o += 1
118
+ buffer.writeUInt16LE(entry.p, o); o += 2
119
+ return buffer.slice(0, o)
132
120
  }
133
- if (entry.power == null) {
134
- return util.format(
135
- '|0b %s %s 02 %s',
136
- numToHex(swap32(this._h.currentEntry), 8),
137
- numToHex(swap32(entry.time - this._h.initialTime), 8),
138
- numToHex(entry.on, 2)
139
- )
121
+ if (entry.p == null) {
122
+ buffer.writeUInt8(0x02, o); o += 1
123
+ buffer.writeUInt8(entry.o, o); o += 1
124
+ return buffer.slice(0, o)
140
125
  }
141
- return util.format(
142
- '|0d %s %s 03 %s %s',
143
- numToHex(swap32(this._h.currentEntry), 8),
144
- numToHex(swap32(entry.time - this._h.initialTime), 8),
145
- numToHex(swap16(entry.power), 4),
146
- numToHex(entry.on, 2)
147
- )
126
+ buffer.writeUInt8(0x03, o); o += 1
127
+ buffer.writeUInt16LE(entry.p, o); o += 2
128
+ buffer.writeUInt8(entry.o, o); o += 1
129
+ return buffer.slice(0, o)
148
130
  }
149
131
  }
150
132
 
@@ -1,20 +1,13 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/History/Contact.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
- //
6
- // The logic for handling Eve history was copied from Simone Tisa's
7
- // fakagato-history repository, copyright © 2017 simont77.
8
- // See https://github.com/simont77/fakegato-history.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
9
5
 
10
6
  'use strict'
11
7
 
12
8
  const homebridgeLib = require('../../../index')
13
- const util = require('util')
14
9
 
15
- const { ServiceDelegate } = homebridgeLib
16
- const { History } = ServiceDelegate
17
- const { swap32, numToHex } = History
10
+ const { History } = homebridgeLib.ServiceDelegate
18
11
 
19
12
  /** Class for an Eve Door _History_ service delegate.
20
13
  *
@@ -64,13 +57,13 @@ class Contact extends History {
64
57
  if (!(params.lastActivationDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
65
58
  throw new TypeError('params.lastActivationDelegate: not a CharacteristicDelegate')
66
59
  }
67
- this._entry = { time: 0, status: params.contactDelegate.value }
60
+ this.entry = { c: params.contactDelegate.value ? 1 : 0 }
68
61
  params.contactDelegate.on('didSet', (value) => {
69
- const now = Math.round(new Date().valueOf() / 1000)
62
+ const now = History.now()
70
63
  params.timesOpenedDelegate.value += value
71
- params.lastActivationDelegate.value = now - this._h.initialTime
72
- this._entry.status = value
73
- this._addEntry(now)
64
+ params.lastActivationDelegate.value = this.lastActivationValue(now)
65
+ this.entry.c = value ? 1 : 0
66
+ this.addEntry({ time: now, c: this.entry.c })
74
67
  })
75
68
  this.addCharacteristicDelegate({
76
69
  key: 'resetTotal',
@@ -81,15 +74,14 @@ class Contact extends History {
81
74
  })
82
75
  }
83
76
 
84
- get _fingerPrint () { return '01 0601' }
77
+ get fingerPrint () { return '01 0601' }
85
78
 
86
- _entryStream (entry) {
87
- return util.format(
88
- '|0b %s %s 01 %s',
89
- numToHex(swap32(this._h.currentEntry), 8),
90
- numToHex(swap32(entry.time - this._h.initialTime), 8),
91
- numToHex(entry.status, 2)
92
- )
79
+ entryToBuffer (entry) {
80
+ const buffer = Buffer.alloc(2)
81
+ let o = 0
82
+ buffer.writeUInt8(0x01, o); o += 1
83
+ buffer.writeUInt8(entry.c, o); o += 1
84
+ return buffer.slice(0, o)
93
85
  }
94
86
  }
95
87
 
@@ -1,18 +1,13 @@
1
1
  // homebridge-lib/lib/ServiceDelegate/History/On.js
2
2
  //
3
3
  // Library for Homebridge plugins.
4
- // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
- //
6
- // The logic for handling Eve history was copied from Simone Tisa's
7
- // fakagato-history repository, copyright © 2017 simont77.
8
- // See https://github.com/simont77/fakegato-history.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
9
5
 
10
6
  'use strict'
11
7
 
12
8
  const homebridgeLib = require('../../../index')
13
9
 
14
- const { ServiceDelegate } = homebridgeLib
15
- const { History } = ServiceDelegate
10
+ const { History } = homebridgeLib.ServiceDelegate
16
11
 
17
12
  /** Class for an Eve Light Strip _History_ service delegate.
18
13
  *
@@ -28,7 +23,12 @@ const { History } = ServiceDelegate
28
23
  * `historyEntries` | `Characteristics.eve.HistoryEntries`
29
24
  *
30
25
  * This delegate creates the history from the associated
31
- * `Characteristics.hap.On` characteristic.
26
+ * `Characteristics.hap.On` characteristic. It updates the
27
+ * values of the associated `Characteristics.eve.LastActivation`
28
+ * characteristic.
29
+ * Note that the Eve Light Strip doesn't actually provide history entries;
30
+ * the _History_ service is used only to provide a reference time for
31
+ * last `LastActivation`.
32
32
  * @extends ServiceDelegate.History
33
33
  * @memberof ServiceDelegate.History
34
34
  */
@@ -54,28 +54,20 @@ class Light extends History {
54
54
  if (!(params.lastActivationDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
55
55
  throw new TypeError('params.lastActivationDelegate: not a CharacteristicDelegate')
56
56
  }
57
- if (
58
- params.temperatureDelegate != null &&
59
- !(params.temperatureDelegate instanceof homebridgeLib.CharacteristicDelegate)
60
- ) {
61
- throw new TypeError('params.temperatureDelegate: not a CharacteristicDelegate')
62
- }
63
- this._entry = {
64
- time: 0
65
- }
57
+ this.entry = { }
66
58
  params.onDelegate.on('didSet', (value) => {
67
- const now = Math.round(new Date().valueOf() / 1000)
59
+ const now = History.now()
68
60
  if (params.lastActivationDelegate != null) {
69
- params.lastActivationDelegate.value = now - this._h.initialTime
61
+ params.lastActivationDelegate.value = this.lastActivationValue(now)
70
62
  }
71
- this._addEntry(now)
63
+ this.addEntry({ time: now })
72
64
  })
73
65
  }
74
66
 
75
- get _fingerPrint () { return '00' }
67
+ get fingerPrint () { return '00' }
76
68
 
77
- _entryStream (entry) {
78
- return ''
69
+ entryToBuffer (entry) {
70
+ return Buffer.alloc(0)
79
71
  }
80
72
  }
81
73
 
@@ -0,0 +1,113 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/LightLevel.js
2
+ //
3
+ // Library for Homebridge plugins.
4
+ // Copyright © 2017-2023 Erik Baauw. All rights reserved.
5
+
6
+ 'use strict'
7
+
8
+ const homebridgeLib = require('../../../index')
9
+
10
+ const { History } = homebridgeLib.ServiceDelegate
11
+
12
+ /** Class for an Eve Motion _History_ service delegate.
13
+ *
14
+ * This delegate sets up a `Services.eve.History` HomeKit service
15
+ * with keys for the following HomeKit characteristics:
16
+ *
17
+ * key | Characteristic
18
+ * ---------------- | ----------------------------------
19
+ * `name` | `Characteristics.hap.Name`
20
+ * `historyRequest` | `Characteristics.eve.HistoryRequest`
21
+ * `setTime` | `Characteristics.eve.SetTime`
22
+ * `historyStatus` | `Characteristics.eve.HistoryStatus`
23
+ * `historyEntries` | `Characteristics.eve.HistoryEntries`
24
+ * `resetTotal` | `Characteristics.eve.ResetTotal`
25
+ *
26
+ * This delegate creates the history from the associated
27
+ * `Characteristics.hap.MotionDetected` characteristic. It updates the
28
+ * value of the associated `Characteristics.eve.LastActivation` characteristic.
29
+ * Optionally, this delegate also mainatins history for
30
+ * `Characteristics.hap.CurrentAmbientLightLevel`,
31
+ * `Characteristics.hap.CurrentTemperature`, and
32
+ * `Characteristics.hap.CurrentRelativeHumidity`.
33
+ * @extends ServiceDelegate.History
34
+ * @memberof ServiceDelegate.History
35
+ */
36
+ class LightLevel extends History {
37
+ /** Create a new instance of an Eve Motion _History_ service delegate.
38
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
39
+ * corresponding HomeKit accessory.
40
+ * @param {!object} params - The parameters for the
41
+ * _History_ HomeKit service.
42
+ * @param {!CharacteristicDelegate} params.lightLevelDelegate - A reference
43
+ * to the delegate of the associated
44
+ * `Characteristics.hap.CurrentAmbientLightLevel` characteristic.
45
+ * For PIR sensors (like the Hue motion sensor) that report light level in
46
+ * addition to motion.
47
+ * @param {?CharacteristicDelegate} params.temperatureDelegate - A reference
48
+ * to the delegate of the associated `Characteristics.hap.CurrentTemperature`
49
+ * characteristic.
50
+ * For PIR sensors (like the Hue motion sensor) that report temperature in
51
+ * addition to motion and light level.
52
+ * @param {?CharacteristicDelegate} params.humidityDelegate - A reference
53
+ * to the delegate of the associated `Characteristics.hap.CurrentRelativeHumidity`
54
+ * characteristic.
55
+ * For PIR sensors (like the OWON PIR313 sensor) that report humidity in
56
+ * addition to motion, light level, and temperature.
57
+ */
58
+ constructor (accessoryDelegate, params) {
59
+ super(accessoryDelegate, params)
60
+ if (!(params.lightLevelDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
61
+ throw new TypeError('params.lightLevelDelegate: not a CharacteristicDelegate')
62
+ }
63
+ this.entry = { l: params.lightLevelDelegate.value }
64
+ params.lightLevelDelegate.on('didSet', (value) => {
65
+ this.entry.l = value
66
+ })
67
+ if (params.temperatureDelegate != null) {
68
+ if (!(params.temperatureDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
69
+ throw new TypeError('params.temperatureDelegate: not a CharacteristicDelegate')
70
+ }
71
+ this.entry.t = params.temperatureDelegate.value
72
+ params.temperatureDelegate.on('didSet', (value) => {
73
+ this.entry.t = value
74
+ })
75
+ if (params.humidityDelegate != null) {
76
+ if (!(params.humidityDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
77
+ throw new TypeError('params.humidityDelegate: not a CharacteristicDelegate')
78
+ }
79
+ this.entry.h = params.humidityDelegate.value
80
+ params.humidityDelegate.on('didSet', (value) => {
81
+ this.entry.h = value
82
+ })
83
+ }
84
+ }
85
+ }
86
+
87
+ get fingerPrint () {
88
+ return '03 3002 0102 0202'
89
+ }
90
+
91
+ entryToBuffer (entry) {
92
+ const buffer = Buffer.alloc(6)
93
+ let o = 0
94
+ if (entry.t == null) {
95
+ buffer.writeUInt8(0x01, o); o += 1
96
+ buffer.writeUInt16LE(entry.l, o); o += 2
97
+ return buffer.slice(0, o)
98
+ }
99
+ if (entry.h == null) {
100
+ buffer.writeUInt8(0x03, o); o += 1
101
+ buffer.writeUInt16LE(entry.l, o); o += 2
102
+ buffer.writeUInt16LE(entry.t * 100, o); o += 2
103
+ return buffer.slice(0, o)
104
+ }
105
+ buffer.writeUInt8(0x07, o); o += 1
106
+ buffer.writeUInt16LE(entry.l, o); o += 2
107
+ buffer.writeUInt16LE(entry.t * 100, o); o += 2
108
+ buffer.writeUInt16LE(entry.h * 100, o); o += 2
109
+ return buffer.slice(0, o)
110
+ }
111
+ }
112
+
113
+ module.exports = LightLevel