@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/plug.ts
CHANGED
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
* plug.ts: @switchbot/homebridge-switchbot.
|
|
4
4
|
*/
|
|
5
5
|
import { deviceBase } from './device.js';
|
|
6
|
+
import { SwitchBotBLEModel, SwitchBotBLEModelName } from 'node-switchbot';
|
|
6
7
|
import { Subject, debounceTime, interval, skipWhile, take, tap } from 'rxjs';
|
|
7
8
|
|
|
9
|
+
import type { devicesConfig } from '../settings.js';
|
|
10
|
+
import type { device } from '../types/devicelist.js';
|
|
8
11
|
import type { SwitchBotPlatform } from '../platform.js';
|
|
9
12
|
import type { CharacteristicValue, PlatformAccessory, Service } from 'homebridge';
|
|
10
|
-
import type {
|
|
13
|
+
import type { plugStatus, plugMiniStatus } from '../types/devicestatus.js';
|
|
14
|
+
import type { plugMiniUSServiceData, plugMiniJPServiceData } from '../types/bledevicestatus.js';
|
|
15
|
+
import type { plugMiniJPWebhookContext, plugMiniUSWebhookContext, plugWebhookContext } from '../types/devicewebhookstatus.js';
|
|
11
16
|
export class Plug extends deviceBase {
|
|
12
17
|
// Services
|
|
13
18
|
private Outlet: {
|
|
@@ -54,6 +59,9 @@ export class Plug extends deviceBase {
|
|
|
54
59
|
// Update Homekit
|
|
55
60
|
this.updateHomeKitCharacteristics();
|
|
56
61
|
|
|
62
|
+
//regisiter webhook event handler
|
|
63
|
+
this.registerWebhook();
|
|
64
|
+
|
|
57
65
|
// Start an update interval
|
|
58
66
|
interval(this.deviceRefreshRate * 1000)
|
|
59
67
|
.pipe(skipWhile(() => this.plugUpdateInProgress))
|
|
@@ -61,9 +69,6 @@ export class Plug extends deviceBase {
|
|
|
61
69
|
await this.refreshStatus();
|
|
62
70
|
});
|
|
63
71
|
|
|
64
|
-
//regisiter webhook event handler
|
|
65
|
-
this.registerWebhook(accessory, device);
|
|
66
|
-
|
|
67
72
|
// Watch for Plug change events
|
|
68
73
|
// We put in a debounce of 100ms so we don't make duplicate calls
|
|
69
74
|
this.doPlugUpdate
|
|
@@ -77,43 +82,30 @@ export class Plug extends deviceBase {
|
|
|
77
82
|
try {
|
|
78
83
|
await this.pushChanges();
|
|
79
84
|
} catch (e: any) {
|
|
80
|
-
this.apiError(e);
|
|
81
|
-
this.errorLog(
|
|
82
|
-
+ ` Error Message: ${JSON.stringify(e.message)}`);
|
|
85
|
+
await this.apiError(e);
|
|
86
|
+
await this.errorLog(`failed pushChanges with ${device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
83
87
|
}
|
|
84
88
|
this.plugUpdateInProgress = false;
|
|
85
89
|
});
|
|
86
90
|
}
|
|
87
91
|
|
|
88
|
-
async BLEparseStatus(serviceData:
|
|
89
|
-
this.debugLog(
|
|
90
|
-
//
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.Outlet.On = true;
|
|
94
|
-
break;
|
|
95
|
-
default:
|
|
96
|
-
this.Outlet.On = false;
|
|
97
|
-
}
|
|
98
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
92
|
+
async BLEparseStatus(serviceData: plugMiniUSServiceData | plugMiniJPServiceData): Promise<void> {
|
|
93
|
+
await this.debugLog('BLEparseStatus');
|
|
94
|
+
// On
|
|
95
|
+
this.Outlet.On = serviceData.state === 'on' ? true : false;
|
|
96
|
+
await this.debugLog(`On: ${this.Outlet.On}`);
|
|
99
97
|
}
|
|
100
98
|
|
|
101
|
-
async openAPIparseStatus(deviceStatus:
|
|
102
|
-
this.debugLog(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
break;
|
|
107
|
-
default:
|
|
108
|
-
this.Outlet.On = false;
|
|
109
|
-
}
|
|
110
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} On: ${this.Outlet.On}`);
|
|
111
|
-
|
|
99
|
+
async openAPIparseStatus(deviceStatus: plugStatus | plugMiniStatus) {
|
|
100
|
+
await this.debugLog('openAPIparseStatus');
|
|
101
|
+
// On
|
|
102
|
+
this.Outlet.On = deviceStatus.power === 'on' ? true : false;
|
|
103
|
+
await this.debugLog(`On: ${this.Outlet.On}`);
|
|
112
104
|
// Firmware Version
|
|
113
|
-
const version = deviceStatus.
|
|
114
|
-
this.debugLog(
|
|
115
|
-
if (deviceStatus.
|
|
116
|
-
const deviceVersion = version
|
|
105
|
+
const version = deviceStatus.version.toString();
|
|
106
|
+
await this.debugLog(`Firmware Version: ${version.replace(/^V|-.*$/g, '')}`);
|
|
107
|
+
if (deviceStatus.version) {
|
|
108
|
+
const deviceVersion = version.replace(/^V|-.*$/g, '') ?? '0.0.0';
|
|
117
109
|
this.accessory
|
|
118
110
|
.getService(this.hap.Service.AccessoryInformation)!
|
|
119
111
|
.setCharacteristic(this.hap.Characteristic.HardwareRevision, deviceVersion)
|
|
@@ -121,29 +113,36 @@ export class Plug extends deviceBase {
|
|
|
121
113
|
.getCharacteristic(this.hap.Characteristic.FirmwareRevision)
|
|
122
114
|
.updateValue(deviceVersion);
|
|
123
115
|
this.accessory.context.deviceVersion = deviceVersion;
|
|
124
|
-
this.debugSuccessLog(
|
|
116
|
+
await this.debugSuccessLog(`deviceVersion: ${this.accessory.context.deviceVersion}`);
|
|
125
117
|
}
|
|
126
118
|
}
|
|
127
119
|
|
|
120
|
+
async parseStatusWebhook(context: plugWebhookContext | plugMiniUSWebhookContext | plugMiniJPWebhookContext): Promise<void> {
|
|
121
|
+
await this.debugLog('parseStatusWebhook');
|
|
122
|
+
await this.debugLog(`(powerState) = Webhook: (${context.powerState}), current:(${this.Outlet.On})`);
|
|
123
|
+
// On
|
|
124
|
+
this.Outlet.On = context.powerState === 'ON' ? true : false;
|
|
125
|
+
await this.debugLog(`On: ${this.Outlet.On}`);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
128
|
/**
|
|
129
129
|
* Asks the SwitchBot API for the latest device information
|
|
130
130
|
*/
|
|
131
131
|
async refreshStatus(): Promise<void> {
|
|
132
132
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
133
|
-
this.errorLog(
|
|
133
|
+
await this.errorLog(`refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
134
134
|
} else if (this.BLE) {
|
|
135
135
|
await this.BLERefreshStatus();
|
|
136
136
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
137
137
|
await this.openAPIRefreshStatus();
|
|
138
138
|
} else {
|
|
139
139
|
await this.offlineOff();
|
|
140
|
-
this.debugWarnLog(
|
|
141
|
-
+ ` ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
140
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, refreshStatus will not happen.`);
|
|
142
141
|
}
|
|
143
142
|
}
|
|
144
143
|
|
|
145
144
|
async BLERefreshStatus(): Promise<void> {
|
|
146
|
-
this.debugLog(
|
|
145
|
+
await this.debugLog('BLERefreshStatus');
|
|
147
146
|
const switchbot = await this.switchbotBLE();
|
|
148
147
|
|
|
149
148
|
if (switchbot === undefined) {
|
|
@@ -152,13 +151,14 @@ export class Plug extends deviceBase {
|
|
|
152
151
|
// Start to monitor advertisement packets
|
|
153
152
|
(async () => {
|
|
154
153
|
// Start to monitor advertisement packets
|
|
155
|
-
const serviceData
|
|
154
|
+
const serviceData = await this.monitorAdvertisementPackets(switchbot) as plugMiniUSServiceData | plugMiniJPServiceData;
|
|
156
155
|
// Update HomeKit
|
|
157
|
-
if (serviceData.model
|
|
156
|
+
if ((serviceData.model === SwitchBotBLEModel.PlugMiniUS || SwitchBotBLEModel.PlugMiniJP)
|
|
157
|
+
&& serviceData.modelName === (SwitchBotBLEModelName.PlugMini || SwitchBotBLEModelName.PlugMini)) {
|
|
158
158
|
await this.BLEparseStatus(serviceData);
|
|
159
159
|
await this.updateHomeKitCharacteristics();
|
|
160
160
|
} else {
|
|
161
|
-
this.errorLog(
|
|
161
|
+
await this.errorLog(`failed to get serviceData, serviceData: ${serviceData}`);
|
|
162
162
|
await this.BLERefreshConnection(switchbot);
|
|
163
163
|
}
|
|
164
164
|
})();
|
|
@@ -166,42 +166,40 @@ export class Plug extends deviceBase {
|
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
async openAPIRefreshStatus(): Promise<void> {
|
|
169
|
-
this.debugLog(
|
|
169
|
+
await this.debugLog('openAPIRefreshStatus');
|
|
170
170
|
try {
|
|
171
171
|
const { body, statusCode } = await this.deviceRefreshStatus();
|
|
172
172
|
const deviceStatus: any = await body.json();
|
|
173
|
-
await this.
|
|
174
|
-
if (
|
|
175
|
-
await this.
|
|
176
|
-
await this.openAPIparseStatus(deviceStatus);
|
|
173
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);;
|
|
174
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
175
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
176
|
+
await this.openAPIparseStatus(deviceStatus.body);
|
|
177
177
|
await this.updateHomeKitCharacteristics();
|
|
178
178
|
} else {
|
|
179
|
-
await this.
|
|
179
|
+
await this.debugWarnLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
180
|
+
await this.debugWarnLog(statusCode, deviceStatus);
|
|
180
181
|
}
|
|
181
182
|
} catch (e: any) {
|
|
182
|
-
this.apiError(e);
|
|
183
|
-
this.errorLog(
|
|
184
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
183
|
+
await this.apiError(e);
|
|
184
|
+
await this.errorLog(`failed openAPIRefreshStatus with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
|
|
188
|
-
async registerWebhook(
|
|
189
|
-
if (device.webhook) {
|
|
190
|
-
this.debugLog(
|
|
191
|
-
this.platform.webhookEventHandler[device.deviceId] = async (context
|
|
188
|
+
async registerWebhook() {
|
|
189
|
+
if (this.device.webhook) {
|
|
190
|
+
await this.debugLog('is listening webhook.');
|
|
191
|
+
this.platform.webhookEventHandler[this.device.deviceId] = async (context: plugWebhookContext | plugMiniUSWebhookContext
|
|
192
|
+
| plugMiniJPWebhookContext) => {
|
|
192
193
|
try {
|
|
193
|
-
this.debugLog(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
this.debugLog(`${device.deviceType}: ${accessory.displayName} (powerState) = Webhook: (${powerState}), current:(${On})`);
|
|
197
|
-
this.Outlet.On = powerState === 'ON' ? true : false;
|
|
198
|
-
this.updateHomeKitCharacteristics();
|
|
194
|
+
await this.debugLog(`received Webhook: ${JSON.stringify(context)}`);
|
|
195
|
+
await this.parseStatusWebhook(context);
|
|
196
|
+
await this.updateHomeKitCharacteristics();
|
|
199
197
|
} catch (e: any) {
|
|
200
|
-
this.errorLog(
|
|
198
|
+
await this.errorLog(`failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
201
199
|
}
|
|
202
200
|
};
|
|
203
201
|
} else {
|
|
204
|
-
this.debugLog(
|
|
202
|
+
await this.debugLog('is not listening webhook.');
|
|
205
203
|
}
|
|
206
204
|
}
|
|
207
205
|
|
|
@@ -217,15 +215,14 @@ export class Plug extends deviceBase {
|
|
|
217
215
|
|
|
218
216
|
async pushChanges(): Promise<void> {
|
|
219
217
|
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
220
|
-
this.errorLog(
|
|
218
|
+
await this.errorLog(`pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
221
219
|
} else if (this.BLE) {
|
|
222
220
|
await this.BLEpushChanges();
|
|
223
221
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
224
222
|
await this.openAPIpushChanges();
|
|
225
223
|
} else {
|
|
226
224
|
await this.offlineOff();
|
|
227
|
-
this.debugWarnLog(
|
|
228
|
-
+ ` ${this.device.connectionType}, pushChanges will not happen.`);
|
|
225
|
+
await this.debugWarnLog(`Connection Type: ${this.device.connectionType}, pushChanges will not happen.`);
|
|
229
226
|
}
|
|
230
227
|
// Refresh the status from the API
|
|
231
228
|
interval(15000)
|
|
@@ -237,83 +234,72 @@ export class Plug extends deviceBase {
|
|
|
237
234
|
}
|
|
238
235
|
|
|
239
236
|
async BLEpushChanges(): Promise<void> {
|
|
240
|
-
this.debugLog(
|
|
237
|
+
await this.debugLog('BLEpushChanges');
|
|
241
238
|
if (this.Outlet.On !== this.accessory.context.On) {
|
|
242
|
-
this.debugLog(
|
|
243
|
-
|
|
244
|
-
const switchbot = await this.platform.connectBLE();
|
|
245
|
-
// Convert to BLE Address
|
|
239
|
+
await this.debugLog(`BLEpushChanges On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
240
|
+
const switchbot = await this.platform.connectBLE(this.accessory, this.device);
|
|
246
241
|
await this.convertBLEAddress();
|
|
247
|
-
switchbot
|
|
248
|
-
|
|
249
|
-
model: this.device.bleModel,
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
242
|
+
if (switchbot !== false) {
|
|
243
|
+
switchbot
|
|
244
|
+
.discover({ model: this.device.bleModel, id: this.device.bleMac })
|
|
245
|
+
.then(async (device_list: any) => {
|
|
246
|
+
await this.infoLog(`On: ${this.Outlet.On}`);
|
|
247
|
+
return await this.retryBLE({
|
|
248
|
+
max: await this.maxRetryBLE(),
|
|
249
|
+
fn: async () => {
|
|
250
|
+
if (this.Outlet.On) {
|
|
251
|
+
return await device_list[0].turnOn({ id: this.device.bleMac });
|
|
252
|
+
} else {
|
|
253
|
+
return await device_list[0].turnOff({ id: this.device.bleMac });
|
|
254
|
+
}
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
})
|
|
258
|
+
.then(async () => {
|
|
259
|
+
await this.successLog(`On: ${this.Outlet.On} sent over SwitchBot BLE, sent successfully`);
|
|
260
|
+
await this.updateHomeKitCharacteristics();
|
|
261
|
+
})
|
|
262
|
+
.catch(async (e: any) => {
|
|
263
|
+
await this.apiError(e);
|
|
264
|
+
await this.errorLog(`failed BLEpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
265
|
+
await this.BLEPushConnection();
|
|
263
266
|
});
|
|
264
|
-
|
|
265
|
-
.
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
+ `On: ${this.Outlet.On} sent over BLE, sent successfully`);
|
|
269
|
-
this.Outlet.On = false;
|
|
270
|
-
})
|
|
271
|
-
.catch(async (e: any) => {
|
|
272
|
-
this.apiError(e);
|
|
273
|
-
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} failed BLEpushChanges with ${this.device.connectionType}`
|
|
274
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
275
|
-
await this.BLEPushConnection();
|
|
276
|
-
});
|
|
267
|
+
} else {
|
|
268
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
|
|
269
|
+
await this.BLEPushConnection();
|
|
270
|
+
}
|
|
277
271
|
} else {
|
|
278
|
-
this.debugLog(
|
|
279
|
-
+ ` On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
272
|
+
await this.debugLog(`No changes (BLEpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
280
273
|
}
|
|
281
274
|
}
|
|
282
275
|
|
|
283
276
|
async openAPIpushChanges() {
|
|
284
|
-
this.debugLog(
|
|
277
|
+
await this.debugLog('openAPIpushChanges');
|
|
285
278
|
if (this.Outlet.On !== this.accessory.context.On) {
|
|
286
|
-
|
|
287
|
-
if (this.Outlet.On) {
|
|
288
|
-
command = 'turnOn';
|
|
289
|
-
} else {
|
|
290
|
-
command = 'turnOff';
|
|
291
|
-
}
|
|
279
|
+
const command = this.Outlet.On ? 'turnOn' : 'turnOff';
|
|
292
280
|
const bodyChange = JSON.stringify({
|
|
293
281
|
command: `${command}`,
|
|
294
282
|
parameter: 'default',
|
|
295
283
|
commandType: 'command',
|
|
296
284
|
});
|
|
297
|
-
this.debugLog(
|
|
285
|
+
await this.debugLog(`SwitchBot OpenAPI bodyChange: ${JSON.stringify(bodyChange)}`);
|
|
298
286
|
try {
|
|
299
287
|
const { body, statusCode } = await this.pushChangeRequest(bodyChange);
|
|
300
288
|
const deviceStatus: any = await body.json();
|
|
301
|
-
await this.
|
|
302
|
-
if (
|
|
303
|
-
await this.
|
|
289
|
+
await this.debugLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
290
|
+
if (await this.successfulStatusCodes(statusCode, deviceStatus)) {
|
|
291
|
+
await this.debugSuccessLog(`statusCode: ${statusCode}, deviceStatus: ${JSON.stringify(deviceStatus)}`);
|
|
304
292
|
await this.updateHomeKitCharacteristics();
|
|
305
293
|
} else {
|
|
306
294
|
await this.statusCode(statusCode);
|
|
307
295
|
await this.statusCode(deviceStatus.statusCode);
|
|
308
296
|
}
|
|
309
297
|
} catch (e: any) {
|
|
310
|
-
this.apiError(e);
|
|
311
|
-
this.errorLog(
|
|
312
|
-
+ ` Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
298
|
+
await this.apiError(e);
|
|
299
|
+
await this.errorLog(`failed openAPIpushChanges with ${this.device.connectionType} Connection, Error Message: ${JSON.stringify(e.message)}`);
|
|
313
300
|
}
|
|
314
301
|
} else {
|
|
315
|
-
this.debugLog(
|
|
316
|
-
+ `On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
302
|
+
await this.debugLog(`No changes (openAPIpushChanges), On: ${this.Outlet.On}, OnCached: ${this.accessory.context.On}`);
|
|
317
303
|
}
|
|
318
304
|
}
|
|
319
305
|
|
|
@@ -321,10 +307,10 @@ export class Plug extends deviceBase {
|
|
|
321
307
|
* Handle requests to set the value of the "On" characteristic
|
|
322
308
|
*/
|
|
323
309
|
async OnSet(value: CharacteristicValue): Promise<void> {
|
|
324
|
-
if (this.Outlet.On
|
|
325
|
-
this.
|
|
310
|
+
if (this.Outlet.On !== this.accessory.context.On) {
|
|
311
|
+
await this.infoLog(`Set On: ${value}`);
|
|
326
312
|
} else {
|
|
327
|
-
this.
|
|
313
|
+
await this.debugLog(`No Changes, On: ${value}`);
|
|
328
314
|
}
|
|
329
315
|
|
|
330
316
|
this.Outlet.On = value;
|
|
@@ -333,27 +319,21 @@ export class Plug extends deviceBase {
|
|
|
333
319
|
|
|
334
320
|
async updateHomeKitCharacteristics(): Promise<void> {
|
|
335
321
|
// On
|
|
336
|
-
|
|
337
|
-
this.
|
|
338
|
-
} else {
|
|
339
|
-
this.accessory.context.On = this.Outlet.On;
|
|
340
|
-
this.Outlet.Service.updateCharacteristic(this.hap.Characteristic.On, this.Outlet.On);
|
|
341
|
-
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic On: ${this.Outlet.On}`);
|
|
342
|
-
}
|
|
322
|
+
await this.updateCharacteristic(this.Outlet.Service, this.hap.Characteristic.On,
|
|
323
|
+
this.Outlet.On, 'On');
|
|
343
324
|
}
|
|
344
325
|
|
|
345
326
|
async BLEPushConnection() {
|
|
346
327
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
347
|
-
this.warnLog(
|
|
328
|
+
await this.warnLog('Using OpenAPI Connection to Push Changes');
|
|
348
329
|
await this.openAPIpushChanges();
|
|
349
330
|
}
|
|
350
331
|
}
|
|
351
332
|
|
|
352
333
|
async BLERefreshConnection(switchbot: any): Promise<void> {
|
|
353
|
-
this.errorLog(
|
|
354
|
-
+ ` ${JSON.stringify(switchbot)}`);
|
|
334
|
+
await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${JSON.stringify(switchbot)}`);
|
|
355
335
|
if (this.platform.config.credentials?.token && this.device.connectionType === 'BLE/OpenAPI') {
|
|
356
|
-
this.warnLog(
|
|
336
|
+
await this.warnLog('Using OpenAPI Connection to Refresh Status');
|
|
357
337
|
await this.openAPIRefreshStatus();
|
|
358
338
|
}
|
|
359
339
|
}
|