@switchbot/homebridge-switchbot 3.5.0-beta.7 → 3.5.0-beta.70

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