homebridge-lib 5.2.0 → 5.2.3

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.
@@ -40,13 +40,12 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
40
40
  super(homebridge)
41
41
 
42
42
  /** @member EveHomeKitTypes#Characteristics
43
- * @property {Class} AirParticulateDensity - Density (in ppm) of air
44
- * particulates.
45
- * <br>Used by: Eve Room.
43
+ * @property {Class} AirParticulateDensity - Deprecated. use `VOCLevel`
44
+ * instead.
46
45
  * @property {Class} AirPressure - Air pressure (in hPa).
47
46
  * <br>Used by: Eve Weather.
48
- * @property {Class} ClosedDuration - Time in seconds that door has been
49
- * open.
47
+ * @property {Class} ClosedDuration - Duration (in seconds) that door has
48
+ * been closed.
50
49
  * <br>Used by: Eve Door.
51
50
  * @property {Class} Clouds - Cloud coverage (in %).
52
51
  * <br>Used by: weather station.
@@ -59,6 +58,10 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
59
58
  * @property {Class} ConditionCategory - Weather condition
60
59
  * (as numberic code).
61
60
  * <br>Used by: weather station.
61
+ * @property {Class} ConfigCommand - Used by Eve app to set configuration.
62
+ * <br> Used by: `History` service.
63
+ * @property {Class} ConfigData - Used by Eve app to read configuration.
64
+ * <br> Used by: `History` service.
62
65
  * @property {Class} CurrentConsumption - Current electric consumption
63
66
  * (in W).
64
67
  * <br>Used by: Eve Energy.
@@ -98,7 +101,7 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
98
101
  * @property {Class} ObservationTime - Time of observation.
99
102
  * <br>Used by: weather station.
100
103
  * @property {Class} OpenDuration - Duration (in seconds) that door has
101
- * been closed.
104
+ * been open.
102
105
  * <br>Used by: Eve Door.
103
106
  * @property {Class} Ozone - Ozone level (in DU).
104
107
  * <br>Used by: weather station.
@@ -138,6 +141,8 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
138
141
  * <br>Used by: Eve Thermo.
139
142
  * @property {Class} Visibility - Visibility (in km).
140
143
  * <br>Used by: weather station.
144
+ * @property {Class} VOCLevel - Volatile Organic Compound level (in ppm).
145
+ * <br>Used by: Eve Room (1st gen).
141
146
  * @property {Class} Voltage - Electric voltage (in V).
142
147
  * <br>Used by: Eve Energy.
143
148
  * @property {Class} WindDirection - Wind direction (as text).
@@ -167,23 +172,32 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
167
172
  maxValue: 5000,
168
173
  minStep: 1,
169
174
  perms: [this.Perms.READ, this.Perms.NOTIFY]
170
- }, 'Air Particulate Density')
175
+ }, 'VOC Level')
176
+
177
+ this.createCharacteristicClass('VOCLevel', uuid('10B'), {
178
+ format: this.Formats.INT16,
179
+ unit: 'ppm',
180
+ minValue: 5,
181
+ maxValue: 5000,
182
+ minStep: 5,
183
+ perms: [this.Perms.READ, this.Perms.NOTIFY]
184
+ }, 'VOC Level')
171
185
 
172
186
  this.createCharacteristicClass('TotalConsumption', uuid('10C'), {
173
187
  format: this.Formats.FLOAT,
188
+ unit: 'kWh',
174
189
  minValue: 0,
175
190
  maxValue: 1000000,
176
191
  minStep: 0.1,
177
- unit: 'kWh',
178
192
  perms: [this.Perms.READ, this.Perms.NOTIFY]
179
193
  }, 'Total Consumption')
180
194
 
181
195
  this.createCharacteristicClass('CurrentConsumption', uuid('10D'), {
182
196
  format: this.Formats.FLOAT,
197
+ unit: 'W',
183
198
  minValue: 0,
184
199
  maxValue: 12000,
185
200
  minStep: 0.1,
186
- unit: 'W',
187
201
  perms: [this.Perms.READ, this.Perms.NOTIFY]
188
202
  }, 'Current Consumption')
189
203
 
@@ -236,6 +250,11 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
236
250
  perms: [this.Perms.WRITE, this.Perms.HIDDEN]
237
251
  }, 'History Request')
238
252
 
253
+ this.createCharacteristicClass('ConfigCommand', uuid('11D'), {
254
+ format: this.Formats.DATA,
255
+ perms: [this.Perms.WRITE, this.Perms.HIDDEN]
256
+ }, 'Config Command')
257
+
239
258
  this.createCharacteristicClass('Sensitivity', uuid('120'), {
240
259
  format: this.Formats.UINT8,
241
260
  minValue: 0,
@@ -285,6 +304,11 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
285
304
  perms: [this.Perms.READ, this.Perms.NOTIFY, this.Perms.WRITE],
286
305
  adminOnlyAccess: [this.Access.WRITE]
287
306
  })
307
+ this.Characteristics.Duration.VALID_VALUES = [
308
+ 5, 10, 20, 30,
309
+ 1 * 60, 2 * 60, 3 * 60, 5 * 60, 10 * 60, 20 * 60, 30 * 60,
310
+ 1 * 3600, 2 * 3600, 3 * 3600, 5 * 3600, 10 * 3600, 12 * 3600, 15 * 3600
311
+ ]
288
312
 
289
313
  this.createCharacteristicClass('ValvePosition', uuid('12E'), {
290
314
  format: this.Formats.UINT8,
@@ -309,6 +333,11 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
309
333
  adminOnlyAccess: [this.Access.WRITE]
310
334
  })
311
335
 
336
+ this.createCharacteristicClass('ConfigData', uuid('131'), {
337
+ format: this.Formats.DATA,
338
+ perms: [this.Perms.READ, this.Perms.NOTIFY, this.Perms.HIDDEN]
339
+ }, 'Config Data')
340
+
312
341
  // =========================================================================
313
342
 
314
343
  // The following custom characteristics are supported by the Eve app.
@@ -536,10 +565,10 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
536
565
  this.Characteristics.HistoryStatus,
537
566
  this.Characteristics.HistoryEntries
538
567
  ], [
539
- // this.Characteristics.Char11E,
568
+ // this.Characteristics.Char11E, // Used for firmware update?
540
569
  this.Characteristics.ResetTotal
541
- // this.Characteristics.Char11D,
542
- // this.Characteristics.Char131
570
+ // this.Characteristics.ConfigCommand,
571
+ // this.Characteristics.ConfigData
543
572
  ])
544
573
 
545
574
  this.createServiceClass('AirPressureSensor', uuid('00A'), [
package/lib/Platform.js CHANGED
@@ -170,7 +170,6 @@ class Platform extends homebridgeLib.Delegate {
170
170
  await this.systemInfo.init()
171
171
  this.log('hardware: %s', this.systemInfo.hwInfo.prettyName)
172
172
  this.log('os: %s', this.systemInfo.osInfo.prettyName)
173
- this.debug('starting heartbeat for %j', this.heartbeatClients)
174
173
  this._heartbeatStart = new Date()
175
174
  setTimeout(() => { this._beat(-1) }, 1000)
176
175
  this.on('exit', () => {
@@ -0,0 +1,111 @@
1
+ // homebridge-lib/lib/ServiceDelegate/AccessoryInformation.js
2
+ //
3
+ // Library for Homebridge plugins.
4
+ // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
+
6
+ 'use strict'
7
+
8
+ const homebridgeLib = require('../../index')
9
+
10
+ const { ServiceDelegate } = homebridgeLib
11
+
12
+ /** Class for an _AccessoryInformation_ service delegate.
13
+ *
14
+ * This delegate sets up a `Services.hap.AccessoryInformation` HomeKit service
15
+ * with the following HomeKit characteristics:
16
+ *
17
+ * key | Characteristic | isOptional
18
+ * -------------- | -------------------------------------- | ----------
19
+ * `name` | `Characteristics.hap.Name` |
20
+ * `id` | `Characteristics.hap.SerialNumber` |
21
+ * `manufacturer` | `Characteristics.hap.Manufacturer` |
22
+ * `model` | `Characteristics.hap.Model` |
23
+ * `firmware` | `Characteristics.hap.FirmwareRevision` |
24
+ * `hardware` | `Characteristics.hap.HardwareRevision` | Y
25
+ * `software` | `Characteristics.hap.SoftwareRevision` | Y
26
+ * @extends ServiceDelegate
27
+ * @memberof ServiceDelegate
28
+ */
29
+ class AccessoryInformation extends ServiceDelegate {
30
+ /** Create a new instance of an _AccessoryInformation_ service delegate.
31
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
32
+ * corresponding HomeKit accessory.
33
+ * @param {!object} params - The parameters for the
34
+ * _AccessoryInformation_ HomeKit service.
35
+ * @param {!string} params.name - Initial value for
36
+ * `Characteristics.hap.Name`. Also used to prefix log and error messages.
37
+ * @param {!string} params.id - Initial value for
38
+ * `Characteristics.hap.SerialNumber`.
39
+ * @param {!string} params.manufacturer - Initial value for
40
+ * `Characteristics.hap.Manufacturer`.
41
+ * @param {!string} params.model - Initial value for
42
+ * `Characteristics.hap.Model`.
43
+ * @param {!string} params.firmware - Initial value for
44
+ * `Characteristics.hap.FirmwareRevision`.
45
+ * @param {?string} params.hardware - Initial value for
46
+ * `Characteristics.hap.HardwareRevision`.
47
+ * @param {?string} params.software - Initial value for
48
+ * `Characteristics.hap.SoftwareRevision`.
49
+ */
50
+ constructor (accessoryDelegate, params = {}) {
51
+ params.name = accessoryDelegate.name
52
+ params.Service = accessoryDelegate.Services.hap.AccessoryInformation
53
+ super(accessoryDelegate, params)
54
+ this.addCharacteristicDelegate({
55
+ key: 'id',
56
+ Characteristic: this.Characteristics.hap.SerialNumber,
57
+ value: params.id
58
+ })
59
+ this.addCharacteristicDelegate({
60
+ key: 'identify',
61
+ Characteristic: this.Characteristics.hap.Identify
62
+ })
63
+ this.addCharacteristicDelegate({
64
+ key: 'manufacturer',
65
+ Characteristic: this.Characteristics.hap.Manufacturer,
66
+ value: params.manufacturer
67
+ })
68
+ this.addCharacteristicDelegate({
69
+ key: 'model',
70
+ Characteristic: this.Characteristics.hap.Model,
71
+ value: params.model
72
+ })
73
+ this.addCharacteristicDelegate({
74
+ key: 'firmware',
75
+ Characteristic: this.Characteristics.hap.FirmwareRevision,
76
+ value: params.firmware
77
+ })
78
+ if (params.hardware != null || this._context.hardware != null) {
79
+ this.addCharacteristicDelegate({
80
+ key: 'hardware',
81
+ Characteristic: this.Characteristics.hap.HardwareRevision,
82
+ value: params.hardware
83
+ })
84
+ }
85
+ if (params.software != null || this._context.software != null) {
86
+ this.addCharacteristicDelegate({
87
+ key: 'software',
88
+ Characteristic: this.Characteristics.hap.SoftwareRevision,
89
+ value: params.software
90
+ })
91
+ }
92
+ }
93
+
94
+ addCharacteristicDelegate (params = {}) {
95
+ const delegate = super.addCharacteristicDelegate(params)
96
+ Object.defineProperty(this.accessoryDelegate.values, params.key, {
97
+ configurable: true, // make sure we can delete it again
98
+ writeable: true,
99
+ get () { return delegate.value },
100
+ set (value) { delegate.value = value }
101
+ })
102
+ return delegate
103
+ }
104
+
105
+ removeCharacteristicDelegate (key) {
106
+ delete this.accessoryDelegate.values[key]
107
+ super.removeCharacteristicDelegate(key)
108
+ }
109
+ }
110
+
111
+ module.exports = AccessoryInformation
@@ -0,0 +1,87 @@
1
+ // homebridge-lib/lib/ServiceDelegate/Battery.js
2
+ //
3
+ // Library for Homebridge plugins.
4
+ // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
+
6
+ 'use strict'
7
+
8
+ const homebridgeLib = require('../../index')
9
+
10
+ const { ServiceDelegate } = homebridgeLib
11
+
12
+ /** Class for a _Battery_ service delegate.
13
+ *
14
+ * This delegate sets up a `Services.hap.Battery` HomeKit service
15
+ * with the following HomeKit characteristics:
16
+ *
17
+ * key | Characteristic | isOptional
18
+ * ------------------ | -------------------------------------- | ----------
19
+ * `name` | `Characteristics.hap.Name` |
20
+ * `batteryLevel` | `Characteristics.hap.BatteryLevel` | Y
21
+ * `chargingState` | `Characteristics.hap.ChargingState` | Y
22
+ * `statusLowBattery` | `Characteristics.hap.StatusLowBattery` | Y
23
+ * `lowBatteryThreshold`| `Characteristics.my.LowBatteryThreshold` | Y
24
+ *
25
+ * @extends ServiceDelegate
26
+ * @memberof ServiceDelegate
27
+ */
28
+ class Battery extends ServiceDelegate {
29
+ /** Create a new instance of an _Battery_ service delegate.
30
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
31
+ * corresponding HomeKit accessory.
32
+ * @param {object} params - The parameters for the
33
+ * _AccessoryInformation_ HomeKit service.
34
+ * @param {integer} [params.batteryLevel=100] - Initial value for
35
+ * `Characteristics.hap.BatteryLevel`.
36
+ * @param {integer} [params.chargingState=NOT_CHARGEABLE] - Initial value for
37
+ * `Characteristics.hap.ChargingState`.
38
+ * @param {integer} [params.statusLowBattery=BATTERY_LEVEL_NORMAL] - Initial
39
+ * value for `Characteristics.hap.StatusLowBattery`.
40
+ * @param {integer} [params.lowBatteryThreshold=20] - Initial value for
41
+ * low battery threshold.
42
+ */
43
+ constructor (accessoryDelegate, params = {}) {
44
+ params.name = accessoryDelegate.name + ' Battery'
45
+ params.Service = accessoryDelegate.Services.hap.BatteryService
46
+ super(accessoryDelegate, params)
47
+ this.addCharacteristicDelegate({
48
+ key: 'batteryLevel',
49
+ Characteristic: this.Characteristics.hap.BatteryLevel,
50
+ unit: '%',
51
+ value: params.batteryLevel != null ? params.batteryLevel : 100
52
+ }).on('didSet', (value) => {
53
+ this.updateStatusLowBattery()
54
+ })
55
+ this.addCharacteristicDelegate({
56
+ key: 'chargingState',
57
+ Characteristic: this.Characteristics.hap.ChargingState,
58
+ value: params.chargingState != null
59
+ ? params.chargingState
60
+ : this.Characteristics.hap.ChargingState.NOT_CHARGEABLE
61
+ })
62
+ this.addCharacteristicDelegate({
63
+ key: 'statusLowBattery',
64
+ Characteristic: this.Characteristics.hap.StatusLowBattery,
65
+ value: params.statusLowBattery != null
66
+ ? params.statusLowBattery
67
+ : this.Characteristics.hap.StatusLowBattery.BATTERY_LEVEL_NORMAL
68
+ })
69
+ this.addCharacteristicDelegate({
70
+ key: 'lowBatteryThreshold',
71
+ Characteristic: this.Characteristics.my.LowBatteryThreshold,
72
+ unit: '%',
73
+ value: params.lowBatteryThreshold != null ? params.lowBatteryThreshold : 20
74
+ }).on('didSet', (value) => {
75
+ this.updateStatusLowBattery()
76
+ })
77
+ }
78
+
79
+ updateStatusLowBattery () {
80
+ this.values.statusLowBattery =
81
+ this.values.batteryLevel <= this.values.lowBatteryThreshold
82
+ ? this.Characteristics.hap.StatusLowBattery.BATTERY_LEVEL_LOW
83
+ : this.Characteristics.hap.StatusLowBattery.BATTERY_LEVEL_NORMAL
84
+ }
85
+ }
86
+
87
+ module.exports = Battery
@@ -0,0 +1,44 @@
1
+ // homebridge-lib/lib/ServiceDelegate/Dummy.js
2
+ //
3
+ // Library for Homebridge plugins.
4
+ // Copyright © 2017-2022 Erik Baauw. All rights reserved.
5
+
6
+ 'use strict'
7
+
8
+ const homebridgeLib = require('../../index')
9
+
10
+ const { ServiceDelegate } = homebridgeLib
11
+
12
+ /** Class for a delegate for a dummy _StatelessProgrammableSwitch_ service.
13
+ *
14
+ * This delegate sets up a dummy `Services.hap.StatelessProgrammableSwitch`
15
+ * HomeKit service with the following HomeKit characteristics:
16
+ *
17
+ * key | Characteristic | isOptional
18
+ * ------------------------- | --------------------------------------------- | ----------
19
+ * `name` | `Characteristics.hap.Name` |
20
+ * `programmableSwitchEvent` | `Characteristics.hap.ProgrammableSwitchEvent` |
21
+ *
22
+ * Including the dummy service prevents Home from showing a _Not Supported_
23
+ * tile, and causes Home on iOS14 to show the _Favorite_ setting.
24
+ * @extends ServiceDelegate
25
+ * @memberof ServiceDelegate
26
+ */
27
+ class Dummy extends ServiceDelegate {
28
+ constructor (nbAccessory, params = {}) {
29
+ params.name = nbAccessory.name
30
+ params.Service = nbAccessory.Services.hap.StatelessProgrammableSwitch
31
+ super(nbAccessory, params)
32
+
33
+ this.addCharacteristicDelegate({
34
+ key: 'programmableSwitchEvent',
35
+ Characteristic: this.Characteristics.hap.ProgrammableSwitchEvent,
36
+ props: {
37
+ minValue: this.Characteristics.hap.ProgrammableSwitchEvent.SINGLE_PRESS,
38
+ maxValue: this.Characteristics.hap.ProgrammableSwitchEvent.SINGLE_PRESS
39
+ }
40
+ })
41
+ }
42
+ }
43
+
44
+ module.exports = Dummy
@@ -0,0 +1,114 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/Consumption.js
2
+ //
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.
9
+
10
+ 'use strict'
11
+
12
+ const homebridgeLib = require('../../../index')
13
+ const util = require('util')
14
+
15
+ const { ServiceDelegate } = homebridgeLib
16
+ const { History } = ServiceDelegate
17
+ const { swap16, swap32, numToHex } = History
18
+
19
+ /** Class for an Eve Energy _History_ service delegate.
20
+ *
21
+ * This delegate sets up a `Services.eve.History` HomeKit service
22
+ * with keys for the following HomeKit characteristics:
23
+ *
24
+ * key | Characteristic
25
+ * ---------------- | ----------------------------------
26
+ * `name` | `Characteristics.hap.Name`
27
+ * `historyRequest` | `Characteristics.eve.HistoryRequest`
28
+ * `setTime` | `Characteristics.eve.SetTime`
29
+ * `historyStatus` | `Characteristics.eve.HistoryStatus`
30
+ * `historyEntries` | `Characteristics.eve.HistoryEntries`
31
+ *
32
+ * This delegate is for sensors that report life-time consumption. The history
33
+ * is computed from the changes to the value of the associated
34
+ * `Characteristics.eve.TotalConsumption` characteristic. If the sensor doesn't
35
+ * also report power, this delegate can update the the value of the associated
36
+ * `Characteristics.eve.CurrentConsumption` characteristic.
37
+ *
38
+ * Note that the key in the history entry is called `power`, but its value
39
+ * is the consumption in Wh since the previous entry.
40
+ *
41
+ * @extends ServiceDelegate.History
42
+ * @memberof ServiceDelegate.History
43
+ */
44
+ class Consumption extends History {
45
+ /** Create a new instance of an Eve Energy _History_ service delegate.
46
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
47
+ * corresponding HomeKit accessory.
48
+ * @param {!object} params - The parameters for the
49
+ * _History_ HomeKit service.
50
+ * @param {!CharacteristicDelegate} consumptionDelegate - A reference to the
51
+ * delegate of the associated `Characteristics.eve.TotalConsumption`
52
+ * characteristic.
53
+ * @param {?CharacteristicDelegate} powerDelegate - A reference to the
54
+ * delegate of the associated `Characteristics.eve.CurrentConsumption`
55
+ * characteristic.
56
+ */
57
+ constructor (
58
+ accessoryDelegate, params, consumptionDelegate, powerDelegate
59
+ ) {
60
+ super(accessoryDelegate, params)
61
+ if (!(consumptionDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
62
+ throw new TypeError('consumptionDelegate: not a CharacteristicDelegate')
63
+ }
64
+ if (
65
+ powerDelegate != null &&
66
+ !(powerDelegate instanceof homebridgeLib.CharacteristicDelegate)
67
+ ) {
68
+ throw new TypeError('powerDelegate: not a CharacteristicDelegate')
69
+ }
70
+ this.addCharacteristicDelegate({
71
+ key: 'consumption',
72
+ silent: true
73
+ })
74
+ this.addCharacteristicDelegate({
75
+ key: 'time',
76
+ silent: true
77
+ })
78
+ this._consumptionDelegate = consumptionDelegate
79
+ this._powerDelegate = powerDelegate
80
+ this._entry = { time: 0, power: 0 }
81
+ }
82
+
83
+ _addEntry () {
84
+ const now = Math.round(new Date().valueOf() / 1000)
85
+ // Sensor deliveres totalConsumption, optionally compute currentConsumption
86
+ if (this.values.consumption != null && this.values.time != null) {
87
+ const delta = this._consumptionDelegate.value - this.values.consumption // kWh
88
+ const period = now - this.values.time // s
89
+ const power = Math.round(1000 * 3600 * delta / period) // W
90
+ if (this._powerDelegate != null) {
91
+ this._powerDelegate.value = power
92
+ }
93
+ this._entry.power = power
94
+ super._addEntry(now)
95
+ }
96
+ this.values.consumption = this._consumptionDelegate.value
97
+ this.values.time = now
98
+ }
99
+
100
+ // get _fingerPrint () { return '04 0102 0202 0702 0f03' }
101
+ get _fingerPrint () { return '01 0702' }
102
+
103
+ _entryStream (entry) {
104
+ return util.format(
105
+ // '|14 %s %s 1f 0000 0000 %s 0000 0000',
106
+ '|0c %s %s 01 %s',
107
+ numToHex(swap32(this._h.currentEntry), 8),
108
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
109
+ numToHex(swap16(entry.power * 10), 4)
110
+ )
111
+ }
112
+ }
113
+
114
+ module.exports = Consumption
@@ -0,0 +1,100 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/Contact.js
2
+ //
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.
9
+
10
+ 'use strict'
11
+
12
+ const homebridgeLib = require('../../../index')
13
+ const util = require('util')
14
+
15
+ const { ServiceDelegate } = homebridgeLib
16
+ const { History } = ServiceDelegate
17
+ const { swap32, numToHex } = History
18
+
19
+ /** Class for an Eve Door _History_ service delegate.
20
+ *
21
+ * This delegate sets up a `Services.eve.History` HomeKit service
22
+ * with keys for the following HomeKit characteristics:
23
+ *
24
+ * key | Characteristic
25
+ * ---------------- | ----------------------------------
26
+ * `name` | `Characteristics.hap.Name`
27
+ * `historyRequest` | `Characteristics.eve.HistoryRequest`
28
+ * `setTime` | `Characteristics.eve.SetTime`
29
+ * `historyStatus` | `Characteristics.eve.HistoryStatus`
30
+ * `historyEntries` | `Characteristics.eve.HistoryEntries`
31
+ * `resetTotal` | `Characteristics.eve.ResetTotal`
32
+ *
33
+ * This delegate creates the history from the associated
34
+ * `Characteristics.hap.ContactSensorState` characteristic. It updates the
35
+ * values of the associated `Characteristics.eve.TimesOpened` and
36
+ * `Characteristics.eve.LastActivation` characteristics.
37
+ * @extends ServiceDelegate.History
38
+ * @memberof ServiceDelegate.History
39
+ */
40
+ class Contact extends History {
41
+ /** Create a new instance of an Eve Door _History_ service delegate.
42
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
43
+ * corresponding HomeKit accessory.
44
+ * @param {!object} params - The parameters for the
45
+ * _History_ HomeKit service.
46
+ * @param {!CharacteristicDelegate} contactDelegate - A reference to the
47
+ * delegate of the associated `Characteristics.hap.ContactSensorState`
48
+ * characteristic.
49
+ * @param {!CharacteristicDelegate} timesOpenedDelegate - A reference to the
50
+ * delegate of the associated `Characteristics.eve.TimesOpened`
51
+ * characteristic.
52
+ * @param {!CharacteristicDelegate} lastActivationDelegate - A reference to the
53
+ * delegate of the associated `Characteristics.eve.LastActivation`
54
+ * characteristic.
55
+ */
56
+ constructor (
57
+ accessoryDelegate, params,
58
+ contactDelegate, timesOpenedDelegate, lastActivationDelegate
59
+ ) {
60
+ super(accessoryDelegate, params)
61
+ if (!(contactDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
62
+ throw new TypeError('contactDelegate: not a CharacteristicDelegate')
63
+ }
64
+ if (!(timesOpenedDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
65
+ throw new TypeError('timesOpenedDelegate: not a CharacteristicDelegate')
66
+ }
67
+ if (!(lastActivationDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
68
+ throw new TypeError('lastActivationDelegate: not a CharacteristicDelegate')
69
+ }
70
+ this._entry = { time: 0, status: contactDelegate.value }
71
+ contactDelegate.on('didSet', (value) => {
72
+ const now = Math.round(new Date().valueOf() / 1000)
73
+ timesOpenedDelegate.value += value
74
+ lastActivationDelegate.value = now - this._h.initialTime
75
+ this._entry.status = value
76
+ this._addEntry(now)
77
+ })
78
+ this.addCharacteristicDelegate({
79
+ key: 'resetTotal',
80
+ Characteristic: this.Characteristics.eve.ResetTotal,
81
+ value: params.resetTotal
82
+ })
83
+ this._characteristicDelegates.resetTotal.on('didSet', (value) => {
84
+ timesOpenedDelegate.value = 0
85
+ })
86
+ }
87
+
88
+ get _fingerPrint () { return '01 0601' }
89
+
90
+ _entryStream (entry) {
91
+ return util.format(
92
+ '|0b %s %s 01 %s',
93
+ numToHex(swap32(this._h.currentEntry), 8),
94
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
95
+ numToHex(entry.status, 2)
96
+ )
97
+ }
98
+ }
99
+
100
+ module.exports = Contact