@switchbot/homebridge-switchbot 3.5.1-beta.0 → 3.5.1-beta.10
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/README.md +16 -0
- package/dist/device/blindtilt.d.ts +15 -6
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +224 -370
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +9 -4
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +197 -221
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +10 -5
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +214 -292
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +10 -5
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +335 -421
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +9 -4
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +121 -200
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +20 -7
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +229 -403
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +41 -22
- package/dist/device/device.d.ts.map +1 -1
- package/dist/device/device.js +212 -181
- package/dist/device/device.js.map +1 -1
- package/dist/device/fan.d.ts +9 -4
- package/dist/device/fan.d.ts.map +1 -1
- package/dist/device/fan.js +204 -249
- package/dist/device/fan.js.map +1 -1
- package/dist/device/hub.d.ts +9 -5
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +107 -233
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +12 -4
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +193 -235
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +9 -4
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +92 -159
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +10 -5
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +288 -370
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -5
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +172 -219
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +9 -4
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +94 -160
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +9 -4
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +95 -157
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +9 -4
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +85 -144
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -4
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +100 -127
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +9 -6
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +172 -259
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +12 -4
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +106 -131
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +2 -1
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +2 -3
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +2 -1
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +2 -3
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +2 -1
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +2 -3
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +2 -1
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +2 -3
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +3 -1
- package/dist/irdevice/irdevice.d.ts.map +1 -1
- package/dist/irdevice/irdevice.js +5 -1
- package/dist/irdevice/irdevice.js.map +1 -1
- package/dist/irdevice/light.d.ts +2 -1
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +2 -3
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +2 -1
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +2 -3
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +2 -1
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +2 -3
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +2 -1
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +2 -3
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +2 -1
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +2 -3
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +14 -14
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +102 -86
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +49 -190
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +19 -0
- package/dist/settings.js.map +1 -1
- package/dist/types/bledevicestatus.d.ts +279 -0
- package/dist/types/bledevicestatus.d.ts.map +1 -0
- package/dist/types/bledevicestatus.js +2 -0
- package/dist/types/bledevicestatus.js.map +1 -0
- package/dist/types/devicelist.d.ts +89 -0
- package/dist/types/devicelist.d.ts.map +1 -0
- package/dist/types/devicelist.js +6 -0
- package/dist/types/devicelist.js.map +1 -0
- package/dist/types/deviceresponse.d.ts +13 -0
- package/dist/types/deviceresponse.d.ts.map +1 -0
- package/dist/types/deviceresponse.js +2 -0
- package/dist/types/deviceresponse.js.map +1 -0
- package/dist/types/devicestatus.d.ts +149 -0
- package/dist/types/devicestatus.d.ts.map +1 -0
- package/dist/types/devicestatus.js +3 -0
- package/dist/types/devicestatus.js.map +1 -0
- package/dist/types/devicewebhookstatus.d.ts +156 -0
- package/dist/types/devicewebhookstatus.d.ts.map +1 -0
- package/dist/types/devicewebhookstatus.js +2 -0
- package/dist/types/devicewebhookstatus.js.map +1 -0
- package/dist/types/irdevicelist.d.ts +10 -0
- package/dist/types/irdevicelist.d.ts.map +1 -0
- package/dist/types/irdevicelist.js +2 -0
- package/dist/types/irdevicelist.js.map +1 -0
- package/dist/types/pushbody.d.ts +6 -0
- package/dist/types/pushbody.d.ts.map +1 -0
- package/dist/types/pushbody.js +2 -0
- package/dist/types/pushbody.js.map +1 -0
- package/dist/utils.d.ts +25 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +28 -0
- package/dist/utils.js.map +1 -1
- package/package.json +11 -11
- package/src/device/blindtilt.ts +254 -376
- package/src/device/bot.ts +209 -224
- package/src/device/ceilinglight.ts +227 -297
- package/src/device/colorbulb.ts +345 -435
- package/src/device/contact.ts +139 -200
- package/src/device/curtain.ts +254 -414
- package/src/device/device.ts +221 -190
- package/src/device/fan.ts +222 -253
- package/src/device/hub.ts +127 -248
- package/src/device/humidifier.ts +217 -246
- package/src/device/iosensor.ts +115 -168
- package/src/device/lightstrip.ts +303 -380
- package/src/device/lock.ts +188 -224
- package/src/device/meter.ts +117 -179
- package/src/device/meterplus.ts +118 -176
- package/src/device/motion.ts +100 -146
- package/src/device/plug.ts +111 -131
- package/src/device/robotvacuumcleaner.ts +191 -258
- package/src/device/waterdetector.ts +124 -141
- package/src/irdevice/airconditioner.ts +4 -4
- package/src/irdevice/airpurifier.ts +4 -4
- package/src/irdevice/camera.ts +4 -4
- package/src/irdevice/fan.ts +4 -4
- package/src/irdevice/irdevice.ts +8 -2
- package/src/irdevice/light.ts +4 -4
- package/src/irdevice/other.ts +4 -4
- package/src/irdevice/tv.ts +4 -4
- package/src/irdevice/vacuumcleaner.ts +4 -4
- package/src/irdevice/waterheater.ts +4 -4
- package/src/platform.ts +104 -87
- package/src/settings.ts +30 -238
- package/src/types/bledevicestatus.ts +305 -0
- package/src/types/devicelist.ts +124 -0
- package/src/types/deviceresponse.ts +15 -0
- package/src/types/devicestatus.ts +178 -0
- package/src/types/devicewebhookstatus.ts +196 -0
- package/src/types/irdevicelist.ts +15 -0
- package/src/types/pushbody.ts +9 -0
- package/src/utils.ts +33 -0
package/src/device/contact.ts
CHANGED
|
@@ -3,12 +3,16 @@
|
|
|
3
3
|
* contact.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import { deviceBase } from './device.js';
|
|
6
|
-
import { interval,
|
|
7
|
-
import {
|
|
6
|
+
import { Subject, interval, skipWhile } from 'rxjs';
|
|
7
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
|
|
8
8
|
|
|
9
|
+
import type { devicesConfig } from '../settings.js';
|
|
10
|
+
import type { device } from '../types/devicelist.js';
|
|
9
11
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
12
|
+
import type { contactSensorServiceData } from '../types/bledevicestatus.js';
|
|
10
13
|
import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
|
|
11
|
-
import type {
|
|
14
|
+
import type { contactSensorStatus } from '../types/devicestatus.js';
|
|
15
|
+
import type { contactSensorWebhookContext } from '../types/devicewebhookstatus.js';
|
|
12
16
|
|
|
13
17
|
/**
|
|
14
18
|
* Platform Accessory
|
|
@@ -158,123 +162,78 @@ export class Contact extends deviceBase {
|
|
|
158
162
|
// Retrieve initial values and updateHomekit
|
|
159
163
|
this.updateHomeKitCharacteristics();
|
|
160
164
|
|
|
165
|
+
//regisiter webhook event handler
|
|
166
|
+
this.registerWebhook();
|
|
167
|
+
|
|
161
168
|
// Start an update interval
|
|
162
169
|
interval(this.deviceRefreshRate * 1000)
|
|
163
170
|
.pipe(skipWhile(() => this.contactUpdateInProgress))
|
|
164
171
|
.subscribe(async () => {
|
|
165
172
|
await this.refreshStatus();
|
|
166
173
|
});
|
|
167
|
-
|
|
168
|
-
//regisiter webhook event handler
|
|
169
|
-
this.registerWebhook(accessory, device);
|
|
170
174
|
}
|
|
171
175
|
|
|
172
|
-
async BLEparseStatus(serviceData:
|
|
173
|
-
this.debugLog(
|
|
174
|
-
//
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
break;
|
|
184
|
-
default:
|
|
185
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} timeout no closed, doorstate: ${serviceData.doorState}`);
|
|
186
|
-
}
|
|
187
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
|
|
188
|
-
if (
|
|
189
|
-
this.ContactSensor.ContactSensorState !== this.accessory.context.ContactSensorState &&
|
|
190
|
-
this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED
|
|
191
|
-
) {
|
|
192
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Opened`);
|
|
193
|
-
}
|
|
194
|
-
// Movement
|
|
195
|
-
if (!this.device.contact?.hide_motionsensor) {
|
|
196
|
-
this.MotionSensor!.MotionDetected = Boolean(serviceData.movement);
|
|
197
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor!.MotionDetected}`);
|
|
198
|
-
if (this.MotionSensor!.MotionDetected !== this.accessory.context.MotionDetected && this.MotionSensor!.MotionDetected) {
|
|
199
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Detected Motion`);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
// Light Level
|
|
203
|
-
if (!this.device.contact?.hide_lightsensor) {
|
|
204
|
-
const set_minLux = this.device.contact?.set_minLux ?? 1;
|
|
205
|
-
const set_maxLux = this.device.contact?.set_maxLux ?? 6001;
|
|
206
|
-
switch (serviceData.lightLevel) {
|
|
207
|
-
case true:
|
|
208
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_minLux;
|
|
209
|
-
break;
|
|
210
|
-
default:
|
|
211
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
|
|
212
|
-
}
|
|
213
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} LightLevel: ${serviceData.lightLevel},`
|
|
214
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
215
|
-
if (this.LightSensor!.CurrentAmbientLightLevel !== this.accessory.context.CurrentAmbientLightLevel) {
|
|
216
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
217
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
218
|
-
}
|
|
176
|
+
async BLEparseStatus(serviceData: contactSensorServiceData): Promise<void> {
|
|
177
|
+
await this.debugLog('BLEparseStatus');
|
|
178
|
+
// ContactSensorState
|
|
179
|
+
this.ContactSensor.ContactSensorState = serviceData.doorState === 'open'
|
|
180
|
+
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
181
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
|
|
182
|
+
|
|
183
|
+
// MotionDetected
|
|
184
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
185
|
+
this.MotionSensor.MotionDetected = serviceData.movement;
|
|
186
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
|
|
219
187
|
}
|
|
220
|
-
//
|
|
221
|
-
this.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
this.
|
|
188
|
+
// CurrentAmbientLightLevel
|
|
189
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
190
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
191
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
192
|
+
const lightLevel = serviceData.lightLevel === 'bright' ? set_maxLux : set_minLux;
|
|
193
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
|
|
194
|
+
await this.debugLog(`LightLevel: ${serviceData.lightLevel}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
226
195
|
}
|
|
227
|
-
|
|
228
|
-
|
|
196
|
+
// BatteryLevel
|
|
197
|
+
this.Battery.BatteryLevel = serviceData.battery;
|
|
198
|
+
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
199
|
+
// StatusLowBattery
|
|
200
|
+
this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
|
|
201
|
+
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
202
|
+
await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
229
203
|
}
|
|
230
204
|
|
|
231
|
-
async openAPIparseStatus(deviceStatus:
|
|
232
|
-
this.debugLog(
|
|
205
|
+
async openAPIparseStatus(deviceStatus: contactSensorStatus): Promise<void> {
|
|
206
|
+
await this.debugLog('openAPIparseStatus');
|
|
233
207
|
// Contact State
|
|
234
|
-
|
|
235
|
-
this.
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this.debugLog(
|
|
242
|
-
}
|
|
243
|
-
// Motion State
|
|
244
|
-
if (!this.device.contact?.hide_motionsensor) {
|
|
245
|
-
this.MotionSensor!.MotionDetected = deviceStatus.body.moveDetected!;
|
|
246
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor!.MotionDetected}`);
|
|
208
|
+
this.ContactSensor.ContactSensorState = deviceStatus.openState === 'open'
|
|
209
|
+
? this.hap.Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED;
|
|
210
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
|
|
211
|
+
|
|
212
|
+
// MotionDetected
|
|
213
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
214
|
+
this.MotionSensor.MotionDetected = deviceStatus.moveDetected;
|
|
215
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
|
|
247
216
|
}
|
|
248
217
|
// Light Level
|
|
249
|
-
if (!this.device.contact?.hide_lightsensor) {
|
|
250
|
-
const set_minLux = this.device.
|
|
251
|
-
const set_maxLux = this.device.
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
break;
|
|
256
|
-
case 'bright':
|
|
257
|
-
default:
|
|
258
|
-
this.LightSensor!.CurrentAmbientLightLevel = set_maxLux;
|
|
259
|
-
}
|
|
260
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
261
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor!.CurrentAmbientLightLevel}`);
|
|
262
|
-
}
|
|
263
|
-
// Battery
|
|
264
|
-
this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
|
|
265
|
-
if (this.Battery.BatteryLevel < 10) {
|
|
266
|
-
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
267
|
-
} else {
|
|
268
|
-
this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
218
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
219
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
220
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
221
|
+
const lightLevel = deviceStatus.brightness === 'bright' ? set_maxLux : set_minLux;
|
|
222
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
|
|
223
|
+
await this.debugLog(`LightLevel: ${deviceStatus.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
269
224
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
//
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
225
|
+
// BatteryLevel
|
|
226
|
+
this.Battery.BatteryLevel = Number(deviceStatus.battery);
|
|
227
|
+
await this.debugLog(`BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
228
|
+
// StatusLowBattery
|
|
229
|
+
this.Battery.StatusLowBattery = this.Battery.BatteryLevel < 10
|
|
230
|
+
? this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
231
|
+
await this.debugLog(`StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
232
|
+
// FirmwareVersion
|
|
233
|
+
const version = deviceStatus.version.toString();
|
|
234
|
+
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
235
|
+
if (deviceStatus.version) {
|
|
236
|
+
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
278
237
|
this.accessory
|
|
279
238
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
280
239
|
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
@@ -282,7 +241,28 @@ export class Contact extends deviceBase {
|
|
|
282
241
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
283
242
|
.updateValue(deviceVersion);
|
|
284
243
|
this.accessory.context.deviceVersion = deviceVersion;
|
|
285
|
-
this.
|
|
244
|
+
await this.debugLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async parseStatusWebhook(context: contactSensorWebhookContext): Promise<void> {
|
|
249
|
+
await this.debugLog('parseStatusWebhook');
|
|
250
|
+
await this.debugLog(`(detectionState, brightness, openState) = Webhook:(${context.detectionState}, ${context.brightness}, ${context.openState}),`
|
|
251
|
+
+ ` current:(${this.MotionSensor?.MotionDetected}, ${this.LightSensor?.CurrentAmbientLightLevel}, ${this.ContactSensor.ContactSensorState})`);
|
|
252
|
+
// ContactSensorState
|
|
253
|
+
this.ContactSensor.ContactSensorState = context.openState === 'open' ? 1 : 0;
|
|
254
|
+
await this.debugLog(`ContactSensorState: ${this.ContactSensor.ContactSensorState}`);
|
|
255
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
256
|
+
// MotionDetected
|
|
257
|
+
this.MotionSensor.MotionDetected = context.detectionState === 'DETECTED' ? true : false;
|
|
258
|
+
await this.debugLog(`MotionDetected: ${this.MotionSensor.MotionDetected}`);
|
|
259
|
+
}
|
|
260
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
261
|
+
const set_minLux = this.device.blindTilt?.set_minLux ?? 1;
|
|
262
|
+
const set_maxLux = this.device.blindTilt?.set_maxLux ?? 6001;
|
|
263
|
+
const lightLevel = context.brightness === 'bright' ? set_maxLux : set_minLux;
|
|
264
|
+
this.LightSensor.CurrentAmbientLightLevel = await this.getLightLevel(lightLevel, set_minLux, set_maxLux, 2);
|
|
265
|
+
await this.debugLog(`LightLevel: ${context.brightness}, CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
286
266
|
}
|
|
287
267
|
}
|
|
288
268
|
|
|
@@ -291,20 +271,19 @@ export class Contact extends deviceBase {
|
|
|
291
271
|
*/
|
|
292
272
|
async refreshStatus(): Promise<void> {
|
|
293
273
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
294
|
-
this.errorLog(
|
|
274
|
+
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
295
275
|
} else if (this.BLE) {
|
|
296
276
|
await this.BLERefreshStatus();
|
|
297
|
-
} else if (this.OpenAPI) {
|
|
277
|
+
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
298
278
|
await this.openAPIRefreshStatus();
|
|
299
279
|
} else {
|
|
300
280
|
await this.offlineOff();
|
|
301
|
-
this.debugWarnLog(
|
|
302
|
-
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
281
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
303
282
|
}
|
|
304
283
|
}
|
|
305
284
|
|
|
306
285
|
async BLERefreshStatus(): Promise<void> {
|
|
307
|
-
this.debugLog(
|
|
286
|
+
await this.debugLog('BLERefreshStatus');
|
|
308
287
|
const switchbot = await this.switchbotBLE();
|
|
309
288
|
|
|
310
289
|
if (switchbot === undefined) {
|
|
@@ -313,13 +292,13 @@ export class Contact extends deviceBase {
|
|
|
313
292
|
// Start to monitor advertisement packets
|
|
314
293
|
(async () => {
|
|
315
294
|
// Start to monitor advertisement packets
|
|
316
|
-
const serviceData
|
|
295
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot) as contactSensorServiceData;
|
|
317
296
|
// Update HomeKit
|
|
318
|
-
if (serviceData.model
|
|
297
|
+
if (serviceData.model === SwitchBotBLEModel.ContactSensor && serviceData.modelName === SwitchBotBLEModelName.ContactSensor) {
|
|
319
298
|
await this.BLEparseStatus(serviceData);
|
|
320
299
|
await this.updateHomeKitCharacteristics();
|
|
321
300
|
} else {
|
|
322
|
-
this.errorLog(
|
|
301
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
|
|
323
302
|
await this.BLERefreshConnection(switchbot);
|
|
324
303
|
}
|
|
325
304
|
})();
|
|
@@ -327,53 +306,39 @@ export class Contact extends deviceBase {
|
|
|
327
306
|
}
|
|
328
307
|
|
|
329
308
|
async openAPIRefreshStatus(): Promise<void> {
|
|
330
|
-
this.debugLog(
|
|
309
|
+
await this.debugLog('openAPIRefreshStatus');
|
|
331
310
|
try {
|
|
332
311
|
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
333
312
|
const deviceStatus: any = await body.json();
|
|
334
|
-
await this.
|
|
335
|
-
if (
|
|
336
|
-
await this.
|
|
337
|
-
await this.openAPIparseStatus(deviceStatus);
|
|
313
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
|
|
314
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
315
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
316
|
+
await this.openAPIparseStatus(deviceStatus.body);
|
|
338
317
|
await this.updateHomeKitCharacteristics();
|
|
339
318
|
} else {
|
|
340
|
-
await this.
|
|
319
|
+
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
320
|
+
await this.debugWarnLog(statusCode, deviceStatus);
|
|
341
321
|
}
|
|
342
322
|
} catch (e: any) {
|
|
343
|
-
this.apiError(e);
|
|
344
|
-
this.errorLog(
|
|
345
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
323
|
+
await this.apiError(e);
|
|
324
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
346
325
|
}
|
|
347
326
|
}
|
|
348
327
|
|
|
349
|
-
async registerWebhook(
|
|
350
|
-
if (device.webhook) {
|
|
351
|
-
this.debugLog(
|
|
352
|
-
this.platform.webhookEventHandler[device.deviceId] = async (context) => {
|
|
328
|
+
async registerWebhook() {
|
|
329
|
+
if (this.device.webhook) {
|
|
330
|
+
await this.debugLog('is listening webhook.');
|
|
331
|
+
this.platform.webhookEventHandler[this.device.deviceId] = async (context: contactSensorWebhookContext) => {
|
|
353
332
|
try {
|
|
354
|
-
this.debugLog(
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
const { CurrentAmbientLightLevel } = this.LightSensor ?? {};
|
|
358
|
-
const { MotionDetected } = this.MotionSensor ?? {};
|
|
359
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} (detectionState, brightness, openState) = Webhook:(${detectionState}, `
|
|
360
|
-
+ `${brightness}, ${openState}), current:(${MotionDetected}, ${CurrentAmbientLightLevel}, ${ContactSensorState})`);
|
|
361
|
-
const set_minLux = this.device.contact?.set_minLux ?? 1;
|
|
362
|
-
const set_maxLux = this.device.contact?.set_maxLux ?? 6001;
|
|
363
|
-
this.ContactSensor.ContactSensorState = openState === 'open' ? 1 : 0;
|
|
364
|
-
if (!device.contact?.hide_motionsensor) {
|
|
365
|
-
this.MotionSensor!.MotionDetected = detectionState === 'DETECTED' ? true : false;
|
|
366
|
-
}
|
|
367
|
-
if (!device.contact?.hide_lightsensor) {
|
|
368
|
-
this.LightSensor!.CurrentAmbientLightLevel = brightness === 'bright' ? set_maxLux : set_minLux;
|
|
369
|
-
}
|
|
370
|
-
this.updateHomeKitCharacteristics();
|
|
333
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
|
|
334
|
+
await this.parseStatusWebhook(context);
|
|
335
|
+
await this.updateHomeKitCharacteristics();
|
|
371
336
|
} catch (e: any) {
|
|
372
|
-
this.errorLog(
|
|
337
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
373
338
|
}
|
|
374
339
|
};
|
|
375
340
|
} else {
|
|
376
|
-
this.debugLog(
|
|
341
|
+
await this.debugLog('is not listening webhook.');
|
|
377
342
|
}
|
|
378
343
|
}
|
|
379
344
|
|
|
@@ -381,57 +346,31 @@ export class Contact extends deviceBase {
|
|
|
381
346
|
* Updates the status for each of the HomeKit Characteristics
|
|
382
347
|
*/
|
|
383
348
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
this.
|
|
390
|
-
|
|
391
|
-
}
|
|
392
|
-
if (!this.device.contact?.hide_motionsensor) {
|
|
393
|
-
if (this.MotionSensor!.MotionDetected === undefined) {
|
|
394
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionSensor!.MotionDetected}`);
|
|
395
|
-
} else {
|
|
396
|
-
this.accessory.context.MotionDetected = this.MotionSensor!.MotionDetected;
|
|
397
|
-
this.MotionSensor!.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionSensor!.MotionDetected);
|
|
398
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
399
|
-
+ ` MotionDetected: ${this.MotionSensor!.MotionDetected}`);
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
if (!this.device.contact?.hide_lightsensor) {
|
|
403
|
-
if (this.LightSensor?.CurrentAmbientLightLevel === undefined) {
|
|
404
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
405
|
-
+ ` CurrentAmbientLightLevel: ${this.LightSensor?.CurrentAmbientLightLevel}`);
|
|
406
|
-
} else {
|
|
407
|
-
this.accessory.context.CurrentAmbientLightLevel = this.LightSensor.CurrentAmbientLightLevel;
|
|
408
|
-
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.LightSensor.CurrentAmbientLightLevel);
|
|
409
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
|
|
410
|
-
+ ` updateCharacteristic CurrentAmbientLightLevel: ${this.LightSensor.CurrentAmbientLightLevel}`);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
if (this.Battery.BatteryLevel === undefined) {
|
|
414
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
415
|
-
} else {
|
|
416
|
-
this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
|
|
417
|
-
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
|
|
418
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
|
|
349
|
+
// ContactSensorState
|
|
350
|
+
await this.updateCharacteristic(this.ContactSensor.Service, this.hap.Characteristic.ContactSensorState,
|
|
351
|
+
this.ContactSensor.ContactSensorState, 'ContactSensorState');
|
|
352
|
+
// MotionDetected
|
|
353
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
354
|
+
await this.updateCharacteristic(this.MotionSensor.Service, this.hap.Characteristic.MotionDetected,
|
|
355
|
+
this.MotionSensor.MotionDetected, 'MotionDetected');
|
|
419
356
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
|
|
425
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
|
|
426
|
-
+ ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
|
|
357
|
+
// CurrentAmbientLightLevel
|
|
358
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
359
|
+
await this.updateCharacteristic(this.LightSensor.Service, this.hap.Characteristic.CurrentAmbientLightLevel,
|
|
360
|
+
this.LightSensor.CurrentAmbientLightLevel, 'CurrentAmbientLightLevel');
|
|
427
361
|
}
|
|
362
|
+
// BatteryLevel
|
|
363
|
+
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.BatteryLevel,
|
|
364
|
+
this.Battery.BatteryLevel, 'BatteryLevel');
|
|
365
|
+
// StatusLowBattery
|
|
366
|
+
await this.updateCharacteristic(this.Battery.Service, this.hap.Characteristic.StatusLowBattery,
|
|
367
|
+
this.Battery.StatusLowBattery, 'StatusLowBattery');
|
|
428
368
|
}
|
|
429
369
|
|
|
430
370
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
431
|
-
this.errorLog(
|
|
432
|
-
+ ` ${JSON.stringify(switchbot)}`);
|
|
371
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
|
|
433
372
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
434
|
-
this.warnLog(
|
|
373
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status');
|
|
435
374
|
await this.openAPIRefreshStatus();
|
|
436
375
|
}
|
|
437
376
|
}
|
|
@@ -440,11 +379,11 @@ export class Contact extends deviceBase {
|
|
|
440
379
|
if (this.device.offline) {
|
|
441
380
|
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState,
|
|
442
381
|
this.hap.Characteristic.ContactSensorState.CONTACT_DETECTED);
|
|
443
|
-
if (!this.device.contact?.hide_motionsensor) {
|
|
444
|
-
this.MotionSensor
|
|
382
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
383
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, false);
|
|
445
384
|
}
|
|
446
|
-
if (!this.device.contact?.hide_lightsensor) {
|
|
447
|
-
this.LightSensor
|
|
385
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
386
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, 100);
|
|
448
387
|
}
|
|
449
388
|
}
|
|
450
389
|
}
|
|
@@ -452,13 +391,13 @@ export class Contact extends deviceBase {
|
|
|
452
391
|
async apiError(e: any): Promise<void> {
|
|
453
392
|
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.ContactSensorState, e);
|
|
454
393
|
this.ContactSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
|
|
455
|
-
if (!this.device.contact?.hide_motionsensor) {
|
|
456
|
-
this.MotionSensor
|
|
457
|
-
this.MotionSensor
|
|
394
|
+
if (!this.device.contact?.hide_motionsensor && this.MotionSensor?.Service) {
|
|
395
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
|
|
396
|
+
this.MotionSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
|
|
458
397
|
}
|
|
459
|
-
if (!this.device.contact?.hide_lightsensor) {
|
|
460
|
-
this.LightSensor
|
|
461
|
-
this.LightSensor
|
|
398
|
+
if (!this.device.contact?.hide_lightsensor && this.LightSensor?.Service) {
|
|
399
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
|
|
400
|
+
this.LightSensor.Service.updateCharacteristic(this.hap.Characteristic.StatusActive, e);
|
|
462
401
|
}
|
|
463
402
|
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
|
|
464
403
|
this.Battery.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
|