@switchbot/homebridge-switchbot 3.5.0-beta.9 → 3.5.1-beta.0

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