@switchbot/homebridge-switchbot 2.14.0 → 3.0.0-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/.github/npm-version-script.cjs +81 -0
- package/.github/workflows/dependabot.yml +10 -11
- package/CHANGELOG.md +11 -2
- package/LICENSE +1 -1
- package/config.schema.json +1 -0
- package/dist/device/blindtilt.d.ts +9 -5
- package/dist/device/blindtilt.d.ts.map +1 -1
- package/dist/device/blindtilt.js +151 -91
- package/dist/device/blindtilt.js.map +1 -1
- package/dist/device/bot.d.ts +9 -5
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +234 -184
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts +9 -5
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +110 -64
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts +9 -5
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +121 -69
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts +9 -5
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +111 -66
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts +9 -5
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +164 -107
- package/dist/device/curtain.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 +87 -51
- package/dist/device/hub.js.map +1 -1
- package/dist/device/humidifier.d.ts +9 -5
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +133 -90
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/iosensor.d.ts +9 -5
- package/dist/device/iosensor.d.ts.map +1 -1
- package/dist/device/iosensor.js +108 -66
- package/dist/device/iosensor.js.map +1 -1
- package/dist/device/lightstrip.d.ts +9 -5
- package/dist/device/lightstrip.d.ts.map +1 -1
- package/dist/device/lightstrip.js +110 -65
- package/dist/device/lightstrip.js.map +1 -1
- package/dist/device/lock.d.ts +10 -6
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +125 -84
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts +9 -5
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +102 -63
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts +9 -5
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +102 -63
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts +9 -5
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +95 -54
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts +9 -5
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +68 -40
- package/dist/device/plug.js.map +1 -1
- package/dist/device/robotvacuumcleaner.d.ts +9 -5
- package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
- package/dist/device/robotvacuumcleaner.js +86 -52
- package/dist/device/robotvacuumcleaner.js.map +1 -1
- package/dist/homebridge-ui/server.d.ts +2 -0
- package/dist/homebridge-ui/server.d.ts.map +1 -0
- package/dist/homebridge-ui/server.js +44 -0
- package/dist/homebridge-ui/server.js.map +1 -0
- package/dist/index.d.ts +1 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -5
- package/dist/index.js.map +1 -1
- package/dist/irdevice/airconditioner.d.ts +8 -4
- package/dist/irdevice/airconditioner.d.ts.map +1 -1
- package/dist/irdevice/airconditioner.js +105 -71
- package/dist/irdevice/airconditioner.js.map +1 -1
- package/dist/irdevice/airpurifier.d.ts +8 -4
- package/dist/irdevice/airpurifier.d.ts.map +1 -1
- package/dist/irdevice/airpurifier.js +78 -46
- package/dist/irdevice/airpurifier.js.map +1 -1
- package/dist/irdevice/camera.d.ts +8 -4
- package/dist/irdevice/camera.d.ts.map +1 -1
- package/dist/irdevice/camera.js +41 -25
- package/dist/irdevice/camera.js.map +1 -1
- package/dist/irdevice/fan.d.ts +8 -4
- package/dist/irdevice/fan.d.ts.map +1 -1
- package/dist/irdevice/fan.js +62 -39
- package/dist/irdevice/fan.js.map +1 -1
- package/dist/irdevice/light.d.ts +8 -4
- package/dist/irdevice/light.d.ts.map +1 -1
- package/dist/irdevice/light.js +67 -45
- package/dist/irdevice/light.js.map +1 -1
- package/dist/irdevice/other.d.ts +8 -4
- package/dist/irdevice/other.d.ts.map +1 -1
- package/dist/irdevice/other.js +176 -150
- package/dist/irdevice/other.js.map +1 -1
- package/dist/irdevice/tv.d.ts +8 -4
- package/dist/irdevice/tv.d.ts.map +1 -1
- package/dist/irdevice/tv.js +81 -62
- package/dist/irdevice/tv.js.map +1 -1
- package/dist/irdevice/vacuumcleaner.d.ts +8 -4
- package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
- package/dist/irdevice/vacuumcleaner.js +41 -25
- package/dist/irdevice/vacuumcleaner.js.map +1 -1
- package/dist/irdevice/waterheater.d.ts +8 -4
- package/dist/irdevice/waterheater.d.ts.map +1 -1
- package/dist/irdevice/waterheater.js +48 -32
- package/dist/irdevice/waterheater.js.map +1 -1
- package/dist/platform.d.ts +8 -9
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +147 -162
- package/dist/platform.js.map +1 -1
- package/dist/settings.js +7 -14
- package/dist/settings.js.map +1 -1
- package/dist/utils.js +1 -5
- package/dist/utils.js.map +1 -1
- package/package.json +28 -16
- package/src/device/blindtilt.ts +80 -69
- package/src/device/bot.ts +189 -178
- package/src/device/ceilinglight.ts +55 -42
- package/src/device/colorbulb.ts +51 -38
- package/src/device/contact.ts +69 -58
- package/src/device/curtain.ts +94 -83
- package/src/device/hub.ts +54 -43
- package/src/device/humidifier.ts +86 -75
- package/src/device/iosensor.ts +63 -52
- package/src/device/lightstrip.ts +54 -41
- package/src/device/lock.ts +89 -79
- package/src/device/meter.ts +59 -48
- package/src/device/meterplus.ts +63 -52
- package/src/device/motion.ts +56 -45
- package/src/device/plug.ts +36 -25
- package/src/device/robotvacuumcleaner.ts +47 -36
- package/src/homebridge-ui/public/index.html +240 -0
- package/src/homebridge-ui/server.ts +48 -0
- package/src/index.ts +10 -7
- package/src/irdevice/airconditioner.ts +70 -62
- package/src/irdevice/airpurifier.ts +47 -39
- package/src/irdevice/camera.ts +27 -19
- package/src/irdevice/fan.ts +41 -33
- package/src/irdevice/light.ts +47 -39
- package/src/irdevice/other.ts +163 -155
- package/src/irdevice/tv.ts +65 -57
- package/src/irdevice/vacuumcleaner.ts +27 -19
- package/src/irdevice/waterheater.ts +34 -26
- package/src/platform.ts +35 -21
- package/tsconfig.json +11 -13
- package/homebridge-ui/server.js +0 -50
- /package/{homebridge-ui → dist/homebridge-ui}/public/index.html +0 -0
package/src/device/meterplus.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { connectAsync } from 'async-mqtt';
|
|
2
|
-
import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
|
|
2
|
+
import { CharacteristicValue, PlatformAccessory, Service, Units, API, Logging, HAP } from 'homebridge';
|
|
3
3
|
import { MqttClient } from 'mqtt';
|
|
4
4
|
import { hostname } from 'os';
|
|
5
5
|
import { interval } from 'rxjs';
|
|
6
6
|
import { request } from 'undici';
|
|
7
7
|
import { SwitchBotPlatform } from '../platform';
|
|
8
|
-
import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature } from '../settings';
|
|
8
|
+
import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, temperature, SwitchBotPlatformConfig } from '../settings';
|
|
9
9
|
import { sleep } from '../utils';
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -14,6 +14,10 @@ import { sleep } from '../utils';
|
|
|
14
14
|
* Each accessory may expose multiple services of different service types.
|
|
15
15
|
*/
|
|
16
16
|
export class MeterPlus {
|
|
17
|
+
public readonly api: API;
|
|
18
|
+
public readonly log: Logging;
|
|
19
|
+
public readonly config!: SwitchBotPlatformConfig;
|
|
20
|
+
protected readonly hap: HAP;
|
|
17
21
|
// Services
|
|
18
22
|
batteryService: Service;
|
|
19
23
|
humidityService?: Service;
|
|
@@ -54,51 +58,58 @@ export class MeterPlus {
|
|
|
54
58
|
deviceRefreshRate!: number;
|
|
55
59
|
|
|
56
60
|
// Connection
|
|
57
|
-
private readonly
|
|
58
|
-
private readonly
|
|
61
|
+
private readonly OpenAPI: boolean;
|
|
62
|
+
private readonly BLE: boolean;
|
|
59
63
|
|
|
60
64
|
constructor(
|
|
61
65
|
private readonly platform: SwitchBotPlatform,
|
|
62
66
|
private accessory: PlatformAccessory,
|
|
63
67
|
public device: device & devicesConfig,
|
|
64
68
|
) {
|
|
69
|
+
this.api = this.platform.api;
|
|
70
|
+
this.log = this.platform.log;
|
|
71
|
+
this.config = this.platform.config;
|
|
72
|
+
this.hap = this.api.hap;
|
|
73
|
+
// Connection
|
|
74
|
+
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
75
|
+
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
65
76
|
// default placeholders
|
|
66
|
-
this.
|
|
77
|
+
this.deviceLogs(device);
|
|
67
78
|
this.scan(device);
|
|
68
79
|
this.refreshRate(device);
|
|
69
80
|
this.context();
|
|
70
81
|
this.setupHistoryService(device);
|
|
71
82
|
this.setupMqtt(device);
|
|
72
|
-
this.
|
|
83
|
+
this.deviceConfig(device);
|
|
73
84
|
|
|
74
85
|
// Retrieve initial values and updateHomekit
|
|
75
86
|
this.refreshStatus();
|
|
76
87
|
|
|
77
88
|
// set accessory information
|
|
78
89
|
accessory
|
|
79
|
-
.getService(this.
|
|
80
|
-
.setCharacteristic(this.
|
|
81
|
-
.setCharacteristic(this.
|
|
82
|
-
.setCharacteristic(this.
|
|
83
|
-
.setCharacteristic(this.
|
|
90
|
+
.getService(this.hap.Service.AccessoryInformation)!
|
|
91
|
+
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
|
|
92
|
+
.setCharacteristic(this.hap.Characteristic.Model, this.model(device))
|
|
93
|
+
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
94
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
|
|
84
95
|
|
|
85
96
|
// Temperature Sensor Service
|
|
86
97
|
if (device.meter?.hide_temperature) {
|
|
87
98
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
|
|
88
|
-
this.temperatureService = this.accessory.getService(this.
|
|
99
|
+
this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor);
|
|
89
100
|
accessory.removeService(this.temperatureService!);
|
|
90
101
|
} else if (!this.temperatureService) {
|
|
91
102
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
|
|
92
103
|
const temperatureService = `${accessory.displayName} Temperature Sensor`;
|
|
93
|
-
(this.temperatureService = this.accessory.getService(this.
|
|
94
|
-
|| this.accessory.addService(this.
|
|
104
|
+
(this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor)
|
|
105
|
+
|| this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureService;
|
|
95
106
|
|
|
96
|
-
this.temperatureService.setCharacteristic(this.
|
|
97
|
-
if (!this.temperatureService.testCharacteristic(this.
|
|
98
|
-
this.temperatureService.addCharacteristic(this.
|
|
107
|
+
this.temperatureService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
|
|
108
|
+
if (!this.temperatureService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
109
|
+
this.temperatureService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
|
|
99
110
|
}
|
|
100
111
|
this.temperatureService
|
|
101
|
-
.getCharacteristic(this.
|
|
112
|
+
.getCharacteristic(this.hap.Characteristic.CurrentTemperature)
|
|
102
113
|
.setProps({
|
|
103
114
|
unit: Units['CELSIUS'],
|
|
104
115
|
validValueRanges: [-273.15, 100],
|
|
@@ -116,20 +127,20 @@ export class MeterPlus {
|
|
|
116
127
|
// Humidity Sensor Service
|
|
117
128
|
if (device.meter?.hide_humidity) {
|
|
118
129
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
|
|
119
|
-
this.humidityService = this.accessory.getService(this.
|
|
130
|
+
this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor);
|
|
120
131
|
accessory.removeService(this.humidityService!);
|
|
121
132
|
} else if (!this.humidityService) {
|
|
122
133
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
|
|
123
134
|
const humidityService = `${accessory.displayName} Humidity Sensor`;
|
|
124
|
-
(this.humidityService = this.accessory.getService(this.
|
|
125
|
-
|| this.accessory.addService(this.
|
|
135
|
+
(this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor)
|
|
136
|
+
|| this.accessory.addService(this.hap.Service.HumiditySensor)), humidityService;
|
|
126
137
|
|
|
127
|
-
this.humidityService.setCharacteristic(this.
|
|
128
|
-
if (!this.humidityService.testCharacteristic(this.
|
|
129
|
-
this.humidityService.addCharacteristic(this.
|
|
138
|
+
this.humidityService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
|
|
139
|
+
if (!this.humidityService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
140
|
+
this.humidityService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
|
|
130
141
|
}
|
|
131
142
|
this.humidityService
|
|
132
|
-
.getCharacteristic(this.
|
|
143
|
+
.getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
|
|
133
144
|
.setProps({
|
|
134
145
|
minStep: 0.1,
|
|
135
146
|
})
|
|
@@ -142,14 +153,14 @@ export class MeterPlus {
|
|
|
142
153
|
|
|
143
154
|
// Battery Service
|
|
144
155
|
const batteryService = `${accessory.displayName} Battery`;
|
|
145
|
-
(this.batteryService = this.accessory.getService(this.
|
|
146
|
-
|| accessory.addService(this.
|
|
156
|
+
(this.batteryService = this.accessory.getService(this.hap.Service.Battery)
|
|
157
|
+
|| accessory.addService(this.hap.Service.Battery)), batteryService;
|
|
147
158
|
|
|
148
|
-
this.batteryService.setCharacteristic(this.
|
|
149
|
-
if (!this.batteryService.testCharacteristic(this.
|
|
150
|
-
this.batteryService.addCharacteristic(this.
|
|
159
|
+
this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
|
|
160
|
+
if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
161
|
+
this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
|
|
151
162
|
}
|
|
152
|
-
this.batteryService.setCharacteristic(this.
|
|
163
|
+
this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
|
|
153
164
|
|
|
154
165
|
// Retrieve initial values and updateHomekit
|
|
155
166
|
this.updateHomeKitCharacteristics();
|
|
@@ -170,16 +181,16 @@ export class MeterPlus {
|
|
|
170
181
|
const { temperature, humidity } = context;
|
|
171
182
|
const { CurrentTemperature, CurrentRelativeHumidity } = this;
|
|
172
183
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
184
|
+
'(temperature, humidity) = ' +
|
|
185
|
+
`Webhook:(${temperature}, ${humidity}), ` +
|
|
186
|
+
`current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
|
|
176
187
|
this.CurrentRelativeHumidity = humidity;
|
|
177
188
|
this.CurrentTemperature = temperature;
|
|
178
189
|
this.updateHomeKitCharacteristics();
|
|
179
190
|
}
|
|
180
191
|
} catch (e: any) {
|
|
181
192
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
182
|
-
|
|
193
|
+
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
183
194
|
}
|
|
184
195
|
};
|
|
185
196
|
}
|
|
@@ -209,9 +220,9 @@ export class MeterPlus {
|
|
|
209
220
|
// Battery
|
|
210
221
|
this.BatteryLevel = Number(this.BLE_BatteryLevel);
|
|
211
222
|
if (this.BatteryLevel < 15) {
|
|
212
|
-
this.StatusLowBattery = this.
|
|
223
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
213
224
|
} else {
|
|
214
|
-
this.StatusLowBattery = this.
|
|
225
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
215
226
|
}
|
|
216
227
|
this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
|
|
217
228
|
|
|
@@ -235,9 +246,9 @@ export class MeterPlus {
|
|
|
235
246
|
// BatteryLevel
|
|
236
247
|
this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
|
|
237
248
|
if (this.BatteryLevel < 15) {
|
|
238
|
-
this.StatusLowBattery = this.
|
|
249
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
239
250
|
} else {
|
|
240
|
-
this.StatusLowBattery = this.
|
|
251
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
241
252
|
}
|
|
242
253
|
this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
|
|
243
254
|
|
|
@@ -256,9 +267,9 @@ export class MeterPlus {
|
|
|
256
267
|
// Battery
|
|
257
268
|
this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
|
|
258
269
|
if (this.BatteryLevel < 10) {
|
|
259
|
-
this.StatusLowBattery = this.
|
|
270
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
260
271
|
} else {
|
|
261
|
-
this.StatusLowBattery = this.
|
|
272
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
262
273
|
}
|
|
263
274
|
if (Number.isNaN(this.BatteryLevel)) {
|
|
264
275
|
this.BatteryLevel = 100;
|
|
@@ -412,7 +423,7 @@ export class MeterPlus {
|
|
|
412
423
|
entry['humidity'] = this.CurrentRelativeHumidity;
|
|
413
424
|
}
|
|
414
425
|
this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
|
|
415
|
-
this.humidityService?.updateCharacteristic(this.
|
|
426
|
+
this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
|
|
416
427
|
this.debugLog(
|
|
417
428
|
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
418
429
|
` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
|
|
@@ -431,7 +442,7 @@ export class MeterPlus {
|
|
|
431
442
|
entry['temp'] = this.CurrentTemperature;
|
|
432
443
|
}
|
|
433
444
|
this.accessory.context.CurrentTemperature = this.CurrentTemperature;
|
|
434
|
-
this.temperatureService?.updateCharacteristic(this.
|
|
445
|
+
this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
|
|
435
446
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
|
|
436
447
|
}
|
|
437
448
|
}
|
|
@@ -443,7 +454,7 @@ export class MeterPlus {
|
|
|
443
454
|
mqttmessage.push(`"battery": ${this.BatteryLevel}`);
|
|
444
455
|
}
|
|
445
456
|
this.accessory.context.BatteryLevel = this.BatteryLevel;
|
|
446
|
-
this.batteryService?.updateCharacteristic(this.
|
|
457
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
447
458
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
448
459
|
}
|
|
449
460
|
// StatusLowBattery
|
|
@@ -454,7 +465,7 @@ export class MeterPlus {
|
|
|
454
465
|
mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
|
|
455
466
|
}
|
|
456
467
|
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
457
|
-
this.batteryService?.updateCharacteristic(this.
|
|
468
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
458
469
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
459
470
|
}
|
|
460
471
|
|
|
@@ -630,13 +641,13 @@ export class MeterPlus {
|
|
|
630
641
|
|
|
631
642
|
async apiError(e: any): Promise<void> {
|
|
632
643
|
if (!this.device.meter?.hide_humidity) {
|
|
633
|
-
this.humidityService?.updateCharacteristic(this.
|
|
644
|
+
this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
|
|
634
645
|
}
|
|
635
646
|
if (!this.device.meter?.hide_temperature) {
|
|
636
|
-
this.temperatureService?.updateCharacteristic(this.
|
|
647
|
+
this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
|
|
637
648
|
}
|
|
638
|
-
this.batteryService?.updateCharacteristic(this.
|
|
639
|
-
this.batteryService?.updateCharacteristic(this.
|
|
649
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
|
|
650
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
|
|
640
651
|
}
|
|
641
652
|
|
|
642
653
|
async context() {
|
|
@@ -656,7 +667,7 @@ export class MeterPlus {
|
|
|
656
667
|
this.BatteryLevel = this.accessory.context.BatteryLevel;
|
|
657
668
|
}
|
|
658
669
|
if (this.StatusLowBattery === undefined) {
|
|
659
|
-
this.StatusLowBattery = this.
|
|
670
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
660
671
|
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
661
672
|
} else {
|
|
662
673
|
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
|
|
@@ -677,7 +688,7 @@ export class MeterPlus {
|
|
|
677
688
|
}
|
|
678
689
|
}
|
|
679
690
|
|
|
680
|
-
async
|
|
691
|
+
async deviceConfig(device: device & devicesConfig): Promise<void> {
|
|
681
692
|
let config = {};
|
|
682
693
|
if (device.meter) {
|
|
683
694
|
config = device.meter;
|
|
@@ -705,7 +716,7 @@ export class MeterPlus {
|
|
|
705
716
|
}
|
|
706
717
|
}
|
|
707
718
|
|
|
708
|
-
async
|
|
719
|
+
async deviceLogs(device: device & devicesConfig): Promise<void> {
|
|
709
720
|
if (this.platform.debugMode) {
|
|
710
721
|
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
711
722
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|
package/src/device/motion.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { sleep } from '../utils';
|
|
|
3
3
|
import { interval, Subject } from 'rxjs';
|
|
4
4
|
import { skipWhile } from 'rxjs/operators';
|
|
5
5
|
import { SwitchBotPlatform } from '../platform';
|
|
6
|
-
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
|
|
7
|
-
import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '../settings';
|
|
6
|
+
import { Service, PlatformAccessory, CharacteristicValue, API, Logging, HAP } from 'homebridge';
|
|
7
|
+
import { device, devicesConfig, serviceData, deviceStatus, ad, Devices, SwitchBotPlatformConfig } from '../settings';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Platform Accessory
|
|
@@ -12,6 +12,10 @@ import { device, devicesConfig, serviceData, deviceStatus, ad, Devices } from '.
|
|
|
12
12
|
* Each accessory may expose multiple services of different service types.
|
|
13
13
|
*/
|
|
14
14
|
export class Motion {
|
|
15
|
+
public readonly api: API;
|
|
16
|
+
public readonly log: Logging;
|
|
17
|
+
public readonly config!: SwitchBotPlatformConfig;
|
|
18
|
+
protected readonly hap: HAP;
|
|
15
19
|
// Services
|
|
16
20
|
batteryService: Service;
|
|
17
21
|
motionSensorService!: Service;
|
|
@@ -51,20 +55,27 @@ export class Motion {
|
|
|
51
55
|
doMotionUpdate!: Subject<void>;
|
|
52
56
|
|
|
53
57
|
// Connection
|
|
54
|
-
private readonly
|
|
55
|
-
private readonly
|
|
58
|
+
private readonly OpenAPI: boolean;
|
|
59
|
+
private readonly BLE: boolean;
|
|
56
60
|
|
|
57
61
|
constructor(
|
|
58
62
|
private readonly platform: SwitchBotPlatform,
|
|
59
63
|
private accessory: PlatformAccessory,
|
|
60
64
|
public device: device & devicesConfig,
|
|
61
65
|
) {
|
|
66
|
+
this.api = this.platform.api;
|
|
67
|
+
this.log = this.platform.log;
|
|
68
|
+
this.config = this.platform.config;
|
|
69
|
+
this.hap = this.api.hap;
|
|
70
|
+
// Connection
|
|
71
|
+
this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
|
|
72
|
+
this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
|
|
62
73
|
// default placeholders
|
|
63
|
-
this.
|
|
74
|
+
this.deviceLogs(device);
|
|
64
75
|
this.scan(device);
|
|
65
76
|
this.refreshRate(device);
|
|
66
77
|
this.context();
|
|
67
|
-
this.
|
|
78
|
+
this.deviceConfig(device);
|
|
68
79
|
|
|
69
80
|
// this is subject we use to track when we need to POST changes to the SwitchBot API
|
|
70
81
|
this.doMotionUpdate = new Subject();
|
|
@@ -75,21 +86,21 @@ export class Motion {
|
|
|
75
86
|
|
|
76
87
|
// set accessory information
|
|
77
88
|
accessory
|
|
78
|
-
.getService(this.
|
|
79
|
-
.setCharacteristic(this.
|
|
80
|
-
.setCharacteristic(this.
|
|
81
|
-
.setCharacteristic(this.
|
|
82
|
-
.setCharacteristic(this.
|
|
89
|
+
.getService(this.hap.Service.AccessoryInformation)!
|
|
90
|
+
.setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
|
|
91
|
+
.setCharacteristic(this.hap.Characteristic.Model, 'W1101500')
|
|
92
|
+
.setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
|
|
93
|
+
.setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
|
|
83
94
|
|
|
84
95
|
// get the Battery service if it exists, otherwise create a new Motion service
|
|
85
96
|
// you can create multiple services for each accessory
|
|
86
97
|
const motionSensorService = `${accessory.displayName} Motion Sensor`;
|
|
87
|
-
(this.motionSensorService = accessory.getService(this.
|
|
88
|
-
|| accessory.addService(this.
|
|
98
|
+
(this.motionSensorService = accessory.getService(this.hap.Service.MotionSensor)
|
|
99
|
+
|| accessory.addService(this.hap.Service.MotionSensor)), motionSensorService;
|
|
89
100
|
|
|
90
|
-
this.motionSensorService.setCharacteristic(this.
|
|
91
|
-
if (!this.motionSensorService.testCharacteristic(this.
|
|
92
|
-
this.motionSensorService.addCharacteristic(this.
|
|
101
|
+
this.motionSensorService.setCharacteristic(this.hap.Characteristic.Name, accessory.displayName);
|
|
102
|
+
if (!this.motionSensorService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
103
|
+
this.motionSensorService.addCharacteristic(this.hap.Characteristic.ConfiguredName, accessory.displayName);
|
|
93
104
|
}
|
|
94
105
|
// each service must implement at-minimum the "required characteristics" for the given service type
|
|
95
106
|
// see https://developers.homebridge.io/#/service/MotionSensor
|
|
@@ -97,30 +108,30 @@ export class Motion {
|
|
|
97
108
|
// Light Sensor Service
|
|
98
109
|
if (device.motion?.hide_lightsensor) {
|
|
99
110
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Light Sensor Service`);
|
|
100
|
-
this.lightSensorService = this.accessory.getService(this.
|
|
111
|
+
this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor);
|
|
101
112
|
accessory.removeService(this.lightSensorService!);
|
|
102
113
|
} else if (!this.lightSensorService) {
|
|
103
114
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Light Sensor Service`);
|
|
104
115
|
const lightSensorService = `${accessory.displayName} Light Sensor`;
|
|
105
|
-
(this.lightSensorService = this.accessory.getService(this.
|
|
106
|
-
|| this.accessory.addService(this.
|
|
116
|
+
(this.lightSensorService = this.accessory.getService(this.hap.Service.LightSensor)
|
|
117
|
+
|| this.accessory.addService(this.hap.Service.LightSensor)), lightSensorService;
|
|
107
118
|
|
|
108
|
-
this.lightSensorService.setCharacteristic(this.
|
|
109
|
-
this.lightSensorService.setCharacteristic(this.
|
|
119
|
+
this.lightSensorService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Light Sensor`);
|
|
120
|
+
this.lightSensorService.setCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Light Sensor`);
|
|
110
121
|
} else {
|
|
111
122
|
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Light Sensor Service Not Added`);
|
|
112
123
|
}
|
|
113
124
|
|
|
114
125
|
// Battery Service
|
|
115
126
|
const batteryService = `${accessory.displayName} Battery`;
|
|
116
|
-
(this.batteryService = this.accessory.getService(this.
|
|
117
|
-
|| accessory.addService(this.
|
|
127
|
+
(this.batteryService = this.accessory.getService(this.hap.Service.Battery)
|
|
128
|
+
|| accessory.addService(this.hap.Service.Battery)), batteryService;
|
|
118
129
|
|
|
119
|
-
this.batteryService.setCharacteristic(this.
|
|
120
|
-
if (!this.batteryService.testCharacteristic(this.
|
|
121
|
-
this.batteryService.addCharacteristic(this.
|
|
130
|
+
this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
|
|
131
|
+
if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
|
|
132
|
+
this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
|
|
122
133
|
}
|
|
123
|
-
this.batteryService.setCharacteristic(this.
|
|
134
|
+
this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
|
|
124
135
|
|
|
125
136
|
// Retrieve initial values and updateHomekit
|
|
126
137
|
this.updateHomeKitCharacteristics();
|
|
@@ -141,14 +152,14 @@ export class Motion {
|
|
|
141
152
|
const { detectionState } = context;
|
|
142
153
|
const { MotionDetected } = this;
|
|
143
154
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
155
|
+
'(detectionState) = ' +
|
|
156
|
+
`Webhook:(${detectionState}), ` +
|
|
157
|
+
`current:(${MotionDetected})`);
|
|
147
158
|
this.MotionDetected = detectionState === 'DETECTED' ? true : false;
|
|
148
159
|
this.updateHomeKitCharacteristics();
|
|
149
160
|
} catch (e: any) {
|
|
150
161
|
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
|
|
151
|
-
|
|
162
|
+
+ `failed to handle webhook. Received: ${JSON.stringify(context)} Error: ${e}`);
|
|
152
163
|
}
|
|
153
164
|
};
|
|
154
165
|
}
|
|
@@ -206,9 +217,9 @@ export class Motion {
|
|
|
206
217
|
}
|
|
207
218
|
this.BatteryLevel = this.BLE_BatteryLevel!;
|
|
208
219
|
if (this.BatteryLevel < 10) {
|
|
209
|
-
this.StatusLowBattery = this.
|
|
220
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
210
221
|
} else {
|
|
211
|
-
this.StatusLowBattery = this.
|
|
222
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
212
223
|
}
|
|
213
224
|
this.debugLog(
|
|
214
225
|
`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},` + ` StatusLowBattery: ${this.StatusLowBattery}`,
|
|
@@ -238,9 +249,9 @@ export class Motion {
|
|
|
238
249
|
// Battery
|
|
239
250
|
this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
|
|
240
251
|
if (this.BatteryLevel < 10) {
|
|
241
|
-
this.StatusLowBattery = this.
|
|
252
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
|
|
242
253
|
} else {
|
|
243
|
-
this.StatusLowBattery = this.
|
|
254
|
+
this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
|
|
244
255
|
}
|
|
245
256
|
if (Number.isNaN(this.BatteryLevel)) {
|
|
246
257
|
this.BatteryLevel = 100;
|
|
@@ -394,21 +405,21 @@ export class Motion {
|
|
|
394
405
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MotionDetected: ${this.MotionDetected}`);
|
|
395
406
|
} else {
|
|
396
407
|
this.accessory.context.MotionDetected = this.MotionDetected;
|
|
397
|
-
this.motionSensorService.updateCharacteristic(this.
|
|
408
|
+
this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, this.MotionDetected);
|
|
398
409
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic MotionDetected: ${this.MotionDetected}`);
|
|
399
410
|
}
|
|
400
411
|
if (this.BatteryLevel === undefined) {
|
|
401
412
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
|
|
402
413
|
} else {
|
|
403
414
|
this.accessory.context.BatteryLevel = this.BatteryLevel;
|
|
404
|
-
this.batteryService?.updateCharacteristic(this.
|
|
415
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
|
|
405
416
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
|
|
406
417
|
}
|
|
407
418
|
if (this.StatusLowBattery === undefined) {
|
|
408
419
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
|
|
409
420
|
} else {
|
|
410
421
|
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
|
|
411
|
-
this.batteryService?.updateCharacteristic(this.
|
|
422
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
|
|
412
423
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
|
|
413
424
|
}
|
|
414
425
|
if (this.BLE) {
|
|
@@ -416,7 +427,7 @@ export class Motion {
|
|
|
416
427
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`);
|
|
417
428
|
} else {
|
|
418
429
|
this.accessory.context.CurrentAmbientLightLevel = this.CurrentAmbientLightLevel;
|
|
419
|
-
this.lightSensorService?.updateCharacteristic(this.
|
|
430
|
+
this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, this.CurrentAmbientLightLevel);
|
|
420
431
|
this.debugLog(
|
|
421
432
|
`${this.device.deviceType}: ${this.accessory.displayName}` +
|
|
422
433
|
` updateCharacteristic CurrentAmbientLightLevel: ${this.CurrentAmbientLightLevel}`,
|
|
@@ -563,11 +574,11 @@ export class Motion {
|
|
|
563
574
|
}
|
|
564
575
|
|
|
565
576
|
async apiError(e: any): Promise<void> {
|
|
566
|
-
this.motionSensorService.updateCharacteristic(this.
|
|
567
|
-
this.batteryService?.updateCharacteristic(this.
|
|
568
|
-
this.batteryService?.updateCharacteristic(this.
|
|
577
|
+
this.motionSensorService.updateCharacteristic(this.hap.Characteristic.MotionDetected, e);
|
|
578
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
|
|
579
|
+
this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
|
|
569
580
|
if (this.BLE) {
|
|
570
|
-
this.lightSensorService?.updateCharacteristic(this.
|
|
581
|
+
this.lightSensorService?.updateCharacteristic(this.hap.Characteristic.CurrentAmbientLightLevel, e);
|
|
571
582
|
}
|
|
572
583
|
}
|
|
573
584
|
|
|
@@ -589,7 +600,7 @@ export class Motion {
|
|
|
589
600
|
}
|
|
590
601
|
}
|
|
591
602
|
|
|
592
|
-
async
|
|
603
|
+
async deviceConfig(device: device & devicesConfig): Promise<void> {
|
|
593
604
|
let config = {};
|
|
594
605
|
if (device.motion) {
|
|
595
606
|
config = device.motion;
|
|
@@ -617,7 +628,7 @@ export class Motion {
|
|
|
617
628
|
}
|
|
618
629
|
}
|
|
619
630
|
|
|
620
|
-
async
|
|
631
|
+
async deviceLogs(device: device & devicesConfig): Promise<void> {
|
|
621
632
|
if (this.platform.debugMode) {
|
|
622
633
|
this.deviceLogging = this.accessory.context.logging = 'debugMode';
|
|
623
634
|
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
|