homebridge-lib 5.2.0 → 5.2.1

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.
@@ -285,6 +285,11 @@ class EveHomeKitTypes extends homebridgeLib.CustomHomeKitTypes {
285
285
  perms: [this.Perms.READ, this.Perms.NOTIFY, this.Perms.WRITE],
286
286
  adminOnlyAccess: [this.Access.WRITE]
287
287
  })
288
+ this.Characteristics.Duration.VALID_VALUES = [
289
+ 5, 10, 20, 30,
290
+ 1 * 60, 2 * 60, 3 * 60, 5 * 60, 10 * 60, 20 * 60, 30 * 60,
291
+ 1 * 3600, 2 * 3600, 3 * 3600, 5 * 3600, 10 * 3600, 12 * 3600, 15 * 3600
292
+ ]
288
293
 
289
294
  this.createCharacteristicClass('ValvePosition', uuid('12E'), {
290
295
  format: this.Formats.UINT8,
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
@@ -0,0 +1,121 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/Motion.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 Motion _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.MotionDetected` characteristic. It updates the
35
+ * value of the associated `Characteristics.eve.LastActivation` characteristic.
36
+ * @extends ServiceDelegate.History
37
+ * @memberof ServiceDelegate.History
38
+ */
39
+ class Motion extends History {
40
+ /** Create a new instance of an Eve Motion _History_ service delegate.
41
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
42
+ * corresponding HomeKit accessory.
43
+ * @param {!object} params - The parameters for the
44
+ * _History_ HomeKit service.
45
+ * @param {!CharacteristicDelegate} motionDelegate - A reference to the
46
+ * delegate of the associated `Characteristics.hap.MotionDetected`
47
+ * characteristic.
48
+ * @param {!CharacteristicDelegate} lastActivationDelegate - A reference to the
49
+ * delegate of the associated `Characteristics.eve.LastActivation`
50
+ * characteristic.
51
+ * @param {?CharacteristicDelegate} temperatureDelegate - A reference to the
52
+ * delegate of the associated `Characteristics.hap.CurrentTemperature`
53
+ * characteristic. For PIR sensors (like the Hue motion sensor) that report
54
+ * temperature in addition to motion.
55
+ */
56
+ constructor (
57
+ accessoryDelegate, params,
58
+ motionDelegate, lastActivationDelegate, temperatureDelegate
59
+ ) {
60
+ super(accessoryDelegate, params)
61
+ if (!(motionDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
62
+ throw new TypeError('motionDelegate: not a CharacteristicDelegate')
63
+ }
64
+ if (!(lastActivationDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
65
+ throw new TypeError('lastActivationDelegate: not a CharacteristicDelegate')
66
+ }
67
+ if (
68
+ temperatureDelegate != null &&
69
+ !(temperatureDelegate instanceof homebridgeLib.CharacteristicDelegate)
70
+ ) {
71
+ throw new TypeError('temperatureDelegate: not a CharacteristicDelegate')
72
+ }
73
+ this._entry = { time: 0, status: motionDelegate.value }
74
+ motionDelegate.on('didSet', (value) => {
75
+ const now = Math.round(new Date().valueOf() / 1000)
76
+ lastActivationDelegate.value = now - this._h.initialTime
77
+ this._entry.status = value
78
+ if (this._entry.temp != null) {
79
+ const temp = this._entry.temp
80
+ this._entry.temp = null
81
+ this._addEntry(now)
82
+ this._entry.temp = temp
83
+ } else {
84
+ this._addEntry(now)
85
+ }
86
+ })
87
+ if (temperatureDelegate != null) {
88
+ this._entry.temp = temperatureDelegate.value
89
+ temperatureDelegate.on('didSet', (value) => {
90
+ this._entry.temp = value
91
+ })
92
+ }
93
+ }
94
+
95
+ get _fingerPrint () {
96
+ if (this._entry.temp != null) {
97
+ return '02 1c01 0102'
98
+ }
99
+ return '01 1c01'
100
+ }
101
+
102
+ _entryStream (entry) {
103
+ if (this._entry.temp != null) {
104
+ return util.format(
105
+ '|0d %s %s 03 %s %s',
106
+ numToHex(swap32(this._h.currentEntry), 8),
107
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
108
+ numToHex(entry.status, 2),
109
+ numToHex(swap16(entry.temp * 100), 4)
110
+ )
111
+ }
112
+ return util.format(
113
+ '|0b %s %s 01 %s',
114
+ numToHex(swap32(this._h.currentEntry), 8),
115
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
116
+ numToHex(entry.status, 2)
117
+ )
118
+ }
119
+ }
120
+
121
+ module.exports = Motion
@@ -0,0 +1,86 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/On.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 a Raspberry Pi _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 creates the history from the associated
33
+ * `Characteristics.hap.On` characteristic.
34
+ * @extends ServiceDelegate.History
35
+ * @memberof ServiceDelegate.History
36
+ */
37
+ class On extends History {
38
+ /** Create a new instance of a Raspberry Pi _History_ service delegate.
39
+ * @param {!AccessoryDelegate} accessoryDelegate - The delegate of the
40
+ * corresponding HomeKit accessory.
41
+ * @param {!object} params - The parameters for the
42
+ * _History_ HomeKit service.
43
+ * @param {!CharacteristicDelegate} onDelegate - A reference to the
44
+ * delegate of the associated `Characteristics.hap.On`
45
+ * characteristic.
46
+ */
47
+ constructor (accessoryDelegate, params, onDelegate, temperatureDelegate) {
48
+ super(accessoryDelegate, params)
49
+ if (!(onDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
50
+ throw new TypeError('onDelegate: not a CharacteristicDelegate')
51
+ }
52
+ if (
53
+ temperatureDelegate != null &&
54
+ !(temperatureDelegate instanceof homebridgeLib.CharacteristicDelegate)
55
+ ) {
56
+ throw new TypeError('temperatureDelegate: not a CharacteristicDelegate')
57
+ }
58
+ this._entry = {
59
+ time: 0,
60
+ on: onDelegate.value ? 1 : 0
61
+ }
62
+ onDelegate.on('didSet', (value) => {
63
+ this._entry.on = value ? 1 : 0
64
+ this._addEntry()
65
+ })
66
+ if (temperatureDelegate != null) {
67
+ this._entry.temp = temperatureDelegate.value
68
+ temperatureDelegate.on('didSet', (value) => {
69
+ this._entry.temp = value
70
+ })
71
+ }
72
+ }
73
+
74
+ get _fingerPrint () { return '01 0e01' }
75
+
76
+ _entryStream (entry) {
77
+ return util.format(
78
+ '|0b %s %s 01 %s',
79
+ numToHex(swap32(this._h.currentEntry), 8),
80
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
81
+ numToHex(entry.on, 2)
82
+ )
83
+ }
84
+ }
85
+
86
+ module.exports = On