@switchbot/homebridge-switchbot 3.7.1-beta.2 → 3.7.1-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +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 +1482 -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 +61 -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 +509 -723
- 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 +64 -57
- package/src/device/blindtilt.ts +537 -456
- package/src/device/bot-old.ts +1540 -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 +277 -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 +1491 -1681
- package/src/settings.ts +172 -169
- package/src/types/bledevicestatus.ts +252 -256
- 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 -131
- 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/contact.ts
CHANGED
|
@@ -2,17 +2,23 @@
|
|
|
2
2
|
*
|
|
3
3
|
* contact.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
import type {
|
|
10
|
-
import type { device } from '../types/devicelist.js'
|
|
11
|
-
import type {
|
|
12
|
-
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
5
|
+
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge'
|
|
6
|
+
|
|
7
|
+
import type { SwitchBotPlatform } from '../platform.js'
|
|
8
|
+
import type { devicesConfig } from '../settings.js'
|
|
9
|
+
import type { contactSensorServiceData } from '../types/bledevicestatus.js'
|
|
10
|
+
import type { device } from '../types/devicelist.js'
|
|
11
|
+
import type { contactSensorStatus } from '../types/devicestatus.js'
|
|
12
|
+
import type { contactSensorWebhookContext } from '../types/devicewebhookstatus.js'
|
|
13
|
+
|
|
14
|
+
/*
|
|
15
|
+
* For Testing Locally:
|
|
16
|
+
* import { SwitchBotBLEModel, SwitchBotBLEModelName } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
|
|
17
|
+
*/
|
|
18
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot'
|
|
19
|
+
import { interval, skipWhile, Subject } from 'rxjs'
|
|
20
|
+
|
|
21
|
+
import { deviceBase } from './device.js'
|
|
16
22
|
|
|
17
23
|
/**
|
|
18
24
|
* Platform Accessory
|
|
@@ -22,259 +28,258 @@ import type { contactSensorWebhookContext } from '../types/devicewebhookstatus.j
|
|
|
22
28
|
export class Contact extends deviceBase {
|
|
23
29
|
// Services
|
|
24
30
|
private ContactSensor: {
|
|
25
|
-
Name: CharacteristicValue
|
|
26
|
-
Service: Service
|
|
27
|
-
ContactSensorState: CharacteristicValue
|
|
28
|
-
}
|
|
31
|
+
Name: CharacteristicValue
|
|
32
|
+
Service: Service
|
|
33
|
+
ContactSensorState: CharacteristicValue
|
|
34
|
+
}
|
|
29
35
|
|
|
30
36
|
private Battery: {
|
|
31
|
-
Name: CharacteristicValue
|
|
32
|
-
Service: Service
|
|
33
|
-
BatteryLevel: CharacteristicValue
|
|
34
|
-
StatusLowBattery: CharacteristicValue
|
|
35
|
-
}
|
|
37
|
+
Name: CharacteristicValue
|
|
38
|
+
Service: Service
|
|
39
|
+
BatteryLevel: CharacteristicValue
|
|
40
|
+
StatusLowBattery: CharacteristicValue
|
|
41
|
+
}
|
|
36
42
|
|
|
37
43
|
private MotionSensor?: {
|
|
38
|
-
Name: CharacteristicValue
|
|
39
|
-
Service: Service
|
|
40
|
-
MotionDetected: CharacteristicValue
|
|
41
|
-
}
|
|
44
|
+
Name: CharacteristicValue
|
|
45
|
+
Service: Service
|
|
46
|
+
MotionDetected: CharacteristicValue
|
|
47
|
+
}
|
|
42
48
|
|
|
43
49
|
private LightSensor?: {
|
|
44
|
-
Name: CharacteristicValue
|
|
45
|
-
Service: Service
|
|
46
|
-
CurrentAmbientLightLevel: CharacteristicValue
|
|
47
|
-
}
|
|
50
|
+
Name: CharacteristicValue
|
|
51
|
+
Service: Service
|
|
52
|
+
CurrentAmbientLightLevel: CharacteristicValue
|
|
53
|
+
}
|
|
48
54
|
|
|
49
55
|
// OpenAPI
|
|
50
|
-
deviceStatus!: contactSensorStatus
|
|
56
|
+
deviceStatus!: contactSensorStatus
|
|
51
57
|
|
|
52
|
-
//Webhook
|
|
53
|
-
webhookContext!: contactSensorWebhookContext
|
|
58
|
+
// Webhook
|
|
59
|
+
webhookContext!: contactSensorWebhookContext
|
|
54
60
|
|
|
55
61
|
// BLE
|
|
56
|
-
serviceData!: contactSensorServiceData
|
|
62
|
+
serviceData!: contactSensorServiceData
|
|
57
63
|
|
|
58
64
|
// Updates
|
|
59
|
-
contactUpdateInProgress!: boolean
|
|
60
|
-
doContactUpdate!: Subject<void
|
|
65
|
+
contactUpdateInProgress!: boolean
|
|
66
|
+
doContactUpdate!: Subject<void>
|
|
61
67
|
|
|
62
68
|
constructor(
|
|
63
69
|
readonly platform: SwitchBotPlatform,
|
|
64
70
|
accessory: PlatformAccessory,
|
|
65
71
|
device: device & devicesConfig,
|
|
66
72
|
) {
|
|
67
|
-
super(platform, accessory, device)
|
|
73
|
+
super(platform, accessory, device)
|
|
68
74
|
// Set category
|
|
69
|
-
accessory.category = this.hap.Categories.SENSOR
|
|
75
|
+
accessory.category = this.hap.Categories.SENSOR
|
|
70
76
|
|
|
71
77
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
72
|
-
this.doContactUpdate = new Subject()
|
|
73
|
-
this.contactUpdateInProgress = false
|
|
78
|
+
this.doContactUpdate = new Subject()
|
|
79
|
+
this.contactUpdateInProgress = false
|
|
74
80
|
|
|
75
81
|
// Initialize Contact Sensor Service
|
|
76
|
-
accessory.context.ContactSensor = accessory.context.ContactSensor ?? {}
|
|
82
|
+
accessory.context.ContactSensor = accessory.context.ContactSensor ?? {}
|
|
77
83
|
this.ContactSensor = {
|
|
78
84
|
Name: accessory.displayName,
|
|
79
85
|
Service: accessory.getService(this.hap.Service.ContactSensor) ?? accessory.addService(this.hap.Service.ContactSensor) as Service,
|
|
80
86
|
ContactSensorState: accessory.context.ContactSensorState ?? this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED,
|
|
81
|
-
}
|
|
82
|
-
accessory.context.ContactSensor = this.ContactSensor as object
|
|
87
|
+
}
|
|
88
|
+
accessory.context.ContactSensor = this.ContactSensor as object
|
|
83
89
|
|
|
84
90
|
// Initialize ContactSensor Characteristics
|
|
85
|
-
this.ContactSensor.Service
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
.getCharacteristic(this.hap.Characteristic.ContactSensorState)
|
|
89
|
-
.onGet(() => {
|
|
90
|
-
return this.ContactSensor.ContactSensorState;
|
|
91
|
-
});
|
|
91
|
+
this.ContactSensor.Service.setCharacteristic(this.hap.Characteristic.Name, this.ContactSensor.Name).setCharacteristic(this.hap.Characteristic.StatusActive, true).getCharacteristic(this.hap.Characteristic.ContactSensorState).onGet(() => {
|
|
92
|
+
return this.ContactSensor.ContactSensorState
|
|
93
|
+
})
|
|
92
94
|
|
|
93
95
|
// Initialize Battery Service
|
|
94
|
-
accessory.context.Battery = accessory.context.Battery ?? {}
|
|
96
|
+
accessory.context.Battery = accessory.context.Battery ?? {}
|
|
95
97
|
this.Battery = {
|
|
96
98
|
Name: `${accessory.displayName} Battery`,
|
|
97
99
|
Service: accessory.getService(this.hap.Service.Battery) ?? accessory.addService(this.hap.Service.Battery) as Service,
|
|
98
100
|
BatteryLevel: accessory.context.BatteryLevel ?? 100,
|
|
99
101
|
StatusLowBattery: accessory.context.StatusLowBattery ?? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
|
|
100
|
-
}
|
|
101
|
-
accessory.context.Battery = this.Battery as object
|
|
102
|
+
}
|
|
103
|
+
accessory.context.Battery = this.Battery as object
|
|
102
104
|
|
|
103
105
|
// Initialize Battery Characteristics
|
|
104
|
-
this.Battery.Service
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.Battery.Service
|
|
113
|
-
.setCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery)
|
|
114
|
-
.getCharacteristic(this.hap.Characteristic.StatusLowBattery)
|
|
115
|
-
.onGet(() => {
|
|
116
|
-
return this.Battery.StatusLowBattery;
|
|
117
|
-
});
|
|
106
|
+
this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, this.Battery.Name).setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE).getCharacteristic(this.hap.Characteristic.BatteryLevel).onGet(() => {
|
|
107
|
+
return this.Battery.BatteryLevel
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
this.Battery.Service.setCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery).getCharacteristic(this.hap.Characteristic.StatusLowBattery).onGet(() => {
|
|
111
|
+
return this.Battery.StatusLowBattery
|
|
112
|
+
})
|
|
118
113
|
|
|
119
114
|
// Initialize Motion Sensor Service
|
|
120
115
|
if (this.device.contact?.hide_motionsensor) {
|
|
121
116
|
if (this.MotionSensor) {
|
|
122
|
-
this.debugLog('Removing Motion Sensor Service')
|
|
123
|
-
this.MotionSensor.Service = accessory.getService(this.hap.Service.MotionSensor) as Service
|
|
124
|
-
accessory.removeService(this.MotionSensor.Service)
|
|
117
|
+
this.debugLog('Removing Motion Sensor Service')
|
|
118
|
+
this.MotionSensor.Service = accessory.getService(this.hap.Service.MotionSensor) as Service
|
|
119
|
+
accessory.removeService(this.MotionSensor.Service)
|
|
125
120
|
}
|
|
126
121
|
} else {
|
|
127
|
-
accessory.context.MotionSensor = accessory.context.MotionSensor ?? {}
|
|
122
|
+
accessory.context.MotionSensor = accessory.context.MotionSensor ?? {}
|
|
128
123
|
this.MotionSensor = {
|
|
129
124
|
Name: `${accessory.displayName} Motion Sensor`,
|
|
130
125
|
Service: accessory.getService(this.hap.Service.MotionSensor) ?? accessory.addService(this.hap.Service.MotionSensor) as Service,
|
|
131
126
|
MotionDetected: accessory.context.MotionDetected ?? false,
|
|
132
|
-
}
|
|
133
|
-
accessory.context.MotionSensor = this.MotionSensor as object
|
|
127
|
+
}
|
|
128
|
+
accessory.context.MotionSensor = this.MotionSensor as object
|
|
134
129
|
|
|
135
130
|
// Motion Sensor Characteristics
|
|
136
|
-
this.MotionSensor.Service
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
.getCharacteristic(this.hap.Characteristic.MotionDetected)
|
|
140
|
-
.onGet(() => {
|
|
141
|
-
return this.MotionSensor!.MotionDetected;
|
|
142
|
-
});
|
|
131
|
+
this.MotionSensor.Service.setCharacteristic(this.hap.Characteristic.Name, this.MotionSensor.Name).setCharacteristic(this.hap.Characteristic.StatusActive, true).getCharacteristic(this.hap.Characteristic.MotionDetected).onGet(() => {
|
|
132
|
+
return this.MotionSensor!.MotionDetected
|
|
133
|
+
})
|
|
143
134
|
}
|
|
144
135
|
|
|
145
136
|
// Initialize Light Sensor Service
|
|
146
137
|
if (device.contact?.hide_lightsensor) {
|
|
147
138
|
if (this.LightSensor) {
|
|
148
|
-
this.debugLog('Removing Light Sensor Service')
|
|
149
|
-
this.LightSensor.Service = accessory.getService(this.hap.Service.LightSensor) as Service
|
|
150
|
-
accessory.removeService(this.LightSensor.Service)
|
|
139
|
+
this.debugLog('Removing Light Sensor Service')
|
|
140
|
+
this.LightSensor.Service = accessory.getService(this.hap.Service.LightSensor) as Service
|
|
141
|
+
accessory.removeService(this.LightSensor.Service)
|
|
151
142
|
}
|
|
152
143
|
} else {
|
|
153
|
-
accessory.context.LightSensor = accessory.context.LightSensor ?? {}
|
|
144
|
+
accessory.context.LightSensor = accessory.context.LightSensor ?? {}
|
|
154
145
|
this.LightSensor = {
|
|
155
146
|
Name: `${accessory.displayName} Light Sensor`,
|
|
156
147
|
Service: accessory.getService(this.hap.Service.LightSensor) ?? accessory.addService(this.hap.Service.LightSensor) as Service,
|
|
157
148
|
CurrentAmbientLightLevel: accessory.context.CurrentAmbientLightLevel ?? 0.0001,
|
|
158
|
-
}
|
|
159
|
-
accessory.context.LightSensor = this.LightSensor as object
|
|
149
|
+
}
|
|
150
|
+
accessory.context.LightSensor = this.LightSensor as object
|
|
160
151
|
|
|
161
152
|
// Light Sensor Characteristics
|
|
162
|
-
this.LightSensor.Service
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
.getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel)
|
|
166
|
-
.onGet(() => {
|
|
167
|
-
return this.LightSensor!.CurrentAmbientLightLevel;
|
|
168
|
-
});
|
|
153
|
+
this.LightSensor.Service.setCharacteristic(this.hap.Characteristic.Name, this.LightSensor.Name).setCharacteristic(this.hap.Characteristic.StatusActive, true).getCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel).onGet(() => {
|
|
154
|
+
return this.LightSensor!.CurrentAmbientLightLevel
|
|
155
|
+
})
|
|
169
156
|
}
|
|
170
157
|
|
|
171
158
|
// Retrieve initial values and updateHomekit
|
|
172
|
-
|
|
173
|
-
|
|
159
|
+
try {
|
|
160
|
+
this.debugLog('Retrieve initial values and update Homekit')
|
|
161
|
+
this.refreshStatus()
|
|
162
|
+
} catch (e: any) {
|
|
163
|
+
this.errorLog(`failed to retrieve initial values and update Homekit, Error: ${e}`)
|
|
164
|
+
}
|
|
174
165
|
|
|
175
|
-
//regisiter webhook event handler
|
|
176
|
-
|
|
177
|
-
|
|
166
|
+
// regisiter webhook event handler if enabled
|
|
167
|
+
try {
|
|
168
|
+
this.debugLog('Registering Webhook Event Handler')
|
|
169
|
+
this.registerWebhook()
|
|
170
|
+
} catch (e: any) {
|
|
171
|
+
this.errorLog(`failed to registerWebhook, Error: ${e}`)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// regisiter platform BLE event handler if enabled
|
|
175
|
+
try {
|
|
176
|
+
this.debugLog('Registering Platform BLE Event Handler')
|
|
177
|
+
this.registerPlatformBLE()
|
|
178
|
+
} catch (e: any) {
|
|
179
|
+
this.errorLog(`failed to registerPlatformBLE, Error: ${e}`)
|
|
180
|
+
}
|
|
178
181
|
|
|
179
182
|
// Start an update interval
|
|
180
183
|
interval(this.deviceRefreshRate * 1000)
|
|
181
184
|
.pipe(skipWhile(() => this.contactUpdateInProgress))
|
|
182
185
|
.subscribe(async () => {
|
|
183
|
-
await this.refreshStatus()
|
|
184
|
-
})
|
|
186
|
+
await this.refreshStatus()
|
|
187
|
+
})
|
|
185
188
|
}
|
|
186
189
|
|
|
187
190
|
async BLEparseStatus(): Promise<void> {
|
|
188
|
-
await this.debugLog('BLEparseStatus')
|
|
191
|
+
await this.debugLog('BLEparseStatus')
|
|
189
192
|
// ContactSensorState
|
|
190
193
|
this.ContactSensor.ContactSensorState = this.serviceData.doorState === 'open'
|
|
191
|
-
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
|
|
192
|
-
|
|
194
|
+
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
|
|
195
|
+
: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
|
|
196
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
|
|
193
197
|
|
|
194
198
|
// MotionDetected
|
|
195
199
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
196
|
-
this.MotionSensor.MotionDetected = this.serviceData.movement
|
|
197
|
-
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
200
|
+
this.MotionSensor.MotionDetected = this.serviceData.movement
|
|
201
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
198
202
|
}
|
|
199
203
|
// CurrentAmbientLightLevel
|
|
200
204
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
201
|
-
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
202
|
-
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
203
|
-
const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux
|
|
204
|
-
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
205
|
-
await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
205
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
206
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
207
|
+
const lightLevel = this.serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux
|
|
208
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
209
|
+
await this.debugLog(`LightLevel: ${this.serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
206
210
|
}
|
|
207
211
|
// BatteryLevel
|
|
208
|
-
this.Battery.BatteryLevel = this.serviceData.battery
|
|
209
|
-
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
|
|
212
|
+
this.Battery.BatteryLevel = this.serviceData.battery
|
|
213
|
+
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
|
|
210
214
|
// StatusLowBattery
|
|
211
215
|
this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
|
|
212
|
-
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
213
|
-
|
|
216
|
+
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
217
|
+
: this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
|
|
218
|
+
await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`)
|
|
214
219
|
}
|
|
215
220
|
|
|
216
221
|
async openAPIparseStatus(): Promise<void> {
|
|
217
|
-
await this.debugLog('openAPIparseStatus')
|
|
222
|
+
await this.debugLog('openAPIparseStatus')
|
|
218
223
|
// Contact State
|
|
219
224
|
this.ContactSensor.ContactSensorState = this.deviceStatus.openState === 'open'
|
|
220
|
-
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
|
|
221
|
-
|
|
225
|
+
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
|
|
226
|
+
: this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED
|
|
227
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
|
|
222
228
|
|
|
223
229
|
// MotionDetected
|
|
224
230
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
225
|
-
this.MotionSensor.MotionDetected = this.deviceStatus.moveDetected
|
|
226
|
-
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
231
|
+
this.MotionSensor.MotionDetected = this.deviceStatus.moveDetected
|
|
232
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
227
233
|
}
|
|
228
234
|
// Light Level
|
|
229
235
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
230
|
-
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
231
|
-
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
232
|
-
const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux
|
|
233
|
-
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
234
|
-
await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
236
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
237
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
238
|
+
const lightLevel = this.deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux
|
|
239
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
240
|
+
await this.debugLog(`LightLevel: ${this.deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
235
241
|
}
|
|
236
242
|
// BatteryLevel
|
|
237
|
-
this.Battery.BatteryLevel = this.deviceStatus.battery
|
|
238
|
-
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
|
|
243
|
+
this.Battery.BatteryLevel = this.deviceStatus.battery
|
|
244
|
+
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`)
|
|
239
245
|
// StatusLowBattery
|
|
240
246
|
this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
|
|
241
|
-
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
242
|
-
|
|
247
|
+
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW
|
|
248
|
+
: this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL
|
|
249
|
+
await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`)
|
|
243
250
|
// FirmwareVersion
|
|
244
251
|
if (this.deviceStatus.version) {
|
|
245
|
-
const version = this.deviceStatus.version.toString()
|
|
246
|
-
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`)
|
|
247
|
-
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
|
|
252
|
+
const version = this.deviceStatus.version.toString()
|
|
253
|
+
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`)
|
|
254
|
+
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0'
|
|
248
255
|
this.accessory
|
|
249
256
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
250
257
|
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
251
258
|
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
|
|
252
259
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
253
|
-
.updateValue(deviceVersion)
|
|
254
|
-
this.accessory.context.version = deviceVersion
|
|
255
|
-
await this.debugLog(`version: ${this.accessory.context.version}`)
|
|
260
|
+
.updateValue(deviceVersion)
|
|
261
|
+
this.accessory.context.version = deviceVersion
|
|
262
|
+
await this.debugLog(`version: ${this.accessory.context.version}`)
|
|
256
263
|
}
|
|
257
264
|
}
|
|
258
265
|
|
|
259
266
|
async parseStatusWebhook(): Promise<void> {
|
|
260
|
-
await this.debugLog('parseStatusWebhook')
|
|
261
|
-
await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${this.webhookContext.detectionState}, ${this.webhookContext.brightness}
|
|
262
|
-
+ ` ${this.webhookContext.openState}), current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel},`
|
|
263
|
-
+ ` ${this.ContactSensor.ContactSensorState})`);
|
|
267
|
+
await this.debugLog('parseStatusWebhook')
|
|
268
|
+
await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${this.webhookContext.detectionState}, ${this.webhookContext.brightness}, ${this.webhookContext.openState}), current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel}, ${this.ContactSensor.ContactSensorState})`)
|
|
264
269
|
// ContactSensorState
|
|
265
|
-
this.ContactSensor.ContactSensorState = this.webhookContext.openState === 'open' ? 1 : 0
|
|
266
|
-
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
|
|
270
|
+
this.ContactSensor.ContactSensorState = this.webhookContext.openState === 'open' ? 1 : 0
|
|
271
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`)
|
|
267
272
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
268
273
|
// MotionDetected
|
|
269
|
-
this.MotionSensor.MotionDetected = this.webhookContext.detectionState === 'DETECTED'
|
|
270
|
-
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
274
|
+
this.MotionSensor.MotionDetected = this.webhookContext.detectionState === 'DETECTED'
|
|
275
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`)
|
|
271
276
|
}
|
|
272
277
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
273
|
-
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
274
|
-
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
275
|
-
const lightLevel = this.webhookContext.brightness === 'bright' ? set_maxLux : set_minLux
|
|
276
|
-
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
277
|
-
await this.debugLog(`LightLevel: ${this.webhookContext.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
278
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1
|
|
279
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001
|
|
280
|
+
const lightLevel = this.webhookContext.brightness === 'bright' ? set_maxLux : set_minLux
|
|
281
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2)
|
|
282
|
+
await this.debugLog(`LightLevel: ${this.webhookContext.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`)
|
|
278
283
|
}
|
|
279
284
|
}
|
|
280
285
|
|
|
@@ -283,93 +288,97 @@ export class Contact extends deviceBase {
|
|
|
283
288
|
*/
|
|
284
289
|
async refreshStatus(): Promise<void> {
|
|
285
290
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
286
|
-
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`)
|
|
287
|
-
} else if (this.BLE
|
|
288
|
-
await this.BLERefreshStatus()
|
|
291
|
+
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`)
|
|
292
|
+
} else if (this.BLE) {
|
|
293
|
+
await this.BLERefreshStatus()
|
|
289
294
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
290
|
-
await this.openAPIRefreshStatus()
|
|
295
|
+
await this.openAPIRefreshStatus()
|
|
291
296
|
} else {
|
|
292
|
-
await this.offlineOff()
|
|
293
|
-
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`)
|
|
297
|
+
await this.offlineOff()
|
|
298
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`)
|
|
294
299
|
}
|
|
295
300
|
}
|
|
296
301
|
|
|
297
302
|
async BLERefreshStatus(): Promise<void> {
|
|
298
|
-
await this.debugLog('BLERefreshStatus')
|
|
303
|
+
await this.debugLog('BLERefreshStatus')
|
|
304
|
+
const switchbot = await this.switchbotBLE()
|
|
305
|
+
if (switchbot === undefined) {
|
|
306
|
+
await this.BLERefreshConnection(switchbot)
|
|
307
|
+
} else {
|
|
308
|
+
// Start to monitor advertisement packets
|
|
309
|
+
(async () => {
|
|
310
|
+
// Start to monitor advertisement packets
|
|
311
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData
|
|
312
|
+
// Update HomeKit
|
|
313
|
+
if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
|
|
314
|
+
this.serviceData = serviceData
|
|
315
|
+
await this.BLEparseStatus()
|
|
316
|
+
await this.updateHomeKitCharacteristics()
|
|
317
|
+
} else {
|
|
318
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`)
|
|
319
|
+
await this.BLERefreshConnection(switchbot)
|
|
320
|
+
}
|
|
321
|
+
})()
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
async registerPlatformBLE(): Promise<void> {
|
|
326
|
+
await this.debugLog('registerPlatformBLE')
|
|
299
327
|
if (this.config.options?.BLE) {
|
|
300
|
-
await this.debugLog('is listening to Platform BLE.')
|
|
301
|
-
this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
|
|
302
|
-
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
328
|
+
await this.debugLog('is listening to Platform BLE.')
|
|
329
|
+
this.device.bleMac = this.device.deviceId!.match(/.{1,2}/g)!.join(':').toLowerCase()
|
|
330
|
+
await this.debugLog(`bleMac: ${this.device.bleMac}`)
|
|
303
331
|
this.platform.bleEventHandler[this.device.bleMac] = async (context: contactSensorServiceData) => {
|
|
304
332
|
try {
|
|
305
|
-
await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
|
|
306
|
-
this.serviceData = context
|
|
307
|
-
await this.BLEparseStatus()
|
|
308
|
-
await this.updateHomeKitCharacteristics()
|
|
333
|
+
await this.debugLog(`received BLE: ${JSON.stringify(context)}`)
|
|
334
|
+
this.serviceData = context
|
|
335
|
+
await this.BLEparseStatus()
|
|
336
|
+
await this.updateHomeKitCharacteristics()
|
|
309
337
|
} catch (e: any) {
|
|
310
|
-
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
338
|
+
await this.errorLog(`failed to handle BLE. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
311
339
|
}
|
|
312
|
-
};
|
|
313
|
-
} else {
|
|
314
|
-
await this.debugLog('is using Device BLE Scanning.');
|
|
315
|
-
const switchbot = await this.switchbotBLE();
|
|
316
|
-
if (switchbot === undefined) {
|
|
317
|
-
await this.BLERefreshConnection(switchbot);
|
|
318
|
-
} else {
|
|
319
|
-
// Start to monitor advertisement packets
|
|
320
|
-
(async () => {
|
|
321
|
-
// Start to monitor advertisement packets
|
|
322
|
-
const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
|
|
323
|
-
// Update HomeKit
|
|
324
|
-
if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
|
|
325
|
-
this.serviceData = serviceData;
|
|
326
|
-
await this.BLEparseStatus();
|
|
327
|
-
await this.updateHomeKitCharacteristics();
|
|
328
|
-
} else {
|
|
329
|
-
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
|
|
330
|
-
await this.BLERefreshConnection(switchbot);
|
|
331
|
-
}
|
|
332
|
-
})();
|
|
333
340
|
}
|
|
341
|
+
} else {
|
|
342
|
+
await this.debugLog('is not listening to Platform BLE')
|
|
334
343
|
}
|
|
335
344
|
}
|
|
336
345
|
|
|
337
346
|
async openAPIRefreshStatus(): Promise<void> {
|
|
338
|
-
await this.debugLog('openAPIRefreshStatus')
|
|
347
|
+
await this.debugLog('openAPIRefreshStatus')
|
|
339
348
|
try {
|
|
340
|
-
const { body, statusCode } = await this.deviceRefreshStatus()
|
|
341
|
-
const deviceStatus: any = await body.json()
|
|
342
|
-
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
349
|
+
const { body, statusCode } = await this.deviceRefreshStatus()
|
|
350
|
+
const deviceStatus: any = await body.json()
|
|
351
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
343
352
|
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
344
|
-
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
345
|
-
this.deviceStatus = deviceStatus.body
|
|
346
|
-
await this.openAPIparseStatus()
|
|
347
|
-
await this.updateHomeKitCharacteristics()
|
|
353
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
354
|
+
this.deviceStatus = deviceStatus.body
|
|
355
|
+
await this.openAPIparseStatus()
|
|
356
|
+
await this.updateHomeKitCharacteristics()
|
|
348
357
|
} else {
|
|
349
|
-
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
350
|
-
await this.debugWarnLog(statusCode, deviceStatus)
|
|
358
|
+
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`)
|
|
359
|
+
await this.debugWarnLog(statusCode, deviceStatus)
|
|
351
360
|
}
|
|
352
361
|
} catch (e: any) {
|
|
353
|
-
await this.apiError(e)
|
|
354
|
-
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
|
|
362
|
+
await this.apiError(e)
|
|
363
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`)
|
|
355
364
|
}
|
|
356
365
|
}
|
|
357
366
|
|
|
358
367
|
async registerWebhook() {
|
|
359
368
|
if (this.device.webhook) {
|
|
360
|
-
await this.debugLog('is listening webhook.')
|
|
369
|
+
await this.debugLog('is listening webhook.')
|
|
361
370
|
this.platform.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
|
|
362
371
|
try {
|
|
363
|
-
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`)
|
|
364
|
-
this.webhookContext = context
|
|
365
|
-
await this.parseStatusWebhook()
|
|
366
|
-
await this.updateHomeKitCharacteristics()
|
|
372
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`)
|
|
373
|
+
this.webhookContext = context
|
|
374
|
+
await this.parseStatusWebhook()
|
|
375
|
+
await this.updateHomeKitCharacteristics()
|
|
367
376
|
} catch (e: any) {
|
|
368
|
-
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
377
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`)
|
|
369
378
|
}
|
|
370
|
-
}
|
|
379
|
+
}
|
|
371
380
|
} else {
|
|
372
|
-
await this.debugLog('is not listening webhook.')
|
|
381
|
+
await this.debugLog('is not listening webhook.')
|
|
373
382
|
}
|
|
374
383
|
}
|
|
375
384
|
|
|
@@ -378,59 +387,53 @@ export class Contact extends deviceBase {
|
|
|
378
387
|
*/
|
|
379
388
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
380
389
|
// ContactSensorState
|
|
381
|
-
await this.updateCharacteristic(this.ContactSensor.Service, this.hap.Characteristic.ContactSensorState,
|
|
382
|
-
this.ContactSensor.ContactSensorState, 'ContactSensorState');
|
|
390
|
+
await this.updateCharacteristic(this.ContactSensor.Service, this.hap.Characteristic.ContactSensorState, this.ContactSensor.ContactSensorState, 'ContactSensorState')
|
|
383
391
|
// MotionDetected
|
|
384
392
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
385
|
-
await this.updateCharacteristic(this.MotionSensor.Service, this.hap.Characteristic.MotionDetected,
|
|
386
|
-
this.MotionSensor.MotionDetected, 'MotionDetected');
|
|
393
|
+
await this.updateCharacteristic(this.MotionSensor.Service, this.hap.Characteristic.MotionDetected, this.MotionSensor.MotionDetected, 'MotionDetected')
|
|
387
394
|
}
|
|
388
395
|
// CurrentAmbientLightLevel
|
|
389
396
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
390
|
-
await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel,
|
|
391
|
-
this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel');
|
|
397
|
+
await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel')
|
|
392
398
|
}
|
|
393
399
|
// BatteryLevel
|
|
394
|
-
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
|
|
395
|
-
this.Battery.BatteryLevel, 'BatteryLevel');
|
|
400
|
+
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel, 'BatteryLevel')
|
|
396
401
|
// StatusLowBattery
|
|
397
|
-
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
|
|
398
|
-
this.Battery.StatusLowBattery, 'StatusLowBattery');
|
|
402
|
+
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery, 'StatusLowBattery')
|
|
399
403
|
}
|
|
400
404
|
|
|
401
405
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
402
|
-
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
|
|
406
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
|
|
403
407
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
404
|
-
await this.warnLog('Using OpenAPI Connection to Refresh Status')
|
|
405
|
-
await this.openAPIRefreshStatus()
|
|
408
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status')
|
|
409
|
+
await this.openAPIRefreshStatus()
|
|
406
410
|
}
|
|
407
411
|
}
|
|
408
412
|
|
|
409
413
|
async offlineOff(): Promise<void> {
|
|
410
414
|
if (this.device.offline) {
|
|
411
|
-
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState,
|
|
412
|
-
this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED);
|
|
415
|
+
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED)
|
|
413
416
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
414
|
-
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false)
|
|
417
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false)
|
|
415
418
|
}
|
|
416
419
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
417
|
-
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100)
|
|
420
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100)
|
|
418
421
|
}
|
|
419
422
|
}
|
|
420
423
|
}
|
|
421
424
|
|
|
422
425
|
async apiError(e: any): Promise<void> {
|
|
423
|
-
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e)
|
|
424
|
-
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
426
|
+
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e)
|
|
427
|
+
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
425
428
|
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
426
|
-
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e)
|
|
427
|
-
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
429
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e)
|
|
430
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
428
431
|
}
|
|
429
432
|
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
430
|
-
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e)
|
|
431
|
-
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
433
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e)
|
|
434
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e)
|
|
432
435
|
}
|
|
433
|
-
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
|
|
434
|
-
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e)
|
|
436
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e)
|
|
437
|
+
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e)
|
|
435
438
|
}
|
|
436
439
|
}
|