@switchbot/homebridge-switchbot 5.0.0-beta.7 → 5.0.0-beta.70
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 +15 -0
- package/README.md +45 -3
- package/config.schema.json +871 -13754
- package/dist/devices-hap/airpurifier.d.ts.map +1 -1
- package/dist/devices-hap/airpurifier.js +12 -6
- package/dist/devices-hap/airpurifier.js.map +1 -1
- package/dist/devices-hap/blindtilt.js +3 -3
- package/dist/devices-hap/bot.d.ts.map +1 -1
- package/dist/devices-hap/bot.js +16 -5
- package/dist/devices-hap/bot.js.map +1 -1
- package/dist/devices-hap/ceilinglight.d.ts.map +1 -1
- package/dist/devices-hap/ceilinglight.js +13 -7
- package/dist/devices-hap/ceilinglight.js.map +1 -1
- package/dist/devices-hap/colorbulb.d.ts.map +1 -1
- package/dist/devices-hap/colorbulb.js +49 -9
- package/dist/devices-hap/colorbulb.js.map +1 -1
- package/dist/devices-hap/contact.js +3 -3
- package/dist/devices-hap/curtain.js +2 -2
- package/dist/devices-hap/curtain.js.map +1 -1
- package/dist/devices-hap/device.d.ts +18 -8
- package/dist/devices-hap/device.d.ts.map +1 -1
- package/dist/devices-hap/device.js +141 -69
- package/dist/devices-hap/device.js.map +1 -1
- package/dist/devices-hap/fan.d.ts.map +1 -1
- package/dist/devices-hap/fan.js +12 -6
- package/dist/devices-hap/fan.js.map +1 -1
- package/dist/devices-hap/hub.d.ts.map +1 -1
- package/dist/devices-hap/hub.js +6 -5
- package/dist/devices-hap/hub.js.map +1 -1
- package/dist/devices-hap/humidifier.d.ts +5 -0
- package/dist/devices-hap/humidifier.d.ts.map +1 -1
- package/dist/devices-hap/humidifier.js +92 -4
- package/dist/devices-hap/humidifier.js.map +1 -1
- package/dist/devices-hap/iosensor.d.ts.map +1 -1
- package/dist/devices-hap/iosensor.js +36 -21
- package/dist/devices-hap/iosensor.js.map +1 -1
- package/dist/devices-hap/lightstrip.d.ts.map +1 -1
- package/dist/devices-hap/lightstrip.js +38 -8
- package/dist/devices-hap/lightstrip.js.map +1 -1
- package/dist/devices-hap/lock.d.ts.map +1 -1
- package/dist/devices-hap/lock.js +14 -6
- package/dist/devices-hap/lock.js.map +1 -1
- package/dist/devices-hap/meter.d.ts.map +1 -1
- package/dist/devices-hap/meter.js +6 -5
- package/dist/devices-hap/meter.js.map +1 -1
- package/dist/devices-hap/meterplus.d.ts.map +1 -1
- package/dist/devices-hap/meterplus.js +6 -5
- package/dist/devices-hap/meterplus.js.map +1 -1
- package/dist/devices-hap/meterpro.d.ts.map +1 -1
- package/dist/devices-hap/meterpro.js +7 -6
- package/dist/devices-hap/meterpro.js.map +1 -1
- package/dist/devices-hap/motion.js +3 -3
- package/dist/devices-hap/plug.d.ts.map +1 -1
- package/dist/devices-hap/plug.js +11 -6
- package/dist/devices-hap/plug.js.map +1 -1
- package/dist/devices-hap/relayswitch.js +3 -3
- package/dist/devices-hap/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/devices-hap/robotvacuumcleaner.js +13 -6
- package/dist/devices-hap/robotvacuumcleaner.js.map +1 -1
- package/dist/devices-hap/waterdetector.js +3 -3
- package/dist/devices-matter/BaseMatterAccessory.d.ts +27 -0
- package/dist/devices-matter/BaseMatterAccessory.d.ts.map +1 -1
- package/dist/devices-matter/BaseMatterAccessory.js +169 -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/RoboticVacuumAccessory.d.ts +36 -43
- package/dist/devices-matter/RoboticVacuumAccessory.d.ts.map +1 -1
- package/dist/devices-matter/RoboticVacuumAccessory.js +478 -268
- package/dist/devices-matter/RoboticVacuumAccessory.js.map +1 -1
- package/dist/devices-matter/VenetianBlindAccessory.d.ts +6 -6
- package/dist/devices-matter/VenetianBlindAccessory.d.ts.map +1 -1
- package/dist/devices-matter/VenetianBlindAccessory.js.map +1 -1
- package/dist/devices-matter/WindowBlindAccessory.d.ts +5 -5
- package/dist/devices-matter/WindowBlindAccessory.d.ts.map +1 -1
- package/dist/devices-matter/WindowBlindAccessory.js +57 -6
- package/dist/devices-matter/WindowBlindAccessory.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +219 -19
- package/dist/homebridge-ui/server.js +0 -31
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -9
- package/dist/index.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 +26 -15
- package/dist/platform-hap.d.ts.map +1 -1
- package/dist/platform-hap.js +333 -153
- package/dist/platform-hap.js.map +1 -1
- package/dist/platform-matter.d.ts +93 -6
- package/dist/platform-matter.d.ts.map +1 -1
- package/dist/platform-matter.js +1878 -229
- package/dist/platform-matter.js.map +1 -1
- package/dist/settings.d.ts +75 -7
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/test/apiRequestTracker.test.d.ts +2 -0
- package/dist/test/apiRequestTracker.test.d.ts.map +1 -0
- package/dist/test/apiRequestTracker.test.js +392 -0
- package/dist/test/apiRequestTracker.test.js.map +1 -0
- package/dist/test/hap/device-webhook-context.test.d.ts +2 -0
- package/dist/test/hap/device-webhook-context.test.d.ts.map +1 -0
- package/dist/test/hap/device-webhook-context.test.js +128 -0
- package/dist/test/hap/device-webhook-context.test.js.map +1 -0
- package/dist/test/hap/platform-hap.logging.test.d.ts +2 -0
- package/dist/test/hap/platform-hap.logging.test.d.ts.map +1 -0
- package/dist/test/hap/platform-hap.logging.test.js +33 -0
- package/dist/test/hap/platform-hap.logging.test.js.map +1 -0
- package/dist/test/hap/platform-hap.test.d.ts +2 -0
- package/dist/test/hap/platform-hap.test.d.ts.map +1 -0
- package/dist/test/hap/platform-hap.test.js +62 -0
- package/dist/test/hap/platform-hap.test.js.map +1 -0
- package/dist/test/helpers/platform-fixtures.d.ts +9 -0
- package/dist/test/helpers/platform-fixtures.d.ts.map +1 -0
- package/dist/test/helpers/platform-fixtures.js +30 -0
- package/dist/test/helpers/platform-fixtures.js.map +1 -0
- package/dist/test/homebridge-ui/server.test.d.ts +2 -0
- package/dist/test/homebridge-ui/server.test.d.ts.map +1 -0
- package/dist/test/homebridge-ui/server.test.js +445 -0
- package/dist/test/homebridge-ui/server.test.js.map +1 -0
- package/dist/{index.test.d.ts.map → test/index.test.d.ts.map} +1 -1
- package/dist/test/index.test.js +19 -0
- package/dist/test/index.test.js.map +1 -0
- package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts +2 -0
- package/dist/test/matter/devices-matter/baseMatterAccessory.test.d.ts.map +1 -0
- package/dist/test/matter/devices-matter/baseMatterAccessory.test.js +71 -0
- package/dist/test/matter/devices-matter/baseMatterAccessory.test.js.map +1 -0
- package/dist/test/matter/platform-matter.additional.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.additional.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.additional.test.js +35 -0
- package/dist/test/matter/platform-matter.additional.test.js.map +1 -0
- package/dist/test/matter/platform-matter.bleparse.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.bleparse.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.bleparse.test.js +43 -0
- package/dist/test/matter/platform-matter.bleparse.test.js.map +1 -0
- package/dist/test/matter/platform-matter.cleanup.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.cleanup.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.cleanup.test.js +70 -0
- package/dist/test/matter/platform-matter.cleanup.test.js.map +1 -0
- package/dist/test/matter/platform-matter.keepstale.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.keepstale.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.keepstale.test.js +27 -0
- package/dist/test/matter/platform-matter.keepstale.test.js.map +1 -0
- package/dist/test/matter/platform-matter.logging.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.logging.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.logging.test.js +29 -0
- package/dist/test/matter/platform-matter.logging.test.js.map +1 -0
- package/dist/test/matter/platform-matter.mapping.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.mapping.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.mapping.test.js +43 -0
- package/dist/test/matter/platform-matter.mapping.test.js.map +1 -0
- package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.openapi-mapping.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.openapi-mapping.test.js +84 -0
- package/dist/test/matter/platform-matter.openapi-mapping.test.js.map +1 -0
- package/dist/test/matter/platform-matter.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.test.js +117 -0
- package/dist/test/matter/platform-matter.test.js.map +1 -0
- package/dist/test/matter/platform-matter.unregister.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.unregister.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.unregister.test.js +30 -0
- package/dist/test/matter/platform-matter.unregister.test.js.map +1 -0
- package/dist/test/matter/platform-matter.webhook.test.d.ts +2 -0
- package/dist/test/matter/platform-matter.webhook.test.d.ts.map +1 -0
- package/dist/test/matter/platform-matter.webhook.test.js +46 -0
- package/dist/test/matter/platform-matter.webhook.test.js.map +1 -0
- package/dist/test/utils.test.d.ts +2 -0
- package/dist/test/utils.test.d.ts.map +1 -0
- package/dist/test/utils.test.js +95 -0
- package/dist/test/utils.test.js.map +1 -0
- package/dist/test/verifyconfig.test.d.ts.map +1 -0
- package/dist/{verifyconfig.test.js → test/verifyconfig.test.js} +2 -2
- package/dist/test/verifyconfig.test.js.map +1 -0
- package/dist/utils.d.ts +204 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +713 -33
- package/dist/utils.js.map +1 -1
- package/docs/assets/highlight.css +14 -0
- package/docs/assets/main.js +2 -2
- package/docs/index.html +31 -2
- package/docs/variables/default.html +1 -1
- package/package.json +15 -15
- package/src/devices-hap/airpurifier.ts +11 -6
- package/src/devices-hap/blindtilt.ts +3 -3
- package/src/devices-hap/bot.ts +15 -5
- package/src/devices-hap/ceilinglight.ts +12 -7
- package/src/devices-hap/colorbulb.ts +46 -10
- package/src/devices-hap/contact.ts +3 -3
- package/src/devices-hap/curtain.ts +2 -2
- package/src/devices-hap/device.ts +149 -70
- package/src/devices-hap/fan.ts +11 -6
- package/src/devices-hap/hub.ts +6 -5
- package/src/devices-hap/humidifier.ts +97 -4
- package/src/devices-hap/iosensor.ts +36 -21
- package/src/devices-hap/lightstrip.ts +35 -8
- package/src/devices-hap/lock.ts +13 -6
- package/src/devices-hap/meter.ts +6 -5
- package/src/devices-hap/meterplus.ts +6 -5
- package/src/devices-hap/meterpro.ts +7 -6
- package/src/devices-hap/motion.ts +3 -3
- package/src/devices-hap/plug.ts +10 -6
- package/src/devices-hap/relayswitch.ts +3 -3
- package/src/devices-hap/robotvacuumcleaner.ts +12 -6
- package/src/devices-hap/waterdetector.ts +3 -3
- package/src/devices-matter/BaseMatterAccessory.ts +176 -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/RoboticVacuumAccessory.ts +529 -315
- package/src/devices-matter/VenetianBlindAccessory.ts +5 -5
- package/src/devices-matter/WindowBlindAccessory.ts +53 -10
- package/src/homebridge-ui/public/index.html +219 -19
- package/src/homebridge-ui/server.ts +0 -34
- package/src/index.ts +4 -10
- package/src/irdevice/irdevice.ts +74 -35
- package/src/platform-hap.ts +365 -169
- package/src/platform-matter.ts +1923 -230
- package/src/settings.ts +78 -3
- package/src/test/apiRequestTracker.test.ts +417 -0
- package/src/test/hap/device-webhook-context.test.ts +136 -0
- package/src/test/hap/platform-hap.logging.test.ts +36 -0
- package/src/test/hap/platform-hap.test.ts +70 -0
- package/src/test/helpers/platform-fixtures.ts +33 -0
- package/src/test/homebridge-ui/server.test.ts +486 -0
- package/src/test/index.test.ts +24 -0
- package/src/test/matter/devices-matter/baseMatterAccessory.test.ts +88 -0
- package/src/test/matter/platform-matter.additional.test.ts +44 -0
- package/src/test/matter/platform-matter.bleparse.test.ts +47 -0
- package/src/test/matter/platform-matter.cleanup.test.ts +86 -0
- package/src/test/matter/platform-matter.keepstale.test.ts +37 -0
- package/src/test/matter/platform-matter.logging.test.ts +33 -0
- package/src/test/matter/platform-matter.mapping.test.ts +57 -0
- package/src/test/matter/platform-matter.openapi-mapping.test.ts +109 -0
- package/src/test/matter/platform-matter.test.ts +144 -0
- package/src/test/matter/platform-matter.unregister.test.ts +39 -0
- package/src/test/matter/platform-matter.webhook.test.ts +54 -0
- package/src/test/utils.test.ts +96 -0
- package/src/{verifyconfig.test.ts → test/verifyconfig.test.ts} +12 -11
- package/src/utils.ts +777 -36
- package/dist/index.test.js +0 -14
- package/dist/index.test.js.map +0 -1
- package/dist/verifyconfig.test.d.ts.map +0 -1
- package/dist/verifyconfig.test.js.map +0 -1
- package/src/index.test.ts +0 -19
- /package/dist/{index.test.d.ts → test/index.test.d.ts} +0 -0
- /package/dist/{verifyconfig.test.d.ts → test/verifyconfig.test.d.ts} +0 -0
package/src/platform-matter.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { Server } from 'node:http'
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
API,
|
|
3
5
|
DynamicPlatformPlugin,
|
|
@@ -5,10 +7,15 @@ import type {
|
|
|
5
7
|
MatterAccessory,
|
|
6
8
|
SerializedMatterAccessory,
|
|
7
9
|
} from 'homebridge'
|
|
8
|
-
import type {
|
|
10
|
+
import type { MqttClient } from 'mqtt'
|
|
11
|
+
import type { bodyChange, device, irdevice } from 'node-switchbot'
|
|
12
|
+
|
|
13
|
+
import type { devicesConfig, irDevicesConfig, SwitchBotPlatformConfig } from './settings.js'
|
|
9
14
|
|
|
10
|
-
import
|
|
15
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
16
|
+
import { join } from 'node:path'
|
|
11
17
|
|
|
18
|
+
import asyncmqtt from 'async-mqtt'
|
|
12
19
|
import { SwitchBotBLE, SwitchBotOpenAPI } from 'node-switchbot'
|
|
13
20
|
|
|
14
21
|
import {
|
|
@@ -35,14 +42,8 @@ import {
|
|
|
35
42
|
WindowBlindAccessory,
|
|
36
43
|
} from './devices-matter/index.js'
|
|
37
44
|
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js'
|
|
38
|
-
import {
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* MatterPlatform
|
|
42
|
-
* Demonstrates all available Matter device types in Homebridge
|
|
43
|
-
*
|
|
44
|
-
* Organized by official Matter Specification v1.4.1 categories
|
|
45
|
-
*/
|
|
45
|
+
import { ApiRequestTracker, applyDeviceTypeTemplates, createPlatformLogger, formatDeviceIdAsMac, hs2rgb, isSuccessfulStatusCode, makeBLESender, makeOpenAPISender, mergeByDeviceId, normalizeDeviceId, rgb2hs, sleep } from './utils.js'
|
|
46
|
+
|
|
46
47
|
export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
47
48
|
// Track restored HAP cached accessories (required for DynamicPlatformPlugin)
|
|
48
49
|
// This is commented out here as this plugin does not have any HAP accessories
|
|
@@ -55,33 +56,88 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
55
56
|
private switchBotBLE?: SwitchBotBLE
|
|
56
57
|
// discovered devices cache
|
|
57
58
|
private discoveredDevices: device[] = []
|
|
59
|
+
// discovered IR devices cache
|
|
60
|
+
private discoveredIRDevices: irdevice[] = []
|
|
61
|
+
// Registry of created accessory instances keyed by normalized deviceId
|
|
62
|
+
private accessoryInstances: Map<string, any> = new Map()
|
|
63
|
+
// Refresh timers keyed by normalized deviceId
|
|
64
|
+
private refreshTimers: Map<string, NodeJS.Timeout> = new Map()
|
|
65
|
+
// Platform-level refresh timer for batch status updates
|
|
66
|
+
private platformRefreshTimer?: NodeJS.Timeout
|
|
67
|
+
// Device status cache from last refresh
|
|
68
|
+
private deviceStatusCache: Map<string, any> = new Map()
|
|
69
|
+
// Backoff cooldowns persisted across restarts: normalized deviceId -> nextAllowedAt(ms)
|
|
70
|
+
private backoffCooldowns: Map<string, number> = new Map()
|
|
71
|
+
private backoffFilePath?: string
|
|
72
|
+
// Devices that have explicit per-device refresh timers (normalized deviceId)
|
|
73
|
+
private perDeviceRefreshSet: Set<string> = new Set()
|
|
58
74
|
// BLE event handlers keyed by device MAC (formatted)
|
|
59
75
|
private bleEventHandler: { [x: string]: (context: any) => void } = {}
|
|
76
|
+
// MQTT and Webhook properties (mirror HAP behavior so Matter platform can
|
|
77
|
+
// receive OpenAPI webhook events and/or MQTT-proxied webhook messages)
|
|
78
|
+
private mqttClient: MqttClient | null = null
|
|
79
|
+
private webhookEventListener: Server | null = null
|
|
80
|
+
private webhookEventHandler: { [x: string]: (context: any) => void } = {}
|
|
81
|
+
// Platform logging toggle (can be controlled via UI or config)
|
|
82
|
+
// Use same shape as HAP platform: string values like 'debug', 'debugMode', 'standard', or 'none'
|
|
83
|
+
private platformLogging?: string
|
|
84
|
+
|
|
85
|
+
// API request tracking (persistent across restarts)
|
|
86
|
+
private apiTracker?: ApiRequestTracker
|
|
87
|
+
|
|
88
|
+
// Platform-provided logging helpers (attached in constructor)
|
|
89
|
+
infoLog!: (...args: any[]) => void
|
|
90
|
+
successLog!: (...args: any[]) => void
|
|
91
|
+
debugSuccessLog!: (...args: any[]) => void
|
|
92
|
+
warnLog!: (...args: any[]) => void
|
|
93
|
+
debugWarnLog!: (...args: any[]) => void
|
|
94
|
+
errorLog!: (...args: any[]) => void
|
|
95
|
+
debugErrorLog!: (...args: any[]) => void
|
|
96
|
+
debugLog!: (...args: any[]) => void
|
|
97
|
+
loggingIsDebug!: () => Promise<boolean>
|
|
98
|
+
enablingPlatformLogging!: () => Promise<boolean>
|
|
60
99
|
|
|
61
100
|
constructor(
|
|
62
101
|
public readonly log: Logging,
|
|
63
102
|
public readonly config: SwitchBotPlatformConfig,
|
|
64
103
|
public readonly api: API,
|
|
65
104
|
) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
105
|
+
// Determine platform logging preference (match HAP behaviour as closely as
|
|
106
|
+
// possible using config values. We default to 'standard' when unspecified.)
|
|
107
|
+
this.platformLogging = (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none')
|
|
108
|
+
? this.config.options.logging
|
|
109
|
+
: 'standard'
|
|
110
|
+
|
|
111
|
+
// Unconditional diagnostic using the raw Homebridge `log` so it always
|
|
112
|
+
// appears regardless of the platform logging helpers' gating logic.
|
|
69
113
|
try {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
;(this.config as any).options.deviceConfig = cleaned
|
|
74
|
-
} else {
|
|
75
|
-
delete (this.config as any).options.deviceConfig
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
} catch (e) {
|
|
79
|
-
this.log.debug('Failed to clean deviceConfig: %s', e)
|
|
114
|
+
this.log.debug?.(`[SwitchBot Matter] effective platformLogging=${String(this.platformLogging)}`)
|
|
115
|
+
} catch (e: any) {
|
|
116
|
+
// swallow any logging errors — diagnostics are best-effort
|
|
80
117
|
}
|
|
81
118
|
|
|
119
|
+
// Attach platform-wide logging helpers from utils so Matter and device
|
|
120
|
+
// classes can use consistent logging methods (infoLog/debugLog/etc.)
|
|
121
|
+
const _pl = createPlatformLogger(async () => (this as any).platformLogging, this.log)
|
|
122
|
+
this.infoLog = _pl.infoLog
|
|
123
|
+
this.successLog = _pl.successLog
|
|
124
|
+
this.debugSuccessLog = _pl.debugSuccessLog
|
|
125
|
+
this.warnLog = _pl.warnLog
|
|
126
|
+
this.debugWarnLog = _pl.debugWarnLog
|
|
127
|
+
this.errorLog = _pl.errorLog
|
|
128
|
+
this.debugErrorLog = _pl.debugErrorLog
|
|
129
|
+
this.debugLog = _pl.debugLog
|
|
130
|
+
this.loggingIsDebug = _pl.loggingIsDebug
|
|
131
|
+
this.enablingPlatformLogging = _pl.enablingPlatformLogging
|
|
132
|
+
|
|
133
|
+
this.debugLog('Finished initializing platform:', this.config.name)
|
|
134
|
+
|
|
135
|
+
// Note: deviceConfig and irdeviceConfig have been removed from the platform.
|
|
136
|
+
// All device-specific configuration should be done via options.devices arrays.
|
|
137
|
+
|
|
82
138
|
// Does the user have a version of Homebridge that is compatible with matter?
|
|
83
139
|
if (!this.api.isMatterAvailable?.()) {
|
|
84
|
-
this.
|
|
140
|
+
this.warnLog('Matter is not available in this version of Homebridge. Please update Homebridge to use this plugin.')
|
|
85
141
|
}
|
|
86
142
|
|
|
87
143
|
// Check if the user has matter enabled, this means:
|
|
@@ -90,35 +146,63 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
90
146
|
// In reality, only the below check is needed, but they are both included here for completeness
|
|
91
147
|
// Remember to use a '?.' optional chaining operator in case the user is running an older version of Homebridge that does not have these APIs
|
|
92
148
|
if (!this.api.isMatterEnabled?.()) {
|
|
93
|
-
this.
|
|
149
|
+
this.warnLog('Matter is not enabled in Homebridge. Please enable Matter in the Homebridge settings to use this plugin.')
|
|
94
150
|
return
|
|
95
151
|
}
|
|
96
152
|
|
|
97
153
|
// Register Matter accessories when Homebridge has finished launching
|
|
98
154
|
this.api.on('didFinishLaunching', () => {
|
|
99
|
-
this.
|
|
155
|
+
this.debugLog('Executed didFinishLaunching callback')
|
|
156
|
+
// Log presence of credentials (do not log actual values)
|
|
157
|
+
try {
|
|
158
|
+
this.debugLog(`SwitchBot credentials present? token=${Boolean(this.config.credentials?.token)}, secret=${Boolean(this.config.credentials?.secret)}`)
|
|
159
|
+
} catch (e: any) {
|
|
160
|
+
this.debugLog('Failed to log credentials presence: %s', e?.message ?? e)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Do not fall back to environment variables here — credentials must
|
|
164
|
+
// come from plugin config (Homebridge UI). If credentials appear
|
|
165
|
+
// missing we'll log that fact and continue; the UI should store token
|
|
166
|
+
// and secret in `config.credentials`.
|
|
100
167
|
// Initialize SwitchBot API clients
|
|
101
168
|
try {
|
|
102
169
|
if (this.config.credentials?.token && this.config.credentials?.secret) {
|
|
103
170
|
this.switchBotAPI = new SwitchBotOpenAPI(this.config.credentials.token, this.config.credentials.secret, this.config.options?.hostname)
|
|
104
171
|
// forward basic logs
|
|
105
172
|
if (!this.config.options?.disableLogsforOpenAPI && this.switchBotAPI?.on) {
|
|
106
|
-
this.switchBotAPI.on('log', (l: any) => this.
|
|
173
|
+
this.switchBotAPI.on('log', (l: any) => this.debugLog('[SwitchBot OpenAPI]', l.message))
|
|
107
174
|
}
|
|
108
175
|
} else {
|
|
109
|
-
this.
|
|
176
|
+
this.debugLog('SwitchBot OpenAPI credentials not provided; cloud devices will be skipped')
|
|
110
177
|
}
|
|
111
178
|
} catch (e: any) {
|
|
112
|
-
this.
|
|
179
|
+
this.errorLog('Failed to initialize SwitchBot OpenAPI:', e?.message ?? e)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Initialize API request tracking
|
|
183
|
+
if (this.switchBotAPI) {
|
|
184
|
+
try {
|
|
185
|
+
this.apiTracker = new ApiRequestTracker(this.api, this.log, 'SwitchBot Matter', {
|
|
186
|
+
dailyLimit: this.config.options?.dailyApiLimit ?? 10000,
|
|
187
|
+
reserveForCommands: this.config.options?.dailyApiReserveForCommands ?? 1000,
|
|
188
|
+
pausePollingAtReserve: this.config.options?.webhookOnlyOnReserve ?? false,
|
|
189
|
+
resetAtLocalMidnight: this.config.options?.dailyApiResetAtLocalMidnight ?? false,
|
|
190
|
+
})
|
|
191
|
+
this.apiTracker.startHourlyLogging()
|
|
192
|
+
// Parity with HAP platform: indicate successful initialization of API tracking (Matter-specific wording)
|
|
193
|
+
this.debugLog('API request tracking initialized (Matter platform, OpenAPI)')
|
|
194
|
+
} catch (e: any) {
|
|
195
|
+
this.errorLog('Failed to initialize API request tracking:', e?.message ?? e)
|
|
196
|
+
}
|
|
113
197
|
}
|
|
114
198
|
|
|
115
199
|
try {
|
|
116
200
|
this.switchBotBLE = new SwitchBotBLE()
|
|
117
201
|
if (!this.config.options?.disableLogsforBLE && this.switchBotBLE?.on) {
|
|
118
|
-
this.switchBotBLE.on('log', (l: any) => this.
|
|
202
|
+
this.switchBotBLE.on('log', (l: any) => this.debugLog('[SwitchBot BLE]', l.message))
|
|
119
203
|
}
|
|
120
204
|
} catch (e: any) {
|
|
121
|
-
this.
|
|
205
|
+
this.errorLog('Failed to initialize SwitchBot BLE client:', e?.message ?? e)
|
|
122
206
|
}
|
|
123
207
|
|
|
124
208
|
// If BLE scanning is enabled, start scanning and route advertisements to registered handlers
|
|
@@ -128,7 +212,7 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
128
212
|
try {
|
|
129
213
|
await ble.startScan()
|
|
130
214
|
} catch (e: any) {
|
|
131
|
-
this.
|
|
215
|
+
this.errorLog(`Failed to start BLE scanning: ${e?.message ?? e}`)
|
|
132
216
|
}
|
|
133
217
|
|
|
134
218
|
// route advertisements to our handlers
|
|
@@ -140,29 +224,317 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
140
224
|
await handler(ad.serviceData)
|
|
141
225
|
}
|
|
142
226
|
} catch (e: any) {
|
|
143
|
-
this.
|
|
227
|
+
this.errorLog(`Failed to handle BLE advertisement: ${e?.message ?? e}`)
|
|
144
228
|
}
|
|
145
229
|
}
|
|
146
230
|
})()
|
|
147
231
|
}
|
|
148
232
|
|
|
149
|
-
//
|
|
150
|
-
|
|
233
|
+
// Ensure we clean up any per-device timers and BLE handlers when Homebridge shuts down
|
|
234
|
+
this.api.on('shutdown', async () => {
|
|
235
|
+
try {
|
|
236
|
+
this.infoLog('Homebridge shutting down: clearing refresh timers and BLE handlers')
|
|
237
|
+
|
|
238
|
+
// Stop API tracking hourly logging
|
|
239
|
+
if (this.apiTracker) {
|
|
240
|
+
this.apiTracker.stopHourlyLogging()
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Clear all refresh timers
|
|
244
|
+
for (const [nid, t] of Array.from(this.refreshTimers.entries())) {
|
|
245
|
+
try {
|
|
246
|
+
clearInterval(t)
|
|
247
|
+
} catch (e: any) {
|
|
248
|
+
this.debugLog(`Failed to clear timer for ${nid}: ${e?.message ?? e}`)
|
|
249
|
+
}
|
|
250
|
+
this.refreshTimers.delete(nid)
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// Clear platform-level refresh timer
|
|
254
|
+
try {
|
|
255
|
+
if (this.platformRefreshTimer) {
|
|
256
|
+
clearInterval(this.platformRefreshTimer)
|
|
257
|
+
this.platformRefreshTimer = undefined
|
|
258
|
+
}
|
|
259
|
+
} catch (e: any) {
|
|
260
|
+
this.debugLog(`Failed to clear platform refresh timer: ${e?.message ?? e}`)
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Clear accessory instances registry
|
|
264
|
+
try {
|
|
265
|
+
this.accessoryInstances.clear()
|
|
266
|
+
} catch (e: any) {
|
|
267
|
+
this.debugLog(`Failed to clear accessoryInstances: ${e?.message ?? e}`)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Remove BLE handlers
|
|
271
|
+
try {
|
|
272
|
+
for (const k of Object.keys(this.bleEventHandler)) {
|
|
273
|
+
delete this.bleEventHandler[k]
|
|
274
|
+
}
|
|
275
|
+
} catch (e: any) {
|
|
276
|
+
this.debugLog(`Failed to clear bleEventHandler: ${e?.message ?? e}`)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Stop BLE scanning if available
|
|
280
|
+
try {
|
|
281
|
+
if (this.switchBotBLE && typeof (this.switchBotBLE as any).stopScan === 'function') {
|
|
282
|
+
await (this.switchBotBLE as any).stopScan()
|
|
283
|
+
this.infoLog('Stopped BLE scanning')
|
|
284
|
+
}
|
|
285
|
+
} catch (e: any) {
|
|
286
|
+
this.debugLog(`Failed to stop BLE scanning: ${e?.message ?? e}`)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Persist backoff cooldowns to disk
|
|
290
|
+
try {
|
|
291
|
+
if (this.backoffFilePath) {
|
|
292
|
+
const obj: Record<string, number> = {}
|
|
293
|
+
for (const [k, v] of this.backoffCooldowns.entries()) {
|
|
294
|
+
if (Number.isFinite(v)) {
|
|
295
|
+
obj[k] = v
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
writeFileSync(this.backoffFilePath, JSON.stringify(obj, null, 2))
|
|
299
|
+
this.debugLog(`Saved ${Object.keys(obj).length} backoff cooldown entries`)
|
|
300
|
+
}
|
|
301
|
+
} catch (e: any) {
|
|
302
|
+
this.debugLog(`Failed to save backoff state: ${e?.message ?? e}`)
|
|
303
|
+
}
|
|
304
|
+
} catch (e: any) {
|
|
305
|
+
this.debugLog('Shutdown cleanup failed: %s', e?.message ?? e)
|
|
306
|
+
}
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
// perform device discovery from SwitchBot OpenAPI (if configured) and
|
|
310
|
+
// register Matter accessories after discovery completes. Previously we
|
|
311
|
+
// called discoverDevices() without awaiting it which caused registration
|
|
312
|
+
// to run before discovery finished and only example accessories were
|
|
313
|
+
// created. Use an async IIFE to sequentially await discovery then register.
|
|
314
|
+
;(async () => {
|
|
315
|
+
try {
|
|
316
|
+
await this.discoverDevices()
|
|
317
|
+
} catch (e: any) {
|
|
318
|
+
this.debugLog('Device discovery failed during startup: %s', e?.message ?? e)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
try {
|
|
322
|
+
await this.registerMatterAccessories()
|
|
323
|
+
} catch (e: any) {
|
|
324
|
+
this.errorLog('Failed to register Matter accessories: %s', e?.message ?? e)
|
|
325
|
+
}
|
|
326
|
+
})()
|
|
327
|
+
})
|
|
328
|
+
|
|
329
|
+
try {
|
|
330
|
+
this.setupMqtt()
|
|
331
|
+
} catch (e: any) {
|
|
332
|
+
this.errorLog(`Setup MQTT, Error Message: ${e?.message ?? e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
this.setupwebhook()
|
|
336
|
+
} catch (e: any) {
|
|
337
|
+
this.errorLog(`Setup Webhook, Error Message: ${e?.message ?? e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Initialize backoff persistence file path and load any saved cooldowns
|
|
341
|
+
try {
|
|
342
|
+
const storagePath = this.api.user.storagePath()
|
|
343
|
+
this.backoffFilePath = join(storagePath, `${PLUGIN_NAME.replace('@', '').replace('/', '-')}-matter-backoff.json`)
|
|
344
|
+
if (existsSync(this.backoffFilePath)) {
|
|
345
|
+
try {
|
|
346
|
+
const raw = readFileSync(this.backoffFilePath, 'utf8')
|
|
347
|
+
const parsed = JSON.parse(raw)
|
|
348
|
+
if (parsed && typeof parsed === 'object') {
|
|
349
|
+
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
|
|
350
|
+
const ts = Number(v)
|
|
351
|
+
if (Number.isFinite(ts)) {
|
|
352
|
+
this.backoffCooldowns.set(String(k), ts)
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
this.debugLog(`Loaded ${this.backoffCooldowns.size} backoff cooldown entries`)
|
|
356
|
+
}
|
|
357
|
+
} catch (e: any) {
|
|
358
|
+
this.debugLog(`Failed to load backoff state: ${e?.message ?? e}`)
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
} catch (e: any) {
|
|
362
|
+
this.debugLog(`Failed to initialize backoff persistence: ${e?.message ?? e}`)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Normalize a deviceId for matching (uppercase alphanumerics only)
|
|
368
|
+
* @deprecated Use shared normalizeDeviceId from utils.js instead
|
|
369
|
+
*/
|
|
370
|
+
private normalizeDeviceId(deviceId: string) {
|
|
371
|
+
return normalizeDeviceId(deviceId)
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** Determine the platform-level batch interval in seconds */
|
|
375
|
+
private getPlatformBatchInterval(): number {
|
|
376
|
+
const opt = this.config.options
|
|
377
|
+
const val = opt?.matterBatchRefreshRate ?? opt?.refreshRate ?? 300
|
|
378
|
+
const n = Number(val)
|
|
379
|
+
return Number.isFinite(n) && n > 0 ? n : 300
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Clear per-device resources: refresh timers, accessory instance registry, BLE handlers
|
|
384
|
+
*/
|
|
385
|
+
private clearDeviceResources(deviceId?: string) {
|
|
386
|
+
if (!deviceId) {
|
|
387
|
+
return
|
|
388
|
+
}
|
|
389
|
+
try {
|
|
390
|
+
const nid = this.normalizeDeviceId(deviceId)
|
|
391
|
+
const existing = this.refreshTimers.get(nid)
|
|
392
|
+
if (existing) {
|
|
393
|
+
try {
|
|
394
|
+
clearInterval(existing)
|
|
395
|
+
} catch (e: any) {
|
|
396
|
+
this.debugLog(`Failed to clear refresh timer for ${deviceId}: ${e?.message ?? e}`)
|
|
397
|
+
}
|
|
398
|
+
this.refreshTimers.delete(nid)
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
try {
|
|
402
|
+
this.accessoryInstances.delete(nid)
|
|
403
|
+
} catch (e: any) {
|
|
404
|
+
this.debugLog(`Failed to delete accessory instance for ${deviceId}: ${e?.message ?? e}`)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
try {
|
|
408
|
+
const mac = formatDeviceIdAsMac(deviceId).toLowerCase()
|
|
409
|
+
if (this.bleEventHandler[mac]) {
|
|
410
|
+
delete this.bleEventHandler[mac]
|
|
411
|
+
}
|
|
412
|
+
} catch (e: any) {
|
|
413
|
+
// formatting failed (not a MAC-like id) — ignore
|
|
414
|
+
this.debugLog(`clearDeviceResources: failed to remove BLE handler for ${deviceId}: ${e?.message ?? e}`)
|
|
415
|
+
}
|
|
416
|
+
} catch (e: any) {
|
|
417
|
+
this.debugLog(`clearDeviceResources top-level error for ${deviceId}: ${e?.message ?? e}`)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Merge discovered devices with per-device overrides from `config.options.devices`.
|
|
423
|
+
* Also applies device-type templates when applyToAllDevicesOfType is set.
|
|
424
|
+
*/
|
|
425
|
+
private async mergeDiscoveredDevices(discovered: device[]): Promise<any[]> {
|
|
426
|
+
// If there's no per-device config, return discovered as-is
|
|
427
|
+
if (!this.config.options?.devices) {
|
|
428
|
+
return discovered
|
|
429
|
+
}
|
|
151
430
|
|
|
152
|
-
|
|
431
|
+
// Assign missing deviceType from configDeviceType if needed
|
|
432
|
+
const devicesWithTypeAssigned = discovered.map((deviceObj) => {
|
|
433
|
+
if (!deviceObj.deviceType) {
|
|
434
|
+
deviceObj.deviceType = (deviceObj as any).configDeviceType !== undefined ? (deviceObj as any).configDeviceType : 'Unknown'
|
|
435
|
+
this.debugLog(`API missing deviceType for ${deviceObj.deviceId}, using configDeviceType: ${(deviceObj as any).configDeviceType}`)
|
|
436
|
+
}
|
|
437
|
+
return deviceObj
|
|
153
438
|
})
|
|
439
|
+
|
|
440
|
+
// Apply device-type templates from config entries with applyToAllDevicesOfType=true
|
|
441
|
+
const devicesWithTemplates = applyDeviceTypeTemplates(
|
|
442
|
+
devicesWithTypeAssigned,
|
|
443
|
+
this.config.options.devices,
|
|
444
|
+
'deviceType',
|
|
445
|
+
msg => this.debugLog(msg),
|
|
446
|
+
)
|
|
447
|
+
|
|
448
|
+
// Merge per-device overrides by matching deviceId
|
|
449
|
+
const allowConfigOnly = Boolean(this.config.options?.allowConfigOnlyDevices)
|
|
450
|
+
const merged = mergeByDeviceId(this.config.options?.devices ?? [], devicesWithTemplates ?? [], allowConfigOnly)
|
|
451
|
+
|
|
452
|
+
// Apply global webhook setting if not explicitly set on individual devices
|
|
453
|
+
if (this.config.options?.webhook === true) {
|
|
454
|
+
merged.forEach((device: any) => {
|
|
455
|
+
if (device.webhook === undefined) {
|
|
456
|
+
device.webhook = true
|
|
457
|
+
this.debugLog(`Applied global webhook setting to Matter device: ${device.deviceId}`)
|
|
458
|
+
}
|
|
459
|
+
})
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
return merged
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Merge discovered IR devices with per-device overrides from `config.options.irdevices`.
|
|
467
|
+
* Also applies device-type templates when applyToAllDevicesOfType is set.
|
|
468
|
+
*/
|
|
469
|
+
private async mergeDiscoveredIRDevices(discovered: irdevice[]): Promise<irdevice[]> {
|
|
470
|
+
// If there's no per-device config, return discovered as-is
|
|
471
|
+
if (!this.config.options?.irdevices) {
|
|
472
|
+
return discovered
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Assign missing remoteType from configRemoteType if needed
|
|
476
|
+
const devicesWithTypeAssigned = discovered.map((deviceObj) => {
|
|
477
|
+
if (!deviceObj.remoteType && (deviceObj as any).configRemoteType) {
|
|
478
|
+
deviceObj.remoteType = (deviceObj as any).configRemoteType
|
|
479
|
+
this.debugLog(`API missing remoteType for ${deviceObj.deviceId}, using configRemoteType: ${(deviceObj as any).configRemoteType}`)
|
|
480
|
+
} else if (!deviceObj.remoteType) {
|
|
481
|
+
this.warnLog(`No remoteType for IR device ${deviceObj.deviceId}, skipping`)
|
|
482
|
+
return null
|
|
483
|
+
}
|
|
484
|
+
return deviceObj
|
|
485
|
+
}).filter(device => device !== null) as irdevice[]
|
|
486
|
+
|
|
487
|
+
// Apply remote-type templates from config entries with applyToAllDevicesOfType=true
|
|
488
|
+
const devicesWithTemplates = applyDeviceTypeTemplates(
|
|
489
|
+
devicesWithTypeAssigned,
|
|
490
|
+
this.config.options.irdevices,
|
|
491
|
+
'remoteType',
|
|
492
|
+
msg => this.debugLog(msg),
|
|
493
|
+
)
|
|
494
|
+
|
|
495
|
+
// Merge per-device overrides by matching deviceId
|
|
496
|
+
const allowConfigOnly = Boolean(this.config.options?.allowConfigOnlyDevices)
|
|
497
|
+
type IRMerged = irdevice & irDevicesConfig
|
|
498
|
+
const merged = mergeByDeviceId(this.config.options?.irdevices ?? [], devicesWithTemplates ?? [], allowConfigOnly) as IRMerged[]
|
|
499
|
+
|
|
500
|
+
// Apply global webhook setting if not explicitly set on individual IR devices
|
|
501
|
+
if (this.config.options?.webhook === true) {
|
|
502
|
+
merged.forEach((device: any) => {
|
|
503
|
+
if (device.webhook === undefined) {
|
|
504
|
+
device.webhook = true
|
|
505
|
+
this.debugLog(`Applied global webhook setting to Matter IR device: ${device.deviceId}`)
|
|
506
|
+
}
|
|
507
|
+
})
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return merged as unknown as irdevice[]
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
/**
|
|
514
|
+
* Select effective connection type for a device: prefer explicit device.connectionType,
|
|
515
|
+
* otherwise prefer BLE when platform BLE is enabled and device provides a BLE model/id.
|
|
516
|
+
*/
|
|
517
|
+
private chooseConnectionType(deviceObj: any): 'BLE' | 'OpenAPI' {
|
|
518
|
+
if (deviceObj?.connectionType) {
|
|
519
|
+
return deviceObj.connectionType === 'BLE' ? 'BLE' : 'OpenAPI'
|
|
520
|
+
}
|
|
521
|
+
// If platform BLE is enabled and we have a bleModel or deviceId that formats to a MAC, prefer BLE
|
|
522
|
+
if (this.config.options?.BLE && (deviceObj?.bleModel || formatDeviceIdAsMac(deviceObj?.deviceId))) {
|
|
523
|
+
return 'BLE'
|
|
524
|
+
}
|
|
525
|
+
return 'OpenAPI'
|
|
154
526
|
}
|
|
155
527
|
|
|
156
528
|
/**
|
|
157
529
|
* Map a SwitchBot device object to a MatterAccessory using the device-specific
|
|
158
530
|
* Matter accessory classes in `src/devices-matter`.
|
|
159
531
|
*/
|
|
160
|
-
private async createAccessoryFromDevice(dev: device): Promise<MatterAccessory<Record<string, unknown>> | undefined> {
|
|
532
|
+
private async createAccessoryFromDevice(dev: device & devicesConfig): Promise<MatterAccessory<Record<string, unknown>> | undefined> {
|
|
161
533
|
// Basic metadata
|
|
162
534
|
const displayName = dev.deviceName ?? dev.deviceId ?? 'SwitchBot Device'
|
|
163
535
|
const serial = dev.deviceId ?? 'unknown'
|
|
164
536
|
const manufacturer = 'SwitchBot'
|
|
165
|
-
const model = dev.deviceType ?? 'SwitchBot'
|
|
537
|
+
const model = dev.model ?? dev.deviceType ?? 'SwitchBot'
|
|
166
538
|
const firmware = (dev as any).firmware ?? (dev as any).version ?? '0.0.0'
|
|
167
539
|
|
|
168
540
|
// Helper to build a default opts object consumed by the matter device classes
|
|
@@ -175,90 +547,90 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
175
547
|
firmwareRevision: String(firmware),
|
|
176
548
|
hardwareRevision: '1.0.0',
|
|
177
549
|
deviceId: dev.deviceId,
|
|
178
|
-
|
|
550
|
+
// Inject handy platform-side helpers into the accessory `context` so Matter
|
|
551
|
+
// accessory classes can perform OpenAPI/BLE actions without reaching into
|
|
552
|
+
// the platform implementation directly.
|
|
553
|
+
context: {
|
|
554
|
+
deviceId: dev.deviceId,
|
|
555
|
+
// Expose the display name so Matter accessory classes can read it from context
|
|
556
|
+
name: displayName,
|
|
557
|
+
// Provide device-level logging override (if present) and platform logging flag
|
|
558
|
+
// so accessories can decide how verbose they should be.
|
|
559
|
+
deviceLogging: (dev as any)?.logging,
|
|
560
|
+
platformLogging: this.platformLogging,
|
|
561
|
+
// Expose effective webhook setting for parity with HAP base device logic
|
|
562
|
+
// Prefer explicit per-device setting; otherwise fall back to global option
|
|
563
|
+
webhook: (dev as any)?.webhook !== undefined ? (dev as any).webhook : (this.config.options?.webhook === true ? true : undefined),
|
|
564
|
+
},
|
|
179
565
|
}
|
|
180
566
|
|
|
181
|
-
//
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
567
|
+
// Log effective webhook for diagnostics
|
|
568
|
+
try {
|
|
569
|
+
const effectiveWebhook = (dev as any)?.webhook !== undefined ? (dev as any).webhook : (this.config.options?.webhook === true ? true : undefined)
|
|
570
|
+
this.debugLog(`Effective webhook for Matter device ${displayName} (${dev.deviceId}): ${String(effectiveWebhook)}`)
|
|
571
|
+
} catch (e: any) {
|
|
572
|
+
this.debugLog(`Failed logging effective webhook for Matter device ${displayName} (${dev.deviceId}): ${e?.message ?? e}`)
|
|
186
573
|
}
|
|
187
574
|
|
|
188
|
-
//
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
while (attempt < maxRetries) {
|
|
199
|
-
try {
|
|
200
|
-
const list = await this.switchBotBLE.discover({ model: (dev as any).bleModel, id })
|
|
201
|
-
if (!Array.isArray(list) || list.length === 0) {
|
|
202
|
-
throw new Error('BLE device not found')
|
|
203
|
-
}
|
|
204
|
-
const deviceInst: any = list[0]
|
|
205
|
-
if (typeof deviceInst[methodName] !== 'function') {
|
|
206
|
-
throw new TypeError(`BLE method ${methodName} not available on device`)
|
|
207
|
-
}
|
|
208
|
-
return await deviceInst[methodName](...args)
|
|
209
|
-
} catch (e: any) {
|
|
210
|
-
attempt++
|
|
211
|
-
if (attempt >= maxRetries) {
|
|
212
|
-
throw e
|
|
213
|
-
}
|
|
214
|
-
this.log.debug(`BLE ${methodName} attempt ${attempt} failed for ${dev.deviceId}: ${e?.message ?? e}, retrying in ${retryDelay}ms`)
|
|
215
|
-
await sleep(retryDelay)
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
throw new Error('BLE operation failed')
|
|
575
|
+
// Build platform-side helpers using shared factories so they can be reused/tested
|
|
576
|
+
const sendOpenAPI = makeOpenAPISender(this.retryCommand.bind(this), dev, { maxRetries: this.config.options?.maxRetries ?? 1, delayBetweenRetries: this.config.options?.delayBetweenRetries ?? 1000 })
|
|
577
|
+
const sendBLE = makeBLESender(this.switchBotBLE, dev, { bleRetries: (this.config.options as any)?.bleRetries ?? 2, bleRetryDelay: (this.config.options as any)?.bleRetryDelay ?? 500 })
|
|
578
|
+
|
|
579
|
+
// Log that we're initializing this device so it's visible in startup logs
|
|
580
|
+
try {
|
|
581
|
+
this.infoLog(`Initializing Matter device: ${displayName} (type=${dev.deviceType ?? 'Unknown'}) id=${dev.deviceId}`)
|
|
582
|
+
} catch (e: any) {
|
|
583
|
+
// best-effort logging — swallow errors to avoid breaking initialization
|
|
584
|
+
this.debugLog('Failed to log initializing device:', e?.message ?? e)
|
|
219
585
|
}
|
|
220
586
|
|
|
221
|
-
const makeOnOffHandlers = (uuid: string) => ({
|
|
587
|
+
const makeOnOffHandlers = (uuid: string, connectionType: 'BLE' | 'OpenAPI') => ({
|
|
222
588
|
onOff: {
|
|
223
589
|
on: async () => {
|
|
224
590
|
try {
|
|
225
|
-
if (
|
|
591
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
226
592
|
await sendBLE('turnOn')
|
|
227
593
|
} else {
|
|
228
594
|
await sendOpenAPI('turnOn')
|
|
229
595
|
}
|
|
230
596
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.OnOff, { onOff: true })
|
|
231
597
|
} catch (e: any) {
|
|
232
|
-
this.
|
|
598
|
+
this.errorLog(`Failed to turn on device ${dev.deviceId}: ${e?.message ?? e}`)
|
|
233
599
|
}
|
|
234
600
|
},
|
|
235
601
|
off: async () => {
|
|
236
602
|
try {
|
|
237
|
-
if (
|
|
603
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
238
604
|
await sendBLE('turnOff')
|
|
239
605
|
} else {
|
|
240
606
|
await sendOpenAPI('turnOff')
|
|
241
607
|
}
|
|
242
608
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.OnOff, { onOff: false })
|
|
243
609
|
} catch (e: any) {
|
|
244
|
-
this.
|
|
610
|
+
this.errorLog(`Failed to turn off device ${dev.deviceId}: ${e?.message ?? e}`)
|
|
245
611
|
}
|
|
246
612
|
},
|
|
247
613
|
},
|
|
248
614
|
})
|
|
249
615
|
|
|
250
|
-
// Mapping from SwitchBot deviceType -> constructor
|
|
616
|
+
// Mapping from SwitchBot deviceType -> constructor (expanded for parity with HAP)
|
|
251
617
|
const mapping: { [key: string]: any } = {
|
|
252
618
|
// Plugs / Outlets
|
|
253
619
|
'Plug': OnOffOutletAccessory,
|
|
254
620
|
'Plug Mini (US)': OnOffOutletAccessory,
|
|
255
621
|
'Plug Mini (JP)': OnOffOutletAccessory,
|
|
622
|
+
'Plug Mini': OnOffOutletAccessory,
|
|
623
|
+
'WoPlug': OnOffOutletAccessory,
|
|
256
624
|
|
|
257
625
|
// Lighting
|
|
258
626
|
'Color Bulb': ColorLightAccessory,
|
|
627
|
+
'Color Bulb Mini': ColorLightAccessory,
|
|
259
628
|
'Ceiling Light': ColorTemperatureLightAccessory,
|
|
260
629
|
'Ceiling Light Pro': ColorTemperatureLightAccessory,
|
|
261
630
|
'Strip Light': ExtendedColorLightAccessory,
|
|
631
|
+
'Light Strip': ExtendedColorLightAccessory,
|
|
632
|
+
'Light Strip Plus': ExtendedColorLightAccessory,
|
|
633
|
+
'Strip Light Pro': ExtendedColorLightAccessory,
|
|
262
634
|
'Dimmable Light': DimmableLightAccessory,
|
|
263
635
|
|
|
264
636
|
// Robot Vacuums
|
|
@@ -269,49 +641,62 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
269
641
|
'Robot Vacuum Cleaner S1': RoboticVacuumAccessory,
|
|
270
642
|
'Robot Vacuum Cleaner S1 Plus': RoboticVacuumAccessory,
|
|
271
643
|
'Robot Vacuum Cleaner S10': RoboticVacuumAccessory,
|
|
644
|
+
'Robot Vacuum Cleaner S1 Pro': RoboticVacuumAccessory,
|
|
645
|
+
'Robot Vacuum Cleaner S1 Mini': RoboticVacuumAccessory,
|
|
272
646
|
|
|
273
647
|
// Locks
|
|
274
648
|
'Smart Lock': DoorLockAccessory,
|
|
275
649
|
'Smart Lock Pro': DoorLockAccessory,
|
|
650
|
+
'Smart Lock Ultra': DoorLockAccessory,
|
|
276
651
|
|
|
277
652
|
// Sensors
|
|
278
653
|
'Motion Sensor': OccupancySensorAccessory,
|
|
279
654
|
'Contact Sensor': ContactSensorAccessory,
|
|
280
655
|
'Water Detector': LeakSensorAccessory,
|
|
281
|
-
|
|
282
|
-
'
|
|
283
|
-
'
|
|
284
|
-
'
|
|
285
|
-
'
|
|
286
|
-
|
|
656
|
+
// Meter devices should expose both temperature and humidity sensors
|
|
657
|
+
'Meter': [TemperatureSensorAccessory, HumiditySensorAccessory],
|
|
658
|
+
'MeterPlus': [TemperatureSensorAccessory, HumiditySensorAccessory],
|
|
659
|
+
'MeterPro': [TemperatureSensorAccessory, HumiditySensorAccessory],
|
|
660
|
+
'WoIOSensor': [TemperatureSensorAccessory, HumiditySensorAccessory],
|
|
661
|
+
'Air Purifier PM2.5': [HumiditySensorAccessory],
|
|
662
|
+
'Air Purifier Table PM2.5': [HumiditySensorAccessory],
|
|
663
|
+
'Air Purifier': [HumiditySensorAccessory],
|
|
664
|
+
'Air Purifier VOC': [HumiditySensorAccessory],
|
|
665
|
+
'Air Purifier Table VOC': [HumiditySensorAccessory],
|
|
287
666
|
// Fans
|
|
288
667
|
'Battery Circulator Fan': FanAccessory,
|
|
289
668
|
|
|
290
669
|
// Curtains / Blinds
|
|
291
670
|
'Blind Tilt': VenetianBlindAccessory,
|
|
292
671
|
'Curtain': WindowBlindAccessory,
|
|
672
|
+
'Curtain2': WindowBlindAccessory,
|
|
293
673
|
'Curtain3': WindowBlindAccessory,
|
|
674
|
+
'Curtain 2': WindowBlindAccessory,
|
|
294
675
|
'WoRollerShade': WindowBlindAccessory,
|
|
295
676
|
'Roller Shade': WindowBlindAccessory,
|
|
677
|
+
'Venetian Blind': VenetianBlindAccessory,
|
|
296
678
|
|
|
297
679
|
// Switches / Relays
|
|
298
680
|
'Relay Switch 1': OnOffSwitchAccessory,
|
|
299
681
|
'Relay Switch 1PM': OnOffSwitchAccessory,
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
'
|
|
305
|
-
'
|
|
306
|
-
'
|
|
307
|
-
'
|
|
308
|
-
'
|
|
309
|
-
'
|
|
682
|
+
'Relay Switch 2': OnOffSwitchAccessory,
|
|
683
|
+
'Relay Switch 3': OnOffSwitchAccessory,
|
|
684
|
+
|
|
685
|
+
// Misc / hubs / other
|
|
686
|
+
'Hub 2': [TemperatureSensorAccessory, HumiditySensorAccessory],
|
|
687
|
+
'Hub 3': [TemperatureSensorAccessory, HumiditySensorAccessory, OccupancySensorAccessory, LightSensorAccessory],
|
|
688
|
+
'Hub Mini': undefined,
|
|
689
|
+
'Bot': OnOffSwitchAccessory,
|
|
690
|
+
'Smart Bot': OnOffSwitchAccessory,
|
|
691
|
+
'Humidifier': [HumiditySensorAccessory],
|
|
692
|
+
'Humidifier2': [HumiditySensorAccessory],
|
|
693
|
+
'Thermostat': ThermostatAccessory,
|
|
694
|
+
'Water Heater': ThermostatAccessory,
|
|
310
695
|
}
|
|
311
696
|
|
|
312
697
|
const Ctor = mapping[dev.deviceType ?? '']
|
|
313
698
|
if (!Ctor) {
|
|
314
|
-
this.
|
|
699
|
+
this.debugLog(`No Matter mapping for deviceType='${dev.deviceType}', deviceId=${dev.deviceId}`)
|
|
315
700
|
return undefined
|
|
316
701
|
}
|
|
317
702
|
|
|
@@ -319,8 +704,11 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
319
704
|
const uuid = baseOpts.uuid
|
|
320
705
|
const handlers: Record<string, any> = {}
|
|
321
706
|
|
|
707
|
+
// Choose connection type for this device (BLE vs OpenAPI)
|
|
708
|
+
const connectionType = this.chooseConnectionType(dev)
|
|
709
|
+
|
|
322
710
|
// On/Off common
|
|
323
|
-
handlers.onOff = makeOnOffHandlers(uuid).onOff
|
|
711
|
+
handlers.onOff = makeOnOffHandlers(uuid, connectionType).onOff
|
|
324
712
|
|
|
325
713
|
// If this is a light, add brightness and color handlers
|
|
326
714
|
if (['Color Bulb', 'Ceiling Light', 'Ceiling Light Pro', 'Strip Light', 'Dimmable Light'].includes(dev.deviceType ?? '')) {
|
|
@@ -330,14 +718,14 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
330
718
|
try {
|
|
331
719
|
const level = request.level as number
|
|
332
720
|
const percent = Math.round((level / 254) * 100)
|
|
333
|
-
if (
|
|
721
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
334
722
|
await sendBLE('setBrightness', percent)
|
|
335
723
|
} else {
|
|
336
724
|
await sendOpenAPI('setBrightness', `${percent}`)
|
|
337
725
|
}
|
|
338
726
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.LevelControl, { currentLevel: level })
|
|
339
727
|
} catch (e: any) {
|
|
340
|
-
this.
|
|
728
|
+
this.errorLog(`Failed to set brightness for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
341
729
|
}
|
|
342
730
|
},
|
|
343
731
|
}
|
|
@@ -349,14 +737,14 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
349
737
|
const hue = request.hue as number
|
|
350
738
|
const saturation = request.saturation as number
|
|
351
739
|
const [r, g, b] = hs2rgb(Math.round((hue / 254) * 360), Math.round((saturation / 254) * 100))
|
|
352
|
-
if (
|
|
740
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
353
741
|
await sendBLE('setRGB', Number(request.level ?? 100), r, g, b)
|
|
354
742
|
} else {
|
|
355
743
|
await sendOpenAPI('setColor', `${r}:${g}:${b}`)
|
|
356
744
|
}
|
|
357
745
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.ColorControl, { currentHue: hue, currentSaturation: saturation })
|
|
358
746
|
} catch (e: any) {
|
|
359
|
-
this.
|
|
747
|
+
this.errorLog(`Failed to set hue/sat for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
360
748
|
}
|
|
361
749
|
},
|
|
362
750
|
moveToColorLogic: async (request: any) => {
|
|
@@ -368,14 +756,14 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
368
756
|
const hueApprox = Math.round((colorX / 65535) * 360)
|
|
369
757
|
const satApprox = Math.round((colorY / 65535) * 100)
|
|
370
758
|
const [r, g, b] = hs2rgb(hueApprox, satApprox)
|
|
371
|
-
if (
|
|
759
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
372
760
|
await sendBLE('setRGB', Number(request.level ?? 100), r, g, b)
|
|
373
761
|
} else {
|
|
374
762
|
await sendOpenAPI('setColor', `${r}:${g}:${b}`)
|
|
375
763
|
}
|
|
376
764
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.ColorControl, { currentX: colorX, currentY: colorY })
|
|
377
765
|
} catch (e: any) {
|
|
378
|
-
this.
|
|
766
|
+
this.errorLog(`Failed to set XY color for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
379
767
|
}
|
|
380
768
|
},
|
|
381
769
|
}
|
|
@@ -385,23 +773,90 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
385
773
|
moveToColorTemperature: async (request: any) => {
|
|
386
774
|
try {
|
|
387
775
|
const kelvin = Math.round(1000000 / Number(request.colorTemperature))
|
|
388
|
-
if (
|
|
776
|
+
if (connectionType === 'BLE' && this.switchBotBLE) {
|
|
389
777
|
await sendBLE('setColorTemperature', kelvin)
|
|
390
778
|
} else {
|
|
391
779
|
await sendOpenAPI('setColorTemperature', `${kelvin}`)
|
|
392
780
|
}
|
|
393
781
|
await this.api.matter.updateAccessoryState(uuid, this.api.matter.clusterNames.ColorControl, { currentX: request.colorX ?? 0, currentY: request.colorY ?? 0 })
|
|
394
782
|
} catch (e: any) {
|
|
395
|
-
this.
|
|
783
|
+
this.errorLog(`Failed to set color temperature for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
784
|
+
}
|
|
785
|
+
},
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
// Ensure windowCovering.goToLiftPercentage handler for VenetianBlindAccessory and WindowBlindAccessory
|
|
789
|
+
if (['Venetian Blind', 'Blind Tilt'].includes(dev.deviceType ?? '')) {
|
|
790
|
+
handlers.windowCovering = {
|
|
791
|
+
goToLiftPercentage: async (request: any) => {
|
|
792
|
+
// Forward to accessory instance if available
|
|
793
|
+
const instance = this.accessoryInstances.get(normalizeDeviceId(dev.deviceId))
|
|
794
|
+
if (instance && typeof instance.handleGoToLift === 'function') {
|
|
795
|
+
return instance.handleGoToLift(request)
|
|
796
|
+
}
|
|
797
|
+
this.warnLog(`No instance or handleGoToLift for VenetianBlindAccessory deviceId=${dev.deviceId}`)
|
|
798
|
+
},
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
if ([
|
|
802
|
+
'Curtain',
|
|
803
|
+
'Curtain2',
|
|
804
|
+
'Curtain3',
|
|
805
|
+
'Curtain 2',
|
|
806
|
+
'WoRollerShade',
|
|
807
|
+
'Roller Shade',
|
|
808
|
+
].includes(dev.deviceType ?? '')) {
|
|
809
|
+
handlers.windowCovering = {
|
|
810
|
+
goToLiftPercentage: async (request: any) => {
|
|
811
|
+
// Forward to accessory instance if available
|
|
812
|
+
const instance = this.accessoryInstances.get(normalizeDeviceId(dev.deviceId))
|
|
813
|
+
if (instance && typeof instance.handleGoToLift === 'function') {
|
|
814
|
+
return instance.handleGoToLift(request)
|
|
396
815
|
}
|
|
816
|
+
this.warnLog(`No instance or handleGoToLift for WindowBlindAccessory deviceId=${dev.deviceId}`)
|
|
397
817
|
},
|
|
398
818
|
}
|
|
399
819
|
}
|
|
400
820
|
|
|
821
|
+
// Expose platform helpers to the accessory via context so accessory
|
|
822
|
+
// classes can call OpenAPI/BLE actions (sendOpenAPI/sendBLE) and know
|
|
823
|
+
// the effective connection type.
|
|
824
|
+
try {
|
|
825
|
+
/* Inject platform helpers (OpenAPI/BLE senders + logging helpers + connection type)
|
|
826
|
+
into the accessory context so Matter accessory classes can use them without
|
|
827
|
+
reaching into the platform implementation directly. */
|
|
828
|
+
;(baseOpts as any).context = Object.assign({}, (baseOpts as any).context, {
|
|
829
|
+
sendOpenAPI,
|
|
830
|
+
sendBLE,
|
|
831
|
+
connectionType,
|
|
832
|
+
// Expose platform logging helpers so accessories can use consistent logging
|
|
833
|
+
infoLog: this.infoLog,
|
|
834
|
+
debugLog: this.debugLog,
|
|
835
|
+
warnLog: this.warnLog,
|
|
836
|
+
errorLog: this.errorLog,
|
|
837
|
+
successLog: this.successLog,
|
|
838
|
+
})
|
|
839
|
+
} catch (e: any) {
|
|
840
|
+
this.debugLog('Failed to attach platform helpers to baseOpts.context: %s', e?.message ?? e)
|
|
841
|
+
}
|
|
842
|
+
|
|
401
843
|
const opts = Object.assign({}, baseOpts, { handlers })
|
|
402
844
|
|
|
403
845
|
// Instantiate the device class and return its serialized accessory
|
|
404
846
|
const instance = new Ctor(this.api, this.log, opts)
|
|
847
|
+
// Save instance in registry so platform can call device-specific update methods if needed
|
|
848
|
+
try {
|
|
849
|
+
if (dev?.deviceId) {
|
|
850
|
+
this.accessoryInstances.set(normalizeDeviceId(dev.deviceId), instance)
|
|
851
|
+
}
|
|
852
|
+
} catch (e: any) {
|
|
853
|
+
this.debugLog('Failed to register accessory instance: %s', e?.message ?? e)
|
|
854
|
+
}
|
|
855
|
+
try {
|
|
856
|
+
this.infoLog(`Initialized Matter accessory: ${displayName} (type=${dev.deviceType ?? 'Unknown'}) id=${dev.deviceId}`)
|
|
857
|
+
} catch (e: any) {
|
|
858
|
+
this.debugLog('Failed to log initialized accessory:', e?.message ?? e)
|
|
859
|
+
}
|
|
405
860
|
|
|
406
861
|
// Register BLE->Matter push handler for this device's MAC (if BLE scanning is active)
|
|
407
862
|
try {
|
|
@@ -413,6 +868,12 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
413
868
|
// First try model-specific / normalized parsing of BLE advertisement
|
|
414
869
|
try {
|
|
415
870
|
const parsed = this.parseAdvertisementForDevice(dev, serviceData)
|
|
871
|
+
try {
|
|
872
|
+
const _p = JSON.stringify(parsed)
|
|
873
|
+
this.debugLog(`BLE advertisement parsed for ${dev.deviceId}: ${_p}`)
|
|
874
|
+
} catch (e) {
|
|
875
|
+
this.debugLog(`BLE advertisement parsed for ${dev.deviceId}: [unstringifiable parse result]`)
|
|
876
|
+
}
|
|
416
877
|
if (parsed) {
|
|
417
878
|
// Power
|
|
418
879
|
if (parsed.power !== undefined) {
|
|
@@ -421,7 +882,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
421
882
|
|
|
422
883
|
// Brightness
|
|
423
884
|
if (parsed.brightness !== undefined) {
|
|
424
|
-
const
|
|
885
|
+
const rawLevel = Math.round((Number(parsed.brightness) / 100) * 254)
|
|
886
|
+
const level = Math.max(0, Math.min(254, rawLevel))
|
|
887
|
+
this.debugLog(`[BLE Brightness Debug] Device ${dev.deviceId}: rawBrightness=${parsed.brightness}, calculated=${rawLevel}, clamped=${level}`)
|
|
425
888
|
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.LevelControl, { currentLevel: level })
|
|
426
889
|
}
|
|
427
890
|
|
|
@@ -429,7 +892,155 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
429
892
|
if (parsed.color !== undefined) {
|
|
430
893
|
const { r, g, b } = parsed.color
|
|
431
894
|
const [h, s] = rgb2hs(r, g, b)
|
|
432
|
-
|
|
895
|
+
const rawHue = Math.round((h / 360) * 254)
|
|
896
|
+
const rawSat = Math.round((s / 100) * 254)
|
|
897
|
+
const hue = Math.max(0, Math.min(254, rawHue))
|
|
898
|
+
const sat = Math.max(0, Math.min(254, rawSat))
|
|
899
|
+
this.debugLog(`[BLE Color Debug] Device ${dev.deviceId}: RGB=[${r},${g},${b}], HS=[${h},${s}], Matter=[${rawHue},${rawSat}], clamped=[${hue},${sat}]`)
|
|
900
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.ColorControl, { currentHue: hue, currentSaturation: sat })
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
// Battery -> powerSource cluster (common mapping)
|
|
904
|
+
if (parsed.battery !== undefined) {
|
|
905
|
+
// Skip battery updates for device types that don't support PowerSource cluster
|
|
906
|
+
const deviceType = String(dev?.deviceType ?? '')
|
|
907
|
+
const unsupportedTypes = ['Curtain', 'Curtain2', 'Curtain3', 'Curtain 2', 'Blind Tilt']
|
|
908
|
+
|
|
909
|
+
if (unsupportedTypes.includes(deviceType)) {
|
|
910
|
+
this.debugLog(`Device ${dev.deviceId} type ${deviceType} does not support PowerSource cluster, skipping BLE battery update`)
|
|
911
|
+
} else {
|
|
912
|
+
try {
|
|
913
|
+
const percentage = Number(parsed.battery)
|
|
914
|
+
const batPercentRemaining = Math.max(0, Math.min(200, Math.round(percentage * 2)))
|
|
915
|
+
let batChargeLevel = 0
|
|
916
|
+
if (percentage < 20) {
|
|
917
|
+
batChargeLevel = 2
|
|
918
|
+
} else if (percentage < 40) {
|
|
919
|
+
batChargeLevel = 1
|
|
920
|
+
}
|
|
921
|
+
try {
|
|
922
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'powerSource', { batPercentRemaining, batChargeLevel })
|
|
923
|
+
} catch (updateError: any) {
|
|
924
|
+
// Silently skip if powerSource cluster doesn't exist on this device
|
|
925
|
+
const msg = String(updateError?.message ?? updateError)
|
|
926
|
+
if (!msg.includes('does not exist') && !msg.includes('not found')) {
|
|
927
|
+
throw updateError
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
} catch (e: any) {
|
|
931
|
+
this.debugLog(`Failed to update battery state for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
// Temperature -> temperatureMeasurement
|
|
937
|
+
if (parsed.temperature !== undefined) {
|
|
938
|
+
try {
|
|
939
|
+
const c = Number(parsed.temperature)
|
|
940
|
+
const measured = Math.round(c * 100)
|
|
941
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'temperatureMeasurement', { measuredValue: measured })
|
|
942
|
+
} catch (e: any) {
|
|
943
|
+
this.debugLog(`Failed to update temperature for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// Humidity -> relativeHumidityMeasurement
|
|
948
|
+
if (parsed.humidity !== undefined) {
|
|
949
|
+
try {
|
|
950
|
+
const percent = Number(parsed.humidity)
|
|
951
|
+
const measured = Math.round(percent * 100)
|
|
952
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'relativeHumidityMeasurement', { measuredValue: measured })
|
|
953
|
+
} catch (e: any) {
|
|
954
|
+
this.debugLog(`Failed to update humidity for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// Contact / Leak -> BooleanState
|
|
959
|
+
if (parsed.contact !== undefined || parsed.leak !== undefined) {
|
|
960
|
+
try {
|
|
961
|
+
// Some devices report contact as true=open; ContactSensor expects inverted value
|
|
962
|
+
const isContactOpen = parsed.contact === undefined ? undefined : Boolean(parsed.contact)
|
|
963
|
+
const leakDetected = parsed.leak === undefined ? undefined : Boolean(parsed.leak)
|
|
964
|
+
|
|
965
|
+
if (isContactOpen !== undefined) {
|
|
966
|
+
// If this is a contact sensor device type, invert; otherwise set conservatively
|
|
967
|
+
if ((dev.deviceType || '').includes('Contact')) {
|
|
968
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.BooleanState, { stateValue: !isContactOpen })
|
|
969
|
+
} else {
|
|
970
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.BooleanState, { stateValue: isContactOpen })
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
if (leakDetected !== undefined) {
|
|
975
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.BooleanState, { stateValue: leakDetected })
|
|
976
|
+
}
|
|
977
|
+
} catch (e: any) {
|
|
978
|
+
this.debugLog(`Failed to update contact/leak for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
// Motion -> occupancy
|
|
983
|
+
if (parsed.motion !== undefined) {
|
|
984
|
+
try {
|
|
985
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'occupancySensing', { occupancy: { occupied: Boolean(parsed.motion) } })
|
|
986
|
+
} catch (e: any) {
|
|
987
|
+
this.debugLog(`Failed to update occupancy for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// Lock state -> doorLock
|
|
992
|
+
if (parsed.lock !== undefined) {
|
|
993
|
+
try {
|
|
994
|
+
const s = String(parsed.lock).toLowerCase()
|
|
995
|
+
let lockState = 0
|
|
996
|
+
if (s === 'locked' || s === '1' || s === 'true') {
|
|
997
|
+
lockState = 1
|
|
998
|
+
} else if (s === 'unlocked' || s === '0' || s === 'false') {
|
|
999
|
+
lockState = 2
|
|
1000
|
+
}
|
|
1001
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.DoorLock, { lockState })
|
|
1002
|
+
} catch (e: any) {
|
|
1003
|
+
this.debugLog(`Failed to update lock for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// Position / Cover -> WindowCovering (convert open percent to closed*100)
|
|
1008
|
+
if (parsed.position !== undefined) {
|
|
1009
|
+
try {
|
|
1010
|
+
const openPercent = Number(parsed.position)
|
|
1011
|
+
const closedPercent = 100 - Math.max(0, Math.min(100, openPercent))
|
|
1012
|
+
const value = Math.round(closedPercent * 100)
|
|
1013
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.WindowCovering, { currentPositionLiftPercent100ths: value, targetPositionLiftPercent100ths: value })
|
|
1014
|
+
} catch (e: any) {
|
|
1015
|
+
this.debugLog(`Failed to update cover position for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
// Fan speed -> FanControl
|
|
1020
|
+
if (parsed.fanSpeed !== undefined) {
|
|
1021
|
+
try {
|
|
1022
|
+
const percent = Number(parsed.fanSpeed)
|
|
1023
|
+
await this.api.matter.updateAccessoryState(uuidLocal, this.api.matter.clusterNames.FanControl, { percentSetting: percent, percentCurrent: percent })
|
|
1024
|
+
} catch (e: any) {
|
|
1025
|
+
this.debugLog(`Failed to update fan speed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
// Robot vacuum fields (battery already handled) - update run/operational/clean modes when present
|
|
1030
|
+
if (parsed.rvcRunMode !== undefined) {
|
|
1031
|
+
try {
|
|
1032
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'rvcRunMode', { currentMode: Number(parsed.rvcRunMode) })
|
|
1033
|
+
} catch (e: any) {
|
|
1034
|
+
this.debugLog(`Failed to update rvcRunMode for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
if (parsed.rvcOperationalState !== undefined) {
|
|
1039
|
+
try {
|
|
1040
|
+
await this.api.matter.updateAccessoryState(uuidLocal, 'rvcOperationalState', { operationalState: Number(parsed.rvcOperationalState) })
|
|
1041
|
+
} catch (e: any) {
|
|
1042
|
+
this.debugLog(`Failed to update rvcOperationalState for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1043
|
+
}
|
|
433
1044
|
}
|
|
434
1045
|
|
|
435
1046
|
// If we parsed something from serviceData prefer it and return early
|
|
@@ -438,7 +1049,7 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
438
1049
|
}
|
|
439
1050
|
}
|
|
440
1051
|
} catch (e: any) {
|
|
441
|
-
this.
|
|
1052
|
+
this.debugLog(`BLE advertisement parsing failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
442
1053
|
}
|
|
443
1054
|
|
|
444
1055
|
// Fallback to OpenAPI getDeviceStatus when serviceData is not present or parsing failed
|
|
@@ -446,50 +1057,122 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
446
1057
|
return
|
|
447
1058
|
}
|
|
448
1059
|
try {
|
|
449
|
-
|
|
450
|
-
if (!(statusCode === 100 || statusCode === 200)) {
|
|
1060
|
+
if (!this.apiTracker?.trySpend('poll')) {
|
|
451
1061
|
return
|
|
452
1062
|
}
|
|
1063
|
+
const { response, statusCode } = await this.switchBotAPI.getDeviceStatus(dev.deviceId, this.config.credentials?.token, this.config.credentials?.secret)
|
|
453
1064
|
const respAny: any = response
|
|
454
1065
|
const body = respAny?.body ?? respAny
|
|
1066
|
+
try {
|
|
1067
|
+
const s = JSON.stringify(body)
|
|
1068
|
+
this.debugLog(`OpenAPI getDeviceStatus for ${dev.deviceId} returned statusCode=${statusCode} body=${s}`)
|
|
1069
|
+
} catch (e) {
|
|
1070
|
+
this.debugLog(`OpenAPI getDeviceStatus for ${dev.deviceId} returned statusCode=${statusCode} (body not stringifiable)`)
|
|
1071
|
+
}
|
|
1072
|
+
if (!isSuccessfulStatusCode(statusCode)) {
|
|
1073
|
+
return
|
|
1074
|
+
}
|
|
455
1075
|
const status = body?.status ?? body
|
|
456
1076
|
|
|
457
|
-
//
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
1077
|
+
// Use centralized mapper which prefers accessory instance update helpers
|
|
1078
|
+
await this.applyStatusWithRegistrationRetry(uuidLocal, dev, status)
|
|
1079
|
+
} catch (e: any) {
|
|
1080
|
+
this.debugLog(`BLE push handler failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
} catch (e: any) {
|
|
1084
|
+
this.debugLog(`Failed to register BLE handler for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
// Schedule periodic OpenAPI refreshes for this device (if OpenAPI is configured)
|
|
1088
|
+
try {
|
|
1089
|
+
const nid = this.normalizeDeviceId(dev.deviceId)
|
|
1090
|
+
// Clear any existing timer for this device
|
|
1091
|
+
const existing = this.refreshTimers.get(nid)
|
|
1092
|
+
if (existing) {
|
|
1093
|
+
clearInterval(existing)
|
|
1094
|
+
this.refreshTimers.delete(nid)
|
|
1095
|
+
}
|
|
462
1096
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
1097
|
+
const platformInterval = this.getPlatformBatchInterval()
|
|
1098
|
+
const hasDeviceInterval = typeof dev.refreshRate === 'number' && Number(dev.refreshRate) > 0
|
|
1099
|
+
if (this.switchBotAPI && (hasDeviceInterval || platformInterval > 0)) {
|
|
1100
|
+
// One-shot to populate initial state AFTER registration has likely completed
|
|
1101
|
+
// Defer slightly to avoid race where updateAccessoryState runs before registration
|
|
1102
|
+
;(async () => {
|
|
1103
|
+
await sleep(250)
|
|
1104
|
+
try {
|
|
1105
|
+
this.infoLog(`Performing initial OpenAPI refresh for ${dev.deviceId}`)
|
|
1106
|
+
if (!this.apiTracker?.trySpend('poll')) {
|
|
1107
|
+
this.warnLog(`Skipping initial OpenAPI refresh for ${dev.deviceId} due to daily budget`)
|
|
1108
|
+
return
|
|
1109
|
+
}
|
|
1110
|
+
const { response, statusCode } = await this.switchBotAPI!.getDeviceStatus(dev.deviceId, this.config.credentials?.token, this.config.credentials?.secret)
|
|
1111
|
+
const respAny: any = response
|
|
1112
|
+
const body = respAny?.body ?? respAny
|
|
1113
|
+
try {
|
|
1114
|
+
const s = JSON.stringify(body)
|
|
1115
|
+
this.debugLog(`Initial OpenAPI refresh for ${dev.deviceId} returned statusCode=${statusCode} body=${s}`)
|
|
1116
|
+
} catch (e) {
|
|
1117
|
+
this.debugLog(`Initial OpenAPI refresh for ${dev.deviceId} returned statusCode=${statusCode} (body not stringifiable)`)
|
|
1118
|
+
}
|
|
1119
|
+
if (isSuccessfulStatusCode(statusCode)) {
|
|
1120
|
+
const status = body?.status ?? body
|
|
1121
|
+
await this.applyStatusWithRegistrationRetry(uuid, dev, status)
|
|
1122
|
+
this.infoLog(`Initial OpenAPI refresh succeeded for ${dev.deviceId}`)
|
|
1123
|
+
} else {
|
|
1124
|
+
this.warnLog(`Initial OpenAPI refresh returned unexpected statusCode=${statusCode} for ${dev.deviceId}`)
|
|
1125
|
+
}
|
|
1126
|
+
} catch (e: any) {
|
|
1127
|
+
this.errorLog(`Initial OpenAPI refresh failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
467
1128
|
}
|
|
1129
|
+
})()
|
|
468
1130
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
1131
|
+
if (hasDeviceInterval) {
|
|
1132
|
+
// Create a per-device timer and exclude it from batch
|
|
1133
|
+
const interval = Number(dev.refreshRate)
|
|
1134
|
+
this.perDeviceRefreshSet.add(nid)
|
|
1135
|
+
const timer = setInterval(async () => {
|
|
1136
|
+
try {
|
|
1137
|
+
// Skip if device is under cooldown
|
|
1138
|
+
const now = Date.now()
|
|
1139
|
+
const nextAllowed = this.backoffCooldowns.get(nid) ?? 0
|
|
1140
|
+
if (now < nextAllowed) {
|
|
1141
|
+
return
|
|
1142
|
+
}
|
|
1143
|
+
await this.refreshSingleDeviceWithRetry(dev)
|
|
1144
|
+
} catch (e: any) {
|
|
1145
|
+
this.debugLog(`Per-device refresh failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
483
1146
|
}
|
|
484
|
-
|
|
485
|
-
|
|
1147
|
+
}, interval * 1000)
|
|
1148
|
+
this.refreshTimers.set(nid, timer)
|
|
1149
|
+
this.infoLog(`Started per-device refresh timer for ${dev.deviceId} at ${interval}s`)
|
|
1150
|
+
} else {
|
|
1151
|
+
// Start platform-level batched refresh timer (only once)
|
|
1152
|
+
this.startPlatformRefreshTimer(platformInterval)
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
} catch (e: any) {
|
|
1156
|
+
this.debugLog(`Failed to schedule refresh for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
// Register webhook handler for this device
|
|
1160
|
+
try {
|
|
1161
|
+
if (dev.webhook && dev.deviceId) {
|
|
1162
|
+
this.debugLog(`Registering webhook handler for Matter device: ${dev.deviceId}`)
|
|
1163
|
+
this.webhookEventHandler[dev.deviceId] = async (context: any) => {
|
|
1164
|
+
try {
|
|
1165
|
+
this.debugLog(`Received webhook for Matter device ${dev.deviceId}: ${JSON.stringify(context)}`)
|
|
1166
|
+
// Apply webhook status update to the accessory
|
|
1167
|
+
await this.applyStatusWithRegistrationRetry(uuid, dev, context)
|
|
1168
|
+
} catch (e: any) {
|
|
1169
|
+
this.errorLog(`Failed to handle webhook for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
486
1170
|
}
|
|
487
|
-
} catch (e: any) {
|
|
488
|
-
this.log.debug(`BLE push handler failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
489
1171
|
}
|
|
1172
|
+
this.debugSuccessLog(`Webhook handler registered for ${dev.deviceId}`)
|
|
490
1173
|
}
|
|
491
1174
|
} catch (e: any) {
|
|
492
|
-
this.
|
|
1175
|
+
this.debugLog(`Failed to register webhook handler for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
493
1176
|
}
|
|
494
1177
|
|
|
495
1178
|
return instance.toAccessory()
|
|
@@ -500,40 +1183,137 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
500
1183
|
*/
|
|
501
1184
|
private async discoverDevices(): Promise<void> {
|
|
502
1185
|
if (!this.switchBotAPI) {
|
|
503
|
-
this.
|
|
1186
|
+
this.debugLog('SwitchBot OpenAPI not configured; skipping discovery')
|
|
504
1187
|
return
|
|
505
1188
|
}
|
|
506
1189
|
|
|
507
1190
|
try {
|
|
1191
|
+
if (!this.apiTracker?.trySpend('discovery')) {
|
|
1192
|
+
this.warnLog('OpenAPI daily budget reached; skipping Matter discovery')
|
|
1193
|
+
return
|
|
1194
|
+
}
|
|
508
1195
|
const { response, statusCode } = await this.switchBotAPI.getDevices()
|
|
509
|
-
this.
|
|
510
|
-
if (statusCode
|
|
1196
|
+
this.debugLog(`SwitchBot getDevices response status: ${statusCode}`)
|
|
1197
|
+
if (isSuccessfulStatusCode(statusCode)) {
|
|
511
1198
|
const deviceList = Array.isArray(response?.body?.deviceList) ? response.body.deviceList : []
|
|
512
1199
|
this.discoveredDevices = deviceList
|
|
513
|
-
this.
|
|
1200
|
+
this.infoLog(`Discovered ${deviceList.length} SwitchBot device(s) from OpenAPI`)
|
|
514
1201
|
for (const d of deviceList) {
|
|
515
|
-
this.
|
|
1202
|
+
this.debugLog(` - ${d.deviceName} (${d.deviceType}) id=${d.deviceId}`)
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
const irDeviceList = Array.isArray(response?.body?.infraredRemoteList) ? response.body.infraredRemoteList : []
|
|
1206
|
+
this.discoveredIRDevices = irDeviceList
|
|
1207
|
+
this.infoLog(`Discovered ${irDeviceList.length} SwitchBot IR device(s) from OpenAPI`)
|
|
1208
|
+
for (const d of irDeviceList) {
|
|
1209
|
+
this.debugLog(` - ${d.deviceName} (${d.remoteType}) id=${d.deviceId}`)
|
|
516
1210
|
}
|
|
1211
|
+
|
|
1212
|
+
// Diagnostic: warn users if their device count + refresh rate may exceed daily limits
|
|
1213
|
+
this.validateApiUsageConfig(deviceList.length, irDeviceList.length)
|
|
517
1214
|
} else {
|
|
518
|
-
this.
|
|
1215
|
+
this.warnLog(`SwitchBot getDevices returned status ${statusCode}`)
|
|
1216
|
+
// If rate limit exceeded (429), log specific message
|
|
1217
|
+
if (statusCode === 429) {
|
|
1218
|
+
this.warnLog('OpenAPI rate limit (429) exceeded during discovery.')
|
|
1219
|
+
this.warnLog('Webhook functionality will still work for manually configured devices.')
|
|
1220
|
+
this.warnLog('Device state updates will be limited until rate limit resets.')
|
|
1221
|
+
}
|
|
519
1222
|
}
|
|
520
1223
|
} catch (e: any) {
|
|
521
|
-
this.
|
|
1224
|
+
this.errorLog('Failed to discover SwitchBot devices:', e?.message ?? e)
|
|
522
1225
|
}
|
|
523
1226
|
}
|
|
524
1227
|
|
|
525
1228
|
/**
|
|
526
|
-
*
|
|
1229
|
+
* Setup MQTT connection (if configured) and route incoming webhook messages
|
|
1230
|
+
* to registered webhook handlers. Mirrors behaviour in platform-hap.
|
|
527
1231
|
*/
|
|
528
|
-
async
|
|
529
|
-
|
|
530
|
-
while (retryCount < maxRetries) {
|
|
1232
|
+
async setupMqtt(): Promise<void> {
|
|
1233
|
+
if (this.config.options?.mqttURL) {
|
|
531
1234
|
try {
|
|
532
|
-
|
|
533
|
-
|
|
1235
|
+
const { connectAsync } = asyncmqtt
|
|
1236
|
+
this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {})
|
|
1237
|
+
this.debugLog('MQTT connection has been established successfully.')
|
|
1238
|
+
this.mqttClient.on('error', async (e: Error) => {
|
|
1239
|
+
this.errorLog(`Failed to publish MQTT messages. ${e.message ?? e}`)
|
|
1240
|
+
})
|
|
1241
|
+
if (!this.config.options?.webhookURL) {
|
|
1242
|
+
// receive webhook events via MQTT
|
|
1243
|
+
this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`)
|
|
1244
|
+
this.mqttClient.subscribe('homebridge-switchbot/webhook/+')
|
|
1245
|
+
this.mqttClient.on('message', async (topic: string, message: any) => {
|
|
1246
|
+
try {
|
|
1247
|
+
this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`)
|
|
1248
|
+
const context = JSON.parse(message.toString())
|
|
1249
|
+
this.webhookEventHandler[context.deviceMac]?.(context)
|
|
1250
|
+
} catch (e: any) {
|
|
1251
|
+
this.errorLog(`Failed to handle webhook event. Error: ${e.message ?? e}`)
|
|
1252
|
+
}
|
|
1253
|
+
})
|
|
534
1254
|
}
|
|
535
|
-
|
|
536
|
-
|
|
1255
|
+
} catch (e: any) {
|
|
1256
|
+
this.mqttClient = null
|
|
1257
|
+
this.errorLog(`Failed to establish MQTT connection. ${e.message ?? e}`)
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
/**
|
|
1263
|
+
* Setup OpenAPI webhook (if webhookURL configured) and forward incoming
|
|
1264
|
+
* webhook events to MQTT (if configured) and local handlers.
|
|
1265
|
+
*/
|
|
1266
|
+
async setupwebhook() {
|
|
1267
|
+
if (this.config.options?.webhookURL) {
|
|
1268
|
+
const url = this.config.options?.webhookURL
|
|
1269
|
+
try {
|
|
1270
|
+
this.switchBotAPI?.setupWebhook(url)
|
|
1271
|
+
this.infoLog(`Webhook configured for URL: ${url}`)
|
|
1272
|
+
// Listen for webhook events
|
|
1273
|
+
this.switchBotAPI?.on('webhookEvent', (body: any) => {
|
|
1274
|
+
try {
|
|
1275
|
+
this.infoLog(`Received webhook event for device: ${body.context.deviceMac}`)
|
|
1276
|
+
if (this.config.options?.mqttURL) {
|
|
1277
|
+
const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':')
|
|
1278
|
+
const options = this.config.options?.mqttPubOptions || {}
|
|
1279
|
+
this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options)
|
|
1280
|
+
}
|
|
1281
|
+
this.webhookEventHandler[body.context.deviceMac]?.(body.context)
|
|
1282
|
+
} catch (e: any) {
|
|
1283
|
+
this.errorLog(`Failed to handle webhook event. Error: ${e.message ?? e}`)
|
|
1284
|
+
}
|
|
1285
|
+
})
|
|
1286
|
+
} catch (e: any) {
|
|
1287
|
+
this.errorLog(`Failed to setup webhook. Error: ${e.message ?? e}`)
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
this.api.on('shutdown', async () => {
|
|
1291
|
+
try {
|
|
1292
|
+
this.switchBotAPI?.deleteWebhook(url)
|
|
1293
|
+
} catch (e: any) {
|
|
1294
|
+
this.errorLog(`Failed to delete webhook. Error: ${e.message ?? e}`)
|
|
1295
|
+
}
|
|
1296
|
+
})
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
/**
|
|
1301
|
+
* Retry wrapper for control commands using SwitchBot OpenAPI
|
|
1302
|
+
*/
|
|
1303
|
+
async retryCommand(deviceObj: device, bodyChange: bodyChange, maxRetries = 1, delayBetweenRetries = 1000): Promise<{ response: any, statusCode: number }> {
|
|
1304
|
+
// Check API budget BEFORE attempting any retries
|
|
1305
|
+
if (!this.apiTracker?.trySpend('command')) {
|
|
1306
|
+
return { response: {}, statusCode: 429 }
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
let retryCount = 0
|
|
1310
|
+
while (retryCount < maxRetries) {
|
|
1311
|
+
try {
|
|
1312
|
+
if (!this.switchBotAPI) {
|
|
1313
|
+
throw new Error('SwitchBot OpenAPI not initialized')
|
|
1314
|
+
}
|
|
1315
|
+
const { response, statusCode } = await this.switchBotAPI.controlDevice(
|
|
1316
|
+
deviceObj.deviceId,
|
|
537
1317
|
bodyChange.command,
|
|
538
1318
|
bodyChange.parameter,
|
|
539
1319
|
bodyChange.commandType as import('node-switchbot').commandType | undefined,
|
|
@@ -542,7 +1322,7 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
542
1322
|
)
|
|
543
1323
|
return { response, statusCode }
|
|
544
1324
|
} catch (e: any) {
|
|
545
|
-
this.
|
|
1325
|
+
this.debugLog(`retryCommand error: ${e?.message ?? e}`)
|
|
546
1326
|
}
|
|
547
1327
|
retryCount++
|
|
548
1328
|
|
|
@@ -585,6 +1365,12 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
585
1365
|
if (c.includes(':')) {
|
|
586
1366
|
const parts = c.split(':').map(Number)
|
|
587
1367
|
;[r, g, b] = parts
|
|
1368
|
+
} else if (c.includes(',')) {
|
|
1369
|
+
const parts = c.split(',').map(s => Number(s.trim()))
|
|
1370
|
+
;[r, g, b] = parts
|
|
1371
|
+
} else if (c.includes(' ')) {
|
|
1372
|
+
const parts = c.split(' ').map(s => Number(s.trim()))
|
|
1373
|
+
;[r, g, b] = parts
|
|
588
1374
|
} else if (c.startsWith('#')) {
|
|
589
1375
|
const hex = c.replace('#', '')
|
|
590
1376
|
r = Number.parseInt(hex.substring(0, 2), 16)
|
|
@@ -604,13 +1390,520 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
604
1390
|
result.battery = Number(battery)
|
|
605
1391
|
}
|
|
606
1392
|
|
|
1393
|
+
// VOC / TVOC (some air quality devices report total volatile organic compounds)
|
|
1394
|
+
const voc = sd.voc ?? sd.tvoc
|
|
1395
|
+
if (voc !== undefined) {
|
|
1396
|
+
result.voc = Number(voc)
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
// PM10 (some devices report PM10 alongside PM2.5)
|
|
1400
|
+
const pm10 = sd.pm10
|
|
1401
|
+
if (pm10 !== undefined) {
|
|
1402
|
+
result.pm10 = Number(pm10)
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1405
|
+
// PM2.5 (some BLE adverts use pm25 / pm_2_5)
|
|
1406
|
+
const pm25 = sd.pm2_5 ?? sd.pm25 ?? sd.pm_2_5
|
|
1407
|
+
if (pm25 !== undefined) {
|
|
1408
|
+
result.pm25 = Number(pm25)
|
|
1409
|
+
}
|
|
1410
|
+
|
|
1411
|
+
// CO2 (carbon dioxide ppm)
|
|
1412
|
+
const co2 = sd.co2 ?? sd.co2ppm ?? sd.carbonDioxide
|
|
1413
|
+
if (co2 !== undefined) {
|
|
1414
|
+
result.co2 = Number(co2)
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
// Temperature (C) and Humidity (%) — support common shorthand keys
|
|
1418
|
+
const temperature = sd.temperature ?? sd.temp ?? sd.t
|
|
1419
|
+
if (temperature !== undefined) {
|
|
1420
|
+
result.temperature = Number(temperature)
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
const humidity = sd.humidity ?? sd.h ?? sd.humid
|
|
1424
|
+
if (humidity !== undefined) {
|
|
1425
|
+
result.humidity = Number(humidity)
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
// Motion, Contact, Leak
|
|
1429
|
+
const motion = sd.motion ?? sd.m
|
|
1430
|
+
if (motion !== undefined) {
|
|
1431
|
+
result.motion = Boolean(motion)
|
|
1432
|
+
}
|
|
1433
|
+
|
|
1434
|
+
const contact = sd.contact ?? sd.open
|
|
1435
|
+
if (contact !== undefined) {
|
|
1436
|
+
result.contact = contact
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
const leak = sd.leak ?? sd.water
|
|
1440
|
+
if (leak !== undefined) {
|
|
1441
|
+
result.leak = Boolean(leak)
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// Position / Cover / Curtain synonyms
|
|
1445
|
+
const position = sd.position ?? sd.percent ?? sd.slidePosition ?? sd.curtainPosition
|
|
1446
|
+
if (position !== undefined) {
|
|
1447
|
+
result.position = Number(position)
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
// Fan speed/speed
|
|
1451
|
+
const fanSpeed = sd.fanSpeed ?? sd.speed
|
|
1452
|
+
if (fanSpeed !== undefined) {
|
|
1453
|
+
result.fanSpeed = Number(fanSpeed)
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
// Lock state
|
|
1457
|
+
const lock = sd.lock
|
|
1458
|
+
if (lock !== undefined) {
|
|
1459
|
+
result.lock = lock
|
|
1460
|
+
}
|
|
1461
|
+
|
|
1462
|
+
// Robot vacuum fields
|
|
1463
|
+
const rvcRunMode = sd.rvcRunMode
|
|
1464
|
+
if (rvcRunMode !== undefined) {
|
|
1465
|
+
result.rvcRunMode = rvcRunMode
|
|
1466
|
+
}
|
|
1467
|
+
const rvcOperationalState = sd.rvcOperationalState
|
|
1468
|
+
if (rvcOperationalState !== undefined) {
|
|
1469
|
+
result.rvcOperationalState = rvcOperationalState
|
|
1470
|
+
}
|
|
1471
|
+
|
|
607
1472
|
return result
|
|
608
1473
|
} catch (e: any) {
|
|
609
|
-
this.
|
|
1474
|
+
this.debugLog(`parseAdvertisementForDevice failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
610
1475
|
return null
|
|
611
1476
|
}
|
|
612
1477
|
}
|
|
613
1478
|
|
|
1479
|
+
/**
|
|
1480
|
+
* Central helper to apply a SwitchBot status object to a Matter accessory.
|
|
1481
|
+
* Tries to call accessory instance update helpers when available, otherwise
|
|
1482
|
+
* falls back to calling api.matter.updateAccessoryState directly.
|
|
1483
|
+
*/
|
|
1484
|
+
private async applyStatusToAccessory(uuidLocal: string, dev: device & devicesConfig, status: any) {
|
|
1485
|
+
if (!status) {
|
|
1486
|
+
return
|
|
1487
|
+
}
|
|
1488
|
+
|
|
1489
|
+
const instance = dev?.deviceId ? this.accessoryInstances.get(this.normalizeDeviceId(dev.deviceId)) : undefined
|
|
1490
|
+
|
|
1491
|
+
// Helper to safely call instance methods or fallback to api.matter.updateAccessoryState
|
|
1492
|
+
const safeUpdate = async (cluster: string, attributes: Record<string, unknown>, methodName?: string) => {
|
|
1493
|
+
try {
|
|
1494
|
+
// Special-case: powerSource cluster is optional on many devices (e.g., Curtains/Blinds).
|
|
1495
|
+
// To avoid noisy Matter server errors ("Behavior ID powerSource does not exist"),
|
|
1496
|
+
// always use the direct updateAccessoryState path wrapped in a guard for this cluster,
|
|
1497
|
+
// even when an accessory instance is present.
|
|
1498
|
+
const powerClusterName = (this.api.matter?.clusterNames && (this.api.matter.clusterNames as any).PowerSource)
|
|
1499
|
+
? (this.api.matter.clusterNames as any).PowerSource
|
|
1500
|
+
: 'powerSource'
|
|
1501
|
+
const isPowerSourceCluster = cluster === powerClusterName || cluster === 'powerSource'
|
|
1502
|
+
|
|
1503
|
+
// If the accessory instance declares supported clusters, skip updates for clusters
|
|
1504
|
+
// not present to avoid triggering Matter server errors and logs.
|
|
1505
|
+
let clusterSupported = true
|
|
1506
|
+
if (instance && (instance as any).clusters) {
|
|
1507
|
+
const declared = (instance as any).clusters
|
|
1508
|
+
if (Array.isArray(declared)) {
|
|
1509
|
+
clusterSupported = declared.includes(cluster)
|
|
1510
|
+
} else if (typeof declared === 'object') {
|
|
1511
|
+
clusterSupported = Object.prototype.hasOwnProperty.call(declared, cluster)
|
|
1512
|
+
}
|
|
1513
|
+
if (!clusterSupported) {
|
|
1514
|
+
this.debugLog(`Cluster ${cluster} not declared on accessory for ${dev.deviceId}, skipping update`)
|
|
1515
|
+
return
|
|
1516
|
+
}
|
|
1517
|
+
}
|
|
1518
|
+
|
|
1519
|
+
if (instance && methodName && typeof (instance as any)[methodName] === 'function') {
|
|
1520
|
+
// prefer device-specific update helpers when available
|
|
1521
|
+
await (instance as any)[methodName](...(Object.values(attributes)))
|
|
1522
|
+
} else if (!isPowerSourceCluster && instance && typeof (instance as any).updateState === 'function') {
|
|
1523
|
+
// some accessories expose updateState that accepts cluster and attributes
|
|
1524
|
+
await (instance as any).updateState(cluster, attributes)
|
|
1525
|
+
} else {
|
|
1526
|
+
try {
|
|
1527
|
+
await this.api.matter.updateAccessoryState(uuidLocal, cluster, attributes)
|
|
1528
|
+
} catch (updateError: any) {
|
|
1529
|
+
// Silently ignore "does not exist" errors for clusters that aren't
|
|
1530
|
+
// supported by this device type (e.g., powerSource on WindowBlind).
|
|
1531
|
+
const msg = String(updateError?.message ?? updateError)
|
|
1532
|
+
if (msg.includes('does not exist') || msg.includes('not found')) {
|
|
1533
|
+
this.debugLog(`Cluster ${cluster} not available on ${dev.deviceId}, skipping update`)
|
|
1534
|
+
return
|
|
1535
|
+
}
|
|
1536
|
+
throw updateError
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
} catch (e: any) {
|
|
1540
|
+
this.debugLog(`safeUpdate failed for ${dev.deviceId} cluster=${cluster}: ${e?.message ?? e}`)
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
|
|
1544
|
+
try {
|
|
1545
|
+
// On/Off
|
|
1546
|
+
if (status?.power !== undefined) {
|
|
1547
|
+
const on = (String(status.power).toLowerCase() === 'on' || Number(status.power) === 1 || Boolean(status.power) === true)
|
|
1548
|
+
await safeUpdate(this.api.matter.clusterNames.OnOff, { onOff: on }, 'updateOnOffState')
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
// Robot vacuum: some OpenAPI responses use 'runState' or similar textual
|
|
1552
|
+
// fields to indicate cleaning/mapping/idle. Map common textual values to
|
|
1553
|
+
// the numeric run mode values expected by the RoboticVacuumAccessory and
|
|
1554
|
+
// prefer calling accessory helpers when present.
|
|
1555
|
+
if (status?.runState !== undefined || status?.run_state !== undefined || status?.run !== undefined) {
|
|
1556
|
+
try {
|
|
1557
|
+
const raw = status?.runState ?? status?.run_state ?? status?.run
|
|
1558
|
+
let mode: number | undefined
|
|
1559
|
+
if (typeof raw === 'number') {
|
|
1560
|
+
mode = Number(raw)
|
|
1561
|
+
} else if (typeof raw === 'string') {
|
|
1562
|
+
const s = raw.toLowerCase()
|
|
1563
|
+
if (s.includes('clean')) {
|
|
1564
|
+
mode = 1 // Cleaning
|
|
1565
|
+
} else if (s.includes('map')) {
|
|
1566
|
+
mode = 2 // Mapping
|
|
1567
|
+
} else if (s.includes('idle') || s.includes('stop') || s.includes('dock') || s.includes('charge') || s.includes('docked')) {
|
|
1568
|
+
mode = 0 // Idle
|
|
1569
|
+
} else {
|
|
1570
|
+
const n = Number(raw)
|
|
1571
|
+
if (!Number.isNaN(n)) {
|
|
1572
|
+
mode = n
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
if (mode !== undefined) {
|
|
1578
|
+
await safeUpdate('rvcRunMode', { currentMode: Number(mode) }, 'updateRunMode')
|
|
1579
|
+
}
|
|
1580
|
+
} catch (e: any) {
|
|
1581
|
+
this.debugLog(`Failed to apply runState for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// Robot vacuum: some firmwares expose 'taskType' or 'task' with similar semantics
|
|
1586
|
+
// to runState (e.g., 'cleaning', 'mapping', 'idle'). Map to rvcRunMode accordingly.
|
|
1587
|
+
if (status?.taskType !== undefined || status?.task_type !== undefined || status?.task !== undefined) {
|
|
1588
|
+
try {
|
|
1589
|
+
const raw = status?.taskType ?? status?.task_type ?? status?.task
|
|
1590
|
+
let mode: number | undefined
|
|
1591
|
+
if (typeof raw === 'number') {
|
|
1592
|
+
mode = Number(raw)
|
|
1593
|
+
} else if (typeof raw === 'string') {
|
|
1594
|
+
const s = raw.toLowerCase()
|
|
1595
|
+
if (s.includes('clean')) {
|
|
1596
|
+
mode = 1 // Cleaning
|
|
1597
|
+
} else if (s.includes('map')) {
|
|
1598
|
+
mode = 2 // Mapping
|
|
1599
|
+
} else if (s.includes('idle') || s.includes('stop') || s.includes('dock') || s.includes('charge') || s.includes('docked')) {
|
|
1600
|
+
mode = 0 // Idle
|
|
1601
|
+
} else {
|
|
1602
|
+
const n = Number(raw)
|
|
1603
|
+
if (!Number.isNaN(n)) {
|
|
1604
|
+
mode = n
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
if (mode !== undefined) {
|
|
1610
|
+
await safeUpdate('rvcRunMode', { currentMode: Number(mode) }, 'updateRunMode')
|
|
1611
|
+
}
|
|
1612
|
+
} catch (e: any) {
|
|
1613
|
+
this.debugLog(`Failed to apply taskType for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
// Brightness
|
|
1618
|
+
if (status?.brightness !== undefined) {
|
|
1619
|
+
const rawBrightness = Number(status.brightness)
|
|
1620
|
+
const clampedBrightness = Math.max(0, Math.min(100, rawBrightness))
|
|
1621
|
+
|
|
1622
|
+
// If instance has updateBrightness method, it expects percentage (0-100)
|
|
1623
|
+
// Otherwise, updateAccessoryState expects Matter-scaled value (0-254)
|
|
1624
|
+
if (instance && typeof instance.updateBrightness === 'function') {
|
|
1625
|
+
this.debugLog(`[Brightness Debug] Device ${dev.deviceId}: calling updateBrightness with percent=${clampedBrightness}`)
|
|
1626
|
+
await safeUpdate(this.api.matter.clusterNames.LevelControl, { currentLevel: clampedBrightness }, 'updateBrightness')
|
|
1627
|
+
} else {
|
|
1628
|
+
const level = Math.round((clampedBrightness / 100) * 254)
|
|
1629
|
+
const clampedLevel = Math.max(0, Math.min(254, level))
|
|
1630
|
+
this.debugLog(`[Brightness Debug] Device ${dev.deviceId}: calling updateAccessoryState with rawBrightness=${rawBrightness}, level=${clampedLevel}`)
|
|
1631
|
+
await safeUpdate(this.api.matter.clusterNames.LevelControl, { currentLevel: clampedLevel })
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
// Color
|
|
1636
|
+
if (status?.color !== undefined) {
|
|
1637
|
+
const color = String(status.color)
|
|
1638
|
+
let r = 0
|
|
1639
|
+
let g = 0
|
|
1640
|
+
let b = 0
|
|
1641
|
+
if (color.includes(':')) {
|
|
1642
|
+
const parts = color.split(':').map(Number)
|
|
1643
|
+
;[r, g, b] = parts
|
|
1644
|
+
} else if (color.includes(',')) {
|
|
1645
|
+
const parts = color.split(',').map(s => Number(s.trim()))
|
|
1646
|
+
;[r, g, b] = parts
|
|
1647
|
+
} else if (color.includes(' ')) {
|
|
1648
|
+
const parts = color.split(' ').map(s => Number(s.trim()))
|
|
1649
|
+
;[r, g, b] = parts
|
|
1650
|
+
} else if (color.startsWith('#')) {
|
|
1651
|
+
const hex = color.replace('#', '')
|
|
1652
|
+
r = Number.parseInt(hex.substring(0, 2), 16)
|
|
1653
|
+
g = Number.parseInt(hex.substring(2, 4), 16)
|
|
1654
|
+
b = Number.parseInt(hex.substring(4, 6), 16)
|
|
1655
|
+
}
|
|
1656
|
+
const [h, s] = rgb2hs(r, g, b)
|
|
1657
|
+
const clampedH = Math.max(0, Math.min(360, h))
|
|
1658
|
+
const clampedS = Math.max(0, Math.min(100, s))
|
|
1659
|
+
|
|
1660
|
+
// If instance has updateHueSaturation method, it expects raw values (h: 0-360, s: 0-100)
|
|
1661
|
+
// Otherwise, updateAccessoryState expects Matter-scaled values (0-254)
|
|
1662
|
+
if (instance && typeof instance.updateHueSaturation === 'function') {
|
|
1663
|
+
this.debugLog(`[Color Debug] Device ${dev.deviceId}: calling updateHueSaturation with color="${color}", RGB=[${r},${g},${b}], HS=[${clampedH},${clampedS}]`)
|
|
1664
|
+
await safeUpdate(this.api.matter.clusterNames.ColorControl, { currentHue: clampedH, currentSaturation: clampedS }, 'updateHueSaturation')
|
|
1665
|
+
} else {
|
|
1666
|
+
const hue = Math.round((clampedH / 360) * 254)
|
|
1667
|
+
const sat = Math.round((clampedS / 100) * 254)
|
|
1668
|
+
const clampedHue = Math.max(0, Math.min(254, hue))
|
|
1669
|
+
const clampedSat = Math.max(0, Math.min(254, sat))
|
|
1670
|
+
this.debugLog(`[Color Debug] Device ${dev.deviceId}: calling updateAccessoryState with color="${color}", RGB=[${r},${g},${b}], HS=[${h},${s}], Matter=[${clampedHue},${clampedSat}]`)
|
|
1671
|
+
await safeUpdate(this.api.matter.clusterNames.ColorControl, { currentHue: clampedHue, currentSaturation: clampedSat })
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
// Battery/powerSource (support many possible field names)
|
|
1676
|
+
// Note: Some device types like WindowBlind and RoboticVacuumCleaner don't support PowerSource cluster
|
|
1677
|
+
if (status?.battery !== undefined || status?.batt !== undefined || status?.batteryLevel !== undefined || status?.batteryPercentage !== undefined || status?.battery_level !== undefined
|
|
1678
|
+
|| status?.baseBattery !== undefined || status?.base_battery !== undefined || status?.stationBattery !== undefined || status?.waterBaseBattery !== undefined || status?.dockBattery !== undefined) {
|
|
1679
|
+
// Skip battery updates for device types that don't support PowerSource cluster
|
|
1680
|
+
const deviceType = String(status?.deviceType ?? dev?.deviceType ?? '')
|
|
1681
|
+
const unsupportedTypes = [
|
|
1682
|
+
'Curtain',
|
|
1683
|
+
'Curtain2',
|
|
1684
|
+
'Curtain3',
|
|
1685
|
+
'Curtain 2',
|
|
1686
|
+
'Blind Tilt',
|
|
1687
|
+
// Robot Vacuums - PowerSource cluster not in Matter spec for RoboticVacuumCleaner
|
|
1688
|
+
'K10+',
|
|
1689
|
+
'K10+ Pro',
|
|
1690
|
+
'WoSweeper',
|
|
1691
|
+
'WoSweeperMini',
|
|
1692
|
+
'Robot Vacuum Cleaner S1',
|
|
1693
|
+
'Robot Vacuum Cleaner S1 Plus',
|
|
1694
|
+
'Robot Vacuum Cleaner S10',
|
|
1695
|
+
'Robot Vacuum Cleaner S1 Pro',
|
|
1696
|
+
'Robot Vacuum Cleaner S1 Mini',
|
|
1697
|
+
]
|
|
1698
|
+
|
|
1699
|
+
if (unsupportedTypes.includes(deviceType)) {
|
|
1700
|
+
this.debugLog(`Device ${dev.deviceId} type ${deviceType} does not support PowerSource cluster, skipping battery update`)
|
|
1701
|
+
} else {
|
|
1702
|
+
try {
|
|
1703
|
+
const percentage = Number(
|
|
1704
|
+
status?.battery ?? status?.batt ?? status?.batteryPercentage ?? status?.batteryLevel ?? status?.battery_level
|
|
1705
|
+
?? status?.baseBattery ?? status?.base_battery ?? status?.stationBattery ?? status?.waterBaseBattery ?? status?.dockBattery,
|
|
1706
|
+
)
|
|
1707
|
+
const batPercentRemaining = Math.max(0, Math.min(200, Math.round(percentage * 2)))
|
|
1708
|
+
let batChargeLevel = 0
|
|
1709
|
+
if (percentage < 20) {
|
|
1710
|
+
batChargeLevel = 2
|
|
1711
|
+
} else if (percentage < 40) {
|
|
1712
|
+
batChargeLevel = 1
|
|
1713
|
+
}
|
|
1714
|
+
const powerCluster = (this.api.matter?.clusterNames && (this.api.matter.clusterNames as any).PowerSource) ? (this.api.matter.clusterNames as any).PowerSource : 'powerSource'
|
|
1715
|
+
await safeUpdate(powerCluster, { batPercentRemaining, batChargeLevel }, 'updateBatteryPercentage')
|
|
1716
|
+
} catch (e: any) {
|
|
1717
|
+
this.debugLog(`Failed to apply battery status for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1718
|
+
}
|
|
1719
|
+
}
|
|
1720
|
+
}
|
|
1721
|
+
|
|
1722
|
+
// Temperature + thermostat
|
|
1723
|
+
if (status?.temperature !== undefined || status?.temp !== undefined) {
|
|
1724
|
+
try {
|
|
1725
|
+
const c = Number(status?.temperature ?? status?.temp)
|
|
1726
|
+
const measured = Math.round(c * 100)
|
|
1727
|
+
await safeUpdate('temperatureMeasurement', { measuredValue: measured }, 'updateTemperature')
|
|
1728
|
+
// Thermostat-specific mapping
|
|
1729
|
+
if (status?.targetTemp !== undefined || status?.targetTemperature !== undefined || status?.heatingSetpoint !== undefined) {
|
|
1730
|
+
const target = Number(status?.targetTemp ?? status?.targetTemperature ?? status?.heatingSetpoint)
|
|
1731
|
+
const val = Math.round(target * 100)
|
|
1732
|
+
await safeUpdate('thermostat', { occupiedHeatingSetpoint: val }, 'updateHeatingSetpoint')
|
|
1733
|
+
}
|
|
1734
|
+
} catch (e: any) {
|
|
1735
|
+
this.debugLog(`Failed to apply temperature for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
// Humidity (support different keys)
|
|
1740
|
+
if (status?.humidity !== undefined || status?.h !== undefined || status?.humid !== undefined) {
|
|
1741
|
+
try {
|
|
1742
|
+
const percent = Number(status?.humidity ?? status?.h ?? status?.humid)
|
|
1743
|
+
const measured = Math.round(percent * 100)
|
|
1744
|
+
await safeUpdate('relativeHumidityMeasurement', { measuredValue: measured }, 'updateHumidity')
|
|
1745
|
+
} catch (e: any) {
|
|
1746
|
+
this.debugLog(`Failed to apply humidity for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
// Contact / Leak -> BooleanState
|
|
1751
|
+
if (status?.contact !== undefined || status?.open !== undefined || status?.leak !== undefined || status?.water !== undefined) {
|
|
1752
|
+
try {
|
|
1753
|
+
const isContactOpen = status?.contact ?? status?.open
|
|
1754
|
+
if (isContactOpen !== undefined) {
|
|
1755
|
+
if ((dev.deviceType || '').includes('Contact')) {
|
|
1756
|
+
await safeUpdate(this.api.matter.clusterNames.BooleanState, { stateValue: !(String(isContactOpen).toLowerCase() === 'true' || Number(isContactOpen) === 1) }, 'updateContactState')
|
|
1757
|
+
} else {
|
|
1758
|
+
await safeUpdate(this.api.matter.clusterNames.BooleanState, { stateValue: (String(isContactOpen).toLowerCase() === 'true' || Number(isContactOpen) === 1) }, 'updateContactState')
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
const leakDetected = status?.leak ?? status?.water
|
|
1762
|
+
if (leakDetected !== undefined) {
|
|
1763
|
+
await safeUpdate(this.api.matter.clusterNames.BooleanState, { stateValue: Boolean(leakDetected) }, 'updateLeakState')
|
|
1764
|
+
}
|
|
1765
|
+
} catch (e: any) {
|
|
1766
|
+
this.debugLog(`Failed to apply contact/leak for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
// Motion -> occupancy
|
|
1771
|
+
if (status?.motion !== undefined || status?.m !== undefined) {
|
|
1772
|
+
try {
|
|
1773
|
+
const detected = Boolean(status?.motion ?? status?.m)
|
|
1774
|
+
await safeUpdate('occupancySensing', { occupancy: { occupied: detected } }, 'updateOccupancy')
|
|
1775
|
+
} catch (e: any) {
|
|
1776
|
+
this.debugLog(`Failed to apply motion for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
// Lock state
|
|
1781
|
+
if (status?.lock !== undefined) {
|
|
1782
|
+
try {
|
|
1783
|
+
const s = String(status.lock).toLowerCase()
|
|
1784
|
+
let lockState = 0
|
|
1785
|
+
if (s === 'locked' || s === '1' || s === 'true') {
|
|
1786
|
+
lockState = 1
|
|
1787
|
+
} else if (s === 'unlocked' || s === '0' || s === 'false') {
|
|
1788
|
+
lockState = 2
|
|
1789
|
+
}
|
|
1790
|
+
await safeUpdate(this.api.matter.clusterNames.DoorLock, { lockState }, 'updateLockState')
|
|
1791
|
+
} catch (e: any) {
|
|
1792
|
+
this.debugLog(`Failed to apply lock for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
// Cover position
|
|
1797
|
+
if (status?.position !== undefined || status?.percent !== undefined) {
|
|
1798
|
+
try {
|
|
1799
|
+
const openPercent = Number(status?.position ?? status?.percent)
|
|
1800
|
+
const closedPercent = 100 - Math.max(0, Math.min(100, openPercent))
|
|
1801
|
+
const value = Math.round(closedPercent * 100)
|
|
1802
|
+
await safeUpdate(this.api.matter.clusterNames.WindowCovering, { currentPositionLiftPercent100ths: value, targetPositionLiftPercent100ths: value }, 'updateLiftPosition')
|
|
1803
|
+
} catch (e: any) {
|
|
1804
|
+
this.debugLog(`Failed to apply cover position for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
|
|
1808
|
+
// Fan
|
|
1809
|
+
if (status?.fanSpeed !== undefined || status?.speed !== undefined) {
|
|
1810
|
+
try {
|
|
1811
|
+
const percent = Number(status?.fanSpeed ?? status?.speed)
|
|
1812
|
+
await safeUpdate(this.api.matter.clusterNames.FanControl, { percentSetting: percent, percentCurrent: percent }, 'updateFanSpeed')
|
|
1813
|
+
} catch (e: any) {
|
|
1814
|
+
this.debugLog(`Failed to apply fan speed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
|
|
1818
|
+
// Robot vacuum: run/operational/clean modes
|
|
1819
|
+
if (status?.rvcRunMode !== undefined) {
|
|
1820
|
+
try {
|
|
1821
|
+
await safeUpdate('rvcRunMode', { currentMode: Number(status.rvcRunMode) }, 'updateRunMode')
|
|
1822
|
+
} catch (e: any) {
|
|
1823
|
+
this.debugLog(`Failed to apply rvcRunMode for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
// CO2 (carbon dioxide) - support common synonyms
|
|
1827
|
+
if (status?.co2 !== undefined || status?.co2ppm !== undefined || status?.carbonDioxide !== undefined) {
|
|
1828
|
+
try {
|
|
1829
|
+
const val = Number(status?.co2 ?? status?.co2ppm ?? status?.carbonDioxide)
|
|
1830
|
+
await safeUpdate('carbonDioxide', { carbonDioxideLevel: val }, 'updateCO2')
|
|
1831
|
+
} catch (e: any) {
|
|
1832
|
+
this.debugLog(`Failed to apply CO2 for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1833
|
+
}
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
// PM2.5 / particulate matter
|
|
1837
|
+
if (status?.pm2_5 !== undefined || status?.pm25 !== undefined || status?.pm_2_5 !== undefined) {
|
|
1838
|
+
try {
|
|
1839
|
+
const pm = Number(status?.pm2_5 ?? status?.pm25 ?? status?.pm_2_5)
|
|
1840
|
+
await safeUpdate('pm2_5', { pm25: pm }, 'updatePM25')
|
|
1841
|
+
} catch (e: any) {
|
|
1842
|
+
this.debugLog(`Failed to apply PM2.5 for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
// PM10 (some devices report pm10)
|
|
1846
|
+
if (status?.pm10 !== undefined) {
|
|
1847
|
+
try {
|
|
1848
|
+
const pm10 = Number(status?.pm10)
|
|
1849
|
+
await safeUpdate('pm10', { pm10 }, 'updatePM10')
|
|
1850
|
+
} catch (e: any) {
|
|
1851
|
+
this.debugLog(`Failed to apply PM10 for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
// VOC / TVOC - volatile organic compounds
|
|
1856
|
+
if (status?.voc !== undefined || status?.tvoc !== undefined) {
|
|
1857
|
+
try {
|
|
1858
|
+
const val = Number(status?.voc ?? status?.tvoc)
|
|
1859
|
+
await safeUpdate('voc', { voc: val }, 'updateVOC')
|
|
1860
|
+
} catch (e: any) {
|
|
1861
|
+
this.debugLog(`Failed to apply VOC for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1862
|
+
}
|
|
1863
|
+
}
|
|
1864
|
+
if (status?.rvcOperationalState !== undefined) {
|
|
1865
|
+
try {
|
|
1866
|
+
await safeUpdate('rvcOperationalState', { operationalState: Number(status.rvcOperationalState) }, 'updateOperationalState')
|
|
1867
|
+
} catch (e: any) {
|
|
1868
|
+
this.debugLog(`Failed to apply rvcOperationalState for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
} catch (e: any) {
|
|
1872
|
+
this.debugLog(`applyStatusToAccessory top-level failure for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
/**
|
|
1877
|
+
* Apply status update with a small retry if the accessory is not yet registered.
|
|
1878
|
+
* Useful for first-time updates that can race with registration.
|
|
1879
|
+
*/
|
|
1880
|
+
private async applyStatusWithRegistrationRetry(
|
|
1881
|
+
uuidLocal: string,
|
|
1882
|
+
dev: device & devicesConfig,
|
|
1883
|
+
status: any,
|
|
1884
|
+
retries = 1,
|
|
1885
|
+
delayMs = 300,
|
|
1886
|
+
): Promise<void> {
|
|
1887
|
+
let attempt = 0
|
|
1888
|
+
// Normalize retries bounds
|
|
1889
|
+
const maxAttempts = Math.max(1, retries + 1)
|
|
1890
|
+
while (attempt < maxAttempts) {
|
|
1891
|
+
try {
|
|
1892
|
+
await this.applyStatusToAccessory(uuidLocal, dev, status)
|
|
1893
|
+
return
|
|
1894
|
+
} catch (e: any) {
|
|
1895
|
+
const msg = String(e?.message ?? e)
|
|
1896
|
+
const notReady = msg.includes('not found') || msg.includes('not registered')
|
|
1897
|
+
if (!notReady || attempt >= maxAttempts - 1) {
|
|
1898
|
+
throw e
|
|
1899
|
+
}
|
|
1900
|
+
this.debugLog(`Accessory ${uuidLocal} not registered yet for ${dev.deviceId}, retrying in ${delayMs}ms`)
|
|
1901
|
+
await sleep(delayMs)
|
|
1902
|
+
attempt++
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
}
|
|
1906
|
+
|
|
614
1907
|
/**
|
|
615
1908
|
* Required for DynamicPlatformPlugin
|
|
616
1909
|
* Called when homebridge restores cached accessories from disk at startup
|
|
@@ -628,7 +1921,7 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
628
1921
|
* any custom data you stored when the accessory was originally registered.
|
|
629
1922
|
*/
|
|
630
1923
|
configureMatterAccessory(accessory: SerializedMatterAccessory) {
|
|
631
|
-
this.
|
|
1924
|
+
this.debugLog('Loading cached Matter accessory:', accessory.displayName)
|
|
632
1925
|
this.matterAccessories.set(accessory.uuid, accessory)
|
|
633
1926
|
}
|
|
634
1927
|
|
|
@@ -636,51 +1929,224 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
636
1929
|
* Register all Matter accessories
|
|
637
1930
|
*/
|
|
638
1931
|
private async registerMatterAccessories() {
|
|
639
|
-
this.log.info('═'.repeat(80))
|
|
640
|
-
this.log.info('Homebridge Matter Plugin')
|
|
641
|
-
this.log.info('═'.repeat(80))
|
|
642
|
-
|
|
643
1932
|
// Remove accessories that are disabled in config
|
|
644
1933
|
await this.removeDisabledAccessories()
|
|
645
1934
|
|
|
646
1935
|
// If we discovered real SwitchBot devices via OpenAPI, map and register them
|
|
647
1936
|
if (this.discoveredDevices && this.discoveredDevices.length > 0) {
|
|
648
|
-
this.
|
|
649
|
-
|
|
650
|
-
|
|
1937
|
+
this.infoLog(`Registering ${this.discoveredDevices.length} discovered SwitchBot device(s) as Matter accessories`)
|
|
1938
|
+
|
|
1939
|
+
// Merge device config (deviceConfig per deviceType and per-device overrides) to match HAP behavior
|
|
1940
|
+
const devicesToProcess = await this.mergeDiscoveredDevices(this.discoveredDevices)
|
|
1941
|
+
|
|
1942
|
+
// By default, automatically remove previously-registered Matter
|
|
1943
|
+
// accessories whose deviceId is not present in the merged discovered
|
|
1944
|
+
// list. If the user explicitly sets `options.keepStaleAccessories` to
|
|
1945
|
+
// true, then we will keep previously-registered accessories (legacy
|
|
1946
|
+
// behavior).
|
|
1947
|
+
if ((this.config as any).options?.keepStaleAccessories) {
|
|
1948
|
+
this.debugLog('Keeping previously-registered stale accessories because options.keepStaleAccessories=true')
|
|
1949
|
+
} else {
|
|
1950
|
+
try {
|
|
1951
|
+
const desiredIds = new Set((devicesToProcess || []).map((d: any) => this.normalizeDeviceId(d.deviceId)))
|
|
1952
|
+
const toUnregister: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
1953
|
+
for (const [uuid, acc] of Array.from(this.matterAccessories.entries())) {
|
|
1954
|
+
try {
|
|
1955
|
+
const deviceId = (acc as any)?.context?.deviceId
|
|
1956
|
+
if (!deviceId) {
|
|
1957
|
+
continue
|
|
1958
|
+
}
|
|
1959
|
+
const nid = this.normalizeDeviceId(deviceId)
|
|
1960
|
+
if (!desiredIds.has(nid)) {
|
|
1961
|
+
// Accessory exists but is no longer desired -> schedule for removal
|
|
1962
|
+
this.infoLog(`Removing previously-registered accessory for deviceId=${deviceId} (no longer discovered or configured)`)
|
|
1963
|
+
try {
|
|
1964
|
+
this.clearDeviceResources(deviceId)
|
|
1965
|
+
} catch (e: any) {
|
|
1966
|
+
this.debugLog(`Failed to clear resources for ${deviceId} before unregister: ${e?.message ?? e}`)
|
|
1967
|
+
}
|
|
1968
|
+
toUnregister.push(acc as unknown as MatterAccessory<Record<string, unknown>>)
|
|
1969
|
+
this.matterAccessories.delete(uuid)
|
|
1970
|
+
}
|
|
1971
|
+
} catch (e: any) {
|
|
1972
|
+
this.debugLog(`Error while checking existing accessory ${uuid}: ${e?.message ?? e}`)
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
if (toUnregister.length > 0) {
|
|
1977
|
+
try {
|
|
1978
|
+
await this.api.matter.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, toUnregister)
|
|
1979
|
+
} catch (e: any) {
|
|
1980
|
+
this.debugLog(`Failed to unregister accessories: ${e?.message ?? e}`)
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
} catch (e: any) {
|
|
1984
|
+
this.debugLog(`Failed to remove stale accessories: ${e?.message ?? e}`)
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
|
|
1988
|
+
// We'll separate discovered devices into two buckets:
|
|
1989
|
+
// - platformAccessories: accessories that will be hosted under the plugin's Matter bridge
|
|
1990
|
+
// - roboticAccessories: robot vacuum devices which require standalone commissioning behaviour
|
|
1991
|
+
const platformAccessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
1992
|
+
const roboticAccessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
1993
|
+
|
|
1994
|
+
// Known robot vacuum deviceType names (matches mapping in createAccessoryFromDevice)
|
|
1995
|
+
const robotTypes = new Set([
|
|
1996
|
+
'K10+',
|
|
1997
|
+
'K10+ Pro',
|
|
1998
|
+
'WoSweeper',
|
|
1999
|
+
'WoSweeperMini',
|
|
2000
|
+
'Robot Vacuum Cleaner S1',
|
|
2001
|
+
'Robot Vacuum Cleaner S1 Plus',
|
|
2002
|
+
'Robot Vacuum Cleaner S10',
|
|
2003
|
+
'Robot Vacuum Cleaner S1 Pro',
|
|
2004
|
+
'Robot Vacuum Cleaner S1 Mini',
|
|
2005
|
+
])
|
|
2006
|
+
|
|
2007
|
+
for (const dev of devicesToProcess) {
|
|
651
2008
|
try {
|
|
652
2009
|
const acc = await this.createAccessoryFromDevice(dev)
|
|
653
|
-
if (acc) {
|
|
654
|
-
|
|
2010
|
+
if (!acc) {
|
|
2011
|
+
continue
|
|
2012
|
+
}
|
|
2013
|
+
if (robotTypes.has(dev.deviceType ?? '')) {
|
|
2014
|
+
roboticAccessories.push(acc)
|
|
2015
|
+
} else {
|
|
2016
|
+
platformAccessories.push(acc)
|
|
655
2017
|
}
|
|
656
2018
|
} catch (e: any) {
|
|
657
|
-
this.
|
|
2019
|
+
this.errorLog(`Failed to create Matter accessory for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
658
2020
|
}
|
|
659
2021
|
}
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
2022
|
+
|
|
2023
|
+
// Register platform-hosted accessories (most devices)
|
|
2024
|
+
if (platformAccessories.length > 0) {
|
|
2025
|
+
this.infoLog(`✓ Registered ${platformAccessories.length} discovered platform-hosted device(s)`)
|
|
2026
|
+
for (const acc of platformAccessories) {
|
|
2027
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
664
2028
|
}
|
|
665
|
-
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME,
|
|
666
|
-
|
|
2029
|
+
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, platformAccessories)
|
|
2030
|
+
}
|
|
2031
|
+
|
|
2032
|
+
// Register robotic accessories (robot vacuums) separately so they can be
|
|
2033
|
+
// commissioned in the way Apple Home expects (these devices often require
|
|
2034
|
+
// standalone commissioning flow). We still call registerPlatformAccessories
|
|
2035
|
+
// because the accessory implementations manage their commissioning behavior.
|
|
2036
|
+
if (roboticAccessories.length > 0) {
|
|
2037
|
+
this.infoLog(`✓ Registered ${roboticAccessories.length} discovered robot vacuum device(s)`)
|
|
2038
|
+
for (const acc of roboticAccessories) {
|
|
2039
|
+
this.infoLog(` - ${acc.displayName} (standalone for Apple Home compatibility)`)
|
|
2040
|
+
}
|
|
2041
|
+
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, roboticAccessories)
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
// Debug/info: how many discovered vs example accessories were registered.
|
|
2045
|
+
// Example accessories are disabled — we intentionally do NOT register them.
|
|
2046
|
+
const discoveredRegistered = platformAccessories.length + roboticAccessories.length
|
|
2047
|
+
const exampleRegistered = 0
|
|
2048
|
+
this.debugLog(`Discovered accessories registered: ${discoveredRegistered}; Example accessories registered: ${exampleRegistered}`)
|
|
2049
|
+
|
|
2050
|
+
// Dump registry state to help runtime debugging: which accessory instances
|
|
2051
|
+
// were created and which refresh timers are scheduled. This helps confirm
|
|
2052
|
+
// whether safeUpdate will prefer accessory helpers and whether periodic
|
|
2053
|
+
// refreshes exist for each device.
|
|
2054
|
+
try {
|
|
2055
|
+
const instanceKeys = Array.from(this.accessoryInstances.keys())
|
|
2056
|
+
this.debugLog(`Accessory instances registered (${instanceKeys.length}): ${JSON.stringify(instanceKeys)}`)
|
|
2057
|
+
const timerKeys = Array.from(this.refreshTimers.keys())
|
|
2058
|
+
this.debugLog(`Refresh timers scheduled (${timerKeys.length}): ${JSON.stringify(timerKeys)}`)
|
|
2059
|
+
} catch (e: any) {
|
|
2060
|
+
this.debugLog(`Failed to dump platform registries: ${e?.message ?? e}`)
|
|
667
2061
|
}
|
|
668
|
-
|
|
2062
|
+
|
|
2063
|
+
return
|
|
669
2064
|
}
|
|
670
2065
|
|
|
671
|
-
//
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
2066
|
+
// If no discovered devices are available, check for cached Matter accessories
|
|
2067
|
+
const hasCachedAccessories = this.matterAccessories.size > 0
|
|
2068
|
+
if (hasCachedAccessories) {
|
|
2069
|
+
this.infoLog(`No devices discovered via OpenAPI, but found ${this.matterAccessories.size} cached Matter accessories.`)
|
|
2070
|
+
this.infoLog('Cached accessories will continue to function with webhook updates.')
|
|
2071
|
+
this.infoLog('Restoring webhook handlers for cached Matter accessories...')
|
|
2072
|
+
await this.restoreCachedMatterAccessoryWebhooks()
|
|
2073
|
+
this.infoLog('Device discovery will resume when API becomes available.')
|
|
2074
|
+
} else {
|
|
2075
|
+
this.infoLog('No discovered SwitchBot devices found.')
|
|
2076
|
+
}
|
|
680
2077
|
|
|
681
|
-
this.
|
|
682
|
-
this.
|
|
683
|
-
this.
|
|
2078
|
+
this.debugLog('═'.repeat(80))
|
|
2079
|
+
this.debugLog('Finished registering Matter accessories')
|
|
2080
|
+
this.debugLog('═'.repeat(80))
|
|
2081
|
+
}
|
|
2082
|
+
|
|
2083
|
+
/**
|
|
2084
|
+
* Restore webhook handlers for cached Matter accessories
|
|
2085
|
+
* This ensures webhook functionality continues to work even when device discovery fails
|
|
2086
|
+
*/
|
|
2087
|
+
private async restoreCachedMatterAccessoryWebhooks() {
|
|
2088
|
+
this.debugLog('Restoring webhook handlers for cached Matter accessories...')
|
|
2089
|
+
|
|
2090
|
+
let restoredCount = 0
|
|
2091
|
+
let fallbackCount = 0
|
|
2092
|
+
for (const [uuid, accessory] of this.matterAccessories.entries()) {
|
|
2093
|
+
try {
|
|
2094
|
+
const context = (accessory as any)?.context
|
|
2095
|
+
const deviceId = context?.deviceId
|
|
2096
|
+
let webhook = context?.webhook as boolean | undefined
|
|
2097
|
+
// If cached accessory predates global webhook context, fall back to global option
|
|
2098
|
+
if (webhook === undefined && this.config.options?.webhook === true) {
|
|
2099
|
+
webhook = true
|
|
2100
|
+
try {
|
|
2101
|
+
;(accessory as any).context.webhook = true
|
|
2102
|
+
this.debugLog(`Applying global webhook fallback for cached Matter device ${deviceId}`)
|
|
2103
|
+
fallbackCount++
|
|
2104
|
+
} catch (e: any) {
|
|
2105
|
+
this.debugLog(`Failed to persist global webhook fallback for ${deviceId}: ${e?.message ?? e}`)
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
if (!deviceId) {
|
|
2110
|
+
this.debugLog(`Skipping cached accessory ${accessory.displayName} - no deviceId in context`)
|
|
2111
|
+
continue
|
|
2112
|
+
}
|
|
2113
|
+
|
|
2114
|
+
// Only register webhook if the device had webhook enabled
|
|
2115
|
+
if (webhook) {
|
|
2116
|
+
this.debugLog(`Restoring webhook handler for Matter device: ${deviceId}`)
|
|
2117
|
+
|
|
2118
|
+
// Create a minimal device object from cached context for webhook handling
|
|
2119
|
+
const dev: any = {
|
|
2120
|
+
deviceId,
|
|
2121
|
+
deviceName: context?.name || accessory.displayName,
|
|
2122
|
+
deviceType: context?.deviceType,
|
|
2123
|
+
webhook: true,
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
this.webhookEventHandler[deviceId] = async (webhookContext: any) => {
|
|
2127
|
+
try {
|
|
2128
|
+
this.debugLog(`Received webhook for cached Matter device ${deviceId}: ${JSON.stringify(webhookContext)}`)
|
|
2129
|
+
// Apply webhook status update to the accessory
|
|
2130
|
+
await this.applyStatusWithRegistrationRetry(uuid, dev, webhookContext)
|
|
2131
|
+
} catch (e: any) {
|
|
2132
|
+
this.errorLog(`Failed to handle webhook for cached device ${deviceId}: ${e?.message ?? e}`)
|
|
2133
|
+
}
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
restoredCount++
|
|
2137
|
+
this.debugSuccessLog(`Webhook handler restored for ${deviceId}`)
|
|
2138
|
+
} else {
|
|
2139
|
+
this.debugLog(`Device ${deviceId} does not have webhook enabled, skipping`)
|
|
2140
|
+
}
|
|
2141
|
+
} catch (e: any) {
|
|
2142
|
+
this.errorLog(`Failed to restore webhook handler for cached accessory ${accessory.displayName}: ${e?.message ?? e}`)
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
|
|
2146
|
+
if (fallbackCount > 0) {
|
|
2147
|
+
this.infoLog(`Applied global webhook fallback for ${fallbackCount} cached Matter accessories`)
|
|
2148
|
+
}
|
|
2149
|
+
this.infoLog(`Restored webhook handlers for ${restoredCount} cached Matter accessories`)
|
|
684
2150
|
}
|
|
685
2151
|
|
|
686
2152
|
/**
|
|
@@ -715,7 +2181,16 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
715
2181
|
if (enabled === false) {
|
|
716
2182
|
const existingAccessory = this.matterAccessories.get(uuid)
|
|
717
2183
|
if (existingAccessory) {
|
|
718
|
-
this.
|
|
2184
|
+
this.infoLog(`Removing accessory '${name}' (disabled in config)`)
|
|
2185
|
+
// Attempt to clear any per-device resources (timers, BLE handlers, instances)
|
|
2186
|
+
try {
|
|
2187
|
+
const deviceId = (existingAccessory as any)?.context?.deviceId
|
|
2188
|
+
if (deviceId) {
|
|
2189
|
+
this.clearDeviceResources(deviceId)
|
|
2190
|
+
}
|
|
2191
|
+
} catch (e: any) {
|
|
2192
|
+
this.debugLog(`Failed to clear resources for disabled accessory ${name}: ${e?.message ?? e}`)
|
|
2193
|
+
}
|
|
719
2194
|
await this.api.matter.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory as unknown as MatterAccessory])
|
|
720
2195
|
this.matterAccessories.delete(uuid)
|
|
721
2196
|
}
|
|
@@ -727,9 +2202,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
727
2202
|
* Section 4: Lighting Devices (Matter Spec § 4)
|
|
728
2203
|
*/
|
|
729
2204
|
private async registerSection4Lighting() {
|
|
730
|
-
this.
|
|
731
|
-
this.
|
|
732
|
-
this.
|
|
2205
|
+
this.debugLog('═'.repeat(80))
|
|
2206
|
+
this.infoLog('Section 4: Lighting Devices (Matter Spec § 4)')
|
|
2207
|
+
this.debugLog('═'.repeat(80))
|
|
733
2208
|
|
|
734
2209
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
735
2210
|
|
|
@@ -764,9 +2239,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
764
2239
|
}
|
|
765
2240
|
|
|
766
2241
|
if (accessories.length > 0) {
|
|
767
|
-
this.
|
|
2242
|
+
this.infoLog(`✓ Registered ${accessories.length} lighting device(s)`)
|
|
768
2243
|
for (const acc of accessories) {
|
|
769
|
-
this.
|
|
2244
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
770
2245
|
}
|
|
771
2246
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
772
2247
|
}
|
|
@@ -776,9 +2251,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
776
2251
|
* Section 5: Smart Plugs/Actuators (Matter Spec § 5)
|
|
777
2252
|
*/
|
|
778
2253
|
private async registerSection5SmartPlugs() {
|
|
779
|
-
this.
|
|
780
|
-
this.
|
|
781
|
-
this.
|
|
2254
|
+
this.debugLog('═'.repeat(80))
|
|
2255
|
+
this.infoLog('Section 5: Smart Plugs/Actuators (Matter Spec § 5)')
|
|
2256
|
+
this.debugLog('═'.repeat(80))
|
|
782
2257
|
|
|
783
2258
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
784
2259
|
|
|
@@ -789,9 +2264,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
789
2264
|
}
|
|
790
2265
|
|
|
791
2266
|
if (accessories.length > 0) {
|
|
792
|
-
this.
|
|
2267
|
+
this.infoLog(`✓ Registered ${accessories.length} smart plug/actuator device(s)`)
|
|
793
2268
|
for (const acc of accessories) {
|
|
794
|
-
this.
|
|
2269
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
795
2270
|
}
|
|
796
2271
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
797
2272
|
}
|
|
@@ -801,9 +2276,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
801
2276
|
* Section 6: Switches & Controllers (Matter Spec § 6)
|
|
802
2277
|
*/
|
|
803
2278
|
private async registerSection6Switches() {
|
|
804
|
-
this.
|
|
805
|
-
this.
|
|
806
|
-
this.
|
|
2279
|
+
this.debugLog('═'.repeat(80))
|
|
2280
|
+
this.infoLog('Section 6: Switches & Controllers (Matter Spec § 6)')
|
|
2281
|
+
this.debugLog('═'.repeat(80))
|
|
807
2282
|
|
|
808
2283
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
809
2284
|
|
|
@@ -814,9 +2289,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
814
2289
|
}
|
|
815
2290
|
|
|
816
2291
|
if (accessories.length > 0) {
|
|
817
|
-
this.
|
|
2292
|
+
this.infoLog(`✓ Registered ${accessories.length} switch/controller device(s)`)
|
|
818
2293
|
for (const acc of accessories) {
|
|
819
|
-
this.
|
|
2294
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
820
2295
|
}
|
|
821
2296
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
822
2297
|
}
|
|
@@ -826,9 +2301,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
826
2301
|
* Section 7: Sensors (Matter Spec § 7)
|
|
827
2302
|
*/
|
|
828
2303
|
private async registerSection7Sensors() {
|
|
829
|
-
this.
|
|
830
|
-
this.
|
|
831
|
-
this.
|
|
2304
|
+
this.debugLog('═'.repeat(80))
|
|
2305
|
+
this.infoLog('Section 7: Sensors (Matter Spec § 7)')
|
|
2306
|
+
this.debugLog('═'.repeat(80))
|
|
832
2307
|
|
|
833
2308
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
834
2309
|
|
|
@@ -875,9 +2350,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
875
2350
|
}
|
|
876
2351
|
|
|
877
2352
|
if (accessories.length > 0) {
|
|
878
|
-
this.
|
|
2353
|
+
this.infoLog(`✓ Registered ${accessories.length} sensor device(s)`)
|
|
879
2354
|
for (const acc of accessories) {
|
|
880
|
-
this.
|
|
2355
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
881
2356
|
}
|
|
882
2357
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
883
2358
|
}
|
|
@@ -887,9 +2362,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
887
2362
|
* Section 8: Closure Devices (Matter Spec § 8)
|
|
888
2363
|
*/
|
|
889
2364
|
private async registerSection8Closure() {
|
|
890
|
-
this.
|
|
891
|
-
this.
|
|
892
|
-
this.
|
|
2365
|
+
this.debugLog('═'.repeat(80))
|
|
2366
|
+
this.infoLog('Section 8: Closure Devices (Matter Spec § 8)')
|
|
2367
|
+
this.debugLog('═'.repeat(80))
|
|
893
2368
|
|
|
894
2369
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
895
2370
|
|
|
@@ -901,20 +2376,38 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
901
2376
|
|
|
902
2377
|
// Window Blind
|
|
903
2378
|
if (this.config.enableWindowBlind !== false) {
|
|
904
|
-
const device = new WindowBlindAccessory(this.api, this.log
|
|
2379
|
+
const device = new WindowBlindAccessory(this.api, this.log, {
|
|
2380
|
+
handlers: {
|
|
2381
|
+
windowCovering: {
|
|
2382
|
+
goToLiftPercentage: async request => device?.handleGoToLift?.(request),
|
|
2383
|
+
upOrOpen: async () => device?.handleUpOrOpen?.(),
|
|
2384
|
+
downOrClose: async () => device?.handleDownOrClose?.(),
|
|
2385
|
+
stopMotion: async () => device?.handleStop?.(),
|
|
2386
|
+
},
|
|
2387
|
+
},
|
|
2388
|
+
})
|
|
905
2389
|
accessories.push(device.toAccessory())
|
|
906
2390
|
}
|
|
907
2391
|
|
|
908
2392
|
// Venetian Blind
|
|
909
2393
|
if (this.config.enableVenetianBlind !== false) {
|
|
910
|
-
const device = new VenetianBlindAccessory(this.api, this.log
|
|
2394
|
+
const device = new VenetianBlindAccessory(this.api, this.log, {
|
|
2395
|
+
handlers: {
|
|
2396
|
+
windowCovering: {
|
|
2397
|
+
goToLiftPercentage: async request => device?.handleGoToLift?.(request),
|
|
2398
|
+
upOrOpen: async () => device?.handleUpOrOpen?.(),
|
|
2399
|
+
downOrClose: async () => device?.handleDownOrClose?.(),
|
|
2400
|
+
stopMotion: async () => device?.handleStop?.(),
|
|
2401
|
+
},
|
|
2402
|
+
},
|
|
2403
|
+
})
|
|
911
2404
|
accessories.push(device.toAccessory())
|
|
912
2405
|
}
|
|
913
2406
|
|
|
914
2407
|
if (accessories.length > 0) {
|
|
915
|
-
this.
|
|
2408
|
+
this.infoLog(`✓ Registered ${accessories.length} closure device(s)`)
|
|
916
2409
|
for (const acc of accessories) {
|
|
917
|
-
this.
|
|
2410
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
918
2411
|
}
|
|
919
2412
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
920
2413
|
}
|
|
@@ -924,9 +2417,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
924
2417
|
* Section 9: HVAC (Matter Spec § 9)
|
|
925
2418
|
*/
|
|
926
2419
|
private async registerSection9HVAC() {
|
|
927
|
-
this.
|
|
928
|
-
this.
|
|
929
|
-
this.
|
|
2420
|
+
this.debugLog('═'.repeat(80))
|
|
2421
|
+
this.infoLog('Section 9: HVAC (Matter Spec § 9)')
|
|
2422
|
+
this.debugLog('═'.repeat(80))
|
|
930
2423
|
|
|
931
2424
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
932
2425
|
|
|
@@ -943,9 +2436,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
943
2436
|
}
|
|
944
2437
|
|
|
945
2438
|
if (accessories.length > 0) {
|
|
946
|
-
this.
|
|
2439
|
+
this.infoLog(`✓ Registered ${accessories.length} HVAC device(s)`)
|
|
947
2440
|
for (const acc of accessories) {
|
|
948
|
-
this.
|
|
2441
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
949
2442
|
}
|
|
950
2443
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
951
2444
|
}
|
|
@@ -958,9 +2451,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
958
2451
|
* Use those codes to pair the vacuum as a separate bridge in your Home app.
|
|
959
2452
|
*/
|
|
960
2453
|
private async registerSection12Robotic() {
|
|
961
|
-
this.
|
|
962
|
-
this.
|
|
963
|
-
this.
|
|
2454
|
+
this.debugLog('═'.repeat(80))
|
|
2455
|
+
this.infoLog('Section 12: Robotic Devices (Matter Spec § 12)')
|
|
2456
|
+
this.debugLog('═'.repeat(80))
|
|
964
2457
|
|
|
965
2458
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
966
2459
|
|
|
@@ -971,9 +2464,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
971
2464
|
}
|
|
972
2465
|
|
|
973
2466
|
if (accessories.length > 0) {
|
|
974
|
-
this.
|
|
2467
|
+
this.infoLog(`✓ Registered ${accessories.length} robot vacuum device(s)`)
|
|
975
2468
|
for (const acc of accessories) {
|
|
976
|
-
this.
|
|
2469
|
+
this.infoLog(` - ${acc.displayName} (standalone for Apple Home compatibility)`)
|
|
977
2470
|
}
|
|
978
2471
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
979
2472
|
}
|
|
@@ -987,9 +2480,9 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
987
2480
|
* like managing multiple logical components within a single device.
|
|
988
2481
|
*/
|
|
989
2482
|
private async registerCustomDevices() {
|
|
990
|
-
this.
|
|
991
|
-
this.
|
|
992
|
-
this.
|
|
2483
|
+
this.debugLog('═'.repeat(80))
|
|
2484
|
+
this.infoLog('Custom Devices')
|
|
2485
|
+
this.debugLog('═'.repeat(80))
|
|
993
2486
|
|
|
994
2487
|
const accessories: Array<MatterAccessory<Record<string, unknown>>> = []
|
|
995
2488
|
|
|
@@ -1000,11 +2493,211 @@ export class SwitchBotMatterPlatform implements DynamicPlatformPlugin {
|
|
|
1000
2493
|
}
|
|
1001
2494
|
|
|
1002
2495
|
if (accessories.length > 0) {
|
|
1003
|
-
this.
|
|
2496
|
+
this.infoLog(`✓ Registered ${accessories.length} custom device(s)`)
|
|
1004
2497
|
for (const acc of accessories) {
|
|
1005
|
-
this.
|
|
2498
|
+
this.infoLog(` - ${acc.displayName}`)
|
|
1006
2499
|
}
|
|
1007
2500
|
await this.api.matter.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, accessories)
|
|
1008
2501
|
}
|
|
1009
2502
|
}
|
|
2503
|
+
|
|
2504
|
+
/**
|
|
2505
|
+
* Start platform-level refresh timer to batch all device status updates
|
|
2506
|
+
*/
|
|
2507
|
+
private startPlatformRefreshTimer(refreshRateSec: number): void {
|
|
2508
|
+
// Only create timer once
|
|
2509
|
+
if (this.platformRefreshTimer) {
|
|
2510
|
+
return
|
|
2511
|
+
}
|
|
2512
|
+
// Respect user toggle
|
|
2513
|
+
if (this.config.options?.matterBatchEnabled === false) {
|
|
2514
|
+
this.infoLog('Matter batch refresh is disabled by configuration')
|
|
2515
|
+
return
|
|
2516
|
+
}
|
|
2517
|
+
const jitterSec = Number(this.config.options?.matterBatchJitter ?? 0)
|
|
2518
|
+
const intervalMs = Number(refreshRateSec) * 1000
|
|
2519
|
+
const jitterMs = Number.isFinite(jitterSec) && jitterSec > 0 ? Math.floor(Math.random() * jitterSec * 1000) : 0
|
|
2520
|
+
// Start after optional jitter, then schedule recurring interval
|
|
2521
|
+
setTimeout(async () => {
|
|
2522
|
+
try {
|
|
2523
|
+
await this.batchRefreshAllDevices()
|
|
2524
|
+
} catch (e: any) {
|
|
2525
|
+
this.debugLog(`Initial batch refresh failed: ${e?.message ?? e}`)
|
|
2526
|
+
}
|
|
2527
|
+
this.platformRefreshTimer = setInterval(async () => {
|
|
2528
|
+
await this.batchRefreshAllDevices()
|
|
2529
|
+
}, intervalMs)
|
|
2530
|
+
}, jitterMs)
|
|
2531
|
+
}
|
|
2532
|
+
|
|
2533
|
+
/**
|
|
2534
|
+
* Batch refresh all devices - still makes individual API calls but batches them together
|
|
2535
|
+
* Note: SwitchBot API doesn't support true batch status calls, but we can parallelize them
|
|
2536
|
+
*/
|
|
2537
|
+
private async batchRefreshAllDevices(): Promise<void> {
|
|
2538
|
+
if (!this.switchBotAPI) {
|
|
2539
|
+
return
|
|
2540
|
+
}
|
|
2541
|
+
|
|
2542
|
+
this.debugLog('Performing batched periodic OpenAPI refresh for all devices')
|
|
2543
|
+
|
|
2544
|
+
// Build list from registered accessory instances (uuid) and discovered devices
|
|
2545
|
+
const devicesToRefresh: Array<{ uuid: string, dev: device }> = []
|
|
2546
|
+
try {
|
|
2547
|
+
for (const [nid, instance] of this.accessoryInstances.entries()) {
|
|
2548
|
+
const uuid = (instance as any)?.uuid as string | undefined
|
|
2549
|
+
if (!uuid) {
|
|
2550
|
+
continue
|
|
2551
|
+
}
|
|
2552
|
+
const dev = this.discoveredDevices.find(d => this.normalizeDeviceId(d.deviceId) === nid)
|
|
2553
|
+
// Skip devices with per-device timers and those in cooldown
|
|
2554
|
+
const now = Date.now()
|
|
2555
|
+
const nextAllowed = this.backoffCooldowns.get(nid) ?? 0
|
|
2556
|
+
if (dev && !this.perDeviceRefreshSet.has(nid) && now >= nextAllowed) {
|
|
2557
|
+
devicesToRefresh.push({ uuid, dev })
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
} catch (e: any) {
|
|
2561
|
+
this.errorLog(`Failed to enumerate devices for batch refresh: ${e?.message ?? e}`)
|
|
2562
|
+
return
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
if (devicesToRefresh.length === 0) {
|
|
2566
|
+
this.debugLog('No devices to refresh')
|
|
2567
|
+
return
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2570
|
+
this.debugLog(`Refreshing ${devicesToRefresh.length} devices in parallel batch`)
|
|
2571
|
+
|
|
2572
|
+
// Randomize order to reduce synchronized spikes
|
|
2573
|
+
for (let i = devicesToRefresh.length - 1; i > 0; i--) {
|
|
2574
|
+
const j = Math.floor(Math.random() * (i + 1))
|
|
2575
|
+
;[devicesToRefresh[i], devicesToRefresh[j]] = [devicesToRefresh[j], devicesToRefresh[i]]
|
|
2576
|
+
}
|
|
2577
|
+
const concurrency = Number(this.config.options?.matterBatchConcurrency ?? 5)
|
|
2578
|
+
await this.runWithConcurrency(devicesToRefresh, async ({ uuid, dev }) => {
|
|
2579
|
+
try {
|
|
2580
|
+
const status = await this.refreshSingleDeviceWithRetry(dev)
|
|
2581
|
+
if (status) {
|
|
2582
|
+
await this.applyStatusToAccessory(uuid, dev as any, status)
|
|
2583
|
+
}
|
|
2584
|
+
} catch (e: any) {
|
|
2585
|
+
this.errorLog(`Periodic OpenAPI refresh failed for ${dev.deviceId}: ${e?.message ?? e}`)
|
|
2586
|
+
}
|
|
2587
|
+
}, Number.isFinite(concurrency) && concurrency > 0 ? concurrency : 5)
|
|
2588
|
+
this.debugLog(`Batch refresh completed for ${devicesToRefresh.length} devices`)
|
|
2589
|
+
}
|
|
2590
|
+
|
|
2591
|
+
/** Refresh a single device with retry and backoff; returns status object if successful */
|
|
2592
|
+
private async refreshSingleDeviceWithRetry(dev: device, retries = 3, baseDelayMs = 500): Promise<any | null> {
|
|
2593
|
+
const deviceId = dev.deviceId
|
|
2594
|
+
|
|
2595
|
+
// Check API budget BEFORE attempting any retries - don't waste cycles on blocked requests
|
|
2596
|
+
if (!this.apiTracker?.trySpend('poll')) {
|
|
2597
|
+
// Don't log on every blocked request - the ApiRequestTracker handles periodic warnings
|
|
2598
|
+
return null
|
|
2599
|
+
}
|
|
2600
|
+
|
|
2601
|
+
let attempt = 0
|
|
2602
|
+
while (attempt <= retries) {
|
|
2603
|
+
try {
|
|
2604
|
+
const { response, statusCode } = await this.switchBotAPI!.getDeviceStatus(deviceId, this.config.credentials?.token, this.config.credentials?.secret)
|
|
2605
|
+
const respAny: any = response
|
|
2606
|
+
const body = respAny?.body ?? respAny
|
|
2607
|
+
if (isSuccessfulStatusCode(statusCode)) {
|
|
2608
|
+
const status = body?.status ?? body
|
|
2609
|
+
this.deviceStatusCache.set(this.normalizeDeviceId(deviceId), { status, timestamp: Date.now() })
|
|
2610
|
+
this.debugLog(`OpenAPI refresh succeeded for ${deviceId} (attempt ${attempt + 1})`)
|
|
2611
|
+
return status
|
|
2612
|
+
}
|
|
2613
|
+
this.debugLog(`OpenAPI refresh unexpected statusCode=${statusCode} for ${deviceId} (attempt ${attempt + 1})`)
|
|
2614
|
+
} catch (e: any) {
|
|
2615
|
+
this.debugLog(`OpenAPI refresh error for ${deviceId} (attempt ${attempt + 1}): ${e?.message ?? e}`)
|
|
2616
|
+
}
|
|
2617
|
+
// backoff before next retry if any left
|
|
2618
|
+
attempt++
|
|
2619
|
+
if (attempt <= retries) {
|
|
2620
|
+
const delay = baseDelayMs * (2 ** (attempt - 1))
|
|
2621
|
+
await sleep(delay)
|
|
2622
|
+
}
|
|
2623
|
+
}
|
|
2624
|
+
// Set a cooldown after exhausting retries to avoid hammering problematic devices
|
|
2625
|
+
try {
|
|
2626
|
+
const nid = this.normalizeDeviceId(deviceId)
|
|
2627
|
+
const cooldownMs = Math.max(60_000, baseDelayMs * (2 ** retries) * 10)
|
|
2628
|
+
this.backoffCooldowns.set(nid, Date.now() + cooldownMs)
|
|
2629
|
+
this.debugLog(`Applied cooldown for ${deviceId}: ${Math.round(cooldownMs / 1000)}s`)
|
|
2630
|
+
} catch {}
|
|
2631
|
+
return null
|
|
2632
|
+
}
|
|
2633
|
+
|
|
2634
|
+
/**
|
|
2635
|
+
* Validate that the user's configuration won't exceed API limits
|
|
2636
|
+
* Warn if device count × polling frequency will hit daily limits
|
|
2637
|
+
*/
|
|
2638
|
+
private validateApiUsageConfig(deviceCount: number, irDeviceCount: number): void {
|
|
2639
|
+
try {
|
|
2640
|
+
const totalDevices = deviceCount + irDeviceCount
|
|
2641
|
+
if (totalDevices === 0) {
|
|
2642
|
+
return
|
|
2643
|
+
}
|
|
2644
|
+
|
|
2645
|
+
// For Matter platform, use matterBatchRefreshRate or fallback to refreshRate
|
|
2646
|
+
const refreshRate = this.getPlatformBatchInterval() // seconds
|
|
2647
|
+
const dailyLimit = this.config.options?.dailyApiLimit ?? 10000
|
|
2648
|
+
const reserveForCommands = this.config.options?.dailyApiReserveForCommands ?? 1000
|
|
2649
|
+
|
|
2650
|
+
// Calculate polls per day (86400 seconds in a day)
|
|
2651
|
+
const pollsPerDevicePerDay = Math.floor(86400 / refreshRate)
|
|
2652
|
+
const totalPollsPerDay = pollsPerDevicePerDay * totalDevices
|
|
2653
|
+
|
|
2654
|
+
// Add discovery calls (typically 1-2 per day)
|
|
2655
|
+
const estimatedDiscoveryCalls = 2
|
|
2656
|
+
const totalEstimatedCalls = totalPollsPerDay + estimatedDiscoveryCalls
|
|
2657
|
+
|
|
2658
|
+
const usableLimit = dailyLimit - reserveForCommands
|
|
2659
|
+
const percentOfLimit = Math.round((totalEstimatedCalls / usableLimit) * 100)
|
|
2660
|
+
|
|
2661
|
+
this.debugLog(`[API Usage Diagnostic] ${totalDevices} devices × ${pollsPerDevicePerDay} polls/day = ${totalPollsPerDay} estimated daily polls`)
|
|
2662
|
+
this.debugLog(`[API Usage Diagnostic] With ${reserveForCommands} reserved for commands, usable limit is ${usableLimit}`)
|
|
2663
|
+
|
|
2664
|
+
if (totalEstimatedCalls > dailyLimit) {
|
|
2665
|
+
this.errorLog(`⚠️ API LIMIT WARNING: Your configuration will exceed the daily API limit!`)
|
|
2666
|
+
this.errorLog(` Devices: ${totalDevices} | Refresh rate: ${refreshRate}s | Estimated daily polls: ${totalEstimatedCalls}`)
|
|
2667
|
+
this.errorLog(` Daily limit: ${dailyLimit} | You will use ${percentOfLimit}% of available budget`)
|
|
2668
|
+
this.errorLog(` SOLUTION: Increase matterBatchRefreshRate to ${Math.ceil((totalDevices * 86400) / usableLimit)} seconds or higher`)
|
|
2669
|
+
this.errorLog(` OR: Enable webhooks and set 'webhookOnlyOnReserve: true' to reduce polling`)
|
|
2670
|
+
} else if (totalEstimatedCalls > usableLimit) {
|
|
2671
|
+
this.warnLog(`⚠️ API USAGE WARNING: Configuration may exceed usable daily API budget`)
|
|
2672
|
+
this.warnLog(` Devices: ${totalDevices} | Refresh rate: ${refreshRate}s | Estimated daily polls: ${totalEstimatedCalls}`)
|
|
2673
|
+
this.warnLog(` Usable limit (after reserve): ${usableLimit} | You will use ${percentOfLimit}% of budget`)
|
|
2674
|
+
this.warnLog(` Polling may pause when approaching limit. Consider increasing matterBatchRefreshRate to ${Math.ceil((totalDevices * 86400) / usableLimit)}s`)
|
|
2675
|
+
} else if (percentOfLimit > 75) {
|
|
2676
|
+
this.infoLog(`[API Usage] Using ${percentOfLimit}% of daily budget (${totalEstimatedCalls}/${usableLimit} calls). Monitor usage if adding more devices.`)
|
|
2677
|
+
} else {
|
|
2678
|
+
this.debugLog(`[API Usage] Configuration looks good: ${percentOfLimit}% of daily budget (${totalEstimatedCalls}/${usableLimit} calls)`)
|
|
2679
|
+
}
|
|
2680
|
+
} catch (e: any) {
|
|
2681
|
+
this.debugLog(`Failed to validate API usage config: ${e?.message ?? e}`)
|
|
2682
|
+
}
|
|
2683
|
+
}
|
|
2684
|
+
|
|
2685
|
+
/** Simple concurrency limiter for an array of items */
|
|
2686
|
+
private async runWithConcurrency<T>(items: T[], worker: (item: T) => Promise<void>, concurrency: number): Promise<void> {
|
|
2687
|
+
const queue = items.slice()
|
|
2688
|
+
const workers: Promise<void>[] = []
|
|
2689
|
+
const runNext = async (): Promise<void> => {
|
|
2690
|
+
const item = queue.shift()
|
|
2691
|
+
if (!item) {
|
|
2692
|
+
return
|
|
2693
|
+
}
|
|
2694
|
+
await worker(item)
|
|
2695
|
+
return runNext()
|
|
2696
|
+
}
|
|
2697
|
+
const pool = Math.min(concurrency, Math.max(1, items.length))
|
|
2698
|
+
for (let i = 0; i < pool; i++) {
|
|
2699
|
+
workers.push(runNext())
|
|
2700
|
+
}
|
|
2701
|
+
await Promise.all(workers)
|
|
2702
|
+
}
|
|
1010
2703
|
}
|