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