@switchbot/homebridge-switchbot 5.0.0-beta.2 → 5.0.0-beta.21
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/CHANGELOG.md +7 -0
- package/config.schema.json +17 -2
- package/dist/devices-hap/device.d.ts +1 -0
- package/dist/devices-hap/device.d.ts.map +1 -1
- package/dist/devices-hap/device.js +70 -30
- package/dist/devices-hap/device.js.map +1 -1
- package/dist/devices-matter/BaseMatterAccessory.d.ts +23 -0
- package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
- package/dist/devices-matter/BaseMatterAccessory.js +167 -5
- package/dist/devices-matter/BaseMatterAccessory.js.map +1 -1
- package/dist/devices-matter/ColorLightAccessory.d.ts.map +1 -1
- package/dist/devices-matter/ColorLightAccessory.js +12 -12
- package/dist/devices-matter/ColorLightAccessory.js.map +1 -1
- package/dist/devices-matter/ColorTemperatureLightAccessory.d.ts.map +1 -1
- package/dist/devices-matter/ColorTemperatureLightAccessory.js +5 -7
- package/dist/devices-matter/ColorTemperatureLightAccessory.js.map +1 -1
- package/dist/devices-matter/DimmableLightAccessory.js +9 -9
- package/dist/devices-matter/DimmableLightAccessory.js.map +1 -1
- package/dist/devices-matter/ExtendedColorLightAccessory.d.ts.map +1 -1
- package/dist/devices-matter/ExtendedColorLightAccessory.js +14 -15
- package/dist/devices-matter/ExtendedColorLightAccessory.js.map +1 -1
- package/dist/devices-matter/OnOffLightAccessory.d.ts.map +1 -1
- package/dist/devices-matter/OnOffLightAccessory.js +8 -16
- package/dist/devices-matter/OnOffLightAccessory.js.map +1 -1
- package/dist/devices-matter/OnOffOutletAccessory.d.ts +2 -0
- package/dist/devices-matter/OnOffOutletAccessory.d.ts.map +1 -1
- package/dist/devices-matter/OnOffOutletAccessory.js +10 -7
- package/dist/devices-matter/OnOffOutletAccessory.js.map +1 -1
- package/dist/devices-matter/OnOffSwitchAccessory.js +2 -2
- package/dist/devices-matter/OnOffSwitchAccessory.js.map +1 -1
- package/dist/devices-matter/baseMatterAccessory.test.d.ts +2 -0
- package/dist/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
- package/dist/devices-matter/baseMatterAccessory.test.js +71 -0
- package/dist/devices-matter/baseMatterAccessory.test.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -5
- package/dist/index.js.map +1 -1
- package/dist/index.test.js +7 -2
- package/dist/index.test.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +11 -10
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +76 -35
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/platform-hap.d.ts +10 -14
- package/dist/platform-hap.d.ts.map +1 -1
- package/dist/platform-hap.js +38 -64
- package/dist/platform-hap.js.map +1 -1
- package/dist/platform-matter.cleanup.test.d.ts +2 -0
- package/dist/platform-matter.cleanup.test.d.ts.map +1 -0
- package/dist/platform-matter.cleanup.test.js +85 -0
- package/dist/platform-matter.cleanup.test.js.map +1 -0
- package/dist/platform-matter.d.ts +68 -6
- package/dist/platform-matter.d.ts.map +1 -1
- package/dist/platform-matter.js +1250 -70
- package/dist/platform-matter.js.map +1 -1
- package/dist/platform-matter.mapping.test.d.ts +2 -0
- package/dist/platform-matter.mapping.test.d.ts.map +1 -0
- package/dist/platform-matter.mapping.test.js +50 -0
- package/dist/platform-matter.mapping.test.js.map +1 -0
- package/dist/platform-matter.test.d.ts +2 -0
- package/dist/platform-matter.test.d.ts.map +1 -0
- package/dist/platform-matter.test.js +127 -0
- package/dist/platform-matter.test.js.map +1 -0
- package/dist/platform-matter.unregister.test.d.ts +2 -0
- package/dist/platform-matter.unregister.test.d.ts.map +1 -0
- package/dist/platform-matter.unregister.test.js +37 -0
- package/dist/platform-matter.unregister.test.js.map +1 -0
- package/dist/settings.d.ts +1 -0
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +87 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +254 -0
- package/dist/utils.js.map +1 -1
- package/dist/utils.test.d.ts +2 -0
- package/dist/utils.test.d.ts.map +1 -0
- package/dist/utils.test.js +95 -0
- package/dist/utils.test.js.map +1 -0
- package/dist/verifyconfig.test.js +2 -2
- package/dist/verifyconfig.test.js.map +1 -1
- package/docs/assets/main.js +2 -2
- package/docs/index.html +2 -2
- package/docs/variables/default.html +1 -1
- package/package.json +14 -14
- package/src/devices-hap/device.ts +68 -30
- package/src/devices-matter/BaseMatterAccessory.ts +168 -5
- package/src/devices-matter/ColorLightAccessory.ts +12 -12
- package/src/devices-matter/ColorTemperatureLightAccessory.ts +5 -7
- package/src/devices-matter/DimmableLightAccessory.ts +9 -9
- package/src/devices-matter/ExtendedColorLightAccessory.ts +14 -15
- package/src/devices-matter/OnOffLightAccessory.ts +8 -16
- package/src/devices-matter/OnOffOutletAccessory.ts +12 -7
- package/src/devices-matter/OnOffSwitchAccessory.ts +2 -2
- package/src/devices-matter/baseMatterAccessory.test.ts +88 -0
- package/src/index.test.ts +7 -2
- package/src/index.ts +4 -5
- package/src/irdevice/irdevice.ts +74 -35
- package/src/platform-hap.ts +39 -73
- package/src/platform-matter.cleanup.test.ts +101 -0
- package/src/platform-matter.mapping.test.ts +60 -0
- package/src/platform-matter.test.ts +155 -0
- package/src/platform-matter.ts +1286 -73
- package/src/platform-matter.unregister.test.ts +48 -0
- package/src/settings.ts +4 -0
- package/src/utils.test.ts +96 -0
- package/src/utils.ts +255 -0
- package/src/verifyconfig.test.ts +11 -10
package/src/irdevice/irdevice.ts
CHANGED
|
@@ -276,69 +276,108 @@ export abstract class irdeviceBase {
|
|
|
276
276
|
/**
|
|
277
277
|
* Logging for Device
|
|
278
278
|
*/
|
|
279
|
-
|
|
280
|
-
if (
|
|
281
|
-
|
|
279
|
+
infoLog(...log: any[]): void {
|
|
280
|
+
if (!this.enablingDeviceLogging()) {
|
|
281
|
+
return
|
|
282
282
|
}
|
|
283
|
+
// Delegate to a single helper that prefers platform-provided loggers.
|
|
284
|
+
this.logWith('info', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
|
|
283
285
|
}
|
|
284
286
|
|
|
285
|
-
|
|
286
|
-
if (
|
|
287
|
-
|
|
287
|
+
successLog(...log: any[]): void {
|
|
288
|
+
if (!this.enablingDeviceLogging()) {
|
|
289
|
+
return
|
|
288
290
|
}
|
|
291
|
+
this.logWith('success', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
|
|
289
292
|
}
|
|
290
293
|
|
|
291
|
-
|
|
292
|
-
if (
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
294
|
+
debugSuccessLog(...log: any[]): void {
|
|
295
|
+
if (!this.enablingDeviceLogging()) {
|
|
296
|
+
return
|
|
297
|
+
}
|
|
298
|
+
if (!this.loggingIsDebug()) {
|
|
299
|
+
return
|
|
296
300
|
}
|
|
301
|
+
this.logWith('success', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugSuccessLog', String(...log))
|
|
297
302
|
}
|
|
298
303
|
|
|
299
|
-
|
|
300
|
-
if (
|
|
301
|
-
|
|
304
|
+
warnLog(...log: any[]): void {
|
|
305
|
+
if (!this.enablingDeviceLogging()) {
|
|
306
|
+
return
|
|
302
307
|
}
|
|
308
|
+
this.logWith('warn', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
|
|
303
309
|
}
|
|
304
310
|
|
|
305
|
-
|
|
306
|
-
if (
|
|
307
|
-
|
|
308
|
-
this.log.warn(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
|
|
309
|
-
}
|
|
311
|
+
debugWarnLog(...log: any[]): void {
|
|
312
|
+
if (!this.enablingDeviceLogging() || !this.loggingIsDebug()) {
|
|
313
|
+
return
|
|
310
314
|
}
|
|
315
|
+
this.logWith('warn', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugWarnLog', String(...log))
|
|
311
316
|
}
|
|
312
317
|
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
|
|
318
|
+
errorLog(...log: any[]): void {
|
|
319
|
+
if (!this.enablingDeviceLogging()) {
|
|
320
|
+
return
|
|
316
321
|
}
|
|
322
|
+
this.logWith('error', `${this.device.remoteType}: ${this.accessory.displayName}`, undefined, String(...log))
|
|
317
323
|
}
|
|
318
324
|
|
|
319
|
-
|
|
320
|
-
if (
|
|
321
|
-
|
|
322
|
-
this.log.error(`[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, String(...log))
|
|
323
|
-
}
|
|
325
|
+
debugErrorLog(...log: any[]): void {
|
|
326
|
+
if (!this.enablingDeviceLogging() || !this.loggingIsDebug()) {
|
|
327
|
+
return
|
|
324
328
|
}
|
|
329
|
+
this.logWith('error', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugErrorLog', String(...log))
|
|
325
330
|
}
|
|
326
331
|
|
|
327
|
-
|
|
328
|
-
if (
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
332
|
+
debugLog(...log: any[]): void {
|
|
333
|
+
if (!this.enablingDeviceLogging()) {
|
|
334
|
+
return
|
|
335
|
+
}
|
|
336
|
+
if (this.deviceLogging === 'debug') {
|
|
337
|
+
this.logWith('debug', `[DEBUG] ${this.device.remoteType}: ${this.accessory.displayName}`, 'debugLog', String(...log))
|
|
338
|
+
} else if (this.deviceLogging === 'debugMode') {
|
|
339
|
+
this.logWith('debug', `${this.device.remoteType}: ${this.accessory.displayName}`, 'debugLog', String(...log))
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Generic platform/local logger delegate
|
|
344
|
+
protected logWith(level: string, message: string, platformMethodName?: string, payload?: string): void {
|
|
345
|
+
const method = platformMethodName ?? `${level}Log`
|
|
346
|
+
const pFn = (this.platform as any)?.[method]
|
|
347
|
+
if (typeof pFn === 'function') {
|
|
348
|
+
try {
|
|
349
|
+
if (payload !== undefined) {
|
|
350
|
+
pFn(message, payload)
|
|
351
|
+
} else {
|
|
352
|
+
pFn(message)
|
|
353
|
+
}
|
|
354
|
+
return
|
|
355
|
+
} catch (_err) {
|
|
356
|
+
// fallthrough to local logger
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
const map: Record<string, string> = {
|
|
360
|
+
info: 'info',
|
|
361
|
+
success: 'success',
|
|
362
|
+
debug: 'debug',
|
|
363
|
+
warn: 'warn',
|
|
364
|
+
error: 'error',
|
|
365
|
+
}
|
|
366
|
+
const local = (this.log as any)[map[level] ?? level]
|
|
367
|
+
if (typeof local === 'function') {
|
|
368
|
+
if (payload !== undefined) {
|
|
369
|
+
local.call(this.log, message, payload)
|
|
370
|
+
} else {
|
|
371
|
+
local.call(this.log, message)
|
|
333
372
|
}
|
|
334
373
|
}
|
|
335
374
|
}
|
|
336
375
|
|
|
337
|
-
|
|
376
|
+
loggingIsDebug(): boolean {
|
|
338
377
|
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug'
|
|
339
378
|
}
|
|
340
379
|
|
|
341
|
-
|
|
380
|
+
enablingDeviceLogging(): boolean {
|
|
342
381
|
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard'
|
|
343
382
|
}
|
|
344
383
|
}
|
package/src/platform-hap.ts
CHANGED
|
@@ -55,7 +55,7 @@ import { TV } from './irdevice/tv.js'
|
|
|
55
55
|
import { VacuumCleaner } from './irdevice/vacuumcleaner.js'
|
|
56
56
|
import { WaterHeater } from './irdevice/waterheater.js'
|
|
57
57
|
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
|
|
58
|
-
import { cleanDeviceConfig, formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, safeStringify, sleep } from './utils.js'
|
|
58
|
+
import { cleanDeviceConfig, createPlatformLogger, formatDeviceIdAsMac, isBlindTiltDevice, isCurtainDevice, safeStringify, sleep } from './utils.js'
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* HomebridgePlatform
|
|
@@ -68,6 +68,18 @@ export class SwitchBotHAPPlatform implements DynamicPlatformPlugin {
|
|
|
68
68
|
public readonly api: API
|
|
69
69
|
public readonly log: Logging
|
|
70
70
|
|
|
71
|
+
// Logging helper functions (attached from utils.createPlatformLogger in constructor)
|
|
72
|
+
infoLog!: (...log: any[]) => Promise<void>
|
|
73
|
+
successLog!: (...log: any[]) => Promise<void>
|
|
74
|
+
debugSuccessLog!: (...log: any[]) => Promise<void>
|
|
75
|
+
warnLog!: (...log: any[]) => Promise<void>
|
|
76
|
+
debugWarnLog!: (...log: any[]) => Promise<void>
|
|
77
|
+
errorLog!: (...log: any[]) => Promise<void>
|
|
78
|
+
debugErrorLog!: (...log: any[]) => Promise<void>
|
|
79
|
+
debugLog!: (...log: any[]) => Promise<void>
|
|
80
|
+
loggingIsDebug!: () => Promise<boolean>
|
|
81
|
+
enablingPlatformLogging!: () => Promise<boolean>
|
|
82
|
+
|
|
71
83
|
// Configuration properties
|
|
72
84
|
platformConfig!: SwitchBotPlatformConfig
|
|
73
85
|
platformLogging!: options['logging']
|
|
@@ -104,9 +116,22 @@ export class SwitchBotHAPPlatform implements DynamicPlatformPlugin {
|
|
|
104
116
|
this.api = api
|
|
105
117
|
this.log = log
|
|
106
118
|
|
|
119
|
+
// Attach shared platform logging helpers (moved to utils for reuse)
|
|
120
|
+
const _pl = createPlatformLogger(async () => this.platformLogging, this.log)
|
|
121
|
+
this.infoLog = _pl.infoLog
|
|
122
|
+
this.successLog = _pl.successLog
|
|
123
|
+
this.debugSuccessLog = _pl.debugSuccessLog
|
|
124
|
+
this.warnLog = _pl.warnLog
|
|
125
|
+
this.debugWarnLog = _pl.debugWarnLog
|
|
126
|
+
this.errorLog = _pl.errorLog
|
|
127
|
+
this.debugErrorLog = _pl.debugErrorLog
|
|
128
|
+
this.debugLog = _pl.debugLog
|
|
129
|
+
this.loggingIsDebug = _pl.loggingIsDebug
|
|
130
|
+
this.enablingPlatformLogging = _pl.enablingPlatformLogging
|
|
131
|
+
|
|
107
132
|
// only load if configured
|
|
108
133
|
if (!config) {
|
|
109
|
-
this.
|
|
134
|
+
this.errorLog('No configuration found for the plugin, please check your config.')
|
|
110
135
|
return
|
|
111
136
|
}
|
|
112
137
|
|
|
@@ -205,7 +230,18 @@ export class SwitchBotHAPPlatform implements DynamicPlatformPlugin {
|
|
|
205
230
|
this.debugLog('Executed didFinishLaunching callback')
|
|
206
231
|
// run the method to discover / register your devices as accessories
|
|
207
232
|
try {
|
|
208
|
-
|
|
233
|
+
// Does the user have a version of Homebridge that is compatible with matter?
|
|
234
|
+
if (!this.api.isMatterAvailable?.()) {
|
|
235
|
+
this.debugLog(`Matter is not available in this version of Homebridge. Please update Homebridge to use this plugin, ${this.api.isMatterAvailable?.() ? '' : ' (Matter is not available in this version of Homebridge)'}`)
|
|
236
|
+
}
|
|
237
|
+
if (!this.api.isMatterEnabled?.()) {
|
|
238
|
+
this.debugLog(`Matter is not enabled in Homebridge. Please enable Matter in the Homebridge settings to use this plugin, ${this.api.isMatterEnabled?.() ? '' : ' (Matter is not enabled in Homebridge)'}`)
|
|
239
|
+
}
|
|
240
|
+
if (!this.api.isMatterAvailable?.() && !this.api.isMatterEnabled?.()) {
|
|
241
|
+
await this.discoverDevices()
|
|
242
|
+
} else {
|
|
243
|
+
this.infoLog('Matter is enabled in Homebridge. SwitchBot Matter devices will be handled by the Matter platform.')
|
|
244
|
+
}
|
|
209
245
|
} catch (e: any) {
|
|
210
246
|
this.errorLog(`Failed to Discover, Error Message: ${e.message ?? e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
|
|
211
247
|
this.debugErrorLog(`Failed to Discover, Error: ${e.message ?? e}`)
|
|
@@ -2913,74 +2949,4 @@ export class SwitchBotHAPPlatform implements DynamicPlatformPlugin {
|
|
|
2913
2949
|
return value
|
|
2914
2950
|
}
|
|
2915
2951
|
}
|
|
2916
|
-
|
|
2917
|
-
/**
|
|
2918
|
-
* If device level logging is turned on, log to log.warn
|
|
2919
|
-
* Otherwise send debug logs to log.debug
|
|
2920
|
-
*/
|
|
2921
|
-
async infoLog(...log: any[]): Promise<void> {
|
|
2922
|
-
if (await this.enablingPlatformLogging()) {
|
|
2923
|
-
this.log.info(String(...log))
|
|
2924
|
-
}
|
|
2925
|
-
}
|
|
2926
|
-
|
|
2927
|
-
async successLog(...log: any[]): Promise<void> {
|
|
2928
|
-
if (await this.enablingPlatformLogging()) {
|
|
2929
|
-
this.log.success(String(...log))
|
|
2930
|
-
}
|
|
2931
|
-
}
|
|
2932
|
-
|
|
2933
|
-
async debugSuccessLog(...log: any[]): Promise<void> {
|
|
2934
|
-
if (await this.enablingPlatformLogging()) {
|
|
2935
|
-
if (await this.loggingIsDebug()) {
|
|
2936
|
-
this.log.success('[DEBUG]', String(...log))
|
|
2937
|
-
}
|
|
2938
|
-
}
|
|
2939
|
-
}
|
|
2940
|
-
|
|
2941
|
-
async warnLog(...log: any[]): Promise<void> {
|
|
2942
|
-
if (await this.enablingPlatformLogging()) {
|
|
2943
|
-
this.log.warn(String(...log))
|
|
2944
|
-
}
|
|
2945
|
-
}
|
|
2946
|
-
|
|
2947
|
-
async debugWarnLog(...log: any[]): Promise<void> {
|
|
2948
|
-
if (await this.enablingPlatformLogging()) {
|
|
2949
|
-
if (await this.loggingIsDebug()) {
|
|
2950
|
-
this.log.warn('[DEBUG]', String(...log))
|
|
2951
|
-
}
|
|
2952
|
-
}
|
|
2953
|
-
}
|
|
2954
|
-
|
|
2955
|
-
async errorLog(...log: any[]): Promise<void> {
|
|
2956
|
-
if (await this.enablingPlatformLogging()) {
|
|
2957
|
-
this.log.error(String(...log))
|
|
2958
|
-
}
|
|
2959
|
-
}
|
|
2960
|
-
|
|
2961
|
-
async debugErrorLog(...log: any[]): Promise<void> {
|
|
2962
|
-
if (await this.enablingPlatformLogging()) {
|
|
2963
|
-
if (await this.loggingIsDebug()) {
|
|
2964
|
-
this.log.error('[DEBUG]', String(...log))
|
|
2965
|
-
}
|
|
2966
|
-
}
|
|
2967
|
-
}
|
|
2968
|
-
|
|
2969
|
-
async debugLog(...log: any[]): Promise<void> {
|
|
2970
|
-
if (await this.enablingPlatformLogging()) {
|
|
2971
|
-
if (this.platformLogging === 'debug') {
|
|
2972
|
-
this.log.info('[DEBUG]', String(...log))
|
|
2973
|
-
} else if (this.platformLogging === 'debugMode') {
|
|
2974
|
-
this.log.debug(String(...log))
|
|
2975
|
-
}
|
|
2976
|
-
}
|
|
2977
|
-
}
|
|
2978
|
-
|
|
2979
|
-
async loggingIsDebug(): Promise<boolean> {
|
|
2980
|
-
return this.platformLogging === 'debugMode' || this.platformLogging === 'debug'
|
|
2981
|
-
}
|
|
2982
|
-
|
|
2983
|
-
async enablingPlatformLogging(): Promise<boolean> {
|
|
2984
|
-
return this.platformLogging === 'debugMode' || this.platformLogging === 'debug' || this.platformLogging === 'standard'
|
|
2985
|
-
}
|
|
2986
2952
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { SwitchBotMatterPlatform } from './platform-matter.js'
|
|
4
|
+
import { formatDeviceIdAsMac } from './utils.js'
|
|
5
|
+
|
|
6
|
+
describe('platform-matter lifecycle cleanup', () => {
|
|
7
|
+
it('clearDeviceResources removes timers, instances and BLE handler entries', async () => {
|
|
8
|
+
// Setup stubbed API and logs
|
|
9
|
+
const handlers: Record<string, (...args: any[]) => any> = {}
|
|
10
|
+
const api: any = {
|
|
11
|
+
matter: {
|
|
12
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
13
|
+
registerPlatformAccessories: vi.fn(),
|
|
14
|
+
unregisterPlatformAccessories: vi.fn(),
|
|
15
|
+
clusterNames: { OnOff: 'OnOff' },
|
|
16
|
+
},
|
|
17
|
+
isMatterAvailable: () => true,
|
|
18
|
+
isMatterEnabled: () => true,
|
|
19
|
+
on: (ev: string, fn: (...args: any[]) => any) => { handlers[ev] = fn },
|
|
20
|
+
_handlers: handlers,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const log = { info: vi.fn(), debug: vi.fn(), warn: vi.fn(), error: vi.fn(), success: vi.fn() }
|
|
24
|
+
|
|
25
|
+
const platform = new SwitchBotMatterPlatform(log as any, {} as any, api)
|
|
26
|
+
|
|
27
|
+
// Insert a fake timer, accessory instance, and BLE handler
|
|
28
|
+
const deviceId = 'AA:BB:CC:11:22:33'
|
|
29
|
+
const nid = (platform as any).normalizeDeviceId(deviceId)
|
|
30
|
+
|
|
31
|
+
const timer = setInterval(() => {}, 100000)
|
|
32
|
+
;(platform as any).refreshTimers.set(nid, timer)
|
|
33
|
+
;(platform as any).accessoryInstances.set(nid, { dummy: true })
|
|
34
|
+
;(platform as any).bleEventHandler[deviceId.toLowerCase()] = () => {}
|
|
35
|
+
|
|
36
|
+
// Ensure they exist prior
|
|
37
|
+
expect((platform as any).refreshTimers.get(nid)).toBeDefined()
|
|
38
|
+
expect((platform as any).accessoryInstances.get(nid)).toBeDefined()
|
|
39
|
+
expect((platform as any).bleEventHandler[deviceId.toLowerCase()]).toBeDefined()
|
|
40
|
+
|
|
41
|
+
// Call the private helper
|
|
42
|
+
;(platform as any).clearDeviceResources(deviceId)
|
|
43
|
+
|
|
44
|
+
// Now they should be removed
|
|
45
|
+
expect((platform as any).refreshTimers.get(nid)).toBeUndefined()
|
|
46
|
+
expect((platform as any).accessoryInstances.get(nid)).toBeUndefined()
|
|
47
|
+
expect((platform as any).bleEventHandler[deviceId.toLowerCase()]).toBeUndefined()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('shutdown handler clears all timers and handlers when invoked', async () => {
|
|
51
|
+
// Setup stubbed API and logs
|
|
52
|
+
const handlers: Record<string, (...args: any[]) => any> = {}
|
|
53
|
+
const api: any = {
|
|
54
|
+
matter: {
|
|
55
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
56
|
+
registerPlatformAccessories: vi.fn(),
|
|
57
|
+
unregisterPlatformAccessories: vi.fn(),
|
|
58
|
+
clusterNames: { OnOff: 'OnOff' },
|
|
59
|
+
},
|
|
60
|
+
isMatterAvailable: () => true,
|
|
61
|
+
isMatterEnabled: () => true,
|
|
62
|
+
on: (ev: string, fn: (...args: any[]) => any) => { handlers[ev] = fn },
|
|
63
|
+
_handlers: handlers,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const log = { info: vi.fn(), debug: vi.fn(), warn: vi.fn(), error: vi.fn(), success: vi.fn() }
|
|
67
|
+
|
|
68
|
+
const platform = new SwitchBotMatterPlatform(log as any, {} as any, api)
|
|
69
|
+
|
|
70
|
+
// Add two timers to the platform (using normalized ids)
|
|
71
|
+
const ids = ['devA', 'devB']
|
|
72
|
+
for (const id of ids) {
|
|
73
|
+
const nid = (platform as any).normalizeDeviceId(id)
|
|
74
|
+
const t = setInterval(() => {}, 100000)
|
|
75
|
+
;(platform as any).refreshTimers.set(nid, t)
|
|
76
|
+
;(platform as any).accessoryInstances.set(nid, { dummy: true })
|
|
77
|
+
try {
|
|
78
|
+
const mac = formatDeviceIdAsMac(id).toLowerCase()
|
|
79
|
+
;(platform as any).bleEventHandler[mac] = () => {}
|
|
80
|
+
} catch {
|
|
81
|
+
// ignore formatting errors in this test
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Invoke didFinishLaunching to ensure the platform registered its shutdown handler
|
|
86
|
+
await Promise.resolve(api._handlers.didFinishLaunching?.())
|
|
87
|
+
|
|
88
|
+
// Shutdown handler should now be registered on api._handlers.shutdown
|
|
89
|
+
expect(typeof api._handlers.shutdown).toBe('function')
|
|
90
|
+
|
|
91
|
+
// Call the shutdown handler
|
|
92
|
+
await Promise.resolve(api._handlers.shutdown())
|
|
93
|
+
|
|
94
|
+
// All refresh timers should be cleared
|
|
95
|
+
for (const id of ids) {
|
|
96
|
+
const nid = (platform as any).normalizeDeviceId(id)
|
|
97
|
+
expect((platform as any).refreshTimers.get(nid)).toBeUndefined()
|
|
98
|
+
expect((platform as any).accessoryInstances.get(nid)).toBeUndefined()
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { SwitchBotMatterPlatform } from './platform-matter.js'
|
|
4
|
+
|
|
5
|
+
describe('platform-matter mapping helper', () => {
|
|
6
|
+
it('prefers accessory instance update methods for battery and falls back to api.matter.updateAccessoryState', async () => {
|
|
7
|
+
const updateAccessoryState = vi.fn()
|
|
8
|
+
const api: any = {
|
|
9
|
+
matter: {
|
|
10
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
11
|
+
updateAccessoryState,
|
|
12
|
+
clusterNames: { OnOff: 'OnOff', LevelControl: 'LevelControl', ColorControl: 'ColorControl', PowerSource: 'powerSource' },
|
|
13
|
+
},
|
|
14
|
+
isMatterAvailable: () => true,
|
|
15
|
+
isMatterEnabled: () => true,
|
|
16
|
+
on: () => {},
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const log: any = { info: vi.fn(), warn: vi.fn(), debug: vi.fn(), error: vi.fn() }
|
|
20
|
+
const platform = new SwitchBotMatterPlatform(log as any, {} as any, api)
|
|
21
|
+
|
|
22
|
+
// Create a fake accessory instance with updateBatteryPercentage
|
|
23
|
+
const deviceId = 'DEV123'
|
|
24
|
+
const nid = (platform as any).normalizeDeviceId(deviceId)
|
|
25
|
+
const fakeInstance = { updateBatteryPercentage: vi.fn() }
|
|
26
|
+
;(platform as any).accessoryInstances.set(nid, fakeInstance)
|
|
27
|
+
|
|
28
|
+
// Call the private helper with different battery field names
|
|
29
|
+
await (platform as any).applyStatusToAccessory('uuid-test', { deviceId } as any, { batteryPercentage: 55 })
|
|
30
|
+
expect(fakeInstance.updateBatteryPercentage).toHaveBeenCalled()
|
|
31
|
+
|
|
32
|
+
// Remove instance to force fallback
|
|
33
|
+
;(platform as any).accessoryInstances.delete(nid)
|
|
34
|
+
await (platform as any).applyStatusToAccessory('uuid-test', { deviceId } as any, { battery: 30 })
|
|
35
|
+
expect(updateAccessoryState).toHaveBeenCalled()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('handles temperature and humidity synonyms', async () => {
|
|
39
|
+
const updateAccessoryState = vi.fn()
|
|
40
|
+
const api: any = {
|
|
41
|
+
matter: {
|
|
42
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
43
|
+
updateAccessoryState,
|
|
44
|
+
clusterNames: {},
|
|
45
|
+
},
|
|
46
|
+
isMatterAvailable: () => true,
|
|
47
|
+
isMatterEnabled: () => true,
|
|
48
|
+
on: () => {},
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const log: any = { info: vi.fn(), warn: vi.fn(), debug: vi.fn(), error: vi.fn() }
|
|
52
|
+
const platform = new SwitchBotMatterPlatform(log as any, {} as any, api)
|
|
53
|
+
|
|
54
|
+
const deviceId = 'DEV-TEMP'
|
|
55
|
+
await (platform as any).applyStatusToAccessory('uuid-temp', { deviceId } as any, { temp: 21.5, humid: 48 })
|
|
56
|
+
|
|
57
|
+
// Expect updateAccessoryState to have been called for temperature and humidity
|
|
58
|
+
expect(updateAccessoryState).toHaveBeenCalled()
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { SwitchBotMatterPlatform } from './platform-matter.js'
|
|
4
|
+
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
|
|
5
|
+
|
|
6
|
+
describe('platform-matter discovered devices', () => {
|
|
7
|
+
it('uses discovered devices and registers them (platform + robotic)', async () => {
|
|
8
|
+
const mockRegister = vi.fn()
|
|
9
|
+
|
|
10
|
+
const api: any = {
|
|
11
|
+
matter: {
|
|
12
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
13
|
+
registerPlatformAccessories: mockRegister,
|
|
14
|
+
clusterNames: { OnOff: 'OnOff', LevelControl: 'LevelControl', ColorControl: 'ColorControl' },
|
|
15
|
+
deviceTypes: { RoboticVacuumCleaner: 'rvc' },
|
|
16
|
+
},
|
|
17
|
+
isMatterAvailable: () => true,
|
|
18
|
+
isMatterEnabled: () => true,
|
|
19
|
+
on: () => {},
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const log: any = {
|
|
23
|
+
info: vi.fn(),
|
|
24
|
+
warn: vi.fn(),
|
|
25
|
+
debug: vi.fn(),
|
|
26
|
+
error: vi.fn(),
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const config: any = {}
|
|
30
|
+
|
|
31
|
+
const platform = new SwitchBotMatterPlatform(log, config, api)
|
|
32
|
+
|
|
33
|
+
// Provide discovered devices
|
|
34
|
+
;(platform as any).discoveredDevices = [
|
|
35
|
+
{ deviceId: 'dev1', deviceName: 'Lamp', deviceType: 'Plug' },
|
|
36
|
+
{ deviceId: 'vac1', deviceName: 'Vac', deviceType: 'K10+' },
|
|
37
|
+
] as any
|
|
38
|
+
|
|
39
|
+
// Stub accessory creation to avoid instantiating full device classes
|
|
40
|
+
;(platform as any).createAccessoryFromDevice = async (dev: any) => ({ displayName: dev.deviceName, uuid: api.matter.uuid.generate(dev.deviceId) } as any)
|
|
41
|
+
|
|
42
|
+
await (platform as any).registerMatterAccessories()
|
|
43
|
+
|
|
44
|
+
// Ensure registerPlatformAccessories was called at least once
|
|
45
|
+
expect(mockRegister).toHaveBeenCalled()
|
|
46
|
+
|
|
47
|
+
// Sum total accessories registered across all calls
|
|
48
|
+
const totalRegistered = mockRegister.mock.calls.reduce((sum: number, call: any) => sum + ((call[2] && call[2].length) || 0), 0)
|
|
49
|
+
expect(totalRegistered).toBe(2)
|
|
50
|
+
|
|
51
|
+
// Verify PLUGIN_NAME and PLATFORM_NAME were used in the calls
|
|
52
|
+
for (const call of mockRegister.mock.calls) {
|
|
53
|
+
expect(call[0]).toBe(PLUGIN_NAME)
|
|
54
|
+
expect(call[1]).toBe(PLATFORM_NAME)
|
|
55
|
+
}
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('applies per-device config overrides when deviceId matches discovered device', async () => {
|
|
59
|
+
const mockRegister = vi.fn()
|
|
60
|
+
|
|
61
|
+
const api: any = {
|
|
62
|
+
matter: {
|
|
63
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
64
|
+
registerPlatformAccessories: mockRegister,
|
|
65
|
+
clusterNames: { OnOff: 'OnOff', LevelControl: 'LevelControl', ColorControl: 'ColorControl' },
|
|
66
|
+
deviceTypes: { RoboticVacuumCleaner: 'rvc' },
|
|
67
|
+
},
|
|
68
|
+
isMatterAvailable: () => true,
|
|
69
|
+
isMatterEnabled: () => true,
|
|
70
|
+
on: () => {},
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const log: any = { info: vi.fn(), warn: vi.fn(), debug: vi.fn(), error: vi.fn() }
|
|
74
|
+
|
|
75
|
+
const config: any = {
|
|
76
|
+
options: {
|
|
77
|
+
devices: [{ deviceId: 'dev1', configDeviceName: 'Configured Lamp', logging: 'debug' }],
|
|
78
|
+
},
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const platform = new SwitchBotMatterPlatform(log, config, api)
|
|
82
|
+
|
|
83
|
+
;(platform as any).discoveredDevices = [{ deviceId: 'dev1', deviceName: 'Lamp', deviceType: 'Plug' }] as any
|
|
84
|
+
|
|
85
|
+
const seen: any[] = []
|
|
86
|
+
;(platform as any).createAccessoryFromDevice = async (dev: any) => {
|
|
87
|
+
seen.push(dev)
|
|
88
|
+
return {
|
|
89
|
+
displayName: dev.deviceName ?? dev.configDeviceName,
|
|
90
|
+
uuid: api.matter.uuid.generate(dev.deviceId),
|
|
91
|
+
} as any
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
await (platform as any).registerMatterAccessories()
|
|
95
|
+
|
|
96
|
+
// createAccessoryFromDevice should have been called once with merged config
|
|
97
|
+
expect(seen.length).toBe(1)
|
|
98
|
+
expect(seen[0].deviceId).toBe('dev1')
|
|
99
|
+
// per-device config values should be present on the merged device
|
|
100
|
+
expect(seen[0].configDeviceName).toBe('Configured Lamp')
|
|
101
|
+
expect(seen[0].logging).toBe('debug')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('ignores config-only devices by default but includes them when allowConfigOnlyDevices=true', async () => {
|
|
105
|
+
const mockRegister = vi.fn()
|
|
106
|
+
|
|
107
|
+
const api: any = {
|
|
108
|
+
matter: {
|
|
109
|
+
uuid: { generate: (s: string) => `uuid-${s}` },
|
|
110
|
+
registerPlatformAccessories: mockRegister,
|
|
111
|
+
clusterNames: { OnOff: 'OnOff', LevelControl: 'LevelControl', ColorControl: 'ColorControl' },
|
|
112
|
+
deviceTypes: { RoboticVacuumCleaner: 'rvc' },
|
|
113
|
+
},
|
|
114
|
+
isMatterAvailable: () => true,
|
|
115
|
+
isMatterEnabled: () => true,
|
|
116
|
+
on: () => {},
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const log: any = { info: vi.fn(), warn: vi.fn(), debug: vi.fn(), error: vi.fn() }
|
|
120
|
+
|
|
121
|
+
// config-only device (cfg1) and one discovered device (dev1)
|
|
122
|
+
const cfgOnly = { deviceId: 'cfg1', configDeviceName: 'OnlyInConfig', deviceType: 'Plug' }
|
|
123
|
+
const configDefault: any = { options: { devices: [cfgOnly] } }
|
|
124
|
+
|
|
125
|
+
const platformDefault = new SwitchBotMatterPlatform(log, configDefault, api)
|
|
126
|
+
;(platformDefault as any).discoveredDevices = [{ deviceId: 'dev1', deviceName: 'Lamp', deviceType: 'Plug' }] as any
|
|
127
|
+
const seenDefault: any[] = []
|
|
128
|
+
;(platformDefault as any).createAccessoryFromDevice = async (dev: any) => {
|
|
129
|
+
seenDefault.push(dev)
|
|
130
|
+
return {
|
|
131
|
+
displayName: dev.deviceName ?? dev.configDeviceName,
|
|
132
|
+
uuid: api.matter.uuid.generate(dev.deviceId),
|
|
133
|
+
} as any
|
|
134
|
+
}
|
|
135
|
+
await (platformDefault as any).registerMatterAccessories()
|
|
136
|
+
// Should only see discovered device (cfg1 ignored)
|
|
137
|
+
expect(seenDefault.find((d: any) => d.deviceId === 'cfg1')).toBeUndefined()
|
|
138
|
+
|
|
139
|
+
// Now opt-in to include config-only devices
|
|
140
|
+
const configOptIn: any = { options: { devices: [cfgOnly], allowConfigOnlyDevices: true } }
|
|
141
|
+
const platformOptIn = new SwitchBotMatterPlatform(log, configOptIn, api)
|
|
142
|
+
;(platformOptIn as any).discoveredDevices = [{ deviceId: 'dev1', deviceName: 'Lamp', deviceType: 'Plug' }] as any
|
|
143
|
+
const seenOptIn: any[] = []
|
|
144
|
+
;(platformOptIn as any).createAccessoryFromDevice = async (dev: any) => {
|
|
145
|
+
seenOptIn.push(dev)
|
|
146
|
+
return {
|
|
147
|
+
displayName: dev.deviceName ?? dev.configDeviceName,
|
|
148
|
+
uuid: api.matter.uuid.generate(dev.deviceId),
|
|
149
|
+
} as any
|
|
150
|
+
}
|
|
151
|
+
await (platformOptIn as any).registerMatterAccessories()
|
|
152
|
+
// Should include the config-only device now
|
|
153
|
+
expect(seenOptIn.find((d: any) => d.deviceId === 'cfg1')).toBeDefined()
|
|
154
|
+
})
|
|
155
|
+
})
|