homebridge-lib 5.1.24-5 → 5.2.2

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.
@@ -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,122 @@
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
+ // Eve v5.4.2 fills in missing temperature at 0.00°C.
79
+ // if (this._entry.temp != null) {
80
+ // const temp = this._entry.temp
81
+ // this._entry.temp = null
82
+ // this._addEntry(now)
83
+ // this._entry.temp = temp
84
+ // } else {
85
+ this._addEntry(now)
86
+ // }
87
+ })
88
+ if (temperatureDelegate != null) {
89
+ this._entry.temp = temperatureDelegate.value
90
+ temperatureDelegate.on('didSet', (value) => {
91
+ this._entry.temp = value
92
+ })
93
+ }
94
+ }
95
+
96
+ get _fingerPrint () {
97
+ if (this._entry.temp != null) {
98
+ return '02 1c01 0102'
99
+ }
100
+ return '01 1c01'
101
+ }
102
+
103
+ _entryStream (entry) {
104
+ if (this._entry.temp != null) {
105
+ return util.format(
106
+ '|0d %s %s 03 %s %s',
107
+ numToHex(swap32(this._h.currentEntry), 8),
108
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
109
+ numToHex(entry.status, 2),
110
+ numToHex(swap16(entry.temp * 100), 4)
111
+ )
112
+ }
113
+ return util.format(
114
+ '|0b %s %s 01 %s',
115
+ numToHex(swap32(this._h.currentEntry), 8),
116
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
117
+ numToHex(entry.status, 2)
118
+ )
119
+ }
120
+ }
121
+
122
+ 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
@@ -0,0 +1,121 @@
1
+ // homebridge-lib/lib/ServiceDelegate/History/Power.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
+ * `resetTotal` | `Characteristics.eve.ResetTotal`
32
+ *
33
+ * This delegate is for sensors that don't report life-time consumption. The
34
+ * history from the value of the associated
35
+ * `Characteristics.eve.CurrentConsumption` over time. It updates the value of
36
+ * the associated `Characteristics.eve.TotalConsumption` characteristic.
37
+ * @extends ServiceDelegate.History
38
+ * @memberof ServiceDelegate.History
39
+ */
40
+ class Power extends History {
41
+ /** Create a new instance of an Eve Energy _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} powerDelegate - A reference to the
47
+ * delegate of the associated `Characteristics.eve.CurrentConsumption`
48
+ * characteristic.
49
+ * @param {!CharacteristicDelegate} consumptionDelegate - A reference to the
50
+ * delegate of the associated `Characteristics.eve.TotalConsumption`
51
+ * characteristic.
52
+ */
53
+ constructor (
54
+ accessoryDelegate, params,
55
+ powerDelegate, consumptionDelegate
56
+ ) {
57
+ super(accessoryDelegate, params)
58
+ if (!(powerDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
59
+ throw new TypeError('powerDelegate: not a CharacteristicDelegate')
60
+ }
61
+ if (!(consumptionDelegate instanceof homebridgeLib.CharacteristicDelegate)) {
62
+ throw new TypeError('consumptionDelegate: not a CharacteristicDelegate')
63
+ }
64
+ this._powerDelegate = powerDelegate
65
+ this._consumptionDelegate = consumptionDelegate
66
+ this._entry = { time: 0, power: 0 }
67
+ this._runningConsumption = 0 // 10-min-interval running value
68
+ this._totalConsumption = consumptionDelegate.value // life-time value
69
+ powerDelegate.on('didSet', (value) => {
70
+ const now = Math.round(new Date().valueOf() / 1000)
71
+ if (this._time != null) {
72
+ const delta = this._power * (now - this._time) // Ws
73
+ this._runningConsumption += Math.round(delta / 600.0) // W * 10 min
74
+ this._totalConsumption += Math.round(delta / 3600.0) // Wh
75
+ }
76
+ this._power = value
77
+ this._time = now
78
+ })
79
+ this.addCharacteristicDelegate({
80
+ key: 'resetTotal',
81
+ Characteristic: this.Characteristics.eve.ResetTotal,
82
+ value: params.resetTotal
83
+ })
84
+ this._characteristicDelegates.resetTotal.on('didSet', (value) => {
85
+ this._runningConsumption = 0
86
+ this._totalConsumption = 0
87
+ this._consumptionDelegate.value = this._totalConsumption
88
+ })
89
+ }
90
+
91
+ _addEntry () {
92
+ // Sensor delivers currentConsumption, compute totalConsumption
93
+ const now = Math.round(new Date().valueOf() / 1000)
94
+ if (this._time != null) {
95
+ const delta = this._power * (now - this._time) // Ws
96
+ this._runningConsumption += Math.round(delta / 600.0) // W * 10 min
97
+ this._totalConsumption += Math.round(delta / 3600.0) // Wh
98
+ this._consumptionDelegate.value = this._totalConsumption
99
+ this._entry.power = this._runningConsumption
100
+ super._addEntry(now)
101
+ }
102
+ this._power = this._powerDelegate.value
103
+ this._time = now
104
+ this._runningConsumption = 0
105
+ }
106
+
107
+ // get _fingerPrint () { return '04 0102 0202 0702 0f03' }
108
+ get _fingerPrint () { return '01 0702' }
109
+
110
+ _entryStream (entry) {
111
+ return util.format(
112
+ // '|14 %s %s 1f 0000 0000 %s 0000 0000',
113
+ '|0c %s %s 01 %s',
114
+ numToHex(swap32(this._h.currentEntry), 8),
115
+ numToHex(swap32(entry.time - this._h.initialTime), 8),
116
+ numToHex(swap16(entry.power * 10), 4)
117
+ )
118
+ }
119
+ }
120
+
121
+ module.exports = Power