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.
- package/lib/EveHomeKitTypes.js +41 -12
- package/lib/Platform.js +0 -1
- package/lib/ServiceDelegate/AccessoryInformation.js +111 -0
- package/lib/ServiceDelegate/Battery.js +87 -0
- package/lib/ServiceDelegate/Dummy.js +44 -0
- package/lib/ServiceDelegate/History/Consumption.js +114 -0
- package/lib/ServiceDelegate/History/Contact.js +100 -0
- package/lib/ServiceDelegate/History/Motion.js +118 -0
- package/lib/ServiceDelegate/History/On.js +86 -0
- package/lib/ServiceDelegate/History/Power.js +121 -0
- package/lib/ServiceDelegate/History/Room.js +115 -0
- package/lib/ServiceDelegate/History/Thermo.js +102 -0
- package/lib/ServiceDelegate/History/Weather.js +127 -0
- package/lib/ServiceDelegate/History/index.js +344 -0
- package/lib/ServiceDelegate/ServiceLabel.js +47 -0
- package/lib/ServiceDelegate/index.js +332 -0
- package/package.json +2 -2
- package/lib/ServiceDelegate.js +0 -1447
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
// homebridge-lib/lib/ServiceDelegate/index.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
|
+
/** Delegate of a HomeKit service.
|
|
11
|
+
*
|
|
12
|
+
* This delegate sets up a HomeKit service with the following HomeKit
|
|
13
|
+
* characteristic:
|
|
14
|
+
*
|
|
15
|
+
* key | Characteristic
|
|
16
|
+
* ---------------- | -----------------------------------
|
|
17
|
+
* `name` | `Characteristics.hap.Name`
|
|
18
|
+
* `configuredName` | `Characteristics.hap.ConfiguredName`
|
|
19
|
+
* @abstract
|
|
20
|
+
* @extends Delegate
|
|
21
|
+
*/
|
|
22
|
+
class ServiceDelegate extends homebridgeLib.Delegate {
|
|
23
|
+
static get AccessoryInformation () { return require('./AccessoryInformation') }
|
|
24
|
+
static get Battery () { return require('./Battery') }
|
|
25
|
+
static get Dummy () { return require('./Dummy') }
|
|
26
|
+
static get History () { return require('./History') }
|
|
27
|
+
static get ServiceLabel () { return require('./ServiceLabel') }
|
|
28
|
+
|
|
29
|
+
/** Create a new instance of a HomeKit service delegate.
|
|
30
|
+
*
|
|
31
|
+
* When the associated HomeKit service was restored from persistent
|
|
32
|
+
* storage, it is linked to the new delegate. Otherwise a new HomeKit
|
|
33
|
+
* service will be created, using the values from `params`.
|
|
34
|
+
* @param {!AccessoryDelegate} accessoryDelegate - Reference to the
|
|
35
|
+
* associated HomeKit accessory delegate.
|
|
36
|
+
* @param {!object} params - Properties of the HomeKit service.<br>
|
|
37
|
+
* Next to the fixed properties below, `params` also contains the value for
|
|
38
|
+
* each key specified in {@link ServiceDelegate#characteristics characteristics}.
|
|
39
|
+
* @param {!string} params.name - The (Siri) name of the service.
|
|
40
|
+
* Also used to prefix log and error messages.
|
|
41
|
+
* @param {!Service} params.Service - The type of the HomeKit service.
|
|
42
|
+
* @param {?string} params.subtype - The subtype of the HomeKit service.
|
|
43
|
+
* Needs to be specified when the accessory has multuple services of the
|
|
44
|
+
* same type.
|
|
45
|
+
* @params {?boolean} params.primaryService - This is the primary service
|
|
46
|
+
* for the accessory.
|
|
47
|
+
* @params {?ServiceDelegate} params.linkedServiceDelegate - The delegate
|
|
48
|
+
* of the service this service links to.
|
|
49
|
+
* @params {?boolean} params.hidden - Hidden service.
|
|
50
|
+
* @params {?boolean} params.exposeConfiguredName - Expose
|
|
51
|
+
* `Characteristics.hap.ConfiguredName`, so device name can be synced with
|
|
52
|
+
* HomeKit service name.
|
|
53
|
+
*/
|
|
54
|
+
constructor (accessoryDelegate, params = {}) {
|
|
55
|
+
if (!(accessoryDelegate instanceof homebridgeLib.AccessoryDelegate)) {
|
|
56
|
+
throw new TypeError('parent: not a AccessoryDelegate')
|
|
57
|
+
}
|
|
58
|
+
if (params.name == null) {
|
|
59
|
+
throw new SyntaxError('params.name: missing')
|
|
60
|
+
}
|
|
61
|
+
super(accessoryDelegate.platform, params.name)
|
|
62
|
+
if (
|
|
63
|
+
typeof params.Service !== 'function' ||
|
|
64
|
+
typeof params.Service.UUID !== 'string'
|
|
65
|
+
) {
|
|
66
|
+
throw new TypeError('params.Service: not a Service')
|
|
67
|
+
}
|
|
68
|
+
this._accessoryDelegate = accessoryDelegate
|
|
69
|
+
this._accessory = this._accessoryDelegate._accessory
|
|
70
|
+
this._key = params.Service.UUID
|
|
71
|
+
if (params.subtype != null) {
|
|
72
|
+
this._key += '.' + params.subtype
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Get or create associated Service.
|
|
76
|
+
this._service = params.subtype == null
|
|
77
|
+
? this._accessory.getService(params.Service)
|
|
78
|
+
: this._accessory.getServiceByUUIDAndSubType(params.Service, params.subtype)
|
|
79
|
+
if (this._service == null) {
|
|
80
|
+
this._service = this._accessory.addService(
|
|
81
|
+
new params.Service(this.name, params.subtype)
|
|
82
|
+
)
|
|
83
|
+
this._service.displayName = this.name
|
|
84
|
+
}
|
|
85
|
+
this._accessoryDelegate._linkServiceDelegate(this)
|
|
86
|
+
this._service.setPrimaryService(!!params.primaryService)
|
|
87
|
+
if (params.linkedServiceDelegate != null) {
|
|
88
|
+
params.linkedServiceDelegate._service.addLinkedService(this._service)
|
|
89
|
+
}
|
|
90
|
+
this._service.setHiddenService(!!params.hidden)
|
|
91
|
+
|
|
92
|
+
// Fix bug in homebridge-lib@<4.3.0 that caused the service context in
|
|
93
|
+
// ~/.homebridge/accessories/cachedAccessories to be stored under the
|
|
94
|
+
// wrong key.
|
|
95
|
+
if (
|
|
96
|
+
params.subtype == null &&
|
|
97
|
+
this._accessory.context[this._key + '.'] != null
|
|
98
|
+
) {
|
|
99
|
+
this._accessory.context[this._key] =
|
|
100
|
+
this._accessory.context[this._key + '.']
|
|
101
|
+
delete this._accessory.context[this._key + '.']
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Setup persisted storage in ~/.homebridge/accessories/cachedAccessories.
|
|
105
|
+
if (this._accessory.context[this._key] == null) {
|
|
106
|
+
this._accessory.context[this._key] = {}
|
|
107
|
+
}
|
|
108
|
+
this._context = this._accessory.context[this._key]
|
|
109
|
+
|
|
110
|
+
// Setup shortcut for characteristic values.
|
|
111
|
+
this._values = {} // by key
|
|
112
|
+
|
|
113
|
+
// Setup characteristics
|
|
114
|
+
this._characteristicDelegates = {} // by key
|
|
115
|
+
this._characteristics = {} // by uuid
|
|
116
|
+
|
|
117
|
+
if (!params.hidden) {
|
|
118
|
+
this.addCharacteristicDelegate({
|
|
119
|
+
key: 'name',
|
|
120
|
+
Characteristic: this.Characteristics.hap.Name,
|
|
121
|
+
silent: true,
|
|
122
|
+
value: params.name
|
|
123
|
+
})
|
|
124
|
+
if (params.exposeConfiguredName) {
|
|
125
|
+
this.addCharacteristicDelegate({
|
|
126
|
+
key: 'configuredName',
|
|
127
|
+
Characteristic: this.Characteristics.hap.ConfiguredName,
|
|
128
|
+
// silent: true,
|
|
129
|
+
props: {
|
|
130
|
+
perms: [
|
|
131
|
+
this.Characteristic.Perms.READ, this.Characteristic.Perms.WRITE
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
value: params.name
|
|
135
|
+
}).on('didSet', (value) => {
|
|
136
|
+
if (value.trim() !== '') {
|
|
137
|
+
this.name = value
|
|
138
|
+
this._service.displayName = value
|
|
139
|
+
this.values.name = value
|
|
140
|
+
for (const key in this._characteristicDelegates) {
|
|
141
|
+
this._characteristicDelegates[key].name = value
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.once('initialised', () => {
|
|
149
|
+
const staleCharacteristics = []
|
|
150
|
+
for (const uuid in this._service.characteristics) {
|
|
151
|
+
const characteristic = this._service.characteristics[uuid]
|
|
152
|
+
if (this._characteristics[characteristic.UUID] == null) {
|
|
153
|
+
staleCharacteristics.push(characteristic)
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
for (const characteristic of staleCharacteristics) {
|
|
157
|
+
this.log('remove stale characteristic %s', characteristic.displayName)
|
|
158
|
+
this._service.removeCharacteristic(characteristic)
|
|
159
|
+
}
|
|
160
|
+
const staleKeys = []
|
|
161
|
+
for (const key in this._context) {
|
|
162
|
+
if (key !== 'context' && this._characteristicDelegates[key] == null) {
|
|
163
|
+
staleKeys.push(key)
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
for (const key of staleKeys) {
|
|
167
|
+
this.log('remove stale value %s', key)
|
|
168
|
+
delete this._context[key]
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Destroy the HomeKit service delegate.
|
|
174
|
+
*
|
|
175
|
+
* Destroys the associated HomeKit characteristic delegates.
|
|
176
|
+
* Removes the associated HomeKit service.
|
|
177
|
+
*/
|
|
178
|
+
destroy () {
|
|
179
|
+
this.debug('destroy %s (%s)', this._key, this._service.displayName)
|
|
180
|
+
this._accessoryDelegate._unlinkServiceDelegate(this)
|
|
181
|
+
this.removeAllListeners()
|
|
182
|
+
for (const key in this._characteristicDelegates) {
|
|
183
|
+
this._characteristicDelegates[key]._destroy()
|
|
184
|
+
}
|
|
185
|
+
this._accessory.removeService(this._service)
|
|
186
|
+
delete this._accessory.context[this._key]
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Add a HomeKit characteristic delegate to the HomeKit service delegate.
|
|
190
|
+
*
|
|
191
|
+
* The characteristic delegate manages a value that:
|
|
192
|
+
* - Is persisted across homebridge restarts;
|
|
193
|
+
* - Can be monitored through homebridge's log output;
|
|
194
|
+
* - Can be monitored programmatially through `didSet` events; and
|
|
195
|
+
* - Mirrors the value of the optionally associated HomeKit characteristic.
|
|
196
|
+
*
|
|
197
|
+
* This value is accessed through {@link ServiceDelegate#values values}.
|
|
198
|
+
* The delegate is returned, but can also be accessed through
|
|
199
|
+
* {@link ServiceDelegate#characteristicDelegate characteristicDelegate()}.
|
|
200
|
+
*
|
|
201
|
+
* When the associated HomeKit characteristic was restored from persistent
|
|
202
|
+
* storage, it is linked to the new delegate. Otherwise a new HomeKit
|
|
203
|
+
* charactertistic will be created, using the values from `params`.
|
|
204
|
+
* @param {!object} params - Properties of the HomeKit characteristic.
|
|
205
|
+
* @param {!string} params.key - The key for the characteristic.
|
|
206
|
+
* @param {?*} params.value - The initial value when the characteristic
|
|
207
|
+
* is added.
|
|
208
|
+
* @param {?boolean} params.silent - Suppress set log messages.
|
|
209
|
+
* @param {?Characteristic} params.Characteristic - The type of the
|
|
210
|
+
* characteristic, from {@link Delegate#Characteristic Characteristic}.
|
|
211
|
+
* @param {?object} params.props - The properties of the HomeKit
|
|
212
|
+
* characteristic.<br>
|
|
213
|
+
* Overrides the properties from the characteristic type.
|
|
214
|
+
* @param {?string} params.unit - The unit of the value of the HomeKit
|
|
215
|
+
* characteristic.<br>
|
|
216
|
+
* Overrides the unit from the characteristic type.
|
|
217
|
+
* @param {?function} params.getter - Asynchronous function to be invoked
|
|
218
|
+
* when HomeKit reads the characteristic value.<br>
|
|
219
|
+
* This must be an `async` function returning a `Promise` to the new
|
|
220
|
+
* characteristic value.
|
|
221
|
+
* @param {?function} params.setter - Asynchronous function to be invoked
|
|
222
|
+
* when HomeKit writes the characteristic value.<br>
|
|
223
|
+
* This must be an `async` function returning a `Promise` that resolves
|
|
224
|
+
* when the corresonding device value has been updated.
|
|
225
|
+
* @returns {CharacteristicDelegate}
|
|
226
|
+
* @throws {TypeError} When a parameter has an invalid type.
|
|
227
|
+
* @throws {RangeError} When a parameter has an invalid value.
|
|
228
|
+
* @throws {SyntaxError} When a mandatory parameter is missing or an
|
|
229
|
+
* optional parameter is not applicable.
|
|
230
|
+
*/
|
|
231
|
+
addCharacteristicDelegate (params = {}) {
|
|
232
|
+
if (typeof params.key !== 'string') {
|
|
233
|
+
throw new TypeError(`params.key: ${params.key}: invalid key`)
|
|
234
|
+
}
|
|
235
|
+
if (params.key === '') {
|
|
236
|
+
throw new RangeError(`params.key: ${params.key}: invalid key`)
|
|
237
|
+
}
|
|
238
|
+
const key = params.key
|
|
239
|
+
if (this.values[key] !== undefined) {
|
|
240
|
+
throw new SyntaxError(`${key}: duplicate key`)
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const characteristicDelegate = new homebridgeLib.CharacteristicDelegate(
|
|
244
|
+
this, params
|
|
245
|
+
)
|
|
246
|
+
this._characteristicDelegates[key] = characteristicDelegate
|
|
247
|
+
if (params.Characteristic != null) {
|
|
248
|
+
this._characteristics[params.Characteristic.UUID] = true
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Create shortcut for characteristic value.
|
|
252
|
+
Object.defineProperty(this.values, key, {
|
|
253
|
+
configurable: true, // make sure we can delete it again
|
|
254
|
+
writeable: true,
|
|
255
|
+
get () { return characteristicDelegate.value },
|
|
256
|
+
set (value) { characteristicDelegate.value = value }
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
return characteristicDelegate
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
removeCharacteristicDelegate (key) {
|
|
263
|
+
delete this.values[key]
|
|
264
|
+
const characteristicDelegate = this._characteristicDelegates[key]
|
|
265
|
+
if (characteristicDelegate._characteristic != null) {
|
|
266
|
+
const characteristic = characteristicDelegate._characteristic
|
|
267
|
+
delete this._characteristics[characteristic.UUID]
|
|
268
|
+
}
|
|
269
|
+
characteristicDelegate._destroy()
|
|
270
|
+
delete this._characteristicDelegates[key]
|
|
271
|
+
delete this._context[key]
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Values of the HomeKit characteristics for the HomeKit service.
|
|
275
|
+
*
|
|
276
|
+
* Contains the key of each characteristic added by
|
|
277
|
+
* {@link ServiceDelegate#addCharacteristic addCharacteristic}.
|
|
278
|
+
* When the value is written, the value of the corresponding HomeKit
|
|
279
|
+
* characteristic is updated; when the characteristic value is changed from
|
|
280
|
+
* HomeKit, this value is updated.
|
|
281
|
+
* @type {object}
|
|
282
|
+
*/
|
|
283
|
+
get values () {
|
|
284
|
+
return this._values
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Returns the HomeKit characteristic delegate correspondig to the key.
|
|
288
|
+
* @param {!string} key - The key for the characteristic.
|
|
289
|
+
* returns {CharacteristicDelegate}
|
|
290
|
+
*/
|
|
291
|
+
characteristicDelegate (key) {
|
|
292
|
+
return this._characteristicDelegates[key]
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** The corrresponding HomeKit accessory delegate.
|
|
296
|
+
* @type {AccessoryDelegate}
|
|
297
|
+
*/
|
|
298
|
+
get accessoryDelegate () {
|
|
299
|
+
return this._accessoryDelegate
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** Service context to be persisted across Homebridge restarts.
|
|
303
|
+
* @type {object}
|
|
304
|
+
*/
|
|
305
|
+
get context () {
|
|
306
|
+
if (this._context.context == null) {
|
|
307
|
+
this._context.context = {}
|
|
308
|
+
}
|
|
309
|
+
return this._context.context
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** Current log level (of the associated accessory delegate).
|
|
313
|
+
*
|
|
314
|
+
* The log level determines what type of messages are printed:
|
|
315
|
+
*
|
|
316
|
+
* 0. Print error and warning messages.
|
|
317
|
+
* 1. Print error, warning, and log messages.
|
|
318
|
+
* 2. Print error, warning, log, and debug messages.
|
|
319
|
+
* 3. Print error, warning, log, debug, and verbose debug messages.
|
|
320
|
+
*
|
|
321
|
+
* Note that debug messages (level 2 and 3) are only printed when
|
|
322
|
+
* Homebridge was started with the `-D` or `--debug` command line option.
|
|
323
|
+
*
|
|
324
|
+
* @type {!integer}
|
|
325
|
+
* @readonly
|
|
326
|
+
*/
|
|
327
|
+
get logLevel () {
|
|
328
|
+
return this._accessoryDelegate.logLevel
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
module.exports = ServiceDelegate
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Library for homebridge plugins",
|
|
4
4
|
"author": "Erik Baauw",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"version": "5.2.
|
|
6
|
+
"version": "5.2.3",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"homekit",
|
|
9
9
|
"homebridge"
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
24
24
|
"homebridge": "^1.4.0",
|
|
25
|
-
"node": "^16.
|
|
25
|
+
"node": "^16.14.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"bonjour-hap": "^3.6.3",
|