@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.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/.vscode/settings.json +4 -1
- package/CHANGELOG.md +4 -1
- package/config.schema.json +164 -22
- package/dist/device/blindtilt.d.ts +21 -96
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +457 -866
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +27 -72
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +851 -913
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +15 -69
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +316 -742
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +15 -76
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +380 -833
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +14 -66
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +267 -602
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +18 -89
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +469 -897
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/device.d.ts +76 -0
- package/dist/device/device.d.ts.map +1 -0
- package/dist/device/device.js +846 -0
- package/dist/device/device.js.map +1 -0
- package/dist/device/fan.d.ts +63 -0
- package/dist/device/fan.d.ts.map +1 -0
- package/dist/device/fan.js +645 -0
- package/dist/device/fan.js.map +1 -0
- package/dist/device/hub.d.ts +19 -58
- package/dist/device/hub.d.ts.map +1 -1
- package/dist/device/hub.js +304 -640
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +11 -66
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +300 -710
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +12 -64
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +225 -643
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +12 -70
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +371 -742
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +17 -63
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +309 -642
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +15 -64
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +223 -638
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +15 -65
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +216 -644
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +12 -69
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +209 -588
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +10 -57
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +141 -532
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +13 -63
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +252 -609
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/device/waterdetector.d.ts +11 -63
- package/dist/device/waterdetector.d.ts.map +1 -1
- package/dist/device/waterdetector.js +179 -521
- package/dist/device/waterdetector.js.map +1 -1
- package/dist/homebridge-ui/public/index.html +22 -16
- package/dist/homebridge-ui/server.js +1 -1
- package/dist/homebridge-ui/server.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +10 -50
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +337 -482
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +8 -39
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +108 -333
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +7 -36
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +47 -299
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +7 -43
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +95 -364
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/irdevice.d.ts +46 -0
- package/dist/irdevice/irdevice.d.ts.map +1 -0
- package/dist/irdevice/irdevice.js +393 -0
- package/dist/irdevice/irdevice.js.map +1 -0
- package/dist/irdevice/light.d.ts +12 -45
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +121 -376
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +19 -48
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +441 -584
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +8 -40
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +94 -353
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +7 -36
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +48 -299
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +7 -36
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +50 -305
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +22 -8
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +342 -191
- package/dist/platform.js.map +1 -1
- package/dist/settings.d.ts +66 -18
- package/dist/settings.d.ts.map +1 -1
- package/dist/settings.js +16 -552
- package/dist/settings.js.map +1 -1
- package/dist/utils.d.ts +95 -0
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +657 -1
- package/dist/utils.js.map +1 -1
- package/eslint.config.js +96 -0
- package/package.json +20 -16
- package/src/custom.d.ts +2 -1
- package/src/device/blindtilt.ts +488 -937
- package/src/device/bot.ts +907 -1016
- package/src/device/ceilinglight.ts +330 -798
- package/src/device/colorbulb.ts +394 -904
- package/src/device/contact.ts +298 -637
- package/src/device/curtain.ts +496 -954
- package/src/device/device.ts +872 -0
- package/src/device/fan.ts +692 -0
- package/src/device/hub.ts +338 -671
- package/src/device/humidifier.ts +330 -777
- package/src/device/iosensor.ts +258 -671
- package/src/device/lightstrip.ts +394 -809
- package/src/device/lock.ts +345 -700
- package/src/device/meter.ts +256 -666
- package/src/device/meterplus.ts +248 -672
- package/src/device/motion.ts +234 -622
- package/src/device/plug.ts +152 -572
- package/src/device/robotvacuumcleaner.ts +273 -653
- package/src/device/waterdetector.ts +198 -549
- package/src/homebridge-ui/public/index.html +22 -16
- package/src/homebridge-ui/server.ts +1 -1
- package/src/index.ts +4 -3
- package/src/irdevice/airconditioner.ts +368 -540
- package/src/irdevice/airpurifier.ts +134 -369
- package/src/irdevice/camera.ts +60 -324
- package/src/irdevice/fan.ts +111 -392
- package/src/irdevice/irdevice.ts +406 -0
- package/src/irdevice/light.ts +159 -425
- package/src/irdevice/other.ts +509 -661
- package/src/irdevice/tv.ts +123 -388
- package/src/irdevice/vacuumcleaner.ts +61 -324
- package/src/irdevice/waterheater.ts +63 -333
- package/src/platform.ts +395 -259
- package/src/settings.ts +120 -610
- package/src/utils.ts +661 -1
- package/src/device/waterdetector.json +0 -11
package/dist/device/plug.js
CHANGED
|
@@ -1,79 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
1
|
+
/* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
|
|
2
|
+
*
|
|
3
|
+
* plug.ts: @switchbot/homebridge-switchbot.
|
|
4
|
+
*/
|
|
5
|
+
import { deviceBase } from './device.js';
|
|
6
|
+
import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
|
|
7
|
+
export class Plug extends deviceBase {
|
|
7
8
|
platform;
|
|
8
|
-
accessory;
|
|
9
|
-
device;
|
|
10
|
-
api;
|
|
11
|
-
log;
|
|
12
|
-
config;
|
|
13
|
-
hap;
|
|
14
9
|
// Services
|
|
15
|
-
|
|
16
|
-
// Characteristic Values
|
|
17
|
-
On;
|
|
18
|
-
FirmwareRevision;
|
|
19
|
-
// OpenAPI Others
|
|
20
|
-
OpenAPI_On;
|
|
21
|
-
OpenAPI_FirmwareRevision;
|
|
22
|
-
// BLE Others
|
|
23
|
-
BLE_IsConnected;
|
|
24
|
-
BLE_On;
|
|
25
|
-
// Config
|
|
26
|
-
scanDuration;
|
|
27
|
-
deviceLogging;
|
|
28
|
-
deviceRefreshRate;
|
|
10
|
+
Outlet;
|
|
29
11
|
// Updates
|
|
30
12
|
plugUpdateInProgress;
|
|
31
13
|
doPlugUpdate;
|
|
32
|
-
// Connection
|
|
33
|
-
OpenAPI;
|
|
34
|
-
BLE;
|
|
35
14
|
constructor(platform, accessory, device) {
|
|
15
|
+
super(platform, accessory, device);
|
|
36
16
|
this.platform = platform;
|
|
37
|
-
this.accessory = accessory;
|
|
38
|
-
this.device = device;
|
|
39
|
-
this.api = this.platform.api;
|
|
40
|
-
this.log = this.platform.log;
|
|
41
|
-
this.config = this.platform.config;
|
|
42
|
-
this.hap = this.api.hap;
|
|
43
|
-
// Connection
|
|
44
|
-
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
45
|
-
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
46
|
-
// default placeholders
|
|
47
|
-
this.deviceLogs(device);
|
|
48
|
-
this.scan(device);
|
|
49
|
-
this.refreshRate(device);
|
|
50
|
-
this.deviceContext();
|
|
51
|
-
this.deviceConfig(device);
|
|
52
17
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
53
18
|
this.doPlugUpdate = new Subject();
|
|
54
19
|
this.plugUpdateInProgress = false;
|
|
20
|
+
// Initialize Outlet Service
|
|
21
|
+
accessory.context.Outlet = accessory.context.Outlet ?? {};
|
|
22
|
+
this.Outlet = {
|
|
23
|
+
Name: accessory.context.Outlet.Name ?? accessory.displayName,
|
|
24
|
+
Service: accessory.getService(this.hap.Service.Outlet) ?? accessory.addService(this.hap.Service.Outlet),
|
|
25
|
+
On: accessory.context.On || false,
|
|
26
|
+
};
|
|
27
|
+
accessory.context.Outlet = this.Outlet;
|
|
28
|
+
// Initialize Outlet Characteristics
|
|
29
|
+
this.Outlet.Service
|
|
30
|
+
.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName)
|
|
31
|
+
.getCharacteristic(this.hap.Characteristic.On)
|
|
32
|
+
.onGet(() => {
|
|
33
|
+
return this.Outlet.On;
|
|
34
|
+
})
|
|
35
|
+
.onSet(this.OnSet.bind(this));
|
|
55
36
|
// Retrieve initial values and updateHomekit
|
|
56
37
|
this.refreshStatus();
|
|
57
|
-
// set accessory information
|
|
58
|
-
accessory
|
|
59
|
-
.getService(this.hap.Service.AccessoryInformation)
|
|
60
|
-
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
|
|
61
|
-
.setCharacteristic(this.hap.Characteristic.Model, this.model(device))
|
|
62
|
-
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
63
|
-
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
|
|
64
|
-
// get the Outlet service if it exists, otherwise create a new Outlet service
|
|
65
|
-
// you can create multiple services for each accessory
|
|
66
|
-
const outletService = `${accessory.displayName} ${device.deviceType}`;
|
|
67
|
-
(this.outletService = accessory.getService(this.hap.Service.Outlet)
|
|
68
|
-
|| accessory.addService(this.hap.Service.Outlet)), outletService;
|
|
69
|
-
this.outletService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
|
|
70
|
-
if (!this.outletService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
71
|
-
this.outletService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
|
|
72
|
-
}
|
|
73
|
-
// each service must implement at-minimum the "required characteristics" for the given service type
|
|
74
|
-
// see https://developers.homebridge.io/#/service/Outlet
|
|
75
|
-
// create handlers for required characteristics
|
|
76
|
-
this.outletService.getCharacteristic(this.hap.Characteristic.On).onSet(this.OnSet.bind(this));
|
|
77
38
|
// Update Homekit
|
|
78
39
|
this.updateHomeKitCharacteristics();
|
|
79
40
|
// Start an update interval
|
|
@@ -83,87 +44,61 @@ export class Plug {
|
|
|
83
44
|
await this.refreshStatus();
|
|
84
45
|
});
|
|
85
46
|
//regisiter webhook event handler
|
|
86
|
-
|
|
87
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} is listening webhook.`);
|
|
88
|
-
this.platform.webhookEventHandler[this.device.deviceId] = async (context) => {
|
|
89
|
-
try {
|
|
90
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
91
|
-
const { powerState } = context;
|
|
92
|
-
const { On } = this;
|
|
93
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
94
|
-
'(powerState) = ' +
|
|
95
|
-
`Webhook:(${powerState}), ` +
|
|
96
|
-
`current:(${On})`);
|
|
97
|
-
this.On = powerState === 'ON' ? true : false;
|
|
98
|
-
this.updateHomeKitCharacteristics();
|
|
99
|
-
}
|
|
100
|
-
catch (e) {
|
|
101
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
102
|
-
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
}
|
|
47
|
+
this.registerWebhook(accessory, device);
|
|
106
48
|
// Watch for Plug change events
|
|
107
49
|
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
108
50
|
this.doPlugUpdate
|
|
109
51
|
.pipe(tap(() => {
|
|
110
52
|
this.plugUpdateInProgress = true;
|
|
111
|
-
}), debounceTime(this.
|
|
53
|
+
}), debounceTime(this.devicePushRate * 1000))
|
|
112
54
|
.subscribe(async () => {
|
|
113
55
|
try {
|
|
114
56
|
await this.pushChanges();
|
|
115
57
|
}
|
|
116
58
|
catch (e) {
|
|
117
59
|
this.apiError(e);
|
|
118
|
-
this.errorLog(`${
|
|
119
|
-
` Error Message: ${JSON.stringify(e.message)}`);
|
|
60
|
+
this.errorLog(`${device.deviceType}: ${this.accessory.displayName} failed pushChanges with ${device.connectionType} Connection,`
|
|
61
|
+
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
120
62
|
}
|
|
121
63
|
this.plugUpdateInProgress = false;
|
|
122
64
|
});
|
|
123
65
|
}
|
|
124
|
-
|
|
125
|
-
* Parse the device status from the SwitchBot api
|
|
126
|
-
*/
|
|
127
|
-
async parseStatus() {
|
|
128
|
-
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
129
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
130
|
-
}
|
|
131
|
-
else if (this.BLE) {
|
|
132
|
-
await this.BLEparseStatus();
|
|
133
|
-
}
|
|
134
|
-
else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
135
|
-
await this.openAPIparseStatus();
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
await this.offlineOff();
|
|
139
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async BLEparseStatus() {
|
|
66
|
+
async BLEparseStatus(serviceData) {
|
|
143
67
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
|
|
144
68
|
// State
|
|
145
|
-
switch (
|
|
69
|
+
switch (serviceData.state) {
|
|
146
70
|
case 'on':
|
|
147
|
-
this.On = true;
|
|
71
|
+
this.Outlet.On = true;
|
|
148
72
|
break;
|
|
149
73
|
default:
|
|
150
|
-
this.On = false;
|
|
74
|
+
this.Outlet.On = false;
|
|
151
75
|
}
|
|
152
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
76
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
153
77
|
}
|
|
154
|
-
async openAPIparseStatus() {
|
|
78
|
+
async openAPIparseStatus(deviceStatus) {
|
|
155
79
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
|
|
156
|
-
switch (
|
|
80
|
+
switch (deviceStatus.body.power) {
|
|
157
81
|
case 'on':
|
|
158
|
-
this.On = true;
|
|
82
|
+
this.Outlet.On = true;
|
|
159
83
|
break;
|
|
160
84
|
default:
|
|
161
|
-
this.On = false;
|
|
85
|
+
this.Outlet.On = false;
|
|
86
|
+
}
|
|
87
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
88
|
+
// Firmware Version
|
|
89
|
+
const version = deviceStatus.body.version?.toString();
|
|
90
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Firmware Version: ${version?.replace(/^V|-.*$/g, '')}`);
|
|
91
|
+
if (deviceStatus.body.version) {
|
|
92
|
+
const deviceVersion = version?.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
93
|
+
this.accessory
|
|
94
|
+
.getService(this.hap.Service.AccessoryInformation)
|
|
95
|
+
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
96
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, deviceVersion)
|
|
97
|
+
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
98
|
+
.updateValue(deviceVersion);
|
|
99
|
+
this.accessory.context.deviceVersion = deviceVersion;
|
|
100
|
+
this.debugSuccessLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
162
101
|
}
|
|
163
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
164
|
-
// FirmwareRevision
|
|
165
|
-
this.FirmwareRevision = this.OpenAPI_FirmwareRevision;
|
|
166
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
167
102
|
}
|
|
168
103
|
/**
|
|
169
104
|
* Asks the SwitchBot API for the latest device information
|
|
@@ -180,121 +115,74 @@ export class Plug {
|
|
|
180
115
|
}
|
|
181
116
|
else {
|
|
182
117
|
await this.offlineOff();
|
|
183
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
184
|
-
` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
118
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
119
|
+
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
185
120
|
}
|
|
186
121
|
}
|
|
187
122
|
async BLERefreshStatus() {
|
|
188
123
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLERefreshStatus`);
|
|
189
|
-
const switchbot = await this.
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
});
|
|
203
|
-
// Set an event handler
|
|
204
|
-
switchbot.onadvertisement = (ad) => {
|
|
205
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
|
|
206
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
|
|
207
|
-
if (this.device.bleMac === ad.address && ad.serviceData.model === this.BLEmodel()) {
|
|
208
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
209
|
-
this.BLE_On = ad.serviceData.state;
|
|
124
|
+
const switchbot = await this.switchbotBLE();
|
|
125
|
+
if (switchbot === undefined) {
|
|
126
|
+
await this.BLERefreshConnection(switchbot);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// Start to monitor advertisement packets
|
|
130
|
+
(async () => {
|
|
131
|
+
// Start to monitor advertisement packets
|
|
132
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot);
|
|
133
|
+
// Update HomeKit
|
|
134
|
+
if (serviceData.model !== '' && serviceData.modelName !== '') {
|
|
135
|
+
await this.BLEparseStatus(serviceData);
|
|
136
|
+
await this.updateHomeKitCharacteristics();
|
|
210
137
|
}
|
|
211
138
|
else {
|
|
212
|
-
this.
|
|
139
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed to get serviceData, serviceData: ${serviceData}`);
|
|
140
|
+
await this.BLERefreshConnection(switchbot);
|
|
213
141
|
}
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
await switchbot.wait(this.scanDuration * 1000);
|
|
217
|
-
// Stop to monitor
|
|
218
|
-
await switchbot.stopScan();
|
|
219
|
-
// Update HomeKit
|
|
220
|
-
await this.BLEparseStatus();
|
|
221
|
-
await this.updateHomeKitCharacteristics();
|
|
222
|
-
})();
|
|
223
|
-
/*if (switchbot !== false) {
|
|
224
|
-
switchbot
|
|
225
|
-
.startScan({
|
|
226
|
-
model: this.BLEmodel(),
|
|
227
|
-
id: this.device.bleMac,
|
|
228
|
-
})
|
|
229
|
-
.then(async () => {
|
|
230
|
-
// Set an event handler
|
|
231
|
-
switchbot.onadvertisement = async (ad: ad) => {
|
|
232
|
-
this.debugLog(
|
|
233
|
-
`${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
|
|
234
|
-
` BLE Address Found: ${ad.address}`,
|
|
235
|
-
);
|
|
236
|
-
this.BLE_On = ad.serviceData.state;
|
|
237
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
|
|
238
|
-
this.debugLog(
|
|
239
|
-
`${this.device.deviceType}: ${this.accessory.displayName} state: ${ad.serviceData.state}, ` +
|
|
240
|
-
`delay: ${ad.serviceData.delay}, timer: ${ad.serviceData.timer}, syncUtcTime: ${ad.serviceData.syncUtcTime} ` +
|
|
241
|
-
`wifiRssi: ${ad.serviceData.wifiRssi}, overload: ${ad.serviceData.overload}, currentPower: ${ad.serviceData.currentPower}`,
|
|
242
|
-
);
|
|
243
|
-
|
|
244
|
-
if (ad.serviceData) {
|
|
245
|
-
this.BLE_IsConnected = true;
|
|
246
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
247
|
-
await this.stopScanning(switchbot);
|
|
248
|
-
} else {
|
|
249
|
-
this.BLE_IsConnected = false;
|
|
250
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
// Wait
|
|
254
|
-
return await sleep(this.scanDuration * 1000);
|
|
255
|
-
})
|
|
256
|
-
.then(async () => {
|
|
257
|
-
// Stop to monitor
|
|
258
|
-
await this.stopScanning(switchbot);
|
|
259
|
-
})
|
|
260
|
-
.catch(async (e: any) => {
|
|
261
|
-
this.apiError(e);
|
|
262
|
-
this.errorLog(
|
|
263
|
-
`${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
|
|
264
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`,
|
|
265
|
-
);
|
|
266
|
-
await this.BLERefreshConnection(switchbot);
|
|
267
|
-
});
|
|
268
|
-
} else {
|
|
269
|
-
await this.BLERefreshConnection(switchbot);
|
|
270
|
-
}*/
|
|
142
|
+
})();
|
|
143
|
+
}
|
|
271
144
|
}
|
|
272
145
|
async openAPIRefreshStatus() {
|
|
273
146
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
|
|
274
147
|
try {
|
|
275
|
-
const { body, statusCode } = await
|
|
276
|
-
headers: this.platform.generateHeaders(),
|
|
277
|
-
});
|
|
278
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
148
|
+
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
279
149
|
const deviceStatus = await body.json();
|
|
280
|
-
this.
|
|
281
|
-
|
|
150
|
+
await this.refreshStatusCodes(statusCode, deviceStatus);
|
|
151
|
+
;
|
|
282
152
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
283
|
-
this.
|
|
284
|
-
|
|
285
|
-
this.
|
|
286
|
-
this.openAPIparseStatus();
|
|
287
|
-
this.updateHomeKitCharacteristics();
|
|
153
|
+
await this.successfulRefreshStatus(statusCode, deviceStatus);
|
|
154
|
+
await this.openAPIparseStatus(deviceStatus);
|
|
155
|
+
await this.updateHomeKitCharacteristics();
|
|
288
156
|
}
|
|
289
157
|
else {
|
|
290
|
-
this.
|
|
291
|
-
this.statusCode(deviceStatus.statusCode);
|
|
158
|
+
await this.statusCodes(statusCode, deviceStatus);
|
|
292
159
|
}
|
|
293
160
|
}
|
|
294
161
|
catch (e) {
|
|
295
162
|
this.apiError(e);
|
|
296
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
297
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
163
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIRefreshStatus with ${this.device.connectionType}`
|
|
164
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async registerWebhook(accessory, device) {
|
|
168
|
+
if (device.webhook) {
|
|
169
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is listening webhook.`);
|
|
170
|
+
this.platform.webhookEventHandler[device.deviceId] = async (context) => {
|
|
171
|
+
try {
|
|
172
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
|
|
173
|
+
const { powerState } = context;
|
|
174
|
+
const { On } = this.Outlet;
|
|
175
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState) = Webhook: (${powerState}), current:(${On})`);
|
|
176
|
+
this.Outlet.On = powerState === 'ON' ? true : false;
|
|
177
|
+
this.updateHomeKitCharacteristics();
|
|
178
|
+
}
|
|
179
|
+
catch (e) {
|
|
180
|
+
this.errorLog(`${device.deviceType}: ${accessory.displayName} failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
this.debugLog(`${device.deviceType}: ${accessory.displayName} is not listening webhook.`);
|
|
298
186
|
}
|
|
299
187
|
}
|
|
300
188
|
/**
|
|
@@ -318,7 +206,8 @@ export class Plug {
|
|
|
318
206
|
}
|
|
319
207
|
else {
|
|
320
208
|
await this.offlineOff();
|
|
321
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
209
|
+
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Connection Type:`
|
|
210
|
+
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
322
211
|
}
|
|
323
212
|
// Refresh the status from the API
|
|
324
213
|
interval(15000)
|
|
@@ -330,26 +219,23 @@ export class Plug {
|
|
|
330
219
|
}
|
|
331
220
|
async BLEpushChanges() {
|
|
332
221
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`);
|
|
333
|
-
if (this.On !== this.accessory.context.On) {
|
|
334
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges
|
|
222
|
+
if (this.Outlet.On !== this.accessory.context.On) {
|
|
223
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEpushChanges`
|
|
224
|
+
+ ` On: ${this.Outlet.On} OnCached: ${this.accessory.context.On}`);
|
|
335
225
|
const switchbot = await this.platform.connectBLE();
|
|
336
226
|
// Convert to BLE Address
|
|
337
|
-
|
|
338
|
-
.deviceId.match(/.{1,2}/g)
|
|
339
|
-
.join(':')
|
|
340
|
-
.toLowerCase();
|
|
341
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLE Address: ${this.device.bleMac}`);
|
|
227
|
+
await this.convertBLEAddress();
|
|
342
228
|
switchbot
|
|
343
229
|
.discover({
|
|
344
|
-
model: this.
|
|
230
|
+
model: this.device.bleModel,
|
|
345
231
|
id: this.device.bleMac,
|
|
346
232
|
})
|
|
347
233
|
.then(async (device_list) => {
|
|
348
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
349
|
-
return await this.
|
|
350
|
-
max: this.
|
|
234
|
+
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
235
|
+
return await this.retryBLE({
|
|
236
|
+
max: await this.maxRetryBLE(),
|
|
351
237
|
fn: async () => {
|
|
352
|
-
if (this.On) {
|
|
238
|
+
if (this.Outlet.On) {
|
|
353
239
|
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
354
240
|
}
|
|
355
241
|
else {
|
|
@@ -360,24 +246,27 @@ export class Plug {
|
|
|
360
246
|
})
|
|
361
247
|
.then(() => {
|
|
362
248
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Done.`);
|
|
363
|
-
this.
|
|
249
|
+
this.successLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
250
|
+
+ `On: ${this.Outlet.On} sent over BLE, sent successfully`);
|
|
251
|
+
this.Outlet.On = false;
|
|
364
252
|
})
|
|
365
253
|
.catch(async (e) => {
|
|
366
254
|
this.apiError(e);
|
|
367
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
368
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
255
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
256
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
369
257
|
await this.BLEPushConnection();
|
|
370
258
|
});
|
|
371
259
|
}
|
|
372
260
|
else {
|
|
373
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges
|
|
261
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No BLEpushChanges,`
|
|
262
|
+
+ ` On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
374
263
|
}
|
|
375
264
|
}
|
|
376
265
|
async openAPIpushChanges() {
|
|
377
266
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIpushChanges`);
|
|
378
|
-
if (this.On !== this.accessory.context.On) {
|
|
267
|
+
if (this.Outlet.On !== this.accessory.context.On) {
|
|
379
268
|
let command = '';
|
|
380
|
-
if (this.On) {
|
|
269
|
+
if (this.Outlet.On) {
|
|
381
270
|
command = 'turnOn';
|
|
382
271
|
}
|
|
383
272
|
else {
|
|
@@ -390,94 +279,51 @@ export class Plug {
|
|
|
390
279
|
});
|
|
391
280
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Sending request to SwitchBot API, body: ${bodyChange},`);
|
|
392
281
|
try {
|
|
393
|
-
const { body, statusCode } = await
|
|
394
|
-
body: bodyChange,
|
|
395
|
-
method: 'POST',
|
|
396
|
-
headers: this.platform.generateHeaders(),
|
|
397
|
-
});
|
|
398
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
|
|
282
|
+
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
399
283
|
const deviceStatus = await body.json();
|
|
400
|
-
this.
|
|
401
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus body: ${JSON.stringify(deviceStatus.body)}`);
|
|
402
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus statusCode: ${deviceStatus.statusCode}`);
|
|
284
|
+
await this.pushStatusCodes(statusCode, deviceStatus);
|
|
403
285
|
if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
|
|
404
|
-
this.
|
|
405
|
-
|
|
286
|
+
await this.successfulPushChange(statusCode, deviceStatus, bodyChange);
|
|
287
|
+
await this.updateHomeKitCharacteristics();
|
|
406
288
|
}
|
|
407
289
|
else {
|
|
408
|
-
this.statusCode(statusCode);
|
|
409
|
-
this.statusCode(deviceStatus.statusCode);
|
|
290
|
+
await this.statusCode(statusCode);
|
|
291
|
+
await this.statusCode(deviceStatus.statusCode);
|
|
410
292
|
}
|
|
411
293
|
}
|
|
412
294
|
catch (e) {
|
|
413
295
|
this.apiError(e);
|
|
414
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
415
|
-
` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
296
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed openAPIpushChanges with ${this.device.connectionType}`
|
|
297
|
+
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
416
298
|
}
|
|
417
299
|
}
|
|
418
300
|
else {
|
|
419
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
|
|
420
|
-
`On: ${this.On}, `
|
|
421
|
-
`OnCached: ${this.accessory.context.On}`);
|
|
301
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No openAPIpushChanges.`
|
|
302
|
+
+ `On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
422
303
|
}
|
|
423
304
|
}
|
|
424
305
|
/**
|
|
425
306
|
* Handle requests to set the value of the "On" characteristic
|
|
426
307
|
*/
|
|
427
308
|
async OnSet(value) {
|
|
428
|
-
if (this.On === this.accessory.context.On) {
|
|
309
|
+
if (this.Outlet.On === this.accessory.context.On) {
|
|
429
310
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} No Changes, Set On: ${value}`);
|
|
430
311
|
}
|
|
431
312
|
else {
|
|
432
313
|
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Set On: ${value}`);
|
|
433
314
|
}
|
|
434
|
-
this.On = value;
|
|
315
|
+
this.Outlet.On = value;
|
|
435
316
|
this.doPlugUpdate.next();
|
|
436
317
|
}
|
|
437
318
|
async updateHomeKitCharacteristics() {
|
|
438
319
|
// On
|
|
439
|
-
if (this.On === undefined) {
|
|
440
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.On}`);
|
|
320
|
+
if (this.Outlet.On === undefined) {
|
|
321
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
441
322
|
}
|
|
442
323
|
else {
|
|
443
|
-
this.accessory.context.On = this.On;
|
|
444
|
-
this.
|
|
445
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.On}`);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
async stopScanning(switchbot) {
|
|
449
|
-
switchbot.stopScan();
|
|
450
|
-
if (this.BLE_IsConnected) {
|
|
451
|
-
await this.BLEparseStatus();
|
|
452
|
-
await this.updateHomeKitCharacteristics();
|
|
453
|
-
}
|
|
454
|
-
else {
|
|
455
|
-
await this.BLERefreshConnection(switchbot);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
BLEmodel() {
|
|
459
|
-
if (this.device.deviceType === 'Plug Mini (US)' || this.device.configDeviceType === 'Plug Mini (US)') {
|
|
460
|
-
return 'g';
|
|
461
|
-
}
|
|
462
|
-
else {
|
|
463
|
-
return 'j';
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
async getCustomBLEAddress(switchbot) {
|
|
467
|
-
if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
|
|
468
|
-
(async () => {
|
|
469
|
-
// Start to monitor advertisement packets
|
|
470
|
-
await switchbot.startScan({
|
|
471
|
-
model: this.BLEmodel(),
|
|
472
|
-
});
|
|
473
|
-
// Set an event handler
|
|
474
|
-
switchbot.onadvertisement = (ad) => {
|
|
475
|
-
this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
|
|
476
|
-
};
|
|
477
|
-
await sleep(10000);
|
|
478
|
-
// Stop to monitor
|
|
479
|
-
switchbot.stopScan();
|
|
480
|
-
})();
|
|
324
|
+
this.accessory.context.On = this.Outlet.On;
|
|
325
|
+
this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, this.Outlet.On);
|
|
326
|
+
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Outlet.On}`);
|
|
481
327
|
}
|
|
482
328
|
}
|
|
483
329
|
async BLEPushConnection() {
|
|
@@ -494,250 +340,13 @@ export class Plug {
|
|
|
494
340
|
await this.openAPIRefreshStatus();
|
|
495
341
|
}
|
|
496
342
|
}
|
|
497
|
-
async retry({ max, fn }) {
|
|
498
|
-
return fn().catch(async (e) => {
|
|
499
|
-
if (max === 0) {
|
|
500
|
-
throw e;
|
|
501
|
-
}
|
|
502
|
-
this.infoLog(e);
|
|
503
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Retrying`);
|
|
504
|
-
await sleep(1000);
|
|
505
|
-
return this.retry({ max: max - 1, fn });
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
maxRetry() {
|
|
509
|
-
if (this.device.maxRetry) {
|
|
510
|
-
return this.device.maxRetry;
|
|
511
|
-
}
|
|
512
|
-
else {
|
|
513
|
-
return 5;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
model(device) {
|
|
517
|
-
let model;
|
|
518
|
-
if (device.deviceType === 'Plug Mini (US)') {
|
|
519
|
-
model = 'W1901400';
|
|
520
|
-
model = 'W1901401';
|
|
521
|
-
}
|
|
522
|
-
else if (device.deviceType === 'Plug Mini (JP)') {
|
|
523
|
-
model = 'W2001400';
|
|
524
|
-
}
|
|
525
|
-
else {
|
|
526
|
-
model = 'SP11';
|
|
527
|
-
}
|
|
528
|
-
return model;
|
|
529
|
-
}
|
|
530
|
-
async scan(device) {
|
|
531
|
-
if (device.scanDuration) {
|
|
532
|
-
this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
|
|
533
|
-
if (this.BLE) {
|
|
534
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
else {
|
|
538
|
-
this.scanDuration = this.accessory.context.scanDuration = 1;
|
|
539
|
-
if (this.BLE) {
|
|
540
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
|
|
541
|
-
}
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
async statusCode(statusCode) {
|
|
545
|
-
switch (statusCode) {
|
|
546
|
-
case 151:
|
|
547
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
|
|
548
|
-
break;
|
|
549
|
-
case 152:
|
|
550
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
|
|
551
|
-
break;
|
|
552
|
-
case 160:
|
|
553
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
|
|
554
|
-
break;
|
|
555
|
-
case 161:
|
|
556
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
|
|
557
|
-
this.offlineOff();
|
|
558
|
-
break;
|
|
559
|
-
case 171:
|
|
560
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
|
|
561
|
-
`Hub: ${this.device.hubDeviceId}`);
|
|
562
|
-
this.offlineOff();
|
|
563
|
-
break;
|
|
564
|
-
case 190:
|
|
565
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
|
|
566
|
-
` Or command format is invalid, statusCode: ${statusCode}`);
|
|
567
|
-
break;
|
|
568
|
-
case 100:
|
|
569
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
|
|
570
|
-
break;
|
|
571
|
-
case 200:
|
|
572
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
|
|
573
|
-
break;
|
|
574
|
-
case 400:
|
|
575
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
|
|
576
|
-
+ `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
|
|
577
|
-
break;
|
|
578
|
-
case 401:
|
|
579
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
|
|
580
|
-
+ `but the request has not been authenticated, statusCode: ${statusCode}`);
|
|
581
|
-
break;
|
|
582
|
-
case 403:
|
|
583
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
|
|
584
|
-
+ `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
|
|
585
|
-
break;
|
|
586
|
-
case 404:
|
|
587
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
|
|
588
|
-
+ `statusCode: ${statusCode}`);
|
|
589
|
-
break;
|
|
590
|
-
case 406:
|
|
591
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
|
|
592
|
-
+ `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
|
|
593
|
-
break;
|
|
594
|
-
case 415:
|
|
595
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
|
|
596
|
-
+ `header that is not supported by the server, statusCode: ${statusCode}`);
|
|
597
|
-
break;
|
|
598
|
-
case 422:
|
|
599
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
|
|
600
|
-
+ `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
|
|
601
|
-
break;
|
|
602
|
-
case 429:
|
|
603
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
|
|
604
|
-
+ `requests allowed for a given time window, statusCode: ${statusCode}`);
|
|
605
|
-
break;
|
|
606
|
-
case 500:
|
|
607
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
|
|
608
|
-
+ `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
|
|
609
|
-
break;
|
|
610
|
-
default:
|
|
611
|
-
this.infoLog(`${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
|
|
612
|
-
`${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
343
|
async offlineOff() {
|
|
616
344
|
if (this.device.offline) {
|
|
617
|
-
|
|
618
|
-
await this.updateHomeKitCharacteristics();
|
|
345
|
+
this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, false);
|
|
619
346
|
}
|
|
620
347
|
}
|
|
621
348
|
async apiError(e) {
|
|
622
|
-
this.
|
|
623
|
-
}
|
|
624
|
-
async deviceContext() {
|
|
625
|
-
if (this.On === undefined) {
|
|
626
|
-
this.On = false;
|
|
627
|
-
}
|
|
628
|
-
else {
|
|
629
|
-
this.On = this.accessory.context.On;
|
|
630
|
-
}
|
|
631
|
-
if (this.FirmwareRevision === undefined) {
|
|
632
|
-
this.FirmwareRevision = this.platform.version;
|
|
633
|
-
this.accessory.context.FirmwareRevision = this.FirmwareRevision;
|
|
634
|
-
}
|
|
635
|
-
}
|
|
636
|
-
async refreshRate(device) {
|
|
637
|
-
if (device.refreshRate) {
|
|
638
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
|
|
639
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
|
|
640
|
-
}
|
|
641
|
-
else if (this.platform.config.options.refreshRate) {
|
|
642
|
-
this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options.refreshRate;
|
|
643
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
async deviceConfig(device) {
|
|
647
|
-
let config = {};
|
|
648
|
-
if (device.plug) {
|
|
649
|
-
config = device.plug;
|
|
650
|
-
}
|
|
651
|
-
if (device.connectionType !== undefined) {
|
|
652
|
-
config['connectionType'] = device.connectionType;
|
|
653
|
-
}
|
|
654
|
-
if (device.external !== undefined) {
|
|
655
|
-
config['external'] = device.external;
|
|
656
|
-
}
|
|
657
|
-
if (device.logging !== undefined) {
|
|
658
|
-
config['logging'] = device.logging;
|
|
659
|
-
}
|
|
660
|
-
if (device.refreshRate !== undefined) {
|
|
661
|
-
config['refreshRate'] = device.refreshRate;
|
|
662
|
-
}
|
|
663
|
-
if (device.scanDuration !== undefined) {
|
|
664
|
-
config['scanDuration'] = device.scanDuration;
|
|
665
|
-
}
|
|
666
|
-
if (device.offline !== undefined) {
|
|
667
|
-
config['offline'] = device.offline;
|
|
668
|
-
}
|
|
669
|
-
if (device.maxRetry !== undefined) {
|
|
670
|
-
config['maxRetry'] = device.maxRetry;
|
|
671
|
-
}
|
|
672
|
-
if (device.webhook !== undefined) {
|
|
673
|
-
config['webhook'] = device.webhook;
|
|
674
|
-
}
|
|
675
|
-
if (Object.entries(config).length !== 0) {
|
|
676
|
-
this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
async deviceLogs(device) {
|
|
680
|
-
if (this.platform.debugMode) {
|
|
681
|
-
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
682
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
|
683
|
-
}
|
|
684
|
-
else if (device.logging) {
|
|
685
|
-
this.deviceLogging = this.accessory.context.logging = device.logging;
|
|
686
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
|
|
687
|
-
}
|
|
688
|
-
else if (this.platform.config.options?.logging) {
|
|
689
|
-
this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
|
|
690
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
|
|
691
|
-
}
|
|
692
|
-
else {
|
|
693
|
-
this.deviceLogging = this.accessory.context.logging = 'standard';
|
|
694
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
/**
|
|
698
|
-
* Logging for Device
|
|
699
|
-
*/
|
|
700
|
-
infoLog(...log) {
|
|
701
|
-
if (this.enablingDeviceLogging()) {
|
|
702
|
-
this.platform.log.info(String(...log));
|
|
703
|
-
}
|
|
704
|
-
}
|
|
705
|
-
warnLog(...log) {
|
|
706
|
-
if (this.enablingDeviceLogging()) {
|
|
707
|
-
this.platform.log.warn(String(...log));
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
debugWarnLog(...log) {
|
|
711
|
-
if (this.enablingDeviceLogging()) {
|
|
712
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
713
|
-
this.platform.log.warn('[DEBUG]', String(...log));
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
errorLog(...log) {
|
|
718
|
-
if (this.enablingDeviceLogging()) {
|
|
719
|
-
this.platform.log.error(String(...log));
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
debugErrorLog(...log) {
|
|
723
|
-
if (this.enablingDeviceLogging()) {
|
|
724
|
-
if (this.deviceLogging?.includes('debug')) {
|
|
725
|
-
this.platform.log.error('[DEBUG]', String(...log));
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
debugLog(...log) {
|
|
730
|
-
if (this.enablingDeviceLogging()) {
|
|
731
|
-
if (this.deviceLogging === 'debug') {
|
|
732
|
-
this.platform.log.info('[DEBUG]', String(...log));
|
|
733
|
-
}
|
|
734
|
-
else {
|
|
735
|
-
this.platform.log.debug(String(...log));
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
enablingDeviceLogging() {
|
|
740
|
-
return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
|
|
349
|
+
this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, e);
|
|
741
350
|
}
|
|
742
351
|
}
|
|
743
352
|
//# sourceMappingURL=plug.js.map
|