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