@switchbot/homebridge-switchbot 3.5.0-beta.22 → 3.5.0-beta.24

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.
Files changed (150) hide show
  1. package/config.schema.json +23 -2
  2. package/dist/device/blindtilt.d.ts +21 -98
  3. package/dist/device/blindtilt.d.ts.map +1 -1
  4. package/dist/device/blindtilt.js +332 -785
  5. package/dist/device/blindtilt.js.map +1 -1
  6. package/dist/device/bot.d.ts +25 -75
  7. package/dist/device/bot.d.ts.map +1 -1
  8. package/dist/device/bot.js +566 -778
  9. package/dist/device/bot.js.map +1 -1
  10. package/dist/device/ceilinglight.d.ts +13 -71
  11. package/dist/device/ceilinglight.d.ts.map +1 -1
  12. package/dist/device/ceilinglight.js +168 -606
  13. package/dist/device/ceilinglight.js.map +1 -1
  14. package/dist/device/colorbulb.d.ts +13 -78
  15. package/dist/device/colorbulb.d.ts.map +1 -1
  16. package/dist/device/colorbulb.js +218 -655
  17. package/dist/device/colorbulb.js.map +1 -1
  18. package/dist/device/contact.d.ts +14 -69
  19. package/dist/device/contact.d.ts.map +1 -1
  20. package/dist/device/contact.js +196 -553
  21. package/dist/device/contact.js.map +1 -1
  22. package/dist/device/curtain.d.ts +17 -90
  23. package/dist/device/curtain.d.ts.map +1 -1
  24. package/dist/device/curtain.js +324 -810
  25. package/dist/device/curtain.js.map +1 -1
  26. package/dist/device/device.d.ts +58 -0
  27. package/dist/device/device.d.ts.map +1 -0
  28. package/dist/device/device.js +629 -0
  29. package/dist/device/device.js.map +1 -0
  30. package/dist/device/hub.d.ts +15 -63
  31. package/dist/device/hub.d.ts.map +1 -1
  32. package/dist/device/hub.js +223 -610
  33. package/dist/device/hub.js.map +1 -1
  34. package/dist/device/humidifier.d.ts +10 -69
  35. package/dist/device/humidifier.d.ts.map +1 -1
  36. package/dist/device/humidifier.js +192 -595
  37. package/dist/device/humidifier.js.map +1 -1
  38. package/dist/device/iosensor.d.ts +11 -68
  39. package/dist/device/iosensor.d.ts.map +1 -1
  40. package/dist/device/iosensor.js +156 -599
  41. package/dist/device/iosensor.js.map +1 -1
  42. package/dist/device/lightstrip.d.ts +10 -72
  43. package/dist/device/lightstrip.d.ts.map +1 -1
  44. package/dist/device/lightstrip.js +204 -591
  45. package/dist/device/lightstrip.js.map +1 -1
  46. package/dist/device/lock.d.ts +15 -66
  47. package/dist/device/lock.d.ts.map +1 -1
  48. package/dist/device/lock.js +199 -566
  49. package/dist/device/lock.js.map +1 -1
  50. package/dist/device/meter.d.ts +13 -67
  51. package/dist/device/meter.d.ts.map +1 -1
  52. package/dist/device/meter.js +152 -592
  53. package/dist/device/meter.js.map +1 -1
  54. package/dist/device/meterplus.d.ts +13 -68
  55. package/dist/device/meterplus.d.ts.map +1 -1
  56. package/dist/device/meterplus.js +151 -599
  57. package/dist/device/meterplus.js.map +1 -1
  58. package/dist/device/motion.d.ts +13 -73
  59. package/dist/device/motion.d.ts.map +1 -1
  60. package/dist/device/motion.js +129 -540
  61. package/dist/device/motion.js.map +1 -1
  62. package/dist/device/plug.d.ts +9 -61
  63. package/dist/device/plug.d.ts.map +1 -1
  64. package/dist/device/plug.js +71 -476
  65. package/dist/device/plug.js.map +1 -1
  66. package/dist/device/robotvacuumcleaner.d.ts +11 -66
  67. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  68. package/dist/device/robotvacuumcleaner.js +122 -471
  69. package/dist/device/robotvacuumcleaner.js.map +1 -1
  70. package/dist/device/waterdetector.d.ts +9 -66
  71. package/dist/device/waterdetector.d.ts.map +1 -1
  72. package/dist/device/waterdetector.js +105 -478
  73. package/dist/device/waterdetector.js.map +1 -1
  74. package/dist/irdevice/airconditioner.d.ts +5 -36
  75. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  76. package/dist/irdevice/airconditioner.js +228 -337
  77. package/dist/irdevice/airconditioner.js.map +1 -1
  78. package/dist/irdevice/airpurifier.d.ts +5 -34
  79. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  80. package/dist/irdevice/airpurifier.js +19 -267
  81. package/dist/irdevice/airpurifier.js.map +1 -1
  82. package/dist/irdevice/camera.d.ts +5 -34
  83. package/dist/irdevice/camera.d.ts.map +1 -1
  84. package/dist/irdevice/camera.js +13 -261
  85. package/dist/irdevice/camera.js.map +1 -1
  86. package/dist/irdevice/fan.d.ts +5 -34
  87. package/dist/irdevice/fan.d.ts.map +1 -1
  88. package/dist/irdevice/fan.js +13 -260
  89. package/dist/irdevice/fan.js.map +1 -1
  90. package/dist/irdevice/irdevice.d.ts +39 -0
  91. package/dist/irdevice/irdevice.d.ts.map +1 -0
  92. package/dist/irdevice/irdevice.js +502 -0
  93. package/dist/irdevice/irdevice.js.map +1 -0
  94. package/dist/irdevice/light.d.ts +5 -34
  95. package/dist/irdevice/light.d.ts.map +1 -1
  96. package/dist/irdevice/light.js +13 -267
  97. package/dist/irdevice/light.js.map +1 -1
  98. package/dist/irdevice/other.d.ts +18 -48
  99. package/dist/irdevice/other.d.ts.map +1 -1
  100. package/dist/irdevice/other.js +349 -551
  101. package/dist/irdevice/other.js.map +1 -1
  102. package/dist/irdevice/tv.d.ts +7 -40
  103. package/dist/irdevice/tv.d.ts.map +1 -1
  104. package/dist/irdevice/tv.js +79 -325
  105. package/dist/irdevice/tv.js.map +1 -1
  106. package/dist/irdevice/vacuumcleaner.d.ts +6 -36
  107. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  108. package/dist/irdevice/vacuumcleaner.js +38 -288
  109. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  110. package/dist/irdevice/waterheater.d.ts +6 -36
  111. package/dist/irdevice/waterheater.d.ts.map +1 -1
  112. package/dist/irdevice/waterheater.js +39 -291
  113. package/dist/irdevice/waterheater.js.map +1 -1
  114. package/dist/settings.d.ts +15 -2
  115. package/dist/settings.d.ts.map +1 -1
  116. package/dist/utils.d.ts +68 -0
  117. package/dist/utils.d.ts.map +1 -1
  118. package/dist/utils.js +71 -0
  119. package/dist/utils.js.map +1 -1
  120. package/package.json +8 -8
  121. package/src/device/blindtilt.ts +356 -809
  122. package/src/device/bot.ts +601 -865
  123. package/src/device/ceilinglight.ts +182 -628
  124. package/src/device/colorbulb.ts +235 -683
  125. package/src/device/contact.ts +220 -575
  126. package/src/device/curtain.ts +344 -836
  127. package/src/device/device.ts +655 -0
  128. package/src/device/hub.ts +246 -636
  129. package/src/device/humidifier.ts +224 -645
  130. package/src/device/iosensor.ts +181 -625
  131. package/src/device/lightstrip.ts +230 -622
  132. package/src/device/lock.ts +236 -607
  133. package/src/device/meter.ts +177 -616
  134. package/src/device/meterplus.ts +175 -624
  135. package/src/device/motion.ts +147 -563
  136. package/src/device/plug.ts +81 -505
  137. package/src/device/robotvacuumcleaner.ts +139 -494
  138. package/src/device/waterdetector.ts +126 -506
  139. package/src/irdevice/airconditioner.ts +228 -353
  140. package/src/irdevice/airpurifier.ts +23 -282
  141. package/src/irdevice/camera.ts +18 -277
  142. package/src/irdevice/fan.ts +18 -275
  143. package/src/irdevice/irdevice.ts +518 -0
  144. package/src/irdevice/light.ts +18 -283
  145. package/src/irdevice/other.ts +394 -621
  146. package/src/irdevice/tv.ts +97 -344
  147. package/src/irdevice/vacuumcleaner.ts +46 -310
  148. package/src/irdevice/waterheater.ts +47 -315
  149. package/src/settings.ts +20 -3
  150. package/src/utils.ts +71 -0
@@ -1,116 +1,89 @@
1
- import asyncmqtt from 'async-mqtt';
2
- import { CharacteristicValue, PlatformAccessory, Service, Units, API, Logging, HAP } from 'homebridge';
3
- import { MqttClient } from 'mqtt';
4
- import { hostname } from 'os';
5
- import { interval } from 'rxjs';
1
+ /* Copyright(C) 2021-2024, donavanbecker (https://github.com/donavanbecker). All rights reserved.
2
+ *
3
+ * meterplus.ts: @switchbot/homebridge-switchbot.
4
+ */
5
+ import { deviceBase } from './device.js';
6
6
  import { SwitchBotPlatform } from '../platform.js';
7
- import { Devices, device, deviceStatus, devicesConfig, serviceData, temperature, SwitchBotPlatformConfig } from '../settings.js';
8
- import { sleep } from '../utils.js';
7
+ import { Subject, interval, skipWhile } from 'rxjs';
8
+ import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
9
+ import { device, devicesConfig, serviceData, deviceStatus, Devices } from '../settings.js';
9
10
 
10
11
  /**
11
12
  * Platform Accessory
12
13
  * An instance of this class is created for each accessory your platform registers
13
14
  * Each accessory may expose multiple services of different service types.
14
15
  */
15
- export class MeterPlus {
16
- public readonly api: API;
17
- public readonly log: Logging;
18
- public readonly config!: SwitchBotPlatformConfig;
19
- protected readonly hap: HAP;
16
+ export class MeterPlus extends deviceBase {
20
17
  // Services
21
- batteryService: Service;
22
- humidityService?: Service;
23
- temperatureService?: Service;
24
-
25
- // Characteristic Values
26
- BatteryLevel!: CharacteristicValue;
27
- FirmwareRevision!: CharacteristicValue;
28
- StatusLowBattery!: CharacteristicValue;
29
- CurrentTemperature?: CharacteristicValue;
30
- CurrentRelativeHumidity?: CharacteristicValue;
31
-
32
- // OpenAPI Status
33
- OpenAPI_BatteryLevel: deviceStatus['battery'];
34
- OpenAPI_FirmwareRevision: deviceStatus['version'];
35
- OpenAPI_CurrentTemperature: deviceStatus['temperature'];
36
- OpenAPI_CurrentRelativeHumidity: deviceStatus['humidity'];
37
-
38
- // BLE Status
39
- BLE_Celsius!: temperature['c'];
40
- BLE_Fahrenheit!: temperature['f'];
41
- BLE_BatteryLevel!: serviceData['battery'];
42
- BLE_CurrentTemperature!: serviceData['temperature'];
43
- BLE_CurrentRelativeHumidity!: serviceData['humidity'];
44
-
45
- // BLE Others
46
- BLE_IsConnected?: boolean;
47
-
48
- //MQTT stuff
49
- mqttClient: MqttClient | null = null;
50
-
51
- // EVE history service handler
52
- historyService?: any;
53
-
54
- // Config
55
- scanDuration!: number;
56
- deviceLogging!: string;
57
- deviceRefreshRate!: number;
58
- maxRetries!: number;
59
- delayBetweenRetries!: number;
60
-
61
- // Connection
62
- private readonly OpenAPI: boolean;
63
- private readonly BLE: boolean;
18
+ private Battery!: {
19
+ Service: Service;
20
+ BatteryLevel: CharacteristicValue;
21
+ StatusLowBattery: CharacteristicValue;
22
+ };
23
+
24
+ private HumiditySensor?: {
25
+ Service: Service;
26
+ CurrentRelativeHumidity: CharacteristicValue;
27
+ };
28
+
29
+ private TemperatureSensor?: {
30
+ Service: Service;
31
+ CurrentTemperature: CharacteristicValue;
32
+ };
33
+
34
+ // Updates
35
+ meterUpdateInProgress!: boolean;
36
+ doMeterUpdate: Subject<void>;
64
37
 
65
38
  constructor(
66
- private readonly platform: SwitchBotPlatform,
67
- private accessory: PlatformAccessory,
68
- public device: device & devicesConfig,
39
+ readonly platform: SwitchBotPlatform,
40
+ accessory: PlatformAccessory,
41
+ device: device & devicesConfig,
69
42
  ) {
70
- this.api = this.platform.api;
71
- this.log = this.platform.log;
72
- this.config = this.platform.config;
73
- this.hap = this.api.hap;
74
- // Connection
75
- this.BLE = this.device.connectionType === 'BLE' || this.device.connectionType === 'BLE/OpenAPI';
76
- this.OpenAPI = this.device.connectionType === 'OpenAPI' || this.device.connectionType === 'BLE/OpenAPI';
77
- // default placeholders
78
- this.deviceLogs(device);
79
- this.scan(device);
80
- this.refreshRate(device);
81
- this.deviceContext();
82
- this.setupHistoryService(device);
83
- this.setupMqtt(device);
84
- this.deviceRetry(device);
85
- this.deviceConfig(device);
43
+ super(platform, accessory, device);
44
+ // this is subject we use to track when we need to POST changes to the SwitchBot API
45
+ this.doMeterUpdate = new Subject();
46
+ this.meterUpdateInProgress = false;
86
47
 
87
48
  // Retrieve initial values and updateHomekit
88
49
  this.refreshStatus();
89
50
 
90
- // set accessory information
91
- accessory
92
- .getService(this.hap.Service.AccessoryInformation)!
93
- .setCharacteristic(this.hap.Characteristic.Manufacturer, 'SwitchBot')
94
- .setCharacteristic(this.hap.Characteristic.Model, this.model(device))
95
- .setCharacteristic(this.hap.Characteristic.SerialNumber, device.deviceId)
96
- .setCharacteristic(this.hap.Characteristic.FirmwareRevision, accessory.context.FirmwareRevision);
51
+ // Initialize Temperature Sensor property
52
+ if (!device.meter?.hide_temperature) {
53
+ this.TemperatureSensor = {
54
+ Service: this.accessory.addService(this.hap.Service.TemperatureSensor),
55
+ CurrentTemperature: accessory.context.CurrentTemperature || 30,
56
+ };
57
+ }
58
+
59
+ // Initialize Humidity Sensor property
60
+ if (!device.meter?.hide_humidity) {
61
+ this.HumiditySensor = {
62
+ Service: this.accessory.addService(this.hap.Service.HumiditySensor),
63
+ CurrentRelativeHumidity: accessory.context.CurrentRelativeHumidity || 50,
64
+ };
65
+ }
66
+
67
+ // Initialize Battery property
68
+ this.Battery = {
69
+ Service: this.accessory.addService(this.hap.Service.Battery),
70
+ BatteryLevel: accessory.context.BatteryLevel || 100,
71
+ StatusLowBattery: accessory.context.StatusLowBattery || this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL,
72
+ };
97
73
 
98
74
  // Temperature Sensor Service
99
75
  if (device.meter?.hide_temperature) {
100
76
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Temperature Sensor Service`);
101
- this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor);
102
- accessory.removeService(this.temperatureService!);
103
- } else if (!this.temperatureService) {
77
+ this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor) as Service;
78
+ accessory.removeService(this.TemperatureSensor!.Service);
79
+ } else if (!this.TemperatureSensor!.Service) {
104
80
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
105
- const temperatureService = `${accessory.displayName} Temperature Sensor`;
106
- (this.temperatureService = this.accessory.getService(this.hap.Service.TemperatureSensor)
107
- || this.accessory.addService(this.hap.Service.TemperatureSensor)), temperatureService;
81
+ const TemperatureSensorService = `${accessory.displayName} Temperature Sensor`;
82
+ (this.TemperatureSensor!.Service = this.accessory.getService(this.hap.Service.TemperatureSensor)
83
+ || this.accessory.addService(this.hap.Service.TemperatureSensor)), TemperatureSensorService;
108
84
 
109
- this.temperatureService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
110
- if (!this.temperatureService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
111
- this.temperatureService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Temperature Sensor`);
112
- }
113
- this.temperatureService
85
+ this.TemperatureSensor!.Service.setCharacteristic(this.hap.Characteristic.Name, TemperatureSensorService);
86
+ this.TemperatureSensor!.Service
114
87
  .getCharacteristic(this.hap.Characteristic.CurrentTemperature)
115
88
  .setProps({
116
89
  unit: Units['CELSIUS'],
@@ -120,7 +93,7 @@ export class MeterPlus {
120
93
  minStep: 0.1,
121
94
  })
122
95
  .onGet(() => {
123
- return this.CurrentTemperature!;
96
+ return this.TemperatureSensor!.CurrentTemperature!;
124
97
  });
125
98
  } else {
126
99
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Temperature Sensor Service Not Added`);
@@ -129,46 +102,41 @@ export class MeterPlus {
129
102
  // Humidity Sensor Service
130
103
  if (device.meter?.hide_humidity) {
131
104
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Humidity Sensor Service`);
132
- this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor);
133
- accessory.removeService(this.humidityService!);
134
- } else if (!this.humidityService) {
105
+ this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor) as Service;
106
+ accessory.removeService(this.HumiditySensor!.Service);
107
+ } else if (!this.HumiditySensor!.Service) {
135
108
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
136
- const humidityService = `${accessory.displayName} Humidity Sensor`;
137
- (this.humidityService = this.accessory.getService(this.hap.Service.HumiditySensor)
138
- || this.accessory.addService(this.hap.Service.HumiditySensor)), humidityService;
109
+ const HumiditySensorService = `${accessory.displayName} Humidity Sensor`;
110
+ (this.HumiditySensor!.Service = this.accessory.getService(this.hap.Service.HumiditySensor)
111
+ || this.accessory.addService(this.hap.Service.HumiditySensor)), HumiditySensorService;
139
112
 
140
- this.humidityService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
141
- if (!this.humidityService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
142
- this.humidityService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Humidity Sensor`);
143
- }
144
- this.humidityService
113
+ this.HumiditySensor!.Service.setCharacteristic(this.hap.Characteristic.Name, HumiditySensorService);
114
+ this.HumiditySensor!.Service
145
115
  .getCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity)
146
116
  .setProps({
147
117
  minStep: 0.1,
148
118
  })
149
119
  .onGet(() => {
150
- return this.CurrentRelativeHumidity!;
120
+ return this.HumiditySensor!.CurrentRelativeHumidity!;
151
121
  });
152
122
  } else {
153
123
  this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Humidity Sensor Service Not Added`);
154
124
  }
155
125
 
156
126
  // Battery Service
157
- const batteryService = `${accessory.displayName} Battery`;
158
- (this.batteryService = this.accessory.getService(this.hap.Service.Battery)
159
- || accessory.addService(this.hap.Service.Battery)), batteryService;
127
+ const BatteryService = `${accessory.displayName} Battery`;
128
+ (this.Battery.Service = this.accessory.getService(this.hap.Service.Battery)
129
+ || accessory.addService(this.hap.Service.Battery)), BatteryService;
160
130
 
161
- this.batteryService.setCharacteristic(this.hap.Characteristic.Name, `${accessory.displayName} Battery`);
162
- if (!this.batteryService.testCharacteristic(this.hap.Characteristic.ConfiguredName)) {
163
- this.batteryService.addCharacteristic(this.hap.Characteristic.ConfiguredName, `${accessory.displayName} Battery`);
164
- }
165
- this.batteryService.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
131
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.Name, BatteryService);
132
+ this.Battery.Service.setCharacteristic(this.hap.Characteristic.ChargingState, this.hap.Characteristic.ChargingState.NOT_CHARGEABLE);
166
133
 
167
134
  // Retrieve initial values and updateHomekit
168
135
  this.updateHomeKitCharacteristics();
169
136
 
170
137
  // Start an update interval
171
138
  interval(this.deviceRefreshRate * 1000)
139
+ .pipe(skipWhile(() => this.meterUpdateInProgress))
172
140
  .subscribe(async () => {
173
141
  await this.refreshStatus();
174
142
  });
@@ -181,13 +149,18 @@ export class MeterPlus {
181
149
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} received Webhook: ${JSON.stringify(context)}`);
182
150
  if (context.scale === 'CELSIUS') {
183
151
  const { temperature, humidity } = context;
184
- const { CurrentTemperature, CurrentRelativeHumidity } = this;
152
+ const { CurrentRelativeHumidity } = this.HumiditySensor || { CurrentRelativeHumidity: undefined };
153
+ const { CurrentTemperature } = this.TemperatureSensor || { CurrentTemperature: undefined };
185
154
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ` +
186
155
  '(temperature, humidity) = ' +
187
156
  `Webhook:(${temperature}, ${humidity}), ` +
188
157
  `current:(${CurrentTemperature}, ${CurrentRelativeHumidity})`);
189
- this.CurrentRelativeHumidity = humidity;
190
- this.CurrentTemperature = temperature;
158
+ if (this.device.meter?.hide_humidity) {
159
+ this.HumiditySensor!.CurrentRelativeHumidity = humidity;
160
+ }
161
+ if (this.device.meter?.hide_temperature) {
162
+ this.TemperatureSensor!.CurrentTemperature = temperature;
163
+ }
191
164
  this.updateHomeKitCharacteristics();
192
165
  }
193
166
  } catch (e: any) {
@@ -198,90 +171,61 @@ export class MeterPlus {
198
171
  }
199
172
  }
200
173
 
201
- /**
202
- * Parse the device status from the SwitchBot api
203
- */
204
- async parseStatus(): Promise<void> {
205
- if (!this.device.enableCloudService && this.OpenAPI) {
206
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
207
- } else if (this.BLE) {
208
- await this.BLEparseStatus();
209
- } else if (this.OpenAPI && this.platform.config.credentials?.token) {
210
- await this.openAPIparseStatus();
211
- } else {
212
- await this.offlineOff();
213
- this.debugWarnLog(
214
- `${this.device.deviceType}: ${this.accessory.displayName} Connection Type:` + ` ${this.device.connectionType}, parseStatus will not happen.`,
215
- );
216
- }
217
- }
218
-
219
- async BLEparseStatus(): Promise<void> {
174
+ async BLEparseStatus(serviceData: serviceData): Promise<void> {
220
175
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BLEparseStatus`);
221
176
 
222
177
  // Battery
223
- this.BatteryLevel = Number(this.BLE_BatteryLevel);
224
- if (this.BatteryLevel < 15) {
225
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
178
+ this.Battery.BatteryLevel = Number(serviceData.battery);
179
+ if (this.Battery.BatteryLevel < 15) {
180
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
226
181
  } else {
227
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
182
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
228
183
  }
229
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
184
+ this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}, StatusLowBattery: ${this.Battery.StatusLowBattery}`);
230
185
 
231
186
  // Humidity
232
187
  if (!this.device.meter?.hide_humidity) {
233
- this.CurrentRelativeHumidity = this.BLE_CurrentRelativeHumidity!;
234
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
188
+ this.HumiditySensor!.CurrentRelativeHumidity = serviceData.humidity!;
189
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
235
190
  }
236
191
 
237
192
  // Current Temperature
238
193
  if (!this.device.meter?.hide_temperature) {
239
- this.BLE_Celsius < 0 ? 0 : this.BLE_Celsius > 100 ? 100 : this.BLE_Celsius;
240
- this.CurrentTemperature = this.BLE_Celsius;
241
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
194
+ serviceData.temperature!.c < 0 ? 0 : serviceData.temperature!.c > 100 ? 100 : serviceData.temperature!.c;
195
+ this.TemperatureSensor!.CurrentTemperature = serviceData.temperature!.c;
196
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
242
197
  }
243
198
  }
244
199
 
245
- async openAPIparseStatus(): Promise<void> {
200
+ async openAPIparseStatus(deviceStatus: deviceStatus): Promise<void> {
246
201
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);
247
202
 
248
203
  // BatteryLevel
249
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
250
- if (this.BatteryLevel < 15) {
251
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
204
+ this.Battery.BatteryLevel = Number(deviceStatus.body.battery);
205
+ if (this.Battery.BatteryLevel < 10) {
206
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
252
207
  } else {
253
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
208
+ this.Battery.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
254
209
  }
255
- this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);
210
+ if (Number.isNaN(this.Battery.BatteryLevel)) {
211
+ this.Battery.BatteryLevel = 100;
212
+ }
213
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel},`
214
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
256
215
 
257
216
  // CurrentRelativeHumidity
258
217
  if (!this.device.meter?.hide_humidity) {
259
- this.CurrentRelativeHumidity = this.OpenAPI_CurrentRelativeHumidity!;
260
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.CurrentRelativeHumidity}%`);
218
+ this.HumiditySensor!.CurrentRelativeHumidity = deviceStatus.body.humidity!;
219
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Humidity: ${this.HumiditySensor!.CurrentRelativeHumidity}%`);
261
220
  }
262
221
 
263
222
  // CurrentTemperature
264
223
  if (!this.device.meter?.hide_temperature) {
265
- this.CurrentTemperature = this.OpenAPI_CurrentTemperature!;
266
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.CurrentTemperature}°c`);
267
- }
268
-
269
- // Battery
270
- this.BatteryLevel = Number(this.OpenAPI_BatteryLevel);
271
- if (this.BatteryLevel < 10) {
272
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
273
- } else {
274
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
224
+ this.TemperatureSensor!.CurrentTemperature = deviceStatus.body.temperature!;
225
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Temperature: ${this.TemperatureSensor!.CurrentTemperature}°c`);
275
226
  }
276
- if (Number.isNaN(this.BatteryLevel)) {
277
- this.BatteryLevel = 100;
278
- }
279
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel},`
280
- + ` StatusLowBattery: ${this.StatusLowBattery}`);
281
-
282
227
  // FirmwareRevision
283
- this.FirmwareRevision = this.OpenAPI_FirmwareRevision!;
284
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
228
+ this.accessory.context.FirmwareRevision = deviceStatus.body.version;
285
229
  }
286
230
 
287
231
  /**
@@ -315,97 +259,36 @@ export class MeterPlus {
315
259
  this.getCustomBLEAddress(switchbot);
316
260
  // Start to monitor advertisement packets
317
261
  (async () => {
318
- await switchbot.startScan({
319
- model: 'i',
320
- id: this.device.bleMac,
321
- });
262
+ // Start to monitor advertisement packets
263
+ await switchbot.startScan({ model: this.device.bleModel, id: this.device.bleMac });
322
264
  // Set an event handler
323
265
  switchbot.onadvertisement = (ad: any) => {
324
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
325
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.serviceData.model}`);
326
- if (this.device.bleMac === ad.address && ad.serviceData.model === 'i') {
266
+ if (this.device.bleMac === ad.address && ad.model === this.device.bleModel) {
267
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} ${JSON.stringify(ad, null, ' ')}`);
268
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} address: ${ad.address}, model: ${ad.model}`);
327
269
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
328
- this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity < 0 ? 0 : ad.serviceData.humidity > 100 ? 100 : ad.serviceData.humidity;
329
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
330
- this.BLE_Celsius = ad.serviceData.temperature!.c;
331
- this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
332
- this.BLE_BatteryLevel = ad.serviceData.battery;
333
270
  } else {
334
271
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
335
272
  }
336
273
  };
337
- // Wait 1 seconds
274
+ // Wait 10 seconds
338
275
  await switchbot.wait(this.scanDuration * 1000);
339
276
  // Stop to monitor
340
277
  await switchbot.stopScan();
341
278
  // Update HomeKit
342
- await this.BLEparseStatus();
279
+ await this.BLEparseStatus(switchbot.onadvertisement.serviceData);
343
280
  await this.updateHomeKitCharacteristics();
344
281
  })();
345
- /*if (switchbot !== false) {
346
- switchbot
347
- .startScan({
348
- model: 'i',
349
- id: this.device.bleMac,
350
- })
351
- .then(async () => {
352
- // Set an event handler
353
- switchbot.onadvertisement = async (ad: ad) => {
354
- this.debugLog(
355
- `${this.device.deviceType}: ${this.accessory.displayName} Config BLE Address: ${this.device.bleMac},` +
356
- ` BLE Address Found: ${ad.address}`,
357
- );
358
- if (ad.serviceData.humidity! > 0) {
359
- // reject unreliable data
360
- this.BLE_CurrentRelativeHumidity = ad.serviceData.humidity;
361
- }
362
- this.BLE_CurrentTemperature = ad.serviceData.temperature;
363
- this.BLE_Celsius = ad.serviceData.temperature!.c;
364
- this.BLE_Fahrenheit = ad.serviceData.temperature!.f;
365
- this.BLE_BatteryLevel = ad.serviceData.battery;
366
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} serviceData: ${JSON.stringify(ad.serviceData)}`);
367
- this.debugLog(
368
- `${this.device.deviceType}: ${this.accessory.displayName} model: ${ad.serviceData.model}, modelName: ${ad.serviceData.modelName}, ` +
369
- `temperature: ${JSON.stringify(ad.serviceData.temperature?.c)}, humidity: ${ad.serviceData.humidity}, ` +
370
- `battery: ${ad.serviceData.battery}`,
371
- );
372
-
373
- if (ad.serviceData) {
374
- this.BLE_IsConnected = true;
375
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
376
- await this.stopScanning(switchbot);
377
- } else {
378
- this.BLE_IsConnected = false;
379
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} connected: ${this.BLE_IsConnected}`);
380
- }
381
- };
382
- // Wait
383
- return await sleep(this.scanDuration * 1000);
384
- })
385
- .then(async () => {
386
- // Stop to monitor
387
- await this.stopScanning(switchbot);
388
- })
389
- .catch(async (e: any) => {
390
- this.apiError(e);
391
- this.errorLog(
392
- `${this.device.deviceType}: ${this.accessory.displayName} failed BLERefreshStatus with ${this.device.connectionType}` +
393
- ` Connection, Error Message: ${JSON.stringify(e.message)}`,
394
- );
395
- await this.BLERefreshConnection(switchbot);
396
- });
397
- } else {
282
+ if (switchbot === undefined) {
398
283
  await this.BLERefreshConnection(switchbot);
399
- }*/
284
+ }
400
285
  }
401
286
 
402
287
  async openAPIRefreshStatus(): Promise<void> {
403
288
  this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIRefreshStatus`);
404
289
  try {
405
- const { body, statusCode } = await this.platform.retryRequest(this.maxRetries, this.delayBetweenRetries,
406
- `${Devices}/${this.device.deviceId}/status`, {
407
- headers: this.platform.generateHeaders(),
408
- });
290
+ const { body, statusCode } = await this.platform.retryRequest(this.deviceMaxRetries, this.deviceDelayBetweenRetries,
291
+ `${Devices}/${this.device.deviceId}/status`, { headers: this.platform.generateHeaders() });
409
292
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} statusCode: ${statusCode}`);
410
293
  const deviceStatus: any = await body.json();
411
294
  this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} deviceStatus: ${JSON.stringify(deviceStatus)}`);
@@ -413,11 +296,7 @@ export class MeterPlus {
413
296
  if ((statusCode === 200 || statusCode === 100) && (deviceStatus.statusCode === 200 || deviceStatus.statusCode === 100)) {
414
297
  this.debugErrorLog(`${this.device.deviceType}: ${this.accessory.displayName} `
415
298
  + `statusCode: ${statusCode} & deviceStatus StatusCode: ${deviceStatus.statusCode}`);
416
- this.OpenAPI_CurrentRelativeHumidity = deviceStatus.body.humidity!;
417
- this.OpenAPI_CurrentTemperature = deviceStatus.body.temperature!;
418
- this.OpenAPI_BatteryLevel = deviceStatus.body.battery;
419
- this.OpenAPI_FirmwareRevision = deviceStatus.body.version;
420
- this.openAPIparseStatus();
299
+ this.openAPIparseStatus(deviceStatus);
421
300
  this.updateHomeKitCharacteristics();
422
301
  } else {
423
302
  this.statusCode(statusCode);
@@ -441,67 +320,73 @@ export class MeterPlus {
441
320
 
442
321
  // CurrentRelativeHumidity
443
322
  if (!this.device.meter?.hide_humidity) {
444
- if (this.CurrentRelativeHumidity === undefined) {
445
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`);
323
+ if (this.HumiditySensor!.CurrentRelativeHumidity === undefined) {
324
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
325
+ + ` CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
446
326
  } else {
327
+ this.accessory.context.CurrentRelativeHumidity = this.HumiditySensor!.CurrentRelativeHumidity;
328
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity,
329
+ this.HumiditySensor!.CurrentRelativeHumidity);
330
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName}`
331
+ + ` updateCharacteristic CurrentRelativeHumidity: ${this.HumiditySensor!.CurrentRelativeHumidity}`);
447
332
  if (this.device.mqttURL) {
448
- mqttmessage.push(`"humidity": ${this.CurrentRelativeHumidity}`);
333
+ mqttmessage.push(`"humidity": ${this.HumiditySensor!.CurrentRelativeHumidity}`);
449
334
  }
450
335
  if (this.device.history) {
451
- entry['humidity'] = this.CurrentRelativeHumidity;
336
+ entry['humidity'] = this.HumiditySensor!.CurrentRelativeHumidity;
452
337
  }
453
- this.accessory.context.CurrentRelativeHumidity = this.CurrentRelativeHumidity;
454
- this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, this.CurrentRelativeHumidity);
455
- this.debugLog(
456
- `${this.device.deviceType}: ${this.accessory.displayName}` +
457
- ` updateCharacteristic CurrentRelativeHumidity: ${this.CurrentRelativeHumidity}`,
458
- );
459
338
  }
460
339
  }
340
+
461
341
  // CurrentTemperature
462
342
  if (!this.device.meter?.hide_temperature) {
463
- if (this.CurrentTemperature === undefined) {
464
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.CurrentTemperature}`);
343
+ if (this.TemperatureSensor!.CurrentTemperature === undefined) {
344
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
465
345
  } else {
346
+ this.accessory.context.CurrentTemperature = this.TemperatureSensor!.CurrentTemperature;
347
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.TemperatureSensor!.CurrentTemperature);
348
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
349
+ + ` CurrentTemperature: ${this.TemperatureSensor!.CurrentTemperature}`);
466
350
  if (this.device.mqttURL) {
467
- mqttmessage.push(`"temperature": ${this.CurrentTemperature}`);
351
+ mqttmessage.push(`"temperature": ${this.TemperatureSensor!.CurrentTemperature}`);
468
352
  }
469
353
  if (this.device.history) {
470
- entry['temp'] = this.CurrentTemperature;
354
+ entry['temp'] = this.TemperatureSensor!.CurrentTemperature;
471
355
  }
472
- this.accessory.context.CurrentTemperature = this.CurrentTemperature;
473
- this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, this.CurrentTemperature);
474
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
475
356
  }
476
357
  }
358
+
477
359
  // BatteryLevel
478
- if (this.BatteryLevel === undefined) {
479
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
360
+ if (this.Battery.BatteryLevel === undefined) {
361
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.Battery.BatteryLevel}`);
480
362
  } else {
363
+ this.accessory.context.BatteryLevel = this.Battery.BatteryLevel;
364
+ this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.Battery.BatteryLevel);
365
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.Battery.BatteryLevel}`);
481
366
  if (this.device.mqttURL) {
482
- mqttmessage.push(`"battery": ${this.BatteryLevel}`);
367
+ mqttmessage.push(`"battery": ${this.Battery.BatteryLevel}`);
483
368
  }
484
- this.accessory.context.BatteryLevel = this.BatteryLevel;
485
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, this.BatteryLevel);
486
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
487
369
  }
488
370
  // StatusLowBattery
489
- if (this.StatusLowBattery === undefined) {
490
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
371
+ if (this.Battery.StatusLowBattery === undefined) {
372
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.Battery.StatusLowBattery}`);
491
373
  } else {
374
+ this.accessory.context.StatusLowBattery = this.Battery.StatusLowBattery;
375
+ this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.Battery.StatusLowBattery);
376
+ this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic`
377
+ + ` StatusLowBattery: ${this.Battery.StatusLowBattery}`);
492
378
  if (this.device.mqttURL) {
493
- mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
379
+ mqttmessage.push(`"lowBattery": ${this.Battery.StatusLowBattery}`);
494
380
  }
495
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
496
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, this.StatusLowBattery);
497
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
498
381
  }
499
382
 
500
- // MQTT
383
+ // MQTT Publish
501
384
  if (this.device.mqttURL) {
502
385
  this.mqttPublish(`{${mqttmessage.join(',')}}`);
503
386
  }
504
- if (Number(this.CurrentRelativeHumidity) > 0) {
387
+
388
+ // History Service
389
+ if (!this.device.meter?.hide_humidity && (Number(this.HumiditySensor!.CurrentRelativeHumidity) > 0)) {
505
390
  // reject unreliable data
506
391
  if (this.device.history) {
507
392
  this.historyService?.addEntry(entry);
@@ -509,84 +394,6 @@ export class MeterPlus {
509
394
  }
510
395
  }
511
396
 
512
- /*
513
- * Publish MQTT message for topics of
514
- * 'homebridge-switchbot/meter/xx:xx:xx:xx:xx:xx'
515
- */
516
- mqttPublish(message: any) {
517
- const mac = this.device.deviceId
518
- ?.toLowerCase()
519
- .match(/[\s\S]{1,2}/g)
520
- ?.join(':');
521
- const options = this.device.mqttPubOptions || {};
522
- this.mqttClient?.publish(`homebridge-switchbot/meter/${mac}`, `${message}`, options);
523
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT message: ${message} options:${JSON.stringify(options)}`);
524
- }
525
-
526
- /*
527
- * Setup MQTT hadler if URL is specified.
528
- */
529
- async setupMqtt(device: device & devicesConfig): Promise<void> {
530
- if (device.mqttURL) {
531
- try {
532
- const { connectAsync } = asyncmqtt;
533
- this.mqttClient = await connectAsync(device.mqttURL, device.mqttOptions || {});
534
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} MQTT connection has been established successfully.`);
535
- this.mqttClient.on('error', (e: Error) => {
536
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to publish MQTT messages. ${e}`);
537
- });
538
- } catch (e) {
539
- this.mqttClient = null;
540
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Failed to establish MQTT connection. ${e}`);
541
- }
542
- }
543
- }
544
-
545
- /*
546
- * Setup EVE history graph feature if enabled.
547
- */
548
- async setupHistoryService(device: device & devicesConfig): Promise<void> {
549
- const mac = this.device
550
- .deviceId!.match(/.{1,2}/g)!
551
- .join(':')
552
- .toLowerCase();
553
- this.historyService = device.history
554
- ? new this.platform.fakegatoAPI('room', this.accessory, {
555
- log: this.platform.log,
556
- storage: 'fs',
557
- filename: `${hostname().split('.')[0]}_${mac}_persist.json`,
558
- })
559
- : null;
560
- }
561
-
562
- async stopScanning(switchbot: any) {
563
- switchbot.stopScan();
564
- if (this.BLE_IsConnected) {
565
- await this.BLEparseStatus();
566
- await this.updateHomeKitCharacteristics();
567
- } else {
568
- await this.BLERefreshConnection(switchbot);
569
- }
570
- }
571
-
572
- async getCustomBLEAddress(switchbot: any) {
573
- if (this.device.customBLEaddress && this.deviceLogging.includes('debug')) {
574
- (async () => {
575
- // Start to monitor advertisement packets
576
- await switchbot.startScan({
577
- model: 'i',
578
- });
579
- // Set an event handler
580
- switchbot.onadvertisement = (ad: any) => {
581
- this.warnLog(`${this.device.deviceType}: ${this.accessory.displayName} ad: ${JSON.stringify(ad, null, ' ')}`);
582
- };
583
- await sleep(10000);
584
- // Stop to monitor
585
- switchbot.stopScan();
586
- })();
587
- }
588
- }
589
-
590
397
  async BLERefreshConnection(switchbot: any): Promise<void> {
591
398
  this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} wasn't able to establish BLE Connection, node-switchbot:`
592
399
  + ` ${JSON.stringify(switchbot)}`);
@@ -596,282 +403,26 @@ export class MeterPlus {
596
403
  }
597
404
  }
598
405
 
599
- model(device: device & devicesConfig): CharacteristicValue {
600
- let model: string;
601
- if (device.deviceType === 'Meter Plus (JP)') {
602
- model = 'W2201500';
603
- } else {
604
- model = 'W2301500';
605
- }
606
- return model;
607
- }
608
-
609
- async scan(device: device & devicesConfig): Promise<void> {
610
- if (device.scanDuration) {
611
- this.scanDuration = this.accessory.context.scanDuration = device.scanDuration;
612
- if (this.BLE) {
613
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config scanDuration: ${this.scanDuration}`);
614
- }
615
- } else {
616
- this.scanDuration = this.accessory.context.scanDuration = 1;
617
- if (this.BLE) {
618
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Default scanDuration: ${this.scanDuration}`);
619
- }
620
- }
621
- }
622
-
623
- async statusCode(statusCode: number): Promise<void> {
624
- switch (statusCode) {
625
- case 151:
626
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command not supported by this deviceType, statusCode: ${statusCode}`);
627
- break;
628
- case 152:
629
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device not found, statusCode: ${statusCode}`);
630
- break;
631
- case 160:
632
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Command is not supported, statusCode: ${statusCode}`);
633
- break;
634
- case 161:
635
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Device is offline, statusCode: ${statusCode}`);
636
- this.offlineOff();
637
- break;
638
- case 171:
639
- this.errorLog(
640
- `${this.device.deviceType}: ${this.accessory.displayName} Hub Device is offline, statusCode: ${statusCode}. ` +
641
- `Hub: ${this.device.hubDeviceId}`,
642
- );
643
- this.offlineOff();
644
- break;
645
- case 190:
646
- this.errorLog(
647
- `${this.device.deviceType}: ${this.accessory.displayName} Device internal error due to device states not synchronized with server,` +
648
- ` Or command format is invalid, statusCode: ${statusCode}`,
649
- );
650
- break;
651
- case 100:
652
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Command successfully sent, statusCode: ${statusCode}`);
653
- break;
654
- case 200:
655
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Request successful, statusCode: ${statusCode}`);
656
- break;
657
- case 400:
658
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Bad Request, The client has issued an invalid request. `
659
- + `This is commonly used to specify validation errors in a request payload, statusCode: ${statusCode}`);
660
- break;
661
- case 401:
662
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unauthorized, Authorization for the API is required, `
663
- + `but the request has not been authenticated, statusCode: ${statusCode}`);
664
- break;
665
- case 403:
666
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Forbidden, The request has been authenticated but does not `
667
- + `have appropriate permissions, or a requested resource is not found, statusCode: ${statusCode}`);
668
- break;
669
- case 404:
670
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Found, Specifies the requested path does not exist, `
671
- + `statusCode: ${statusCode}`);
672
- break;
673
- case 406:
674
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Not Acceptable, The client has requested a MIME type via `
675
- + `the Accept header for a value not supported by the server, statusCode: ${statusCode}`);
676
- break;
677
- case 415:
678
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unsupported Media Type, The client has defined a contentType `
679
- + `header that is not supported by the server, statusCode: ${statusCode}`);
680
- break;
681
- case 422:
682
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Unprocessable Entity, The client has made a valid request, `
683
- + `but the server cannot process it. This is often used for APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
684
- break;
685
- case 429:
686
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Too Many Requests, The client has exceeded the number of `
687
- + `requests allowed for a given time window, statusCode: ${statusCode}`);
688
- break;
689
- case 500:
690
- this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} Internal Server Error, An unexpected error on the SmartThings `
691
- + `servers has occurred. These errors should be rare, statusCode: ${statusCode}`);
692
- break;
693
- default:
694
- this.infoLog(
695
- `${this.device.deviceType}: ${this.accessory.displayName} Unknown statusCode: ` +
696
- `${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`,
697
- );
698
- }
699
- }
700
-
701
406
  async offlineOff(): Promise<void> {
702
407
  if (this.device.offline) {
703
- await this.deviceContext();
704
- await this.updateHomeKitCharacteristics();
408
+ if (!this.device.meter?.hide_humidity) {
409
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, 50);
410
+ }
411
+ if (!this.device.meter?.hide_temperature) {
412
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, 30);
413
+ }
414
+ this.Battery.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, 100);
705
415
  }
706
416
  }
707
417
 
708
418
  async apiError(e: any): Promise<void> {
709
419
  if (!this.device.meter?.hide_humidity) {
710
- this.humidityService?.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
420
+ this.HumiditySensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentRelativeHumidity, e);
711
421
  }
712
422
  if (!this.device.meter?.hide_temperature) {
713
- this.temperatureService?.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
714
- }
715
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
716
- this.batteryService?.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
717
- }
718
-
719
- async deviceContext() {
720
- if (this.CurrentRelativeHumidity === undefined) {
721
- this.CurrentRelativeHumidity = 0;
722
- } else {
723
- this.CurrentRelativeHumidity = this.accessory.context.CurrentRelativeHumidity;
724
- }
725
- if (this.CurrentTemperature === undefined) {
726
- this.CurrentTemperature = 0;
727
- } else {
728
- this.CurrentTemperature = this.accessory.context.CurrentTemperature;
729
- }
730
- if (this.BatteryLevel === undefined) {
731
- this.BatteryLevel = 100;
732
- } else {
733
- this.BatteryLevel = this.accessory.context.BatteryLevel;
734
- }
735
- if (this.StatusLowBattery === undefined) {
736
- this.StatusLowBattery = this.hap.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
737
- this.accessory.context.StatusLowBattery = this.StatusLowBattery;
738
- } else {
739
- this.StatusLowBattery = this.accessory.context.StatusLowBattery;
740
- }
741
- if (this.FirmwareRevision === undefined) {
742
- this.FirmwareRevision = this.platform.version;
743
- this.accessory.context.FirmwareRevision = this.FirmwareRevision;
744
- }
745
- }
746
-
747
- async refreshRate(device: device & devicesConfig): Promise<void> {
748
- if (device.refreshRate) {
749
- this.deviceRefreshRate = this.accessory.context.refreshRate = device.refreshRate;
750
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config refreshRate: ${this.deviceRefreshRate}`);
751
- } else if (this.platform.config.options!.refreshRate) {
752
- this.deviceRefreshRate = this.accessory.context.refreshRate = this.platform.config.options!.refreshRate;
753
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config refreshRate: ${this.deviceRefreshRate}`);
754
- }
755
- }
756
-
757
- async deviceRetry(device: device & devicesConfig): Promise<void> {
758
- if (!device.maxRetries) {
759
- this.maxRetries = 5; // Maximum number of retries
760
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Max Retries Not Set, Using: ${this.maxRetries}`);
761
- } else {
762
- this.maxRetries = device.maxRetries;
763
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Max Retries: ${this.maxRetries}`);
764
- }
765
- if (!device.delayBetweenRetries) {
766
- this.delayBetweenRetries = 3000; // Delay between retries in milliseconds
767
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Delay Between Retries Not Set, Using: ${this.delayBetweenRetries}`);
768
- } else {
769
- this.delayBetweenRetries = device.delayBetweenRetries * 1000;
770
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Delay Between Retries: ${this.delayBetweenRetries}`);
771
- }
772
- }
773
-
774
- async deviceConfig(device: device & devicesConfig): Promise<void> {
775
- let config = {};
776
- if (device.meter) {
777
- config = device.meter;
778
- }
779
- if (device.connectionType !== undefined) {
780
- config['connectionType'] = device.connectionType;
781
- }
782
- if (device.external !== undefined) {
783
- config['external'] = device.external;
784
- }
785
- if (device.mqttURL !== undefined) {
786
- config['mqttURL'] = device.mqttURL;
787
- }
788
- if (device.logging !== undefined) {
789
- config['logging'] = device.logging;
423
+ this.TemperatureSensor!.Service.updateCharacteristic(this.hap.Characteristic.CurrentTemperature, e);
790
424
  }
791
- if (device.refreshRate !== undefined) {
792
- config['refreshRate'] = device.refreshRate;
793
- }
794
- if (device.scanDuration !== undefined) {
795
- config['scanDuration'] = device.scanDuration;
796
- }
797
- if (device.webhook !== undefined) {
798
- config['webhook'] = device.webhook;
799
- }
800
- if (Object.entries(config).length !== 0) {
801
- this.debugWarnLog(`${this.device.deviceType}: ${this.accessory.displayName} Config: ${JSON.stringify(config)}`);
802
- }
803
- }
804
-
805
- async deviceLogs(device: device & devicesConfig): Promise<void> {
806
- if (this.platform.debugMode) {
807
- this.deviceLogging = this.accessory.context.logging = 'debugMode';
808
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Debug Mode Logging: ${this.deviceLogging}`);
809
- } else if (device.logging) {
810
- this.deviceLogging = this.accessory.context.logging = device.logging;
811
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Device Config Logging: ${this.deviceLogging}`);
812
- } else if (this.platform.config.options?.logging) {
813
- this.deviceLogging = this.accessory.context.logging = this.platform.config.options?.logging;
814
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Using Platform Config Logging: ${this.deviceLogging}`);
815
- } else {
816
- this.deviceLogging = this.accessory.context.logging = 'standard';
817
- this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} Logging Not Set, Using: ${this.deviceLogging}`);
818
- }
819
- }
820
-
821
- /**
822
- * Logging for Device
823
- */
824
- successLog(...log: any[]): void {
825
- if (this.enablingDeviceLogging()) {
826
- this.platform.log.success(String(...log));
827
- }
828
- }
829
-
830
- infoLog(...log: any[]): void {
831
- if (this.enablingDeviceLogging()) {
832
- this.platform.log.info(String(...log));
833
- }
834
- }
835
-
836
- warnLog(...log: any[]): void {
837
- if (this.enablingDeviceLogging()) {
838
- this.platform.log.warn(String(...log));
839
- }
840
- }
841
-
842
- debugWarnLog(...log: any[]): void {
843
- if (this.enablingDeviceLogging()) {
844
- if (this.deviceLogging?.includes('debug')) {
845
- this.platform.log.warn('[DEBUG]', String(...log));
846
- }
847
- }
848
- }
849
-
850
- errorLog(...log: any[]): void {
851
- if (this.enablingDeviceLogging()) {
852
- this.platform.log.error(String(...log));
853
- }
854
- }
855
-
856
- debugErrorLog(...log: any[]): void {
857
- if (this.enablingDeviceLogging()) {
858
- if (this.deviceLogging?.includes('debug')) {
859
- this.platform.log.error('[DEBUG]', String(...log));
860
- }
861
- }
862
- }
863
-
864
- debugLog(...log: any[]): void {
865
- if (this.enablingDeviceLogging()) {
866
- if (this.deviceLogging === 'debug') {
867
- this.platform.log.info('[DEBUG]', String(...log));
868
- } else {
869
- this.platform.log.debug(String(...log));
870
- }
871
- }
872
- }
873
-
874
- enablingDeviceLogging(): boolean {
875
- return this.deviceLogging.includes('debug') || this.deviceLogging === 'standard';
425
+ this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.BatteryLevel, e);
426
+ this.Battery!.Service.updateCharacteristic(this.hap.Characteristic.StatusLowBattery, e);
876
427
  }
877
428
  }