@switchbot/homebridge-switchbot 3.7.1-beta.2 → 3.7.1-beta.20
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 +10 -0
- package/README.md +14 -0
- package/assets/security-privacy-bluetooth.png +0 -0
- package/config.schema.json +74 -2
- package/dist/device/blindtilt.d.ts +21 -9
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +167 -96
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot-old.d.ts +132 -0
- package/dist/device/bot-old.d.ts.map +1 -0
- package/dist/device/bot-old.js +1514 -0
- package/dist/device/bot-old.js.map +1 -0
- package/dist/device/bot.d.ts +10 -9
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +186 -187
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +14 -13
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +87 -97
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +13 -12
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +92 -105
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +6 -5
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +71 -68
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +17 -5
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +184 -118
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +18 -18
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +33 -36
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +9 -8
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +94 -101
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +6 -5
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +69 -65
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +6 -5
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +89 -104
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +6 -5
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +72 -66
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +7 -6
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +85 -99
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -9
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +89 -79
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +6 -5
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +72 -65
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +6 -5
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +72 -64
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +6 -5
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +65 -58
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +13 -12
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +68 -52
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +12 -11
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +93 -82
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +6 -5
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +73 -63
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/homebridge-ui/server.js +2 -2
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -4
- package/dist/index.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +8 -8
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +64 -105
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +10 -10
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +22 -42
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +9 -9
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +15 -25
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +8 -8
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +21 -40
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +11 -11
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +14 -22
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +9 -9
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +24 -47
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +9 -9
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +40 -83
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +9 -9
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +29 -54
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +5 -5
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +11 -21
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +5 -5
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +11 -22
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +24 -23
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +510 -722
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +5 -2
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js.map +1 -1
- package/dist/types/bledevicestatus.d.ts +9 -9
- package/dist/types/bledevicestatus.d.ts.map +1 -1
- package/dist/types/devicelist.d.ts +6 -6
- package/dist/types/devicelist.d.ts.map +1 -1
- package/dist/types/deviceresponse.d.ts +4 -4
- package/dist/types/deviceresponse.d.ts.map +1 -1
- package/dist/types/devicestatus.d.ts +2 -2
- package/dist/types/devicestatus.d.ts.map +1 -1
- package/dist/types/devicewebhookstatus.d.ts +5 -5
- package/dist/types/devicewebhookstatus.d.ts.map +1 -1
- package/dist/types/irdevicelist.d.ts +4 -4
- package/dist/types/irdevicelist.d.ts.map +1 -1
- package/dist/types/pushbody.d.ts +2 -2
- package/dist/types/pushbody.d.ts.map +1 -1
- package/dist/utils.d.ts +4 -4
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +9 -13
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +48 -93
- package/package.json +62 -53
- package/src/device/blindtilt.ts +537 -456
- package/src/device/bot-old.ts +1578 -0
- package/src/device/bot.ts +732 -725
- package/src/device/ceilinglight.ts +359 -368
- package/src/device/colorbulb.ts +441 -453
- package/src/device/contact.ts +229 -226
- package/src/device/curtain.ts +526 -444
- package/src/device/device.ts +206 -208
- package/src/device/fan.ts +336 -345
- package/src/device/hub.ts +213 -214
- package/src/device/humidifier.ts +326 -347
- package/src/device/iosensor.ts +211 -209
- package/src/device/lightstrip.ts +391 -404
- package/src/device/lock.ts +292 -282
- package/src/device/meter.ts +212 -210
- package/src/device/meterplus.ts +212 -209
- package/src/device/motion.ts +198 -190
- package/src/device/plug.ts +203 -182
- package/src/device/robotvacuumcleaner.ts +280 -269
- package/src/device/waterdetector.ts +198 -189
- package/src/homebridge-ui/server.ts +16 -15
- package/src/index.ts +5 -6
- package/src/irdevice/airconditioner.ts +280 -326
- package/src/irdevice/airpurifier.ts +124 -143
- package/src/irdevice/camera.ts +58 -64
- package/src/irdevice/fan.ts +106 -123
- package/src/irdevice/irdevice.ts +116 -119
- package/src/irdevice/light.ts +119 -143
- package/src/irdevice/other.ts +417 -461
- package/src/irdevice/tv.ts +155 -177
- package/src/irdevice/vacuumcleaner.ts +54 -60
- package/src/irdevice/waterheater.ts +56 -63
- package/src/platform.ts +1493 -1681
- package/src/settings.ts +172 -169
- package/src/types/bledevicestatus.ts +255 -255
- package/src/types/devicelist.ts +79 -79
- package/src/types/deviceresponse.ts +12 -13
- package/src/types/devicestatus.ts +124 -124
- package/src/types/devicewebhookstatus.ts +126 -127
- package/src/types/irdevicelist.ts +10 -10
- package/src/types/pushbody.ts +5 -5
- package/src/utils.ts +64 -65
- package/tsconfig.json +10 -9
package/src/device/device.ts
CHANGED
|
@@ -3,76 +3,79 @@
|
|
|
3
3
|
* device.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
import type {
|
|
13
|
-
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
import
|
|
17
|
-
import
|
|
6
|
+
import type { API, CharacteristicValue, HAP, Logging, PlatformAccessory, Service } from 'homebridge'
|
|
7
|
+
import type { MqttClient } from 'mqtt'
|
|
8
|
+
|
|
9
|
+
import type { SwitchBotPlatform } from '../platform.js'
|
|
10
|
+
import type { devicesConfig, SwitchBotPlatformConfig } from '../settings.js'
|
|
11
|
+
import type { ad } from '../types/bledevicestatus.js'
|
|
12
|
+
import type { device } from '../types/devicelist.js'
|
|
13
|
+
|
|
14
|
+
import { hostname } from 'node:os'
|
|
15
|
+
|
|
16
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelFriendlyName, SwitchBotBLEModelName, SwitchBotModel } from 'node-switchbot'
|
|
17
|
+
import { request } from 'undici'
|
|
18
|
+
|
|
19
|
+
import { Devices } from '../settings.js'
|
|
20
|
+
import { BlindTiltMappingMode, sleep } from '../utils.js'
|
|
18
21
|
|
|
19
22
|
export abstract class deviceBase {
|
|
20
|
-
public readonly api: API
|
|
21
|
-
public readonly log: Logging
|
|
22
|
-
public readonly config!: SwitchBotPlatformConfig
|
|
23
|
-
protected readonly hap: HAP
|
|
23
|
+
public readonly api: API
|
|
24
|
+
public readonly log: Logging
|
|
25
|
+
public readonly config!: SwitchBotPlatformConfig
|
|
26
|
+
protected readonly hap: HAP
|
|
24
27
|
|
|
25
28
|
// Config
|
|
26
|
-
protected deviceLogging!: string
|
|
27
|
-
protected deviceRefreshRate!: number
|
|
28
|
-
protected deviceUpdateRate!: number
|
|
29
|
-
protected devicePushRate!: number
|
|
30
|
-
protected deviceMaxRetries!: number
|
|
31
|
-
protected deviceDelayBetweenRetries!: number
|
|
29
|
+
protected deviceLogging!: string
|
|
30
|
+
protected deviceRefreshRate!: number
|
|
31
|
+
protected deviceUpdateRate!: number
|
|
32
|
+
protected devicePushRate!: number
|
|
33
|
+
protected deviceMaxRetries!: number
|
|
34
|
+
protected deviceDelayBetweenRetries!: number
|
|
32
35
|
|
|
33
36
|
// Connection
|
|
34
|
-
protected readonly BLE: boolean
|
|
35
|
-
protected readonly OpenAPI: boolean
|
|
37
|
+
protected readonly BLE: boolean
|
|
38
|
+
protected readonly OpenAPI: boolean
|
|
36
39
|
|
|
37
40
|
// Accsrroy Information
|
|
38
|
-
protected deviceModel!: SwitchBotModel
|
|
39
|
-
protected deviceBLEModel!: SwitchBotBLEModel
|
|
41
|
+
protected deviceModel!: SwitchBotModel
|
|
42
|
+
protected deviceBLEModel!: SwitchBotBLEModel
|
|
40
43
|
|
|
41
44
|
// MQTT
|
|
42
|
-
protected deviceMqttURL!: string
|
|
43
|
-
protected deviceMqttOptions!: any
|
|
44
|
-
protected deviceMqttPubOptions!: any
|
|
45
|
+
protected deviceMqttURL!: string
|
|
46
|
+
protected deviceMqttOptions!: any
|
|
47
|
+
protected deviceMqttPubOptions!: any
|
|
45
48
|
|
|
46
49
|
// BLE
|
|
47
|
-
protected scanDuration!: number
|
|
50
|
+
protected scanDuration!: number
|
|
48
51
|
|
|
49
52
|
// EVE history service handler
|
|
50
|
-
protected historyService?: any = null
|
|
53
|
+
protected historyService?: any = null
|
|
51
54
|
|
|
52
|
-
//MQTT stuff
|
|
53
|
-
protected mqttClient: MqttClient | null = null
|
|
55
|
+
// MQTT stuff
|
|
56
|
+
protected mqttClient: MqttClient | null = null
|
|
54
57
|
|
|
55
58
|
constructor(
|
|
56
59
|
protected readonly platform: SwitchBotPlatform,
|
|
57
60
|
protected accessory: PlatformAccessory,
|
|
58
61
|
protected device: device & devicesConfig,
|
|
59
62
|
) {
|
|
60
|
-
this.api = this.platform.api
|
|
61
|
-
this.log = this.platform.log
|
|
62
|
-
this.config = this.platform.config
|
|
63
|
-
this.hap = this.api.hap
|
|
63
|
+
this.api = this.platform.api
|
|
64
|
+
this.log = this.platform.log
|
|
65
|
+
this.config = this.platform.config
|
|
66
|
+
this.hap = this.api.hap
|
|
64
67
|
|
|
65
68
|
// Connection
|
|
66
|
-
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI'
|
|
67
|
-
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI'
|
|
69
|
+
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI'
|
|
70
|
+
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI'
|
|
68
71
|
|
|
69
|
-
this.getDeviceLogSettings(accessory, device)
|
|
70
|
-
this.getDeviceRateSettings(accessory, device)
|
|
71
|
-
this.getDeviceRetry(device)
|
|
72
|
-
this.getDeviceConfigSettings(device)
|
|
73
|
-
this.getDeviceContext(accessory, device)
|
|
74
|
-
this.getDeviceScanDuration(accessory, device)
|
|
75
|
-
this.getMqttSettings(device)
|
|
72
|
+
this.getDeviceLogSettings(accessory, device)
|
|
73
|
+
this.getDeviceRateSettings(accessory, device)
|
|
74
|
+
this.getDeviceRetry(device)
|
|
75
|
+
this.getDeviceConfigSettings(device)
|
|
76
|
+
this.getDeviceContext(accessory, device)
|
|
77
|
+
this.getDeviceScanDuration(accessory, device)
|
|
78
|
+
this.getMqttSettings(device)
|
|
76
79
|
|
|
77
80
|
// Set accessory information
|
|
78
81
|
accessory
|
|
@@ -83,81 +86,80 @@ export abstract class deviceBase {
|
|
|
83
86
|
.setCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName)
|
|
84
87
|
.setCharacteristic(this.hap.Characteristic.Model, device.model)
|
|
85
88
|
.setCharacteristic(this.hap.Characteristic.ProductData, device.deviceId)
|
|
86
|
-
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
89
|
+
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
async getDeviceLogSettings(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
|
|
90
|
-
this.deviceLogging = this.platform.debugMode ? 'debugMode' : device.logging ?? this.config.logging ?? 'standard'
|
|
91
|
-
const logging = this.platform.debugMode ? 'Debug Mode' : device.logging ? 'Device Config' : this.config.logging ? 'Platform Config' : 'Default'
|
|
92
|
-
accessory.context.deviceLogging = this.deviceLogging
|
|
93
|
-
await this.debugLog(`Using ${logging} Logging: ${this.deviceLogging}`)
|
|
93
|
+
this.deviceLogging = this.platform.debugMode ? 'debugMode' : device.logging ?? this.config.logging ?? 'standard'
|
|
94
|
+
const logging = this.platform.debugMode ? 'Debug Mode' : device.logging ? 'Device Config' : this.config.logging ? 'Platform Config' : 'Default'
|
|
95
|
+
accessory.context.deviceLogging = this.deviceLogging
|
|
96
|
+
await this.debugLog(`Using ${logging} Logging: ${this.deviceLogging}`)
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
async getDeviceRateSettings(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
|
|
97
100
|
// refreshRate
|
|
98
|
-
this.deviceRefreshRate = device.refreshRate ?? this.config.options?.refreshRate ?? 5
|
|
99
|
-
accessory.context.deviceRefreshRate = this.deviceRefreshRate
|
|
100
|
-
const refreshRate = device.refreshRate ? 'Device Config' : this.config.options?.refreshRate ? 'Platform Config' : 'Default'
|
|
101
|
+
this.deviceRefreshRate = device.refreshRate ?? this.config.options?.refreshRate ?? 5
|
|
102
|
+
accessory.context.deviceRefreshRate = this.deviceRefreshRate
|
|
103
|
+
const refreshRate = device.refreshRate ? 'Device Config' : this.config.options?.refreshRate ? 'Platform Config' : 'Default'
|
|
101
104
|
// updateRate
|
|
102
|
-
this.deviceUpdateRate = device.updateRate ?? this.config.options?.updateRate ?? 5
|
|
103
|
-
accessory.context.deviceUpdateRate = this.deviceUpdateRate
|
|
104
|
-
const updateRate = device.updateRate ? 'Device Config' : this.config.options?.updateRate ? 'Platform Config' : 'Default'
|
|
105
|
+
this.deviceUpdateRate = device.updateRate ?? this.config.options?.updateRate ?? 5
|
|
106
|
+
accessory.context.deviceUpdateRate = this.deviceUpdateRate
|
|
107
|
+
const updateRate = device.updateRate ? 'Device Config' : this.config.options?.updateRate ? 'Platform Config' : 'Default'
|
|
105
108
|
// pushRate
|
|
106
|
-
this.devicePushRate = device.pushRate ?? this.config.options?.pushRate ?? 1
|
|
107
|
-
accessory.context.devicePushRate = this.devicePushRate
|
|
108
|
-
const pushRate = device.pushRate ? 'Device Config' : this.config.options?.pushRate ? 'Platform Config' : 'Default'
|
|
109
|
-
await this.debugLog(`Using ${refreshRate} refreshRate: ${this.deviceRefreshRate}, ${updateRate} updateRate: ${this.deviceUpdateRate}
|
|
110
|
-
+ ` ${pushRate} pushRate: ${this.devicePushRate}`);
|
|
109
|
+
this.devicePushRate = device.pushRate ?? this.config.options?.pushRate ?? 1
|
|
110
|
+
accessory.context.devicePushRate = this.devicePushRate
|
|
111
|
+
const pushRate = device.pushRate ? 'Device Config' : this.config.options?.pushRate ? 'Platform Config' : 'Default'
|
|
112
|
+
await this.debugLog(`Using ${refreshRate} refreshRate: ${this.deviceRefreshRate}, ${updateRate} updateRate: ${this.deviceUpdateRate}, ${pushRate} pushRate: ${this.devicePushRate}`)
|
|
111
113
|
}
|
|
112
114
|
|
|
113
115
|
async getDeviceRetry(device: device & devicesConfig): Promise<void> {
|
|
114
|
-
this.deviceMaxRetries = device.maxRetries ?? 5
|
|
115
|
-
const maxRetries = device.maxRetries ? 'Device' : 'Default'
|
|
116
|
-
this.deviceDelayBetweenRetries = device.delayBetweenRetries ? (device.delayBetweenRetries * 1000) : 3000
|
|
117
|
-
const delayBetweenRetries = device.delayBetweenRetries ? 'Device' : 'Default'
|
|
118
|
-
await this.debugLog(`Using ${maxRetries} Max Retries: ${this.deviceMaxRetries}
|
|
119
|
-
+ ` ${delayBetweenRetries} Delay Between Retries: ${this.deviceDelayBetweenRetries}`);
|
|
116
|
+
this.deviceMaxRetries = device.maxRetries ?? 5
|
|
117
|
+
const maxRetries = device.maxRetries ? 'Device' : 'Default'
|
|
118
|
+
this.deviceDelayBetweenRetries = device.delayBetweenRetries ? (device.delayBetweenRetries * 1000) : 3000
|
|
119
|
+
const delayBetweenRetries = device.delayBetweenRetries ? 'Device' : 'Default'
|
|
120
|
+
await this.debugLog(`Using ${maxRetries} Max Retries: ${this.deviceMaxRetries}, ${delayBetweenRetries} Delay Between Retries: ${this.deviceDelayBetweenRetries}`)
|
|
120
121
|
}
|
|
121
122
|
|
|
122
|
-
async retryBLE({ max, fn }: { max: number
|
|
123
|
+
async retryBLE({ max, fn }: { max: number, fn: { (): any, (): Promise<any> } }): Promise<null> {
|
|
123
124
|
return fn().catch(async (e: any) => {
|
|
124
125
|
if (max === 0) {
|
|
125
|
-
throw e
|
|
126
|
+
throw e
|
|
126
127
|
}
|
|
127
|
-
await this.warnLog(e)
|
|
128
|
-
await this.infoLog('Retrying')
|
|
129
|
-
await sleep(1000)
|
|
130
|
-
return this.retryBLE({ max: max - 1, fn })
|
|
131
|
-
})
|
|
128
|
+
await this.warnLog(e)
|
|
129
|
+
await this.infoLog('Retrying')
|
|
130
|
+
await sleep(1000)
|
|
131
|
+
return this.retryBLE({ max: max - 1, fn })
|
|
132
|
+
})
|
|
132
133
|
}
|
|
133
134
|
|
|
134
135
|
async maxRetryBLE(): Promise<number> {
|
|
135
|
-
return this.device.maxRetry ? this.device.maxRetry : 5
|
|
136
|
+
return this.device.maxRetry ? this.device.maxRetry : 5
|
|
136
137
|
}
|
|
137
138
|
|
|
138
139
|
async getDeviceScanDuration(accessory: PlatformAccessory, device: device & devicesConfig): Promise<void> {
|
|
139
|
-
this.scanDuration = device.scanDuration
|
|
140
|
-
? (this.deviceUpdateRate >
|
|
140
|
+
this.scanDuration = device.scanDuration
|
|
141
|
+
? (this.deviceUpdateRate > device.scanDuration) ? this.deviceUpdateRate : device.scanDuration ? (this.deviceUpdateRate > 1) ? this.deviceUpdateRate : 1 : this.deviceUpdateRate
|
|
142
|
+
: 1
|
|
141
143
|
if (device.scanDuration) {
|
|
142
144
|
if (this.deviceUpdateRate > device.scanDuration) {
|
|
143
|
-
this.scanDuration = this.deviceUpdateRate
|
|
145
|
+
this.scanDuration = this.deviceUpdateRate
|
|
144
146
|
if (this.BLE) {
|
|
145
|
-
this.warnLog('scanDuration is less than updateRate, overriding scanDuration with updateRate')
|
|
147
|
+
this.warnLog('scanDuration is less than updateRate, overriding scanDuration with updateRate')
|
|
146
148
|
}
|
|
147
149
|
} else {
|
|
148
|
-
this.scanDuration = accessory.context.scanDuration = device.scanDuration
|
|
150
|
+
this.scanDuration = accessory.context.scanDuration = device.scanDuration
|
|
149
151
|
}
|
|
150
152
|
if (this.BLE) {
|
|
151
|
-
this.debugLog(`Using Device Config scanDuration: ${this.scanDuration}`)
|
|
153
|
+
this.debugLog(`Using Device Config scanDuration: ${this.scanDuration}`)
|
|
152
154
|
}
|
|
153
155
|
} else {
|
|
154
156
|
if (this.deviceUpdateRate > 1) {
|
|
155
|
-
this.scanDuration = this.deviceUpdateRate
|
|
157
|
+
this.scanDuration = this.deviceUpdateRate
|
|
156
158
|
} else {
|
|
157
|
-
this.scanDuration = accessory.context.scanDuration = 1
|
|
159
|
+
this.scanDuration = accessory.context.scanDuration = 1
|
|
158
160
|
}
|
|
159
161
|
if (this.BLE) {
|
|
160
|
-
this.debugLog(`Using Default scanDuration: ${this.scanDuration}`)
|
|
162
|
+
this.debugLog(`Using Default scanDuration: ${this.scanDuration}`)
|
|
161
163
|
}
|
|
162
164
|
}
|
|
163
165
|
}
|
|
@@ -179,7 +181,7 @@ export abstract class deviceBase {
|
|
|
179
181
|
device.mqttPubOptions && { mqttPubOptions: device.mqttPubOptions },
|
|
180
182
|
device.maxRetries !== 0 && { maxRetries: device.maxRetries },
|
|
181
183
|
device.delayBetweenRetries !== 0 && { delayBetweenRetries: device.delayBetweenRetries },
|
|
182
|
-
)
|
|
184
|
+
)
|
|
183
185
|
const config = Object.assign(
|
|
184
186
|
{},
|
|
185
187
|
deviceConfig,
|
|
@@ -199,30 +201,31 @@ export abstract class deviceBase {
|
|
|
199
201
|
device.plug,
|
|
200
202
|
device.blindTilt?.mode === undefined ? { mode: BlindTiltMappingMode.OnlyUp } : {},
|
|
201
203
|
device.blindTilt,
|
|
202
|
-
)
|
|
204
|
+
)
|
|
203
205
|
|
|
204
206
|
if (Object.keys(config).length !== 0) {
|
|
205
|
-
this.debugSuccessLog(`Config: ${JSON.stringify(config)}`)
|
|
207
|
+
this.debugSuccessLog(`Config: ${JSON.stringify(config)}`)
|
|
206
208
|
}
|
|
207
209
|
}
|
|
208
210
|
|
|
209
211
|
/**
|
|
210
212
|
* Get the current ambient light level based on the light level, set_minLux, set_maxLux, and spaceBetweenLevels.
|
|
211
|
-
* @param lightLevel
|
|
212
|
-
* @param set_minLux
|
|
213
|
-
* @param set_maxLux
|
|
214
|
-
* @param spaceBetweenLevels
|
|
213
|
+
* @param lightLevel number
|
|
214
|
+
* @param set_minLux number
|
|
215
|
+
* @param set_maxLux number
|
|
216
|
+
* @param spaceBetweenLevels number
|
|
215
217
|
* @returns CurrentAmbientLightLevel
|
|
216
218
|
*/
|
|
217
219
|
async getLightLevel(lightLevel: number, set_minLux: number, set_maxLux: number, spaceBetweenLevels: number): Promise<number> {
|
|
218
|
-
const numberOfLevels = spaceBetweenLevels + 1
|
|
219
|
-
this.debugLog(`LightLevel: ${lightLevel}, set_minLux: ${set_minLux}, set_maxLux: ${set_maxLux}, spaceBetweenLevels: ${spaceBetweenLevels}
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
220
|
+
const numberOfLevels = spaceBetweenLevels + 1
|
|
221
|
+
this.debugLog(`LightLevel: ${lightLevel}, set_minLux: ${set_minLux}, set_maxLux: ${set_maxLux}, spaceBetweenLevels: ${spaceBetweenLevels}, numberOfLevels: ${numberOfLevels}`)
|
|
222
|
+
const CurrentAmbientLightLevel = lightLevel === 1
|
|
223
|
+
? set_minLux
|
|
224
|
+
: lightLevel = numberOfLevels
|
|
225
|
+
? set_maxLux
|
|
226
|
+
: ((set_maxLux - set_minLux) / spaceBetweenLevels) * (Number(lightLevel) - 1)
|
|
227
|
+
await this.debugLog(`CurrentAmbientLightLevel: ${CurrentAmbientLightLevel}, LightLevel: ${lightLevel}, set_minLux: ${set_minLux}, set_maxLux: ${set_maxLux}`)
|
|
228
|
+
return CurrentAmbientLightLevel
|
|
226
229
|
}
|
|
227
230
|
|
|
228
231
|
/*
|
|
@@ -230,12 +233,12 @@ export abstract class deviceBase {
|
|
|
230
233
|
* 'homebridge-switchbot/${this.device.deviceType}/xx:xx:xx:xx:xx:xx'
|
|
231
234
|
*/
|
|
232
235
|
async mqttPublish(message: string, topic?: string) {
|
|
233
|
-
const mac = this.device.deviceId?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':')
|
|
234
|
-
const options = this.deviceMqttPubOptions ?? {}
|
|
235
|
-
const mqttTopic = topic ? `/${topic}` : ''
|
|
236
|
-
const mqttMessageTopic = topic ? `${topic}/` : ''
|
|
237
|
-
this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options)
|
|
238
|
-
this.debugLog(`MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`)
|
|
236
|
+
const mac = this.device.deviceId?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':')
|
|
237
|
+
const options = this.deviceMqttPubOptions ?? {}
|
|
238
|
+
const mqttTopic = topic ? `/${topic}` : ''
|
|
239
|
+
const mqttMessageTopic = topic ? `${topic}/` : ''
|
|
240
|
+
this.mqttClient?.publish(`homebridge-switchbot/${this.device.deviceType}/${mac}${mqttTopic}`, `${message}`, options)
|
|
241
|
+
this.debugLog(`MQTT message: ${mqttMessageTopic}${message} options:${JSON.stringify(options)}`)
|
|
239
242
|
}
|
|
240
243
|
|
|
241
244
|
/*
|
|
@@ -243,16 +246,15 @@ export abstract class deviceBase {
|
|
|
243
246
|
*/
|
|
244
247
|
async getMqttSettings(device: device & devicesConfig): Promise<void> {
|
|
245
248
|
// mqttURL
|
|
246
|
-
this.deviceMqttURL = device.mqttURL ?? this.config.options?.mqttURL ?? ''
|
|
247
|
-
const mqttURL = device.mqttURL ? 'Device Config' : this.config.options?.mqttURL ? 'Platform Config' : 'Default'
|
|
249
|
+
this.deviceMqttURL = device.mqttURL ?? this.config.options?.mqttURL ?? ''
|
|
250
|
+
const mqttURL = device.mqttURL ? 'Device Config' : this.config.options?.mqttURL ? 'Platform Config' : 'Default'
|
|
248
251
|
// mqttOptions
|
|
249
|
-
this.deviceMqttOptions = device.mqttOptions ?? this.config.options?.mqttOptions ?? {}
|
|
250
|
-
const mqttOptions = device.mqttOptions ? 'Device Config' : this.config.options?.mqttOptions ? 'Platform Config' : 'Default'
|
|
252
|
+
this.deviceMqttOptions = device.mqttOptions ?? this.config.options?.mqttOptions ?? {}
|
|
253
|
+
const mqttOptions = device.mqttOptions ? 'Device Config' : this.config.options?.mqttOptions ? 'Platform Config' : 'Default'
|
|
251
254
|
// mqttPubOptions
|
|
252
|
-
this.deviceMqttPubOptions = device.mqttPubOptions ?? this.config.options?.mqttPubOptions ?? {}
|
|
253
|
-
const mqttPubOptions = device.mqttPubOptions ? 'Device Config' : this.config.options?.mqttPubOptions ? 'Platform Config' : 'Default'
|
|
254
|
-
await this.debugLog(`Using ${mqttURL} MQTT URL: ${this.deviceMqttURL}, ${mqttOptions} mqttOptions: ${JSON.stringify(this.deviceMqttOptions)}
|
|
255
|
-
+ ` ${mqttPubOptions} mqttPubOptions: ${JSON.stringify(this.deviceMqttPubOptions)}`);
|
|
255
|
+
this.deviceMqttPubOptions = device.mqttPubOptions ?? this.config.options?.mqttPubOptions ?? {}
|
|
256
|
+
const mqttPubOptions = device.mqttPubOptions ? 'Device Config' : this.config.options?.mqttPubOptions ? 'Platform Config' : 'Default'
|
|
257
|
+
await this.debugLog(`Using ${mqttURL} MQTT URL: ${this.deviceMqttURL}, ${mqttOptions} mqttOptions: ${JSON.stringify(this.deviceMqttOptions)}, ${mqttPubOptions} mqttPubOptions: ${JSON.stringify(this.deviceMqttPubOptions)}`)
|
|
256
258
|
}
|
|
257
259
|
|
|
258
260
|
/*
|
|
@@ -262,53 +264,53 @@ export abstract class deviceBase {
|
|
|
262
264
|
const mac = this.device
|
|
263
265
|
.deviceId!.match(/.{1,2}/g)!
|
|
264
266
|
.join(':')
|
|
265
|
-
.toLowerCase()
|
|
267
|
+
.toLowerCase()
|
|
266
268
|
this.historyService = device.history
|
|
267
269
|
? new this.platform.fakegatoAPI('room', accessory, {
|
|
268
270
|
log: this.platform.log,
|
|
269
271
|
storage: 'fs',
|
|
270
272
|
filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
|
|
271
273
|
})
|
|
272
|
-
: null
|
|
274
|
+
: null
|
|
273
275
|
}
|
|
274
276
|
|
|
275
277
|
async switchbotBLE(): Promise<any> {
|
|
276
|
-
const switchbot = await this.platform.connectBLE(this.accessory, this.device)
|
|
278
|
+
const switchbot = await this.platform.connectBLE(this.accessory, this.device)
|
|
277
279
|
// Convert to BLE Address
|
|
278
|
-
await this.convertBLEAddress()
|
|
279
|
-
await this.getCustomBLEAddress(switchbot)
|
|
280
|
-
return switchbot
|
|
280
|
+
await this.convertBLEAddress()
|
|
281
|
+
await this.getCustomBLEAddress(switchbot)
|
|
282
|
+
return switchbot
|
|
281
283
|
}
|
|
282
284
|
|
|
283
285
|
async convertBLEAddress() {
|
|
284
286
|
this.device.bleMac = this.device
|
|
285
287
|
.deviceId!.match(/.{1,2}/g)!
|
|
286
288
|
.join(':')
|
|
287
|
-
.toLowerCase()
|
|
288
|
-
await this.debugLog(`BLE Address: ${this.device.bleMac}`)
|
|
289
|
+
.toLowerCase()
|
|
290
|
+
await this.debugLog(`BLE Address: ${this.device.bleMac}`)
|
|
289
291
|
}
|
|
290
292
|
|
|
291
293
|
async monitorAdvertisementPackets(switchbot: any) {
|
|
292
|
-
await this.debugLog(`Scanning for ${this.device.bleModelName} devices...`)
|
|
293
|
-
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac })
|
|
294
|
+
await this.debugLog(`Scanning for ${this.device.bleModelName} devices...`)
|
|
295
|
+
await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac })
|
|
294
296
|
// Set an event handler
|
|
295
|
-
let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as ad['serviceData']
|
|
297
|
+
let serviceData = { model: this.device.bleModel, modelName: this.device.bleModelName } as ad['serviceData']
|
|
296
298
|
switchbot.onadvertisement = async (ad: ad) => {
|
|
297
299
|
if (this.device.bleMac === ad.address && ad.serviceData.model === this.device.bleModel) {
|
|
298
|
-
this.debugLog(`${JSON.stringify(ad, null, ' ')}`)
|
|
299
|
-
this.debugLog(`address: ${ad.address}, model: ${ad.serviceData.model}`)
|
|
300
|
-
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
301
|
-
serviceData = ad.serviceData
|
|
300
|
+
this.debugLog(`${JSON.stringify(ad, null, ' ')}`)
|
|
301
|
+
this.debugLog(`address: ${ad.address}, model: ${ad.serviceData.model}`)
|
|
302
|
+
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
303
|
+
serviceData = ad.serviceData
|
|
302
304
|
} else {
|
|
303
|
-
serviceData = { model: '', modelName: '' } as ad['serviceData']
|
|
304
|
-
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
305
|
+
serviceData = { model: '', modelName: '' } as ad['serviceData']
|
|
306
|
+
this.debugLog(`serviceData: ${JSON.stringify(ad.serviceData)}`)
|
|
305
307
|
}
|
|
306
|
-
}
|
|
308
|
+
}
|
|
307
309
|
// Wait
|
|
308
|
-
await switchbot.wait(this.scanDuration * 1000)
|
|
310
|
+
await switchbot.wait(this.scanDuration * 1000)
|
|
309
311
|
// Stop to monitor
|
|
310
|
-
await switchbot.stopScan()
|
|
311
|
-
return serviceData
|
|
312
|
+
await switchbot.stopScan()
|
|
313
|
+
return serviceData
|
|
312
314
|
}
|
|
313
315
|
|
|
314
316
|
async getCustomBLEAddress(switchbot: any): Promise<void> {
|
|
@@ -316,66 +318,64 @@ export abstract class deviceBase {
|
|
|
316
318
|
this.debugLog(`customBLEaddress: ${this.device.customBLEaddress}`);
|
|
317
319
|
(async () => {
|
|
318
320
|
// Start to monitor advertisement packets
|
|
319
|
-
await switchbot.startScan({ model: this.device.bleModel })
|
|
321
|
+
await switchbot.startScan({ model: this.device.bleModel })
|
|
320
322
|
// Set an event handler
|
|
321
323
|
switchbot.onadvertisement = async (ad: ad) => {
|
|
322
|
-
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`)
|
|
323
|
-
}
|
|
324
|
-
await sleep(10000)
|
|
324
|
+
this.warnLog(`ad: ${JSON.stringify(ad, null, ' ')}`)
|
|
325
|
+
}
|
|
326
|
+
await sleep(10000)
|
|
325
327
|
// Stop to monitor
|
|
326
|
-
switchbot.stopScan()
|
|
327
|
-
})()
|
|
328
|
+
switchbot.stopScan()
|
|
329
|
+
})()
|
|
328
330
|
}
|
|
329
331
|
}
|
|
330
332
|
|
|
331
|
-
async pushChangeRequest(bodyChange: string): Promise<{ body: any
|
|
333
|
+
async pushChangeRequest(bodyChange: string): Promise<{ body: any, statusCode: any }> {
|
|
332
334
|
return await request(`${Devices}/${this.device.deviceId}/commands`, {
|
|
333
335
|
body: bodyChange,
|
|
334
336
|
method: 'POST',
|
|
335
337
|
headers: this.platform.generateHeaders(),
|
|
336
|
-
})
|
|
338
|
+
})
|
|
337
339
|
}
|
|
338
340
|
|
|
339
|
-
async deviceRefreshStatus(): Promise<{ body: any
|
|
340
|
-
return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
|
|
341
|
-
`${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
|
|
341
|
+
async deviceRefreshStatus(): Promise<{ body: any, statusCode: any }> {
|
|
342
|
+
return await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries, `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() })
|
|
342
343
|
}
|
|
343
344
|
|
|
344
345
|
async successfulStatusCodes(statusCode: any, deviceStatus: any) {
|
|
345
|
-
return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
|
|
346
|
+
return (statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)
|
|
346
347
|
}
|
|
347
348
|
|
|
348
349
|
/**
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
async updateCharacteristic(Service: Service, Characteristic: any,
|
|
360
|
-
CharacteristicValue: CharacteristicValue | undefined, CharacteristicName: string, history?: object): Promise<void> {
|
|
350
|
+
* Update the characteristic value and log the change.
|
|
351
|
+
*
|
|
352
|
+
* @param Service Service
|
|
353
|
+
* @param Characteristic Characteristic
|
|
354
|
+
* @param CharacteristicValue CharacteristicValue | undefined
|
|
355
|
+
* @param CharacteristicName string
|
|
356
|
+
* @param history object
|
|
357
|
+
* @return: void
|
|
358
|
+
*
|
|
359
|
+
*/
|
|
360
|
+
async updateCharacteristic(Service: Service, Characteristic: any, CharacteristicValue: CharacteristicValue | undefined, CharacteristicName: string, history?: object): Promise<void> {
|
|
361
361
|
if (CharacteristicValue === undefined) {
|
|
362
|
-
this.debugLog(`${CharacteristicName}: ${CharacteristicValue}`)
|
|
362
|
+
this.debugLog(`${CharacteristicName}: ${CharacteristicValue}`)
|
|
363
363
|
} else {
|
|
364
|
-
await this.mqtt(CharacteristicName, CharacteristicValue)
|
|
364
|
+
await this.mqtt(CharacteristicName, CharacteristicValue)
|
|
365
365
|
if (this.device.history) {
|
|
366
|
-
this.historyService?.addEntry(history)
|
|
366
|
+
this.historyService?.addEntry(history)
|
|
367
367
|
}
|
|
368
|
-
Service.updateCharacteristic(Characteristic, CharacteristicValue)
|
|
369
|
-
this.debugLog(`updateCharacteristic ${CharacteristicName}: ${CharacteristicValue}`)
|
|
370
|
-
this.debugWarnLog(`${CharacteristicName} context before: ${this.accessory.context[CharacteristicName]}`)
|
|
371
|
-
this.accessory.context[CharacteristicName] = CharacteristicValue
|
|
372
|
-
this.debugWarnLog(`${CharacteristicName} context after: ${this.accessory.context[CharacteristicName]}`)
|
|
368
|
+
Service.updateCharacteristic(Characteristic, CharacteristicValue)
|
|
369
|
+
this.debugLog(`updateCharacteristic ${CharacteristicName}: ${CharacteristicValue}`)
|
|
370
|
+
this.debugWarnLog(`${CharacteristicName} context before: ${this.accessory.context[CharacteristicName]}`)
|
|
371
|
+
this.accessory.context[CharacteristicName] = CharacteristicValue
|
|
372
|
+
this.debugWarnLog(`${CharacteristicName} context after: ${this.accessory.context[CharacteristicName]}`)
|
|
373
373
|
}
|
|
374
374
|
}
|
|
375
375
|
|
|
376
376
|
async mqtt(CharacteristicName: string, CharacteristicValue: CharacteristicValue) {
|
|
377
377
|
if (this.device.mqttURL) {
|
|
378
|
-
this.mqttPublish(CharacteristicName, CharacteristicValue.toString())
|
|
378
|
+
this.mqttPublish(CharacteristicName, CharacteristicValue.toString())
|
|
379
379
|
}
|
|
380
380
|
}
|
|
381
381
|
|
|
@@ -585,32 +585,31 @@ export abstract class deviceBase {
|
|
|
585
585
|
bleModelName: SwitchBotBLEModelName.Unknown,
|
|
586
586
|
bleModelFriendlyName: SwitchBotBLEModelFriendlyName.Unknown,
|
|
587
587
|
},
|
|
588
|
-
}
|
|
588
|
+
}
|
|
589
589
|
const defaultDevice = {
|
|
590
590
|
model: SwitchBotModel.Unknown,
|
|
591
591
|
bleModel: SwitchBotBLEModel.Unknown,
|
|
592
592
|
bleModelName: SwitchBotBLEModelName.Unknown,
|
|
593
593
|
bleModelFriendlyName: SwitchBotBLEModelFriendlyName.Unknown,
|
|
594
|
-
}
|
|
595
|
-
const deviceConfig = deviceMapping[device.deviceType] || defaultDevice
|
|
596
|
-
device.model = deviceConfig.model
|
|
597
|
-
device.bleModel = deviceConfig.bleModel
|
|
598
|
-
device.bleModelName = deviceConfig.bleModelName
|
|
599
|
-
device.bleModelFriednlyName = deviceConfig.bleModelFriednlyName
|
|
600
|
-
await this.debugLog(`Model: ${device.model}, BLE Model: ${device.bleModel}, BLE Model Name: ${device.bleModelName}, `
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
const
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
let deviceVersion: string;
|
|
594
|
+
}
|
|
595
|
+
const deviceConfig = deviceMapping[device.deviceType] || defaultDevice
|
|
596
|
+
device.model = deviceConfig.model
|
|
597
|
+
device.bleModel = deviceConfig.bleModel
|
|
598
|
+
device.bleModelName = deviceConfig.bleModelName
|
|
599
|
+
device.bleModelFriednlyName = deviceConfig.bleModelFriednlyName
|
|
600
|
+
await this.debugLog(`Model: ${device.model}, BLE Model: ${device.bleModel}, BLE Model Name: ${device.bleModelName}, BLE Model Friendly Name: ${device.bleModelFriednlyName}`)
|
|
601
|
+
|
|
602
|
+
const deviceFirmwareVersion = device.firmware ?? device.version ?? accessory.context.version ?? this.platform.version ?? '0.0.0'
|
|
603
|
+
const version = deviceFirmwareVersion.toString()
|
|
604
|
+
this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`)
|
|
605
|
+
let deviceVersion: string
|
|
607
606
|
if (version?.includes('.') === false) {
|
|
608
|
-
const replace = version?.replace(/^V|-.*$/g, '')
|
|
609
|
-
const match = replace?.match(
|
|
610
|
-
const validVersion = match?.join('.')
|
|
611
|
-
deviceVersion = validVersion ?? '0.0.0'
|
|
607
|
+
const replace = version?.replace(/^V|-.*$/g, '')
|
|
608
|
+
const match = replace?.match(/./g)
|
|
609
|
+
const validVersion = match?.join('.')
|
|
610
|
+
deviceVersion = validVersion ?? '0.0.0'
|
|
612
611
|
} else {
|
|
613
|
-
deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
|
|
612
|
+
deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
|
|
614
613
|
}
|
|
615
614
|
accessory
|
|
616
615
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
@@ -618,9 +617,9 @@ export abstract class deviceBase {
|
|
|
618
617
|
.setCharacteristic(this.hap.Characteristic.SoftwareRevision, deviceVersion)
|
|
619
618
|
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
|
|
620
619
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
621
|
-
.updateValue(deviceVersion)
|
|
622
|
-
accessory.context.version = deviceVersion
|
|
623
|
-
this.debugSuccessLog(`version: ${accessory.context.version}`)
|
|
620
|
+
.updateValue(deviceVersion)
|
|
621
|
+
accessory.context.version = deviceVersion
|
|
622
|
+
this.debugSuccessLog(`version: ${accessory.context.version}`)
|
|
624
623
|
}
|
|
625
624
|
|
|
626
625
|
async statusCode(statusCode: number): Promise<void> {
|
|
@@ -642,15 +641,14 @@ export abstract class deviceBase {
|
|
|
642
641
|
422: 'Unprocessable Entity: The server cannot process the request, often due to exceeded API limits.',
|
|
643
642
|
429: 'Too Many Requests, exceeded the number of requests allowed for a given time window',
|
|
644
643
|
500: 'Internal Server Error, An unexpected error occurred. These errors should be rare',
|
|
645
|
-
}
|
|
644
|
+
}
|
|
646
645
|
if (statusCode === 171 && (this.device.hubDeviceId === this.device.deviceId || this.device.hubDeviceId === '000000000000')) {
|
|
647
|
-
this.debugErrorLog(`statusCode 171 changed to 161: hubDeviceId ${this.device.hubDeviceId} matches deviceId
|
|
648
|
-
|
|
649
|
-
statusCode = 161;
|
|
646
|
+
this.debugErrorLog(`statusCode 171 changed to 161: hubDeviceId ${this.device.hubDeviceId} matches deviceId ${this.device.deviceId}, device is its own hub.`)
|
|
647
|
+
statusCode = 161
|
|
650
648
|
}
|
|
651
|
-
const logMessage = statusMessages[statusCode] || `Unknown statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug
|
|
652
|
-
const logMethod = [100, 200].includes(statusCode) ? 'debugLog' : statusMessages[statusCode] ? 'errorLog' : 'infoLog'
|
|
653
|
-
this[logMethod](`${logMessage}, statusCode: ${statusCode}`)
|
|
649
|
+
const logMessage = statusMessages[statusCode] || `Unknown statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`
|
|
650
|
+
const logMethod = [100, 200].includes(statusCode) ? 'debugLog' : statusMessages[statusCode] ? 'errorLog' : 'infoLog'
|
|
651
|
+
this[logMethod](`${logMessage}, statusCode: ${statusCode}`)
|
|
654
652
|
}
|
|
655
653
|
|
|
656
654
|
/**
|
|
@@ -658,48 +656,48 @@ export abstract class deviceBase {
|
|
|
658
656
|
*/
|
|
659
657
|
async infoLog(...log: any[]): Promise<void> {
|
|
660
658
|
if (await this.enablingDeviceLogging()) {
|
|
661
|
-
this.log.info(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
659
|
+
this.log.info(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
662
660
|
}
|
|
663
661
|
}
|
|
664
662
|
|
|
665
663
|
async successLog(...log: any[]): Promise<void> {
|
|
666
664
|
if (await this.enablingDeviceLogging()) {
|
|
667
|
-
this.log.success(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
665
|
+
this.log.success(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
668
666
|
}
|
|
669
667
|
}
|
|
670
668
|
|
|
671
669
|
async debugSuccessLog(...log: any[]): Promise<void> {
|
|
672
670
|
if (await this.enablingDeviceLogging()) {
|
|
673
671
|
if (await this.loggingIsDebug()) {
|
|
674
|
-
this.log.success(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
672
|
+
this.log.success(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
675
673
|
}
|
|
676
674
|
}
|
|
677
675
|
}
|
|
678
676
|
|
|
679
677
|
async warnLog(...log: any[]): Promise<void> {
|
|
680
678
|
if (await this.enablingDeviceLogging()) {
|
|
681
|
-
this.log.warn(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
679
|
+
this.log.warn(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
682
680
|
}
|
|
683
681
|
}
|
|
684
682
|
|
|
685
683
|
async debugWarnLog(...log: any[]): Promise<void> {
|
|
686
684
|
if (await this.enablingDeviceLogging()) {
|
|
687
685
|
if (await this.loggingIsDebug()) {
|
|
688
|
-
this.log.warn(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
686
|
+
this.log.warn(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
689
687
|
}
|
|
690
688
|
}
|
|
691
689
|
}
|
|
692
690
|
|
|
693
691
|
async errorLog(...log: any[]): Promise<void> {
|
|
694
692
|
if (await this.enablingDeviceLogging()) {
|
|
695
|
-
this.log.error(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
693
|
+
this.log.error(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
696
694
|
}
|
|
697
695
|
}
|
|
698
696
|
|
|
699
697
|
async debugErrorLog(...log: any[]): Promise<void> {
|
|
700
698
|
if (await this.enablingDeviceLogging()) {
|
|
701
699
|
if (await this.loggingIsDebug()) {
|
|
702
|
-
this.log.error(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
700
|
+
this.log.error(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
703
701
|
}
|
|
704
702
|
}
|
|
705
703
|
}
|
|
@@ -707,18 +705,18 @@ export abstract class deviceBase {
|
|
|
707
705
|
async debugLog(...log: any[]): Promise<void> {
|
|
708
706
|
if (await this.enablingDeviceLogging()) {
|
|
709
707
|
if (this.deviceLogging === 'debug') {
|
|
710
|
-
this.log.info(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
708
|
+
this.log.info(`[DEBUG] ${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
711
709
|
} else if (this.deviceLogging === 'debugMode') {
|
|
712
|
-
this.log.debug(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
710
|
+
this.log.debug(`${this.device.deviceType}: ${this.accessory.displayName}`, String(...log))
|
|
713
711
|
}
|
|
714
712
|
}
|
|
715
713
|
}
|
|
716
714
|
|
|
717
715
|
async loggingIsDebug(): Promise<boolean> {
|
|
718
|
-
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug'
|
|
716
|
+
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug'
|
|
719
717
|
}
|
|
720
718
|
|
|
721
719
|
async enablingDeviceLogging(): Promise<boolean> {
|
|
722
|
-
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard'
|
|
720
|
+
return this.deviceLogging === 'debugMode' || this.deviceLogging === 'debug' || this.deviceLogging === 'standard'
|
|
723
721
|
}
|
|
724
722
|
}
|