@switchbot/homebridge-switchbot 3.7.1-beta.2 → 3.7.1-beta.20

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 (199) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +14 -0
  3. package/assets/security-privacy-bluetooth.png +0 -0
  4. package/config.schema.json +74 -2
  5. package/dist/device/blindtilt.d.ts +21 -9
  6. package/dist/device/blindtilt.d.ts.map +1 -1
  7. package/dist/device/blindtilt.js +167 -96
  8. package/dist/device/blindtilt.js.map +1 -1
  9. package/dist/device/bot-old.d.ts +132 -0
  10. package/dist/device/bot-old.d.ts.map +1 -0
  11. package/dist/device/bot-old.js +1514 -0
  12. package/dist/device/bot-old.js.map +1 -0
  13. package/dist/device/bot.d.ts +10 -9
  14. package/dist/device/bot.d.ts.map +1 -1
  15. package/dist/device/bot.js +186 -187
  16. package/dist/device/bot.js.map +1 -1
  17. package/dist/device/ceilinglight.d.ts +14 -13
  18. package/dist/device/ceilinglight.d.ts.map +1 -1
  19. package/dist/device/ceilinglight.js +87 -97
  20. package/dist/device/ceilinglight.js.map +1 -1
  21. package/dist/device/colorbulb.d.ts +13 -12
  22. package/dist/device/colorbulb.d.ts.map +1 -1
  23. package/dist/device/colorbulb.js +92 -105
  24. package/dist/device/colorbulb.js.map +1 -1
  25. package/dist/device/contact.d.ts +6 -5
  26. package/dist/device/contact.d.ts.map +1 -1
  27. package/dist/device/contact.js +71 -68
  28. package/dist/device/contact.js.map +1 -1
  29. package/dist/device/curtain.d.ts +17 -5
  30. package/dist/device/curtain.d.ts.map +1 -1
  31. package/dist/device/curtain.js +184 -118
  32. package/dist/device/curtain.js.map +1 -1
  33. package/dist/device/device.d.ts +18 -18
  34. package/dist/device/device.d.ts.map +1 -1
  35. package/dist/device/device.js +33 -36
  36. package/dist/device/device.js.map +1 -1
  37. package/dist/device/fan.d.ts +9 -8
  38. package/dist/device/fan.d.ts.map +1 -1
  39. package/dist/device/fan.js +94 -101
  40. package/dist/device/fan.js.map +1 -1
  41. package/dist/device/hub.d.ts +6 -5
  42. package/dist/device/hub.d.ts.map +1 -1
  43. package/dist/device/hub.js +69 -65
  44. package/dist/device/hub.js.map +1 -1
  45. package/dist/device/humidifier.d.ts +6 -5
  46. package/dist/device/humidifier.d.ts.map +1 -1
  47. package/dist/device/humidifier.js +89 -104
  48. package/dist/device/humidifier.js.map +1 -1
  49. package/dist/device/iosensor.d.ts +6 -5
  50. package/dist/device/iosensor.d.ts.map +1 -1
  51. package/dist/device/iosensor.js +72 -66
  52. package/dist/device/iosensor.js.map +1 -1
  53. package/dist/device/lightstrip.d.ts +7 -6
  54. package/dist/device/lightstrip.d.ts.map +1 -1
  55. package/dist/device/lightstrip.js +85 -99
  56. package/dist/device/lightstrip.js.map +1 -1
  57. package/dist/device/lock.d.ts +10 -9
  58. package/dist/device/lock.d.ts.map +1 -1
  59. package/dist/device/lock.js +89 -79
  60. package/dist/device/lock.js.map +1 -1
  61. package/dist/device/meter.d.ts +6 -5
  62. package/dist/device/meter.d.ts.map +1 -1
  63. package/dist/device/meter.js +72 -65
  64. package/dist/device/meter.js.map +1 -1
  65. package/dist/device/meterplus.d.ts +6 -5
  66. package/dist/device/meterplus.d.ts.map +1 -1
  67. package/dist/device/meterplus.js +72 -64
  68. package/dist/device/meterplus.js.map +1 -1
  69. package/dist/device/motion.d.ts +6 -5
  70. package/dist/device/motion.d.ts.map +1 -1
  71. package/dist/device/motion.js +65 -58
  72. package/dist/device/motion.js.map +1 -1
  73. package/dist/device/plug.d.ts +13 -12
  74. package/dist/device/plug.d.ts.map +1 -1
  75. package/dist/device/plug.js +68 -52
  76. package/dist/device/plug.js.map +1 -1
  77. package/dist/device/robotvacuumcleaner.d.ts +12 -11
  78. package/dist/device/robotvacuumcleaner.d.ts.map +1 -1
  79. package/dist/device/robotvacuumcleaner.js +93 -82
  80. package/dist/device/robotvacuumcleaner.js.map +1 -1
  81. package/dist/device/waterdetector.d.ts +6 -5
  82. package/dist/device/waterdetector.d.ts.map +1 -1
  83. package/dist/device/waterdetector.js +73 -63
  84. package/dist/device/waterdetector.js.map +1 -1
  85. package/dist/homebridge-ui/server.js +2 -2
  86. package/dist/homebridge-ui/server.js.map +1 -1
  87. package/dist/index.d.ts.map +1 -1
  88. package/dist/index.js +0 -4
  89. package/dist/index.js.map +1 -1
  90. package/dist/irdevice/airconditioner.d.ts +8 -8
  91. package/dist/irdevice/airconditioner.d.ts.map +1 -1
  92. package/dist/irdevice/airconditioner.js +64 -105
  93. package/dist/irdevice/airconditioner.js.map +1 -1
  94. package/dist/irdevice/airpurifier.d.ts +10 -10
  95. package/dist/irdevice/airpurifier.d.ts.map +1 -1
  96. package/dist/irdevice/airpurifier.js +22 -42
  97. package/dist/irdevice/airpurifier.js.map +1 -1
  98. package/dist/irdevice/camera.d.ts +9 -9
  99. package/dist/irdevice/camera.d.ts.map +1 -1
  100. package/dist/irdevice/camera.js +15 -25
  101. package/dist/irdevice/camera.js.map +1 -1
  102. package/dist/irdevice/fan.d.ts +8 -8
  103. package/dist/irdevice/fan.d.ts.map +1 -1
  104. package/dist/irdevice/fan.js +21 -40
  105. package/dist/irdevice/fan.js.map +1 -1
  106. package/dist/irdevice/irdevice.d.ts +11 -11
  107. package/dist/irdevice/irdevice.d.ts.map +1 -1
  108. package/dist/irdevice/irdevice.js +14 -22
  109. package/dist/irdevice/irdevice.js.map +1 -1
  110. package/dist/irdevice/light.d.ts +9 -9
  111. package/dist/irdevice/light.d.ts.map +1 -1
  112. package/dist/irdevice/light.js +24 -47
  113. package/dist/irdevice/light.js.map +1 -1
  114. package/dist/irdevice/other.d.ts +9 -9
  115. package/dist/irdevice/other.d.ts.map +1 -1
  116. package/dist/irdevice/other.js +40 -83
  117. package/dist/irdevice/other.js.map +1 -1
  118. package/dist/irdevice/tv.d.ts +9 -9
  119. package/dist/irdevice/tv.d.ts.map +1 -1
  120. package/dist/irdevice/tv.js +29 -54
  121. package/dist/irdevice/tv.js.map +1 -1
  122. package/dist/irdevice/vacuumcleaner.d.ts +5 -5
  123. package/dist/irdevice/vacuumcleaner.d.ts.map +1 -1
  124. package/dist/irdevice/vacuumcleaner.js +11 -21
  125. package/dist/irdevice/vacuumcleaner.js.map +1 -1
  126. package/dist/irdevice/waterheater.d.ts +5 -5
  127. package/dist/irdevice/waterheater.d.ts.map +1 -1
  128. package/dist/irdevice/waterheater.js +11 -22
  129. package/dist/irdevice/waterheater.js.map +1 -1
  130. package/dist/platform.d.ts +24 -23
  131. package/dist/platform.d.ts.map +1 -1
  132. package/dist/platform.js +510 -722
  133. package/dist/platform.js.map +1 -1
  134. package/dist/settings.d.ts +5 -2
  135. package/dist/settings.d.ts.map +1 -1
  136. package/dist/settings.js.map +1 -1
  137. package/dist/types/bledevicestatus.d.ts +9 -9
  138. package/dist/types/bledevicestatus.d.ts.map +1 -1
  139. package/dist/types/devicelist.d.ts +6 -6
  140. package/dist/types/devicelist.d.ts.map +1 -1
  141. package/dist/types/deviceresponse.d.ts +4 -4
  142. package/dist/types/deviceresponse.d.ts.map +1 -1
  143. package/dist/types/devicestatus.d.ts +2 -2
  144. package/dist/types/devicestatus.d.ts.map +1 -1
  145. package/dist/types/devicewebhookstatus.d.ts +5 -5
  146. package/dist/types/devicewebhookstatus.d.ts.map +1 -1
  147. package/dist/types/irdevicelist.d.ts +4 -4
  148. package/dist/types/irdevicelist.d.ts.map +1 -1
  149. package/dist/types/pushbody.d.ts +2 -2
  150. package/dist/types/pushbody.d.ts.map +1 -1
  151. package/dist/utils.d.ts +4 -4
  152. package/dist/utils.d.ts.map +1 -1
  153. package/dist/utils.js +9 -13
  154. package/dist/utils.js.map +1 -1
  155. package/eslint.config.js +48 -93
  156. package/package.json +62 -53
  157. package/src/device/blindtilt.ts +537 -456
  158. package/src/device/bot-old.ts +1578 -0
  159. package/src/device/bot.ts +732 -725
  160. package/src/device/ceilinglight.ts +359 -368
  161. package/src/device/colorbulb.ts +441 -453
  162. package/src/device/contact.ts +229 -226
  163. package/src/device/curtain.ts +526 -444
  164. package/src/device/device.ts +206 -208
  165. package/src/device/fan.ts +336 -345
  166. package/src/device/hub.ts +213 -214
  167. package/src/device/humidifier.ts +326 -347
  168. package/src/device/iosensor.ts +211 -209
  169. package/src/device/lightstrip.ts +391 -404
  170. package/src/device/lock.ts +292 -282
  171. package/src/device/meter.ts +212 -210
  172. package/src/device/meterplus.ts +212 -209
  173. package/src/device/motion.ts +198 -190
  174. package/src/device/plug.ts +203 -182
  175. package/src/device/robotvacuumcleaner.ts +280 -269
  176. package/src/device/waterdetector.ts +198 -189
  177. package/src/homebridge-ui/server.ts +16 -15
  178. package/src/index.ts +5 -6
  179. package/src/irdevice/airconditioner.ts +280 -326
  180. package/src/irdevice/airpurifier.ts +124 -143
  181. package/src/irdevice/camera.ts +58 -64
  182. package/src/irdevice/fan.ts +106 -123
  183. package/src/irdevice/irdevice.ts +116 -119
  184. package/src/irdevice/light.ts +119 -143
  185. package/src/irdevice/other.ts +417 -461
  186. package/src/irdevice/tv.ts +155 -177
  187. package/src/irdevice/vacuumcleaner.ts +54 -60
  188. package/src/irdevice/waterheater.ts +56 -63
  189. package/src/platform.ts +1493 -1681
  190. package/src/settings.ts +172 -169
  191. package/src/types/bledevicestatus.ts +255 -255
  192. package/src/types/devicelist.ts +79 -79
  193. package/src/types/deviceresponse.ts +12 -13
  194. package/src/types/devicestatus.ts +124 -124
  195. package/src/types/devicewebhookstatus.ts +126 -127
  196. package/src/types/irdevicelist.ts +10 -10
  197. package/src/types/pushbody.ts +5 -5
  198. package/src/utils.ts +64 -65
  199. package/tsconfig.json +10 -9
package/src/platform.ts CHANGED
@@ -2,56 +2,63 @@
2
2
  *
3
3
  * platform.ts: @switchbot/homebridge-switchbot platform class.
4
4
  */
5
- import { Hub } from './device/hub.js';
6
- import { Bot } from './device/bot.js';
7
- import { Plug } from './device/plug.js';
8
- import { Lock } from './device/lock.js';
9
- import { Meter } from './device/meter.js';
10
- import { Motion } from './device/motion.js';
11
- import { Contact } from './device/contact.js';
12
- import { Curtain } from './device/curtain.js';
13
- import { IOSensor } from './device/iosensor.js';
14
- import { MeterPlus } from './device/meterplus.js';
15
- import { ColorBulb } from './device/colorbulb.js';
16
- import { StripLight } from './device/lightstrip.js';
17
- import { Humidifier } from './device/humidifier.js';
18
- import { CeilingLight } from './device/ceilinglight.js';
19
- import { WaterDetector } from './device/waterdetector.js';
20
- import { RobotVacuumCleaner } from './device/robotvacuumcleaner.js';
21
-
22
- import { Fan } from './device/fan.js';
23
- import { TV } from './irdevice/tv.js';
24
- import { IRFan } from './irdevice/fan.js';
25
- import { Light } from './irdevice/light.js';
26
- import { Others } from './irdevice/other.js';
27
- import { Camera } from './irdevice/camera.js';
28
- import { BlindTilt } from './device/blindtilt.js';
29
- import { AirPurifier } from './irdevice/airpurifier.js';
30
- import { WaterHeater } from './irdevice/waterheater.js';
31
- import { VacuumCleaner } from './irdevice/vacuumcleaner.js';
32
- import { AirConditioner } from './irdevice/airconditioner.js';
33
-
34
- import { Buffer } from 'buffer';
35
- import { request } from 'undici';
36
- import asyncmqtt from 'async-mqtt';
37
- import { createServer } from 'http';
38
- import { queueScheduler } from 'rxjs';
39
- import fakegato from 'fakegato-history';
40
- import crypto, { randomUUID } from 'crypto';
41
- import { readFileSync, writeFileSync } from 'fs';
42
- import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes';
43
- import { PLATFORM_NAME, PLUGIN_NAME, Devices, deleteWebhook, queryWebhook, setupWebhook, updateWebhook } from './settings.js';
44
- import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js';
45
-
46
- import type { UrlObject } from 'url';
47
- import type { MqttClient } from 'mqtt';
48
- import type { Dispatcher } from 'undici';
49
- import type { irdevice } from './types/irdevicelist.js';
50
- import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js';
51
- import type { API, DynamicPlatformPlugin, Logging, PlatformAccessory } from 'homebridge';
52
- import type { SwitchBotPlatformConfig, devicesConfig, irDevicesConfig } from './settings.js';
53
- import type { IncomingMessage, Server, ServerResponse } from 'http';
54
- import { SwitchBotModel } from 'node-switchbot';
5
+ import type { IncomingMessage, Server, ServerResponse } from 'node:http'
6
+ import type { UrlObject } from 'node:url'
7
+
8
+ import type { API, DynamicPlatformPlugin, Logging, PlatformAccessory } from 'homebridge'
9
+ import type { MqttClient } from 'mqtt'
10
+ import type { Dispatcher } from 'undici'
11
+
12
+ import type { devicesConfig, irDevicesConfig, options, SwitchBotPlatformConfig } from './settings.js'
13
+ import type { blindTilt, curtain, curtain3, device } from './types/devicelist.js'
14
+ import type { irdevice } from './types/irdevicelist.js'
15
+
16
+ import { Buffer } from 'node:buffer'
17
+ import crypto, { randomUUID } from 'node:crypto'
18
+ import { readFileSync, writeFileSync } from 'node:fs'
19
+ import { createServer } from 'node:http'
20
+ import process from 'node:process'
21
+
22
+ import asyncmqtt from 'async-mqtt'
23
+ import fakegato from 'fakegato-history'
24
+ import { EveHomeKitTypes } from 'homebridge-lib/EveHomeKitTypes'
25
+ /*
26
+ * For Testing Locally:
27
+ * import { SwitchBotModel } from '/Users/Shared/GitHub/OpenWonderLabs/node-switchbot/dist/index.js';
28
+ */
29
+ import { SwitchBotModel } from 'node-switchbot'
30
+ import { queueScheduler } from 'rxjs'
31
+ import { request } from 'undici'
32
+
33
+ import { BlindTilt } from './device/blindtilt.js'
34
+ import { Bot } from './device/bot-old.js'
35
+ import { CeilingLight } from './device/ceilinglight.js'
36
+ import { ColorBulb } from './device/colorbulb.js'
37
+ import { Contact } from './device/contact.js'
38
+ import { Curtain } from './device/curtain.js'
39
+ import { Fan } from './device/fan.js'
40
+ import { Hub } from './device/hub.js'
41
+ import { Humidifier } from './device/humidifier.js'
42
+ import { IOSensor } from './device/iosensor.js'
43
+ import { StripLight } from './device/lightstrip.js'
44
+ import { Lock } from './device/lock.js'
45
+ import { Meter } from './device/meter.js'
46
+ import { MeterPlus } from './device/meterplus.js'
47
+ import { Motion } from './device/motion.js'
48
+ import { Plug } from './device/plug.js'
49
+ import { RobotVacuumCleaner } from './device/robotvacuumcleaner.js'
50
+ import { WaterDetector } from './device/waterdetector.js'
51
+ import { AirConditioner } from './irdevice/airconditioner.js'
52
+ import { AirPurifier } from './irdevice/airpurifier.js'
53
+ import { Camera } from './irdevice/camera.js'
54
+ import { IRFan } from './irdevice/fan.js'
55
+ import { Light } from './irdevice/light.js'
56
+ import { Others } from './irdevice/other.js'
57
+ import { TV } from './irdevice/tv.js'
58
+ import { VacuumCleaner } from './irdevice/vacuumcleaner.js'
59
+ import { WaterHeater } from './irdevice/waterheater.js'
60
+ import { deleteWebhook, Devices, PLATFORM_NAME, PLUGIN_NAME, queryWebhook, setupWebhook, updateWebhook } from './settings.js'
61
+ import { isBlindTiltDevice, isCurtainDevice, sleep } from './utils.js'
55
62
 
56
63
  /**
57
64
  * HomebridgePlatform
@@ -59,40 +66,44 @@ import { SwitchBotModel } from 'node-switchbot';
59
66
  * parse the user config and discover/register accessories with Homebridge.
60
67
  */
61
68
  export class SwitchBotPlatform implements DynamicPlatformPlugin {
62
- public accessories: PlatformAccessory[];
63
- public readonly api: API;
64
- public readonly log: Logging;
65
-
66
- version!: string;
67
- Logging?: string;
68
- debugMode!: boolean;
69
- maxRetries!: number;
70
- delayBetweenRetries!: number;
71
- platformConfig!: SwitchBotPlatformConfig['options'];
72
- platformLogging!: SwitchBotPlatformConfig['logging'];
73
- config!: SwitchBotPlatformConfig;
74
-
75
- mqttClient: MqttClient | null = null;
76
- webhookEventListener: Server | null = null;
77
-
78
- public readonly eve: any;
79
- public readonly fakegatoAPI: any;
80
- public readonly webhookEventHandler: { [x: string]: (context: any) => void } = {};
81
- public readonly bleEventHandler: { [x: string]: (context: any) => void } = {};
69
+ // Platform properties
70
+ public accessories: PlatformAccessory[] = []
71
+ public readonly api: API
72
+ public readonly log: Logging
73
+
74
+ // Configuration properties
75
+ version!: string
76
+ Logging?: string
77
+ debugMode!: boolean
78
+ maxRetries!: number
79
+ delayBetweenRetries!: number
80
+ platformConfig!: SwitchBotPlatformConfig['options']
81
+ platformLogging!: SwitchBotPlatformConfig['logging']
82
+ config!: SwitchBotPlatformConfig
83
+
84
+ // MQTT and Webhook properties
85
+ mqttClient: MqttClient | null = null
86
+ webhookEventListener: Server | null = null
87
+
88
+ // External APIs
89
+ public readonly eve: any
90
+ public readonly fakegatoAPI: any
91
+
92
+ // Event Handlers
93
+ public readonly webhookEventHandler: { [x: string]: (context: any) => void } = {}
94
+ public readonly bleEventHandler: { [x: string]: (context: any) => void } = {}
82
95
 
83
96
  constructor(
84
97
  log: Logging,
85
98
  config: SwitchBotPlatformConfig,
86
99
  api: API,
87
100
  ) {
88
- // initialize
89
- this.accessories = [];
90
- this.api = api;
91
- this.log = log;
101
+ this.api = api
102
+ this.log = log
92
103
 
93
104
  // only load if configured
94
105
  if (!config) {
95
- return;
106
+ return
96
107
  }
97
108
 
98
109
  // Plugin options into our config variables.
@@ -101,130 +112,138 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
101
112
  name: config.name,
102
113
  credentials: config.credentials as object,
103
114
  options: config.options as object,
104
- };
115
+ }
105
116
 
106
117
  // Plugin Configuration
107
- this.getPlatformLogSettings();
108
- this.getPlatformConfigSettings();
109
- this.getVersion();
118
+ this.getPlatformLogSettings()
119
+ this.getPlatformConfigSettings()
120
+ this.getVersion()
110
121
 
111
122
  // Finish initializing the platform
112
- this.debugLog(`Finished initializing platform: ${config.name}`);
123
+ this.debugLog(`Finished initializing platform: ${config.name}`)
113
124
 
114
125
  // verify the config
115
126
  try {
116
- this.verifyConfig();
117
- this.debugLog('Config OK');
127
+ this.verifyConfig()
128
+ this.debugLog('Config OK')
118
129
  } catch (e: any) {
119
- this.errorLog(`Verify Config, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
120
- this.debugErrorLog(`Verify Config, Error: ${e}`);
121
- return;
130
+ this.errorLog(`Verify Config, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
131
+ this.debugErrorLog(`Verify Config, Error: ${e}`)
132
+ return
122
133
  }
123
134
 
124
135
  // import fakegato-history module and EVE characteristics
125
- this.fakegatoAPI = fakegato(api);
126
- this.eve = new EveHomeKitTypes(api);
136
+ this.fakegatoAPI = fakegato(api)
137
+ this.eve = new EveHomeKitTypes(api)
127
138
 
128
139
  // When this event is fired it means Homebridge has restored all cached accessories from disk.
129
140
  // Dynamic Platform plugins should only register new accessories after this event was fired,
130
141
  // in order to ensure they weren't added to homebridge already. This event can also be used
131
142
  // to start discovery of new accessories.
132
143
  this.api.on('didFinishLaunching', async () => {
133
- this.debugLog('Executed didFinishLaunching callback');
144
+ await this.debugLog('Executed didFinishLaunching callback')
134
145
  // run the method to discover / register your devices as accessories
135
146
  try {
136
147
  if (this.config.credentials?.openToken && !this.config.credentials.token) {
137
- await this.updateToken();
148
+ await this.updateToken()
138
149
  } else if (this.config.credentials?.token && !this.config.credentials?.secret) {
139
-
140
- this.errorLog('"secret" config is not populated, you must populate then please restart Homebridge.');
150
+ await this.errorLog('"secret" config is not populated, you must populate then please restart Homebridge.')
141
151
  } else {
142
- this.discoverDevices();
152
+ await this.discoverDevices()
143
153
  }
144
154
  } catch (e: any) {
145
- this.errorLog(`Failed to Discover, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
146
- await this.debugErrorLog(`Failed to Discover, Error: ${e}`);
155
+ await this.errorLog(`Failed to Discover, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
156
+ await this.debugErrorLog(`Failed to Discover, Error: ${e}`)
147
157
  }
148
- });
158
+ })
149
159
 
150
- this.setupMqtt();
151
- this.setupwebhook();
152
- this.setupBlE();
160
+ try {
161
+ this.setupMqtt()
162
+ } catch (e: any) {
163
+ this.errorLog(`Setup MQTT, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
164
+ }
165
+ try {
166
+ this.setupwebhook()
167
+ } catch (e: any) {
168
+ this.errorLog(`Setup Webhook, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
169
+ }
170
+ try {
171
+ this.setupBlE()
172
+ } catch (e: any) {
173
+ this.errorLog(`Setup Platform BLE, Error Message: ${e.message}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug')
174
+ }
153
175
  }
154
176
 
155
177
  async setupMqtt(): Promise<void> {
156
178
  if (this.config.options?.mqttURL) {
157
179
  try {
158
- const { connectAsync } = asyncmqtt;
159
- this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {});
160
- await this.debugLog('MQTT connection has been established successfully.');
180
+ const { connectAsync } = asyncmqtt
181
+ this.mqttClient = await connectAsync(this.config.options?.mqttURL, this.config.options.mqttOptions || {})
182
+ await this.debugLog('MQTT connection has been established successfully.')
161
183
  this.mqttClient.on('error', async (e: Error) => {
162
- await this.errorLog(`Failed to publish MQTT messages. ${e}`);
163
- });
184
+ await this.errorLog(`Failed to publish MQTT messages. ${e}`)
185
+ })
164
186
  if (!this.config.options?.webhookURL) {
165
187
  // receive webhook events via MQTT
166
- await this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`);
167
- this.mqttClient.subscribe('homebridge-switchbot/webhook/+');
188
+ await this.infoLog(`Webhook is configured to be received through ${this.config.options.mqttURL}/homebridge-switchbot/webhook.`)
189
+ this.mqttClient.subscribe('homebridge-switchbot/webhook/+')
168
190
  this.mqttClient.on('message', async (topic: string, message) => {
169
191
  try {
170
- await await this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`);
171
- const context = JSON.parse(message.toString());
172
- this.webhookEventHandler[context.deviceMac]?.(context);
192
+ await this.debugLog(`Received Webhook via MQTT: ${topic}=${message}`)
193
+ const context = JSON.parse(message.toString())
194
+ this.webhookEventHandler[context.deviceMac]?.(context)
173
195
  } catch (e: any) {
174
- await this.errorLog(`Failed to handle webhook event. Error:${e}`);
196
+ await this.errorLog(`Failed to handle webhook event. Error:${e}`)
175
197
  }
176
- });
198
+ })
177
199
  }
178
200
  } catch (e) {
179
- this.mqttClient = null;
180
- await this.errorLog(`Failed to establish MQTT connection. ${e}`);
201
+ this.mqttClient = null
202
+ await this.errorLog(`Failed to establish MQTT connection. ${e}`)
181
203
  }
182
204
  }
183
205
  }
184
206
 
185
207
  async setupwebhook() {
186
- //webhook configuration
208
+ // webhook configuration
187
209
  if (this.config.options?.webhookURL) {
188
- const url = this.config.options?.webhookURL;
210
+ const url = this.config.options?.webhookURL
189
211
 
190
212
  try {
191
- const xurl = new URL(url);
192
- const port = Number(xurl.port);
193
- const path = xurl.pathname;
213
+ const xurl = new URL(url)
214
+ const port = Number(xurl.port)
215
+ const path = xurl.pathname
194
216
  this.webhookEventListener = createServer((request: IncomingMessage, response: ServerResponse) => {
195
217
  try {
196
218
  if (request.url === path && request.method === 'POST') {
197
219
  request.on('data', async (data) => {
198
220
  try {
199
- const body = JSON.parse(data);
200
- await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`);
221
+ const body = JSON.parse(data)
222
+ await this.debugLog(`Received Webhook: ${JSON.stringify(body)}`)
201
223
  if (this.config.options?.mqttURL) {
202
- const mac = body.context.deviceMac
203
- ?.toLowerCase()
204
- .match(/[\s\S]{1,2}/g)
205
- ?.join(':');
206
- const options = this.config.options?.mqttPubOptions || {};
207
- this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options);
224
+ const mac = body.context.deviceMac?.toLowerCase().match(/[\s\S]{1,2}/g)?.join(':')
225
+ const options = this.config.options?.mqttPubOptions || {}
226
+ this.mqttClient?.publish(`homebridge-switchbot/webhook/${mac}`, `${JSON.stringify(body.context)}`, options)
208
227
  }
209
- this.webhookEventHandler[body.context.deviceMac]?.(body.context);
228
+ this.webhookEventHandler[body.context.deviceMac]?.(body.context)
210
229
  } catch (e: any) {
211
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
230
+ await this.errorLog(`Failed to handle webhook event. Error:${e}`)
212
231
  }
213
- });
214
- response.writeHead(200, { 'Content-Type': 'text/plain' });
215
- response.end('OK');
232
+ })
233
+ response.writeHead(200, { 'Content-Type': 'text/plain' })
234
+ response.end('OK')
216
235
  }
217
236
  // else {
218
237
  // response.writeHead(403, {'Content-Type': 'text/plain'});
219
238
  // response.end(`NG`);
220
239
  // }
221
240
  } catch (e: any) {
222
- this.errorLog(`Failed to handle webhook event. Error:${e}`);
241
+ this.errorLog(`Failed to handle webhook event. Error:${e}`)
223
242
  }
224
- }).listen(port ? port : 80);
243
+ }).listen(port || 80)
225
244
  } catch (e: any) {
226
- this.errorLog(`Failed to create webhook listener. Error:${e.message}`);
227
- return;
245
+ await this.errorLog(`Failed to create webhook listener. Error:${e.message}`)
246
+ return
228
247
  }
229
248
 
230
249
  try {
@@ -232,42 +251,39 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
232
251
  method: 'POST',
233
252
  headers: this.generateHeaders(),
234
253
  body: JSON.stringify({
235
- 'action': 'setupWebhook',
236
- 'url': url,
237
- 'deviceList': 'ALL',
254
+ action: 'setupWebhook',
255
+ url,
256
+ deviceList: 'ALL',
238
257
  }),
239
- });
240
- const response: any = await body.json();
241
- await this.debugLog(`setupWebhook: url:${url}`);
242
- await this.debugLog(`setupWebhook: body:${JSON.stringify(response)}`);
243
- await this.debugLog(`setupWebhook: statusCode:${statusCode}`);
258
+ })
259
+ const response: any = await body.json()
260
+ await this.debugLog(`setupWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
244
261
  if (statusCode !== 200 || response?.statusCode !== 100) {
245
- this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} `
246
- + `message:${response?.message}`);
262
+ await this.errorLog(`Failed to configure webhook. Existing webhook well be overridden. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
247
263
  }
248
264
  } catch (e: any) {
249
- this.errorLog(`Failed to configure webhook. Error: ${e.message}`);
265
+ await this.errorLog(`Failed to configure webhook. Error: ${e.message}`)
250
266
  }
251
267
 
252
268
  try {
253
269
  const { body, statusCode } = await request(updateWebhook, {
254
- method: 'POST', headers: this.generateHeaders(), body: JSON.stringify({
255
- 'action': 'updateWebhook',
256
- 'config': {
257
- 'url': url,
258
- 'enable': true,
270
+ method: 'POST',
271
+ headers: this.generateHeaders(),
272
+ body: JSON.stringify({
273
+ action: 'updateWebhook',
274
+ config: {
275
+ url,
276
+ enable: true,
259
277
  },
260
278
  }),
261
- });
262
- const response: any = await body.json();
263
- await this.debugLog(`updateWebhook: url:${url}`);
264
- await this.debugLog(`updateWebhook: body:${JSON.stringify(response)}`);
265
- await this.debugLog(`updateWebhook: statusCode:${statusCode}`);
279
+ })
280
+ const response: any = await body.json()
281
+ await this.debugLog(`updateWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
266
282
  if (statusCode !== 200 || response?.statusCode !== 100) {
267
- this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
283
+ await this.errorLog(`Failed to update webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
268
284
  }
269
285
  } catch (e: any) {
270
- this.errorLog(`Failed to update webhook. Error:${e.message}`);
286
+ await this.errorLog(`Failed to update webhook. Error:${e.message}`)
271
287
  }
272
288
 
273
289
  try {
@@ -275,19 +291,19 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
275
291
  method: 'POST',
276
292
  headers: this.generateHeaders(),
277
293
  body: JSON.stringify({
278
- 'action': 'queryUrl',
294
+ action: 'queryUrl',
279
295
  }),
280
- });
281
- const response: any = await body.json();
282
- await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`);
283
- await this.debugLog(`queryWebhook: statusCode:${statusCode}`);
296
+ })
297
+ const response: any = await body.json()
298
+ await this.debugLog(`queryWebhook: body:${JSON.stringify(response)}`)
299
+ await this.debugLog(`queryWebhook: statusCode:${statusCode}`)
284
300
  if (statusCode !== 200 || response?.statusCode !== 100) {
285
- this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
301
+ await this.errorLog(`Failed to query webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
286
302
  } else {
287
- this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`);
303
+ await this.infoLog(`Listening webhook on ${response?.body?.urls[0]}`)
288
304
  }
289
305
  } catch (e: any) {
290
- this.errorLog(`Failed to query webhook. Error:${e}`);
306
+ await this.errorLog(`Failed to query webhook. Error:${e}`)
291
307
  }
292
308
 
293
309
  this.api.on('shutdown', async () => {
@@ -296,58 +312,58 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
296
312
  method: 'POST',
297
313
  headers: this.generateHeaders(),
298
314
  body: JSON.stringify({
299
- 'action': 'deleteWebhook',
300
- 'url': url,
315
+ action: 'deleteWebhook',
316
+ url,
301
317
  }),
302
- });
303
- const response: any = await body.json();
304
- await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`);
318
+ })
319
+ const response: any = await body.json()
320
+ await this.debugLog(`deleteWebhook: url:${url}, body:${JSON.stringify(response)}, statusCode:${statusCode}`)
305
321
  if (statusCode !== 200 || response?.statusCode !== 100) {
306
- this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`);
322
+ await this.errorLog(`Failed to delete webhook. HTTP:${statusCode} API:${response?.statusCode} message:${response?.message}`)
307
323
  } else {
308
- this.infoLog('Unregistered webhook to close listening.');
324
+ await this.infoLog('Unregistered webhook to close listening.')
309
325
  }
310
326
  } catch (e: any) {
311
- this.errorLog(`Failed to delete webhook. Error:${e.message}`);
327
+ await this.errorLog(`Failed to delete webhook. Error:${e.message}`)
312
328
  }
313
- });
329
+ })
314
330
  }
315
331
  }
316
332
 
317
333
  async setupBlE() {
318
334
  if (this.config.options?.BLE) {
319
- await this.debugLog('setupBLE');
320
- const SwitchBot = (await import('node-switchbot')).SwitchBot;
321
- const switchbot = new SwitchBot();
335
+ await this.debugLog('setupBLE')
336
+ const SwitchBot = (await import('node-switchbot')).SwitchBot
337
+ const switchbot = new SwitchBot()
322
338
  if (switchbot === undefined) {
323
- await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`);
339
+ await this.errorLog(`wasn't able to establish BLE Connection, node-switchbot: ${switchbot}`)
324
340
  } else {
325
341
  // Start to monitor advertisement packets
326
342
  (async () => {
327
343
  // Start to monitor advertisement packets
328
- await this.debugLog('Scanning for BLE SwitchBot devices...');
329
- await switchbot.startScan();
344
+ await this.debugLog('Scanning for BLE SwitchBot devices...')
345
+ await switchbot.startScan()
330
346
  // Set an event handler to monitor advertisement packets
331
347
  switchbot.onadvertisement = async (ad: any) => {
332
348
  try {
333
- this.bleEventHandler[ad.address]?.(ad.serviceData);
349
+ this.bleEventHandler[ad.address]?.(ad.serviceData)
334
350
  } catch (e: any) {
335
- await this.errorLog(`Failed to handle BLE event. Error:${e}`);
351
+ await this.errorLog(`Failed to handle BLE event. Error:${e}`)
336
352
  }
337
- };
338
- })();
353
+ }
354
+ })()
339
355
 
340
356
  this.api.on('shutdown', async () => {
341
357
  try {
342
- switchbot.stopScan();
343
- this.infoLog('Stopped BLE scanning to close listening.');
358
+ switchbot.stopScan()
359
+ await this.infoLog('Stopped BLE scanning to close listening.')
344
360
  } catch (e: any) {
345
- this.errorLog(`Failed to stop Platform BLE scanning. Error:${e.message}`);
361
+ await this.errorLog(`Failed to stop Platform BLE scanning. Error:${e.message}`)
346
362
  }
347
- });
363
+ })
348
364
  }
349
365
  } else {
350
- await this.debugLog('Platform BLE is not enabled');
366
+ await this.debugLog('Platform BLE is not enabled')
351
367
  }
352
368
  }
353
369
 
@@ -356,32 +372,33 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
356
372
  * It should be used to setup event handlers for characteristics and update respective values.
357
373
  */
358
374
  async configureAccessory(accessory: PlatformAccessory) {
359
- await this.debugLog(`Loading accessory from cache: ${accessory.displayName}`);
375
+ const { displayName } = accessory
376
+ await this.debugLog(`Loading accessory from cache: ${displayName}`)
360
377
 
361
378
  // add the restored accessory to the accessories cache so we can track if it has already been registered
362
- this.accessories.push(accessory);
379
+ this.accessories.push(accessory)
363
380
  }
364
381
 
365
382
  /**
366
383
  * Verify the config passed to the plugin is valid
367
384
  */
368
385
  async verifyConfig() {
369
- this.debugLog('Verifying Config');
370
- this.config = this.config || {};
371
- this.config.options = this.config.options || {};
386
+ await this.debugLog('Verifying Config')
387
+ this.config = this.config || {}
388
+ this.config.options = this.config.options || {}
372
389
 
373
- const platformConfig = {};
390
+ const platformConfig: options = {}
374
391
  if (this.config.options.logging) {
375
- platformConfig['logging'] = this.config.options.logging;
392
+ platformConfig.logging = this.config.options.logging
376
393
  }
377
394
  if (this.config.options.logging && this.config.options.refreshRate) {
378
- platformConfig['refreshRate'] = this.config.options.refreshRate;
395
+ platformConfig.refreshRate = this.config.options.refreshRate
379
396
  }
380
397
  if (this.config.options.logging && this.config.options.pushRate) {
381
- platformConfig['pushRate'] = this.config.options.pushRate;
398
+ platformConfig.pushRate = this.config.options.pushRate
382
399
  }
383
400
  if (Object.entries(platformConfig).length !== 0) {
384
- await this.debugWarnLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
401
+ await this.debugWarnLog(`Platform Config: ${JSON.stringify(platformConfig)}`)
385
402
  }
386
403
 
387
404
  if (this.config.options) {
@@ -390,10 +407,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
390
407
  for (const deviceConfig of this.config.options.devices) {
391
408
  if (!deviceConfig.hide_device) {
392
409
  if (!deviceConfig.deviceId) {
393
- throw new Error('The devices config section is missing the *Device ID* in the config. Please check your config.');
410
+ throw new Error('The devices config section is missing the *Device ID* in the config. Please check your config.')
394
411
  }
395
412
  if (!deviceConfig.configDeviceType && deviceConfig.connectionType) {
396
- throw new Error('The devices config section is missing the *Device Type* in the config. Please check your config.');
413
+ throw new Error('The devices config section is missing the *Device Type* in the config. Please check your config.')
397
414
  }
398
415
  }
399
416
  }
@@ -404,10 +421,10 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
404
421
  for (const irDeviceConfig of this.config.options.irdevices) {
405
422
  if (!irDeviceConfig.hide_device) {
406
423
  if (!irDeviceConfig.deviceId) {
407
- this.errorLog('The devices config section is missing the *Device ID* in the config. Please check your config.');
424
+ await this.errorLog('The devices config section is missing the *Device ID* in the config. Please check your config.')
408
425
  }
409
426
  if (!irDeviceConfig.deviceId && !irDeviceConfig.configRemoteType) {
410
- this.errorLog('The devices config section is missing the *Device Type* in the config. Please check your config.');
427
+ await this.errorLog('The devices config section is missing the *Device Type* in the config. Please check your config.')
411
428
  }
412
429
  }
413
430
  }
@@ -415,47 +432,47 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
415
432
  }
416
433
 
417
434
  if (this.config.options!.refreshRate! < 5) {
418
- throw new Error('Refresh Rate must be above 5 (5 seconds).');
435
+ throw new Error('Refresh Rate must be above 5 (5 seconds).')
419
436
  }
420
437
 
421
438
  if (!this.config.options.refreshRate) {
422
439
  // default 120 seconds (2 minutes)
423
- this.config.options!.refreshRate! = 120;
424
- this.debugWarnLog('Using Default Refresh Rate (2 minutes).');
440
+ this.config.options!.refreshRate! = 120
441
+ await this.debugWarnLog('Using Default Refresh Rate (2 minutes).')
425
442
  }
426
443
 
427
444
  if (!this.config.options.pushRate) {
428
445
  // default 100 milliseconds
429
- this.config.options!.pushRate! = 0.1;
430
- this.debugWarnLog('Using Default Push Rate.');
446
+ this.config.options!.pushRate! = 0.1
447
+ await this.debugWarnLog('Using Default Push Rate.')
431
448
  }
432
449
 
433
450
  if (!this.config.options.maxRetries) {
434
- this.config.options.maxRetries = 5;
435
- this.debugWarnLog('Using Default Max Retries.');
451
+ this.config.options.maxRetries = 5
452
+ await this.debugWarnLog('Using Default Max Retries.')
436
453
  } else {
437
- this.maxRetries = this.config.options.maxRetries;
454
+ this.maxRetries = this.config.options.maxRetries
438
455
  }
439
456
 
440
457
  if (!this.config.options.delayBetweenRetries) {
441
458
  // default 3 seconds
442
- this.config.options!.delayBetweenRetries! = 3000;
443
- this.debugWarnLog('Using Default Delay Between Retries.');
459
+ this.config.options!.delayBetweenRetries! = 3000
460
+ await this.debugWarnLog('Using Default Delay Between Retries.')
444
461
  } else {
445
- this.delayBetweenRetries = this.config.options.delayBetweenRetries * 1000;
462
+ this.delayBetweenRetries = this.config.options.delayBetweenRetries * 1000
446
463
  }
447
464
 
448
465
  if (!this.config.credentials && !this.config.options) {
449
- this.debugWarnLog('Missing Credentials');
466
+ await this.debugWarnLog('Missing Credentials')
450
467
  } else if (this.config.credentials && !this.config.credentials.notice) {
451
468
  if (!this.config.credentials?.token) {
452
- this.debugErrorLog('Missing token');
453
- this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
469
+ await this.debugErrorLog('Missing token')
470
+ await this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work')
454
471
  }
455
472
  if (this.config.credentials?.token) {
456
473
  if (!this.config.credentials?.secret) {
457
- this.debugErrorLog('Missing secret');
458
- this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work');
474
+ await this.debugErrorLog('Missing secret')
475
+ await this.debugWarnLog('Cloud Enabled SwitchBot Devices & IR Devices will not work')
459
476
  }
460
477
  }
461
478
  }
@@ -464,1007 +481,892 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
464
481
  /**
465
482
  * The openToken was old config.
466
483
  * This method saves the openToken as the token in the config.json file
467
- * @param this.config.credentials.openToken
468
484
  */
469
485
  async updateToken() {
470
486
  try {
471
487
  // check the new token was provided
472
488
  if (!this.config.credentials?.openToken) {
473
- throw new Error('New token not provided');
489
+ throw new Error('New token not provided')
474
490
  }
475
491
 
476
492
  // load in the current config
477
- const currentConfig = JSON.parse(readFileSync(this.api.user.configPath(), 'utf8'));
493
+ const currentConfig = JSON.parse(readFileSync(this.api.user.configPath(), 'utf8'))
478
494
 
479
495
  // check the platforms section is an array before we do array things on it
480
496
  if (!Array.isArray(currentConfig.platforms)) {
481
- throw new Error('Cannot find platforms array in config');
497
+ throw new TypeError('Cannot find platforms array in config')
482
498
  }
483
499
 
484
500
  // find this plugins current config
485
- const pluginConfig = currentConfig.platforms.find((x: { platform: string }) => x.platform === PLATFORM_NAME);
501
+ const pluginConfig = currentConfig.platforms.find((x: { platform: string }) => x.platform === PLATFORM_NAME)
486
502
 
487
503
  if (!pluginConfig) {
488
- throw new Error(`Cannot find config for ${PLATFORM_NAME} in platforms array`);
504
+ throw new Error(`Cannot find config for ${PLATFORM_NAME} in platforms array`)
489
505
  }
490
506
 
491
507
  // check the .credentials is an object before doing object things with it
492
508
  if (typeof pluginConfig.credentials !== 'object') {
493
- throw new Error('pluginConfig.credentials is not an object');
509
+ throw new TypeError('pluginConfig.credentials is not an object')
494
510
  }
495
511
  // Move openToken to token
496
512
  if (!this.config.credentials.secret) {
497
- this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken,'
498
- + ' "openToken" cconfig has been moved to the "token" config');
499
- this.errorLog('"secret" config is not populated, you must populate then please restart Homebridge.');
513
+ await this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken, "openToken" cconfig has been moved to the "token" config')
514
+ this.errorLog('"secret" config is not populated, you must populate then please restart Homebridge.')
500
515
  } else {
501
- this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken, '
502
- + '"openToken" config has been moved to the "token" config, please restart Homebridge.');
516
+ await this.warnLog('This plugin has been updated to use OpenAPI v1.1, config is set with openToken, "openToken" config has been moved to the "token" config, please restart Homebridge.')
503
517
  }
504
518
 
505
519
  // set the refresh token
506
- pluginConfig.credentials.token = this.config.credentials?.openToken;
520
+ pluginConfig.credentials.token = this.config.credentials?.openToken
507
521
  if (pluginConfig.credentials.token) {
508
- pluginConfig.credentials.openToken = undefined;
522
+ pluginConfig.credentials.openToken = undefined
509
523
  }
510
524
 
511
- await this.debugWarnLog(`token: ${pluginConfig.credentials.token}`);
525
+ await this.debugWarnLog(`token: ${pluginConfig.credentials.token}`)
512
526
 
513
527
  // save the config, ensuring we maintain pretty json
514
- writeFileSync(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4));
515
- this.verifyConfig();
528
+ writeFileSync(this.api.user.configPath(), JSON.stringify(currentConfig, null, 4))
529
+ await this.verifyConfig()
516
530
  } catch (e: any) {
517
- this.errorLog(`Update Token: ${e}`);
531
+ await this.errorLog(`Update Token: ${e}`)
518
532
  }
519
533
  }
520
534
 
521
535
  generateHeaders = () => {
522
- const t = `${Date.now()}`;
523
- const nonce = randomUUID();
524
- const data = this.config.credentials?.token + t + nonce;
536
+ const t = `${Date.now()}`
537
+ const nonce = randomUUID()
538
+ const data = this.config.credentials?.token + t + nonce
525
539
  const signTerm = crypto
526
540
  .createHmac('sha256', this.config.credentials?.secret)
527
541
  .update(Buffer.from(data, 'utf-8'))
528
- .digest();
529
- const sign = signTerm.toString('base64');
542
+ .digest()
543
+ const sign = signTerm.toString('base64')
530
544
 
531
545
  return {
532
- Authorization: this.config.credentials?.token,
533
- sign: sign,
534
- nonce: nonce,
535
- t: t,
546
+ 'Authorization': this.config.credentials?.token,
547
+ 'sign': sign,
548
+ 'nonce': nonce,
549
+ 't': t,
536
550
  'Content-Type': 'application/json',
537
- };
538
- };
551
+ }
552
+ }
539
553
 
540
- /**
541
- * this method discovers devices
542
- *
543
- const t = `${Date.now()}`;
544
- const nonce = 'requestID';
545
- const data = this.config.credentials?.token + t + nonce;
546
- const signTerm = crypto.createHmac('sha256', this.config.credentials?.secret).update(Buffer.from(data, 'utf-8')).digest();
547
- const sign = signTerm.toString('base64');
548
- */
549
554
  async discoverDevices() {
550
- if (this.config.credentials?.token) {
551
- let retryCount = 0;
552
- const maxRetries = this.maxRetries; // Maximum number of retries
553
- const delayBetweenRetries = this.delayBetweenRetries; // Delay between retries in milliseconds
554
- await this.debugWarnLog(`Retry Count: ${retryCount}`);
555
- await this.debugWarnLog(`Max Retries: ${maxRetries}`);
556
- await this.debugWarnLog(`Delay Between Retries: ${delayBetweenRetries}`);
557
- while (retryCount < maxRetries) {
558
- try {
559
- const { body, statusCode } = await request(Devices, {
560
- headers: this.generateHeaders(),
561
- });
562
- await this.debugWarnLog(`statusCode: ${statusCode}`);
563
- const devicesAPI: any = await body.json();
564
- await this.debugWarnLog(`devicesAPI: ${JSON.stringify(devicesAPI)}`);
565
- await this.debugWarnLog(`devicesAPI Body: ${JSON.stringify(devicesAPI.body)}`);
566
- await this.debugWarnLog(`devicesAPI StatusCode: ${devicesAPI.statusCode}`);
567
- if ((statusCode === 200 || statusCode === 100) && (devicesAPI.statusCode === 200 || devicesAPI.statusCode === 100)) {
568
- await this.debugErrorLog(`statusCode: ${statusCode} & devicesAPI StatusCode: ${devicesAPI.statusCode}`);
569
- // SwitchBot Devices
570
- const deviceLists = devicesAPI.body.deviceList;
571
- await this.debugWarnLog(`DeviceLists: ${JSON.stringify(deviceLists)}`);
572
- await this.debugWarnLog(`DeviceLists Length: ${deviceLists.length}`);
573
- if (!this.config.options?.devices) {
574
- await this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
575
- if (deviceLists.length === 0) {
576
- await this.debugLog(`SwitchBot API Has No Devices With Cloud Services Enabled: ${JSON.stringify(devicesAPI.body)}`);
577
- } else {
578
- const devices = deviceLists.map((v: any) => v);
579
- for (const device of devices) {
580
- if (device.deviceType) {
581
- if (device.configDeviceName) {
582
- device.deviceName = device.configDeviceName;
583
- }
584
- this.createDevice(device);
585
- }
586
- }
587
- }
588
- } else if (this.config.credentials?.token && this.config.options.devices) {
589
- await this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`);
590
- if (deviceLists.length === 0) {
591
- await this.debugLog(`SwitchBot API Has No Devices With Cloud Services Enabled: ${JSON.stringify(devicesAPI.body)}`);
592
- } else {
593
- const deviceConfigs = this.config.options?.devices;
594
-
595
- const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
596
- a1.map((itm: { deviceId: string }) => ({
597
- ...a2.find(
598
- (item: { deviceId: string }) =>
599
- item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
600
- ),
601
- ...itm,
602
- }));
603
-
604
- const devices = mergeBydeviceId(deviceLists, deviceConfigs);
605
- await this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`);
606
- for (const device of devices) {
607
- if (!device.deviceType) {
608
- device.deviceType = device.configDeviceType;
609
- this.errorLog(`API has displaying no deviceType: ${device.deviceType}, So using configDeviceType: ${device.configDeviceType}`);
610
- }
611
- if (device.deviceType) {
612
- if (device.configDeviceName) {
613
- device.deviceName = device.configDeviceName;
614
- }
615
- this.createDevice(device);
616
- }
617
- }
618
- }
619
- } else {
620
- this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
621
- }
622
- if (devicesAPI.body.deviceList.length !== 0) {
623
- this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
624
- } else {
625
- await this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
626
- }
555
+ if (!this.config.credentials?.token) {
556
+ return this.handleManualConfig()
557
+ }
627
558
 
628
- // IR Devices
629
- const irDeviceLists = devicesAPI.body.infraredRemoteList;
630
- if (!this.config.options?.irdevices) {
631
- await this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
632
- const devices = irDeviceLists.map((v: any) => v);
633
- for (const device of devices) {
634
- if (device.remoteType) {
635
- this.createIRDevice(device);
636
- }
637
- }
638
- } else {
639
- await this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`);
640
- const irDeviceConfig = this.config.options?.irdevices;
641
-
642
- const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
643
- a1.map((itm: { deviceId: string }) => ({
644
- ...a2.find(
645
- (item: { deviceId: string }) =>
646
- item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
647
- ),
648
- ...itm,
649
- }));
650
-
651
- const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
652
- await this.debugLog(`IR Devices: ${JSON.stringify(devices)}`);
653
- for (const device of devices) {
654
- this.createIRDevice(device);
655
- }
656
- }
657
- if (devicesAPI.body.infraredRemoteList.length !== 0) {
658
- this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
659
- } else {
660
- await this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
661
- }
662
- break;
663
- } else {
664
- this.statusCode(statusCode);
665
- this.statusCode(devicesAPI.statusCode);
666
- if (statusCode === 500) {
667
- retryCount++;
668
- this.infoLog(`statusCode: ${statusCode} Attempt ${retryCount} of ${maxRetries}`);
669
- await sleep(delayBetweenRetries);
559
+ let retryCount = 0
560
+ const maxRetries = this.maxRetries
561
+ const delayBetweenRetries = this.delayBetweenRetries
562
+
563
+ await this.debugWarnLog(`Retry Count: ${retryCount}`)
564
+ await this.debugWarnLog(`Max Retries: ${maxRetries}`)
565
+ await this.debugWarnLog(`Delay Between Retries: ${delayBetweenRetries}`)
566
+
567
+ while (retryCount < maxRetries) {
568
+ try {
569
+ const { body, statusCode } = await request(Devices, { headers: this.generateHeaders() })
570
+ await this.debugWarnLog(`statusCode: ${statusCode}`)
571
+ const devicesAPI: any = await body.json()
572
+ await this.debugWarnLog(`devicesAPI: ${JSON.stringify(devicesAPI)}`)
573
+
574
+ if (this.isSuccessfulResponse(statusCode, devicesAPI.statusCode)) {
575
+ await this.handleDevices(devicesAPI.body.deviceList)
576
+ await this.handleIRDevices(devicesAPI.body.infraredRemoteList)
577
+ break
578
+ } else {
579
+ await this.handleErrorResponse(statusCode, devicesAPI.statusCode, retryCount, maxRetries, delayBetweenRetries)
580
+ retryCount++
581
+ }
582
+ } catch (e: any) {
583
+ retryCount++
584
+ await this.debugErrorLog(`Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`)
585
+ await this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`)
586
+ }
587
+ }
588
+ }
589
+
590
+ private async handleManualConfig() {
591
+ if (this.config.options?.devices) {
592
+ await this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`)
593
+ const devices = this.config.options.devices.map((v: any) => v)
594
+ for (const device of devices) {
595
+ device.deviceType = device.configDeviceType
596
+ device.deviceName = device.configDeviceName
597
+ if (device.deviceType) {
598
+ await this.createDevice(device)
599
+ }
600
+ }
601
+ } else {
602
+ await this.errorLog('Neither SwitchBot Token or Device Config are set.')
603
+ }
604
+ }
605
+
606
+ private isSuccessfulResponse(statusCode: number, apiStatusCode: number): boolean {
607
+ return (statusCode === 200 || statusCode === 100) && (apiStatusCode === 200 || apiStatusCode === 100)
608
+ }
609
+
610
+ private async handleDevices(deviceLists: any[]) {
611
+ if (!this.config.options?.devices) {
612
+ await this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`)
613
+ if (deviceLists.length === 0) {
614
+ await this.debugLog('SwitchBot API Has No Devices With Cloud Services Enabled')
615
+ } else {
616
+ for (const device of deviceLists) {
617
+ if (device.deviceType) {
618
+ if (device.configDeviceName) {
619
+ device.deviceName = device.configDeviceName
670
620
  }
621
+ await this.createDevice(device)
671
622
  }
672
- } catch (e: any) {
673
- retryCount++;
674
- this.debugErrorLog(
675
- `Failed to Discover Devices, Error Message: ${JSON.stringify(e.message)}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
676
- await this.debugErrorLog(`Failed to Discover Devices, Error: ${e}`);
677
623
  }
678
624
  }
679
- } else if (!this.config.credentials?.token && this.config.options?.devices) {
680
- await this.debugLog(`SwitchBot Device Manual Config Set: ${JSON.stringify(this.config.options?.devices)}`);
681
- const deviceConfigs = this.config.options?.devices;
682
- const devices = deviceConfigs.map((v: any) => v);
625
+ } else {
626
+ await this.debugLog(`SwitchBot Device Config Set: ${JSON.stringify(this.config.options?.devices)}`)
627
+ const devices = this.mergeByDeviceId(deviceLists, this.config.options.devices)
628
+ await this.debugLog(`SwitchBot Devices: ${JSON.stringify(devices)}`)
683
629
  for (const device of devices) {
684
- device.deviceType = device.configDeviceType;
685
- device.deviceName = device.configDeviceName;
630
+ if (!device.deviceType && device.configDeviceType) {
631
+ device.deviceType = device.configDeviceType
632
+ await this.warnLog(`API is displaying no deviceType: ${device.deviceType}, So using configDeviceType: ${device.configDeviceType}`)
633
+ } else if (!device.deviceType && !device.configDeviceName) {
634
+ await this.errorLog('No deviceType or configDeviceType for device. No device will be created.')
635
+ }
686
636
  if (device.deviceType) {
687
- this.createDevice(device);
637
+ if (device.configDeviceName) {
638
+ device.deviceName = device.configDeviceName
639
+ }
640
+ await this.createDevice(device)
641
+ }
642
+ }
643
+ }
644
+ }
645
+
646
+ private async handleIRDevices(irDeviceLists: any[]) {
647
+ if (!this.config.options?.irdevices) {
648
+ await this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`)
649
+ for (const device of irDeviceLists) {
650
+ if (device.remoteType) {
651
+ await this.createIRDevice(device)
688
652
  }
689
653
  }
690
654
  } else {
691
- this.errorLog('Neither SwitchBot Token or Device Config are not set.');
655
+ await this.debugLog(`IR Device Config Set: ${JSON.stringify(this.config.options?.irdevices)}`)
656
+ const devices = this.mergeByDeviceId(irDeviceLists, this.config.options.irdevices)
657
+ await this.debugLog(`IR Devices: ${JSON.stringify(devices)}`)
658
+ for (const device of devices) {
659
+ await this.createIRDevice(device)
660
+ }
661
+ }
662
+ }
663
+
664
+ private mergeByDeviceId(a1: { deviceId: string }[], a2: any[]) {
665
+ const normalizeDeviceId = (deviceId: string) => deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '')
666
+ return a1.map((itm) => {
667
+ const matchingItem = a2.find(item => normalizeDeviceId(item.deviceId) === normalizeDeviceId(itm.deviceId))
668
+ return { ...matchingItem, ...itm }
669
+ })
670
+ }
671
+
672
+ private async handleErrorResponse(statusCode: number, apiStatusCode: number, retryCount: number, maxRetries: number, delayBetweenRetries: number) {
673
+ await this.statusCode(statusCode)
674
+ await this.statusCode(apiStatusCode)
675
+ if (statusCode === 500) {
676
+ this.infoLog(`statusCode: ${statusCode} Attempt ${retryCount + 1} of ${maxRetries}`)
677
+ await sleep(delayBetweenRetries)
692
678
  }
693
679
  }
694
680
 
695
681
  private async createDevice(device: device & devicesConfig) {
696
- switch (device.deviceType!) {
697
- case 'Humidifier':
698
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
699
- this.createHumidifier(device);
700
- break;
701
- case 'Hub Mini':
702
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
703
- break;
704
- case 'Hub Plus':
705
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
706
- break;
707
- case 'Hub 2':
708
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
709
- this.createHub2(device);
710
- break;
711
- case 'Bot':
712
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
713
- this.createBot(device);
714
- break;
715
- case 'Meter':
716
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
717
- this.createMeter(device);
718
- break;
719
- case 'MeterPlus':
720
- case 'Meter Plus (JP)':
721
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
722
- this.createMeterPlus(device);
723
- break;
724
- case 'WoIOSensor':
725
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
726
- this.createIOSensor(device);
727
- break;
728
- case 'Water Detector':
729
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
730
- this.createWaterDetector(device);
731
- break;
732
- case 'Motion Sensor':
733
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
734
- this.createMotion(device);
735
- break;
736
- case 'Contact Sensor':
737
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
738
- this.createContact(device);
739
- break;
740
- case 'Curtain':
741
- case 'Curtain3':
742
- await this.debugLog(`Discovered ${device.deviceType} ${device.deviceName}: ${device.deviceId}`);
743
- this.createCurtain(device);
744
- break;
745
- case 'Blind Tilt':
746
- await this.debugLog(`Discovered ${device.deviceType} ${device.deviceName}: ${device.deviceId}`);
747
- this.createBlindTilt(device);
748
- break;
749
- case 'Plug':
750
- case 'Plug Mini (US)':
751
- case 'Plug Mini (JP)':
752
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
753
- this.createPlug(device);
754
- break;
755
- case 'Smart Lock':
756
- case 'Smart Lock Pro':
757
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
758
- this.createLock(device);
759
- break;
760
- case 'Color Bulb':
761
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
762
- this.createColorBulb(device);
763
- break;
764
- case 'K10+':
765
- case 'WoSweeper':
766
- case 'WoSweeperMini':
767
- case 'Robot Vacuum Cleaner S1':
768
- case 'Robot Vacuum Cleaner S1 Plus':
769
- case 'Robot Vacuum Cleaner S10':
770
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
771
- this.createRobotVacuumCleaner(device);
772
- break;
773
- case 'Ceiling Light':
774
- case 'Ceiling Light Pro':
775
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
776
- this.createCeilingLight(device);
777
- break;
778
- case 'Strip Light':
779
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
780
- this.createStripLight(device);
781
- break;
782
- case 'Battery Circulator Fan':
783
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`);
784
- this.createFan(device);
785
- break;
786
- case 'Remote':
787
- case 'Indoor Cam':
788
- case 'remote with screen+':
789
- await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}, is currently not supported, device: ${JSON.stringify(device)}`);
790
- break;
791
- default:
792
- this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported.`,
793
- + `Submit Feature Requests Here: https://tinyurl.com/SwitchBotFeatureRequest, device: ${JSON.stringify(device)}`);
682
+ const deviceTypeHandlers: { [key: string]: (device: device & devicesConfig) => Promise<void> } = {
683
+ 'Humidifier': this.createHumidifier.bind(this),
684
+ 'Hub 2': this.createHub2.bind(this),
685
+ 'Bot': this.createBot.bind(this),
686
+ 'Meter': this.createMeter.bind(this),
687
+ 'MeterPlus': this.createMeterPlus.bind(this),
688
+ 'Meter Plus (JP)': this.createMeterPlus.bind(this),
689
+ 'WoIOSensor': this.createIOSensor.bind(this),
690
+ 'Water Detector': this.createWaterDetector.bind(this),
691
+ 'Motion Sensor': this.createMotion.bind(this),
692
+ 'Contact Sensor': this.createContact.bind(this),
693
+ 'Curtain': this.createCurtain.bind(this),
694
+ 'Curtain3': this.createCurtain.bind(this),
695
+ 'WoRollerShade': this.createCurtain.bind(this),
696
+ 'Roller Shade': this.createCurtain.bind(this),
697
+ 'Blind Tilt': this.createBlindTilt.bind(this),
698
+ 'Plug': this.createPlug.bind(this),
699
+ 'Plug Mini (US)': this.createPlug.bind(this),
700
+ 'Plug Mini (JP)': this.createPlug.bind(this),
701
+ 'Smart Lock': this.createLock.bind(this),
702
+ 'Smart Lock Pro': this.createLock.bind(this),
703
+ 'Color Bulb': this.createColorBulb.bind(this),
704
+ 'K10+': this.createRobotVacuumCleaner.bind(this),
705
+ 'WoSweeper': this.createRobotVacuumCleaner.bind(this),
706
+ 'WoSweeperMini': this.createRobotVacuumCleaner.bind(this),
707
+ 'Robot Vacuum Cleaner S1': this.createRobotVacuumCleaner.bind(this),
708
+ 'Robot Vacuum Cleaner S1 Plus': this.createRobotVacuumCleaner.bind(this),
709
+ 'Robot Vacuum Cleaner S10': this.createRobotVacuumCleaner.bind(this),
710
+ 'Ceiling Light': this.createCeilingLight.bind(this),
711
+ 'Ceiling Light Pro': this.createCeilingLight.bind(this),
712
+ 'Strip Light': this.createStripLight.bind(this),
713
+ 'Battery Circulator Fan': this.createFan.bind(this),
714
+ }
715
+
716
+ if (deviceTypeHandlers[device.deviceType!]) {
717
+ await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}`)
718
+ await deviceTypeHandlers[device.deviceType!](device)
719
+ } else if (['Hub Mini', 'Hub Plus', 'Remote', 'Indoor Cam', 'remote with screen'].includes(device.deviceType!)) {
720
+ await this.debugLog(`Discovered ${device.deviceType}: ${device.deviceId}, is currently not supported, device: ${JSON.stringify(device)}`)
721
+ } else {
722
+ await this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.deviceType}, is currently not supported. Submit Feature Requests Here: https://tinyurl.com/SwitchBotFeatureRequest, device: ${JSON.stringify(device)}`)
794
723
  }
795
724
  }
796
725
 
797
726
  private async createIRDevice(device: irdevice & devicesConfig) {
798
- if (device.connectionType === undefined) {
799
- device.connectionType = 'OpenAPI';
800
- }
801
- switch (device.remoteType) {
802
- case 'TV':
803
- case 'DIY TV':
804
- case 'Projector':
805
- case 'DIY Projector':
806
- case 'Set Top Box':
807
- case 'DIY Set Top Box':
808
- case 'IPTV':
809
- case 'DIY IPTV':
810
- case 'DVD':
811
- case 'DIY DVD':
812
- case 'Speaker':
813
- case 'DIY Speaker':
814
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
815
- if (device.external === undefined) {
816
- device.external = true;
817
- this.createTV(device);
818
- } else {
819
- this.createTV(device);
820
- }
821
- break;
822
- case 'Fan':
823
- case 'DIY Fan':
824
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
825
- this.createIRFan(device);
826
- break;
827
- case 'Air Conditioner':
828
- case 'DIY Air Conditioner':
829
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
830
- this.createAirConditioner(device);
831
- break;
832
- case 'Light':
833
- case 'DIY Light':
834
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
835
- this.createLight(device);
836
- break;
837
- case 'Air Purifier':
838
- case 'DIY Air Purifier':
839
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
840
- this.createAirPurifier(device);
841
- break;
842
- case 'Water Heater':
843
- case 'DIY Water Heater':
844
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
845
- this.createWaterHeater(device);
846
- break;
847
- case 'Vacuum Cleaner':
848
- case 'DIY Vacuum Cleaner':
849
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
850
- this.createVacuumCleaner(device);
851
- break;
852
- case 'Camera':
853
- case 'DIY Camera':
854
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
855
- this.createCamera(device);
856
- break;
857
- case 'Others':
858
- await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`);
859
- this.createOthers(device);
860
- break;
861
- default:
862
- await this.debugLog(`Unsupported Device: ${JSON.stringify(device)}`);
863
- this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.remoteType}, is currently not supported.`);
864
- this.warnLog('Submit Feature Requests Here: ' + 'https://tinyurl.com/SwitchBotFeatureRequest');
727
+ device.connectionType = device.connectionType ?? 'OpenAPI'
728
+ const deviceTypeHandlers: { [key: string]: (device: irdevice & devicesConfig) => Promise<void> } = {
729
+ 'TV': this.createTV.bind(this),
730
+ 'DIY TV': this.createTV.bind(this),
731
+ 'Projector': this.createTV.bind(this),
732
+ 'DIY Projector': this.createTV.bind(this),
733
+ 'Set Top Box': this.createTV.bind(this),
734
+ 'DIY Set Top Box': this.createTV.bind(this),
735
+ 'IPTV': this.createTV.bind(this),
736
+ 'DIY IPTV': this.createTV.bind(this),
737
+ 'DVD': this.createTV.bind(this),
738
+ 'DIY DVD': this.createTV.bind(this),
739
+ 'Speaker': this.createTV.bind(this),
740
+ 'DIY Speaker': this.createTV.bind(this),
741
+ 'Fan': this.createIRFan.bind(this),
742
+ 'DIY Fan': this.createIRFan.bind(this),
743
+ 'Air Conditioner': this.createAirConditioner.bind(this),
744
+ 'DIY Air Conditioner': this.createAirConditioner.bind(this),
745
+ 'Light': this.createLight.bind(this),
746
+ 'DIY Light': this.createLight.bind(this),
747
+ 'Air Purifier': this.createAirPurifier.bind(this),
748
+ 'DIY Air Purifier': this.createAirPurifier.bind(this),
749
+ 'Water Heater': this.createWaterHeater.bind(this),
750
+ 'DIY Water Heater': this.createWaterHeater.bind(this),
751
+ 'Vacuum Cleaner': this.createVacuumCleaner.bind(this),
752
+ 'DIY Vacuum Cleaner': this.createVacuumCleaner.bind(this),
753
+ 'Camera': this.createCamera.bind(this),
754
+ 'DIY Camera': this.createCamera.bind(this),
755
+ 'Others': this.createOthers.bind(this),
756
+ }
757
+
758
+ if (deviceTypeHandlers[device.remoteType!]) {
759
+ await this.debugLog(`Discovered ${device.remoteType}: ${device.deviceId}`)
760
+ if (device.remoteType.startsWith('DIY') && device.external === undefined) {
761
+ device.external = true
762
+ }
763
+ await deviceTypeHandlers[device.remoteType!](device)
764
+ } else {
765
+ await this.warnLog(`Device: ${device.deviceName} with Device Type: ${device.remoteType}, is currently not supported. Submit Feature Requests Here: https://tinyurl.com/SwitchBotFeatureRequest, device: ${JSON.stringify(device)}`)
865
766
  }
866
767
  }
867
768
 
868
769
  private async createHumidifier(device: device & devicesConfig) {
869
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
770
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
870
771
 
871
772
  // see if an accessory with the same uuid has already been registered and restored from
872
773
  // the cached devices we stored in the `configureAccessory` method above
873
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
774
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
874
775
 
875
776
  if (existingAccessory) {
876
777
  // the accessory already exists
877
778
  if (await this.registerDevice(device)) {
878
779
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
879
- existingAccessory.context.device = device;
880
- existingAccessory.context.deviceId = device.deviceId;
881
- existingAccessory.context.deviceType = device.deviceType;
882
- existingAccessory.context.model = SwitchBotModel.Humidifier;
780
+ existingAccessory.context.device = device
781
+ existingAccessory.context.deviceId = device.deviceId
782
+ existingAccessory.context.deviceType = device.deviceType
783
+ existingAccessory.context.model = SwitchBotModel.Humidifier
883
784
  existingAccessory.displayName = device.configDeviceName
884
785
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
885
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
886
- existingAccessory.context.connectionType = await this.connectionType(device);
887
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
888
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
889
- this.api.updatePlatformAccessories([existingAccessory]);
786
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
787
+ existingAccessory.context.connectionType = await this.connectionType(device)
788
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
789
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
790
+ this.api.updatePlatformAccessories([existingAccessory])
890
791
  // create the accessory handler for the restored accessory
891
792
  // this is imported from `platformAccessory.ts`
892
- new Humidifier(this, existingAccessory, device);
893
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
793
+ new Humidifier(this, existingAccessory, device)
794
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
894
795
  } else {
895
- this.unregisterPlatformAccessories(existingAccessory);
796
+ this.unregisterPlatformAccessories(existingAccessory)
896
797
  }
897
798
  } else if (await this.registerDevice(device)) {
898
799
  // create a new accessory
899
800
  const accessory = new this.api.platformAccessory(device.configDeviceName
900
801
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
901
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
802
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
902
803
 
903
804
  // store a copy of the device object in the `accessory.context`
904
805
  // the `context` property can be used to store any data about the accessory you may need
905
- accessory.context.device = device;
906
- accessory.context.deviceId = device.deviceId;
907
- accessory.context.deviceType = device.deviceType;
908
- accessory.context.model = SwitchBotModel.Humidifier;
806
+ accessory.context.device = device
807
+ accessory.context.deviceId = device.deviceId
808
+ accessory.context.deviceType = device.deviceType
809
+ accessory.context.model = SwitchBotModel.Humidifier
909
810
  accessory.displayName = device.configDeviceName
910
811
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
911
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
912
- accessory.context.connectionType = await this.connectionType(device);
913
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
914
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
915
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
812
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
813
+ accessory.context.connectionType = await this.connectionType(device)
814
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
815
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
816
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
916
817
  // create the accessory handler for the newly create accessory
917
818
  // this is imported from `platformAccessory.ts`
918
- new Humidifier(this, accessory, device);
919
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
819
+ new Humidifier(this, accessory, device)
820
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
920
821
 
921
822
  // publish device externally or link the accessory to your platform
922
- this.externalOrPlatform(device, accessory);
923
- this.accessories.push(accessory);
823
+ this.externalOrPlatform(device, accessory)
824
+ this.accessories.push(accessory)
924
825
  } else {
925
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
826
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
926
827
  }
927
828
  }
928
829
 
929
830
  private async createBot(device: device & devicesConfig) {
930
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
831
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
931
832
 
932
833
  // see if an accessory with the same uuid has already been registered and restored from
933
834
  // the cached devices we stored in the `configureAccessory` method above
934
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
835
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
935
836
 
936
837
  if (existingAccessory) {
937
838
  // the accessory already exists
938
839
  if (await this.registerDevice(device)) {
939
840
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
940
- existingAccessory.context.device = device;
941
- existingAccessory.context.deviceId = device.deviceId;
942
- existingAccessory.context.deviceType = device.deviceType;
943
- existingAccessory.context.model = SwitchBotModel.Bot;
841
+ existingAccessory.context.device = device
842
+ existingAccessory.context.deviceId = device.deviceId
843
+ existingAccessory.context.deviceType = device.deviceType
844
+ existingAccessory.context.model = SwitchBotModel.Bot
944
845
  existingAccessory.displayName = device.configDeviceName
945
846
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
946
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
947
- existingAccessory.context.connectionType = await this.connectionType(device);
948
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
949
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
950
- this.api.updatePlatformAccessories([existingAccessory]);
847
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
848
+ existingAccessory.context.connectionType = await this.connectionType(device)
849
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
850
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
851
+ this.api.updatePlatformAccessories([existingAccessory])
951
852
  // create the accessory handler for the restored accessory
952
853
  // this is imported from `platformAccessory.ts`
953
- new Bot(this, existingAccessory, device);
954
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
854
+ new Bot(this, existingAccessory, device)
855
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
955
856
  } else {
956
- this.unregisterPlatformAccessories(existingAccessory);
857
+ this.unregisterPlatformAccessories(existingAccessory)
957
858
  }
958
859
  } else if (await this.registerDevice(device)) {
959
860
  // create a new accessory
960
861
  const accessory = new this.api.platformAccessory(device.configDeviceName
961
862
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
962
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
863
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
963
864
 
964
865
  // store a copy of the device object in the `accessory.context`
965
866
  // the `context` property can be used to store any data about the accessory you may need
966
- accessory.context.device = device;
967
- accessory.context.deviceId = device.deviceId;
968
- accessory.context.deviceType = device.deviceType;
969
- accessory.context.model = SwitchBotModel.Bot;
867
+ accessory.context.device = device
868
+ accessory.context.deviceId = device.deviceId
869
+ accessory.context.deviceType = device.deviceType
870
+ accessory.context.model = SwitchBotModel.Bot
970
871
  accessory.displayName = device.configDeviceName
971
872
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
972
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
973
- accessory.context.connectionType = await this.connectionType(device);
974
- accessory.context.connectionType = await this.connectionType(device);
975
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
976
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
977
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
873
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
874
+ accessory.context.connectionType = await this.connectionType(device)
875
+ accessory.context.connectionType = await this.connectionType(device)
876
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
877
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
878
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
978
879
  // accessory.context.version = findaccessories.accessoryAttribute.softwareRevision;
979
880
  // create the accessory handler for the newly create accessory
980
881
  // this is imported from `platformAccessory.ts`
981
- new Bot(this, accessory, device);
982
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
882
+ new Bot(this, accessory, device)
883
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
983
884
 
984
885
  // publish device externally or link the accessory to your platform
985
- this.externalOrPlatform(device, accessory);
986
- this.accessories.push(accessory);
886
+ this.externalOrPlatform(device, accessory)
887
+ this.accessories.push(accessory)
987
888
  } else {
988
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
889
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
989
890
  }
990
891
  }
991
892
 
992
893
  private async createMeter(device: device & devicesConfig) {
993
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
894
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
994
895
 
995
896
  // see if an accessory with the same uuid has already been registered and restored from
996
897
  // the cached devices we stored in the `configureAccessory` method above
997
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
898
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
998
899
 
999
900
  if (existingAccessory) {
1000
901
  // the accessory already exists
1001
902
  if (await this.registerDevice(device)) {
1002
903
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1003
- existingAccessory.context.device = device;
1004
- existingAccessory.context.model = SwitchBotModel.Meter;
1005
- existingAccessory.context.deviceId = device.deviceId;
1006
- existingAccessory.context.deviceType = device.deviceType;
904
+ existingAccessory.context.device = device
905
+ existingAccessory.context.model = SwitchBotModel.Meter
906
+ existingAccessory.context.deviceId = device.deviceId
907
+ existingAccessory.context.deviceType = device.deviceType
1007
908
  existingAccessory.displayName = device.configDeviceName
1008
909
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1009
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1010
- existingAccessory.context.connectionType = await this.connectionType(device);
1011
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1012
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1013
- this.api.updatePlatformAccessories([existingAccessory]);
910
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
911
+ existingAccessory.context.connectionType = await this.connectionType(device)
912
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
913
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
914
+ this.api.updatePlatformAccessories([existingAccessory])
1014
915
  // create the accessory handler for the restored accessory
1015
916
  // this is imported from `platformAccessory.ts`
1016
- new Meter(this, existingAccessory, device);
1017
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
917
+ new Meter(this, existingAccessory, device)
918
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1018
919
  } else {
1019
- this.unregisterPlatformAccessories(existingAccessory);
920
+ this.unregisterPlatformAccessories(existingAccessory)
1020
921
  }
1021
922
  } else if (await this.registerDevice(device)) {
1022
923
  // create a new accessory
1023
924
  const accessory = new this.api.platformAccessory(device.configDeviceName
1024
925
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1025
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
926
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1026
927
 
1027
928
  // store a copy of the device object in the `accessory.context`
1028
929
  // the `context` property can be used to store any data about the accessory you may need
1029
- accessory.context.device = device;
1030
- accessory.context.model = SwitchBotModel.Meter;
1031
- accessory.context.deviceId = device.deviceId;
1032
- accessory.context.deviceType = device.deviceType;
930
+ accessory.context.device = device
931
+ accessory.context.model = SwitchBotModel.Meter
932
+ accessory.context.deviceId = device.deviceId
933
+ accessory.context.deviceType = device.deviceType
1033
934
  accessory.displayName = device.configDeviceName
1034
935
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1035
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1036
- accessory.context.connectionType = await this.connectionType(device);
1037
- accessory.context.connectionType = await this.connectionType(device);
1038
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1039
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1040
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
936
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
937
+ accessory.context.connectionType = await this.connectionType(device)
938
+ accessory.context.connectionType = await this.connectionType(device)
939
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
940
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
941
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1041
942
  // create the accessory handler for the newly create accessory
1042
943
  // this is imported from `platformAccessory.ts`
1043
- new Meter(this, accessory, device);
1044
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
944
+ new Meter(this, accessory, device)
945
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1045
946
 
1046
947
  // publish device externally or link the accessory to your platform
1047
- this.externalOrPlatform(device, accessory);
1048
- this.accessories.push(accessory);
948
+ this.externalOrPlatform(device, accessory)
949
+ this.accessories.push(accessory)
1049
950
  } else {
1050
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
951
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1051
952
  }
1052
953
  }
1053
954
 
1054
955
  private async createMeterPlus(device: device & devicesConfig) {
1055
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
956
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1056
957
 
1057
958
  // see if an accessory with the same uuid has already been registered and restored from
1058
959
  // the cached devices we stored in the `configureAccessory` method above
1059
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
960
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1060
961
 
1061
962
  if (existingAccessory) {
1062
963
  // the accessory already exists
1063
964
  if (await this.registerDevice(device)) {
1064
965
  // console.log("existingAccessory", existingAccessory);
1065
966
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1066
- existingAccessory.context.device = device;
1067
- existingAccessory.context.model = SwitchBotModel.MeterPlusUS ?? SwitchBotModel.MeterPlusJP;
1068
- existingAccessory.context.deviceId = device.deviceId;
1069
- existingAccessory.context.deviceType = device.deviceType;
967
+ existingAccessory.context.device = device
968
+ existingAccessory.context.model = SwitchBotModel.MeterPlusUS ?? SwitchBotModel.MeterPlusJP
969
+ existingAccessory.context.deviceId = device.deviceId
970
+ existingAccessory.context.deviceType = device.deviceType
1070
971
  existingAccessory.displayName = device.configDeviceName
1071
972
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1072
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1073
- existingAccessory.context.connectionType = await this.connectionType(device);
1074
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1075
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1076
- this.api.updatePlatformAccessories([existingAccessory]);
973
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
974
+ existingAccessory.context.connectionType = await this.connectionType(device)
975
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
976
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
977
+ this.api.updatePlatformAccessories([existingAccessory])
1077
978
  // create the accessory handler for the restored accessory
1078
979
  // this is imported from `platformAccessory.ts`
1079
- new MeterPlus(this, existingAccessory, device);
1080
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
980
+ new MeterPlus(this, existingAccessory, device)
981
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1081
982
  } else {
1082
- this.unregisterPlatformAccessories(existingAccessory);
983
+ this.unregisterPlatformAccessories(existingAccessory)
1083
984
  }
1084
985
  } else if (await this.registerDevice(device)) {
1085
986
  // create a new accessory
1086
987
  const accessory = new this.api.platformAccessory(device.configDeviceName
1087
988
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1088
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
989
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1089
990
 
1090
991
  // store a copy of the device object in the `accessory.context`
1091
992
  // the `context` property can be used to store any data about the accessory you may need
1092
- accessory.context.device = device;
1093
- accessory.context.model = SwitchBotModel.MeterPlusUS ?? SwitchBotModel.MeterPlusJP;
1094
- accessory.context.deviceId = device.deviceId;
1095
- accessory.context.deviceType = device.deviceType;
993
+ accessory.context.device = device
994
+ accessory.context.model = SwitchBotModel.MeterPlusUS ?? SwitchBotModel.MeterPlusJP
995
+ accessory.context.deviceId = device.deviceId
996
+ accessory.context.deviceType = device.deviceType
1096
997
  accessory.displayName = device.configDeviceName
1097
998
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1098
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1099
- accessory.context.connectionType = await this.connectionType(device);
1100
- accessory.context.connectionType = await this.connectionType(device);
1101
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1102
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1103
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
999
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1000
+ accessory.context.connectionType = await this.connectionType(device)
1001
+ accessory.context.connectionType = await this.connectionType(device)
1002
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1003
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1004
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1104
1005
  // create the accessory handler for the newly create accessory
1105
1006
  // this is imported from `platformAccessory.ts`
1106
- new MeterPlus(this, accessory, device);
1107
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1007
+ new MeterPlus(this, accessory, device)
1008
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1108
1009
 
1109
1010
  // publish device externally or link the accessory to your platform
1110
- this.externalOrPlatform(device, accessory);
1111
- this.accessories.push(accessory);
1011
+ this.externalOrPlatform(device, accessory)
1012
+ this.accessories.push(accessory)
1112
1013
  } else {
1113
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1014
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1114
1015
  }
1115
1016
  }
1116
1017
 
1117
1018
  private async createHub2(device: device & devicesConfig) {
1118
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1019
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1119
1020
 
1120
1021
  // see if an accessory with the same uuid has already been registered and restored from
1121
1022
  // the cached devices we stored in the `configureAccessory` method above
1122
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1023
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1123
1024
 
1124
1025
  if (existingAccessory) {
1125
1026
  // the accessory already exists
1126
1027
  if (await this.registerDevice(device)) {
1127
1028
  // console.log("existingAccessory", existingAccessory);
1128
1029
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1129
- existingAccessory.context.device = device;
1130
- existingAccessory.context.model = SwitchBotModel.Hub2;
1131
- existingAccessory.context.deviceId = device.deviceId;
1132
- existingAccessory.context.deviceType = device.deviceType;
1030
+ existingAccessory.context.device = device
1031
+ existingAccessory.context.model = SwitchBotModel.Hub2
1032
+ existingAccessory.context.deviceId = device.deviceId
1033
+ existingAccessory.context.deviceType = device.deviceType
1133
1034
  existingAccessory.displayName = device.configDeviceName
1134
1035
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1135
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1136
- existingAccessory.context.connectionType = await this.connectionType(device);
1137
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1138
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1139
- this.api.updatePlatformAccessories([existingAccessory]);
1036
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1037
+ existingAccessory.context.connectionType = await this.connectionType(device)
1038
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1039
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1040
+ this.api.updatePlatformAccessories([existingAccessory])
1140
1041
  // create the accessory handler for the restored accessory
1141
1042
  // this is imported from `platformAccessory.ts`
1142
- new Hub(this, existingAccessory, device);
1143
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1043
+ new Hub(this, existingAccessory, device)
1044
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1144
1045
  } else {
1145
- this.unregisterPlatformAccessories(existingAccessory);
1046
+ this.unregisterPlatformAccessories(existingAccessory)
1146
1047
  }
1147
1048
  } else if (await this.registerDevice(device)) {
1148
1049
  // create a new accessory
1149
1050
  const accessory = new this.api.platformAccessory(device.configDeviceName
1150
1051
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1151
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1052
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1152
1053
 
1153
1054
  // store a copy of the device object in the `accessory.context`
1154
1055
  // the `context` property can be used to store any data about the accessory you may need
1155
- accessory.context.device = device;
1156
- accessory.context.model = SwitchBotModel.Hub2;
1157
- accessory.context.deviceId = device.deviceId;
1158
- accessory.context.deviceType = device.deviceType;
1056
+ accessory.context.device = device
1057
+ accessory.context.model = SwitchBotModel.Hub2
1058
+ accessory.context.deviceId = device.deviceId
1059
+ accessory.context.deviceType = device.deviceType
1159
1060
  accessory.displayName = device.configDeviceName
1160
1061
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1161
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1162
- accessory.context.connectionType = await this.connectionType(device);
1163
- accessory.context.connectionType = await this.connectionType(device);
1164
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1165
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1166
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1062
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1063
+ accessory.context.connectionType = await this.connectionType(device)
1064
+ accessory.context.connectionType = await this.connectionType(device)
1065
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1066
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1067
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1167
1068
  // create the accessory handler for the newly create accessory
1168
1069
  // this is imported from `platformAccessory.ts`
1169
- new Hub(this, accessory, device);
1170
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1070
+ new Hub(this, accessory, device)
1071
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1171
1072
 
1172
1073
  // publish device externally or link the accessory to your platform
1173
- this.externalOrPlatform(device, accessory);
1174
- this.accessories.push(accessory);
1074
+ this.externalOrPlatform(device, accessory)
1075
+ this.accessories.push(accessory)
1175
1076
  } else {
1176
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1077
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1177
1078
  }
1178
1079
  }
1179
1080
 
1180
1081
  private async createIOSensor(device: device & devicesConfig) {
1181
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1082
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1182
1083
 
1183
1084
  // see if an accessory with the same uuid has already been registered and restored from
1184
1085
  // the cached devices we stored in the `configureAccessory` method above
1185
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1086
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1186
1087
 
1187
1088
  if (existingAccessory) {
1188
1089
  // the accessory already exists
1189
1090
  if (await this.registerDevice(device)) {
1190
1091
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1191
- existingAccessory.context.device = device;
1192
- existingAccessory.context.model = SwitchBotModel.OutdoorMeter;
1193
- existingAccessory.context.deviceId = device.deviceId;
1194
- existingAccessory.context.deviceType = device.deviceType;
1092
+ existingAccessory.context.device = device
1093
+ existingAccessory.context.model = SwitchBotModel.OutdoorMeter
1094
+ existingAccessory.context.deviceId = device.deviceId
1095
+ existingAccessory.context.deviceType = device.deviceType
1195
1096
  existingAccessory.displayName = device.configDeviceName
1196
1097
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1197
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1198
- existingAccessory.context.connectionType = await this.connectionType(device);
1199
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1200
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1201
- this.api.updatePlatformAccessories([existingAccessory]);
1098
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1099
+ existingAccessory.context.connectionType = await this.connectionType(device)
1100
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1101
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1102
+ this.api.updatePlatformAccessories([existingAccessory])
1202
1103
  // create the accessory handler for the restored accessory
1203
1104
  // this is imported from `platformAccessory.ts`
1204
- new IOSensor(this, existingAccessory, device);
1205
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1105
+ new IOSensor(this, existingAccessory, device)
1106
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1206
1107
  } else {
1207
- this.unregisterPlatformAccessories(existingAccessory);
1108
+ this.unregisterPlatformAccessories(existingAccessory)
1208
1109
  }
1209
1110
  } else if (await this.registerDevice(device)) {
1210
1111
  // create a new accessory
1211
1112
  const accessory = new this.api.platformAccessory(device.configDeviceName
1212
1113
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1213
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1114
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1214
1115
 
1215
1116
  // store a copy of the device object in the `accessory.context`
1216
1117
  // the `context` property can be used to store any data about the accessory you may need
1217
- accessory.context.device = device;
1218
- accessory.context.model = SwitchBotModel.OutdoorMeter;
1219
- accessory.context.deviceId = device.deviceId;
1220
- accessory.context.deviceType = device.deviceType;
1118
+ accessory.context.device = device
1119
+ accessory.context.model = SwitchBotModel.OutdoorMeter
1120
+ accessory.context.deviceId = device.deviceId
1121
+ accessory.context.deviceType = device.deviceType
1221
1122
  accessory.displayName = device.configDeviceName
1222
1123
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1223
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1224
- accessory.context.connectionType = await this.connectionType(device);
1225
- accessory.context.connectionType = await this.connectionType(device);
1226
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1227
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1228
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1124
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1125
+ accessory.context.connectionType = await this.connectionType(device)
1126
+ accessory.context.connectionType = await this.connectionType(device)
1127
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1128
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1129
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1229
1130
  // create the accessory handler for the newly create accessory
1230
1131
  // this is imported from `platformAccessory.ts`
1231
- new IOSensor(this, accessory, device);
1232
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1132
+ new IOSensor(this, accessory, device)
1133
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1233
1134
 
1234
1135
  // publish device externally or link the accessory to your platform
1235
- this.externalOrPlatform(device, accessory);
1236
- this.accessories.push(accessory);
1136
+ this.externalOrPlatform(device, accessory)
1137
+ this.accessories.push(accessory)
1237
1138
  } else {
1238
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1139
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1239
1140
  }
1240
1141
  }
1241
1142
 
1242
1143
  private async createWaterDetector(device: device & devicesConfig) {
1243
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1144
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1244
1145
 
1245
1146
  // see if an accessory with the same uuid has already been registered and restored from
1246
1147
  // the cached devices we stored in the `configureAccessory` method above
1247
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1148
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1248
1149
 
1249
1150
  if (existingAccessory) {
1250
1151
  // the accessory already exists
1251
1152
  if (await this.registerDevice(device)) {
1252
1153
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1253
- existingAccessory.context.device = device;
1254
- existingAccessory.context.deviceId = device.deviceId;
1255
- existingAccessory.context.deviceType = device.deviceType;
1256
- existingAccessory.context.model = SwitchBotModel.WaterDetector;
1154
+ existingAccessory.context.device = device
1155
+ existingAccessory.context.deviceId = device.deviceId
1156
+ existingAccessory.context.deviceType = device.deviceType
1157
+ existingAccessory.context.model = SwitchBotModel.WaterDetector
1257
1158
  existingAccessory.displayName = device.configDeviceName
1258
1159
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1259
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1260
- existingAccessory.context.connectionType = await this.connectionType(device);
1261
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1262
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1263
- this.api.updatePlatformAccessories([existingAccessory]);
1160
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1161
+ existingAccessory.context.connectionType = await this.connectionType(device)
1162
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1163
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1164
+ this.api.updatePlatformAccessories([existingAccessory])
1264
1165
  // create the accessory handler for the restored accessory
1265
1166
  // this is imported from `platformAccessory.ts`
1266
- new WaterDetector(this, existingAccessory, device);
1267
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1167
+ new WaterDetector(this, existingAccessory, device)
1168
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1268
1169
  } else {
1269
- this.unregisterPlatformAccessories(existingAccessory);
1170
+ this.unregisterPlatformAccessories(existingAccessory)
1270
1171
  }
1271
1172
  } else if (await this.registerDevice(device)) {
1272
1173
  // create a new accessory
1273
1174
  const accessory = new this.api.platformAccessory(device.configDeviceName
1274
1175
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1275
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1176
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1276
1177
 
1277
1178
  // store a copy of the device object in the `accessory.context`
1278
1179
  // the `context` property can be used to store any data about the accessory you may need
1279
- accessory.context.device = device;
1280
- accessory.context.deviceId = device.deviceId;
1281
- accessory.context.deviceType = device.deviceType;
1282
- accessory.context.model = SwitchBotModel.WaterDetector;
1180
+ accessory.context.device = device
1181
+ accessory.context.deviceId = device.deviceId
1182
+ accessory.context.deviceType = device.deviceType
1183
+ accessory.context.model = SwitchBotModel.WaterDetector
1283
1184
  accessory.displayName = device.configDeviceName
1284
1185
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1285
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1286
- accessory.context.connectionType = await this.connectionType(device);
1287
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1288
- accessory.context.connectionType = await this.connectionType(device);
1289
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1290
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1186
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1187
+ accessory.context.connectionType = await this.connectionType(device)
1188
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1189
+ accessory.context.connectionType = await this.connectionType(device)
1190
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1191
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1291
1192
  // create the accessory handler for the newly create accessory
1292
1193
  // this is imported from `platformAccessory.ts`
1293
- new WaterDetector(this, accessory, device);
1294
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1194
+ new WaterDetector(this, accessory, device)
1195
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1295
1196
 
1296
1197
  // publish device externally or link the accessory to your platform
1297
- this.externalOrPlatform(device, accessory);
1298
- this.accessories.push(accessory);
1198
+ this.externalOrPlatform(device, accessory)
1199
+ this.accessories.push(accessory)
1299
1200
  } else {
1300
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1201
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1301
1202
  }
1302
1203
  }
1303
1204
 
1304
1205
  private async createMotion(device: device & devicesConfig) {
1305
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1206
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1306
1207
 
1307
1208
  // see if an accessory with the same uuid has already been registered and restored from
1308
1209
  // the cached devices we stored in the `configureAccessory` method above
1309
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1210
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1310
1211
 
1311
1212
  if (existingAccessory) {
1312
1213
  // the accessory already exists
1313
1214
  if (await this.registerDevice(device)) {
1314
1215
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1315
- existingAccessory.context.device = device;
1316
- existingAccessory.context.deviceId = device.deviceId;
1317
- existingAccessory.context.deviceType = device.deviceType;
1318
- existingAccessory.context.model = SwitchBotModel.MotionSensor;
1216
+ existingAccessory.context.device = device
1217
+ existingAccessory.context.deviceId = device.deviceId
1218
+ existingAccessory.context.deviceType = device.deviceType
1219
+ existingAccessory.context.model = SwitchBotModel.MotionSensor
1319
1220
  existingAccessory.displayName = device.configDeviceName
1320
1221
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1321
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1322
- existingAccessory.context.connectionType = await this.connectionType(device);
1323
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1324
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1325
- this.api.updatePlatformAccessories([existingAccessory]);
1222
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1223
+ existingAccessory.context.connectionType = await this.connectionType(device)
1224
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1225
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1226
+ this.api.updatePlatformAccessories([existingAccessory])
1326
1227
  // create the accessory handler for the restored accessory
1327
1228
  // this is imported from `platformAccessory.ts`
1328
- new Motion(this, existingAccessory, device);
1329
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1229
+ new Motion(this, existingAccessory, device)
1230
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1330
1231
  } else {
1331
- this.unregisterPlatformAccessories(existingAccessory);
1232
+ this.unregisterPlatformAccessories(existingAccessory)
1332
1233
  }
1333
1234
  } else if (await this.registerDevice(device)) {
1334
1235
  // create a new accessory
1335
1236
  const accessory = new this.api.platformAccessory(device.configDeviceName
1336
1237
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1337
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1238
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1338
1239
 
1339
1240
  // store a copy of the device object in the `accessory.context`
1340
1241
  // the `context` property can be used to store any data about the accessory you may need
1341
- accessory.context.device = device;
1342
- accessory.context.deviceId = device.deviceId;
1343
- accessory.context.deviceType = device.deviceType;
1344
- accessory.context.model = SwitchBotModel.MotionSensor;
1242
+ accessory.context.device = device
1243
+ accessory.context.deviceId = device.deviceId
1244
+ accessory.context.deviceType = device.deviceType
1245
+ accessory.context.model = SwitchBotModel.MotionSensor
1345
1246
  accessory.displayName = device.configDeviceName
1346
1247
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1347
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1348
- accessory.context.connectionType = await this.connectionType(device);
1349
- accessory.context.connectionType = await this.connectionType(device);
1350
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1351
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1352
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1248
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1249
+ accessory.context.connectionType = await this.connectionType(device)
1250
+ accessory.context.connectionType = await this.connectionType(device)
1251
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1252
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1253
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1353
1254
  // create the accessory handler for the newly create accessory
1354
1255
  // this is imported from `platformAccessory.ts`
1355
- new Motion(this, accessory, device);
1356
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1256
+ new Motion(this, accessory, device)
1257
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1357
1258
 
1358
1259
  // publish device externally or link the accessory to your platform
1359
- this.externalOrPlatform(device, accessory);
1360
- this.accessories.push(accessory);
1260
+ this.externalOrPlatform(device, accessory)
1261
+ this.accessories.push(accessory)
1361
1262
  } else {
1362
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1263
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1363
1264
  }
1364
1265
  }
1365
1266
 
1366
1267
  private async createContact(device: device & devicesConfig) {
1367
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1268
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1368
1269
 
1369
1270
  // see if an accessory with the same uuid has already been registered and restored from
1370
1271
  // the cached devices we stored in the `configureAccessory` method above
1371
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1272
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1372
1273
 
1373
1274
  if (existingAccessory) {
1374
1275
  // the accessory already exists
1375
1276
  if (await this.registerDevice(device)) {
1376
1277
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1377
- existingAccessory.context.device = device;
1378
- existingAccessory.context.deviceId = device.deviceId;
1379
- existingAccessory.context.deviceType = device.deviceType;
1380
- existingAccessory.context.model = SwitchBotModel.ContactSensor;
1278
+ existingAccessory.context.device = device
1279
+ existingAccessory.context.deviceId = device.deviceId
1280
+ existingAccessory.context.deviceType = device.deviceType
1281
+ existingAccessory.context.model = SwitchBotModel.ContactSensor
1381
1282
  existingAccessory.displayName = device.configDeviceName
1382
1283
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1383
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1384
- existingAccessory.context.connectionType = await this.connectionType(device);
1385
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1386
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1387
- this.api.updatePlatformAccessories([existingAccessory]);
1284
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1285
+ existingAccessory.context.connectionType = await this.connectionType(device)
1286
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1287
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1288
+ this.api.updatePlatformAccessories([existingAccessory])
1388
1289
  // create the accessory handler for the restored accessory
1389
1290
  // this is imported from `platformAccessory.ts`
1390
- new Contact(this, existingAccessory, device);
1391
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1291
+ new Contact(this, existingAccessory, device)
1292
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1392
1293
  } else {
1393
- this.unregisterPlatformAccessories(existingAccessory);
1294
+ this.unregisterPlatformAccessories(existingAccessory)
1394
1295
  }
1395
1296
  } else if (await this.registerDevice(device)) {
1396
1297
  // create a new accessory
1397
1298
  const accessory = new this.api.platformAccessory(device.configDeviceName
1398
1299
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1399
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1300
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1400
1301
 
1401
1302
  // store a copy of the device object in the `accessory.context`
1402
1303
  // the `context` property can be used to store any data about the accessory you may need
1403
- accessory.context.device = device;
1404
- accessory.context.deviceId = device.deviceId;
1405
- accessory.context.deviceType = device.deviceType;
1406
- accessory.context.model = SwitchBotModel.ContactSensor;
1304
+ accessory.context.device = device
1305
+ accessory.context.deviceId = device.deviceId
1306
+ accessory.context.deviceType = device.deviceType
1307
+ accessory.context.model = SwitchBotModel.ContactSensor
1407
1308
  accessory.displayName = device.configDeviceName
1408
1309
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1409
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1410
- accessory.context.connectionType = await this.connectionType(device);
1411
- accessory.context.connectionType = await this.connectionType(device);
1412
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1413
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1414
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1310
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1311
+ accessory.context.connectionType = await this.connectionType(device)
1312
+ accessory.context.connectionType = await this.connectionType(device)
1313
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1314
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1315
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1415
1316
  // create the accessory handler for the newly create accessory
1416
1317
  // this is imported from `platformAccessory.ts`
1417
- new Contact(this, accessory, device);
1418
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1318
+ new Contact(this, accessory, device)
1319
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1419
1320
 
1420
1321
  // publish device externally or link the accessory to your platform
1421
- this.externalOrPlatform(device, accessory);
1422
- this.accessories.push(accessory);
1322
+ this.externalOrPlatform(device, accessory)
1323
+ this.accessories.push(accessory)
1423
1324
  } else {
1424
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1325
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1425
1326
  }
1426
1327
  }
1427
1328
 
1428
1329
  private async createBlindTilt(device: device & devicesConfig) {
1429
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1330
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1430
1331
 
1431
1332
  // see if an accessory with the same uuid has already been registered and restored from
1432
1333
  // the cached devices we stored in the `configureAccessory` method above
1433
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1334
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1434
1335
 
1435
1336
  if (existingAccessory) {
1436
1337
  // the accessory already exists
1437
1338
  if (await this.registerDevice(device)) {
1438
1339
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1439
- existingAccessory.context.device = device;
1440
- existingAccessory.context.deviceId = device.deviceId;
1441
- existingAccessory.context.deviceType = device.deviceType;
1442
- existingAccessory.context.model = SwitchBotModel.BlindTilt;
1340
+ existingAccessory.context.device = device
1341
+ existingAccessory.context.deviceId = device.deviceId
1342
+ existingAccessory.context.deviceType = device.deviceType
1343
+ existingAccessory.context.model = SwitchBotModel.BlindTilt
1443
1344
  existingAccessory.displayName = device.configDeviceName
1444
1345
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1445
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1446
- existingAccessory.context.connectionType = await this.connectionType(device);
1447
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1448
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1449
- this.api.updatePlatformAccessories([existingAccessory]);
1346
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1347
+ existingAccessory.context.connectionType = await this.connectionType(device)
1348
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1349
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1350
+ this.api.updatePlatformAccessories([existingAccessory])
1450
1351
  // create the accessory handler for the restored accessory
1451
1352
  // this is imported from `platformAccessory.ts`
1452
- new BlindTilt(this, existingAccessory, device);
1453
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1353
+ new BlindTilt(this, existingAccessory, device)
1354
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1454
1355
  } else {
1455
- this.unregisterPlatformAccessories(existingAccessory);
1356
+ this.unregisterPlatformAccessories(existingAccessory)
1456
1357
  }
1457
1358
  } else if (await this.registerDevice(device)) {
1458
1359
  if (isBlindTiltDevice(device)) {
1459
1360
  if (device.group && !device.curtain?.disable_group) {
1460
1361
  this.debugLog(
1461
- 'Your Curtains are grouped, ' +
1462
- `, Secondary curtain automatically hidden. Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1362
+ 'Your Curtains are grouped, '
1363
+ + `, Secondary curtain automatically hidden. Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`,
1364
+ )
1463
1365
  } else {
1464
1366
  if (device.master) {
1465
- this.warnLog(`Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1367
+ this.warnLog(`Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`)
1466
1368
  } else {
1467
- this.errorLog(`Secondary Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1369
+ this.errorLog(`Secondary Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`)
1468
1370
  }
1469
1371
  }
1470
1372
  }
@@ -1472,75 +1374,76 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1472
1374
  // create a new accessory
1473
1375
  const accessory = new this.api.platformAccessory(device.configDeviceName
1474
1376
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1475
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1377
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1476
1378
 
1477
1379
  // store a copy of the device object in the `accessory.context`
1478
1380
  // the `context` property can be used to store any data about the accessory you may need
1479
- accessory.context.device = device;;
1480
- accessory.context.deviceId = device.deviceId;
1481
- accessory.context.deviceType = device.deviceType;
1482
- accessory.context.model = SwitchBotModel.BlindTilt;
1381
+ accessory.context.device = device
1382
+ accessory.context.deviceId = device.deviceId
1383
+ accessory.context.deviceType = device.deviceType
1384
+ accessory.context.model = SwitchBotModel.BlindTilt
1483
1385
  accessory.displayName = device.configDeviceName
1484
1386
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1485
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1486
- accessory.context.connectionType = await this.connectionType(device);
1487
- accessory.context.connectionType = await this.connectionType(device);
1488
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1489
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1490
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1387
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1388
+ accessory.context.connectionType = await this.connectionType(device)
1389
+ accessory.context.connectionType = await this.connectionType(device)
1390
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1391
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1392
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1491
1393
  // create the accessory handler for the newly create accessory
1492
1394
  // this is imported from `platformAccessory.ts`
1493
- new BlindTilt(this, accessory, device);
1494
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1395
+ new BlindTilt(this, accessory, device)
1396
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1495
1397
 
1496
1398
  // publish device externally or link the accessory to your platform
1497
- this.externalOrPlatform(device, accessory);
1498
- this.accessories.push(accessory);
1399
+ this.externalOrPlatform(device, accessory)
1400
+ this.accessories.push(accessory)
1499
1401
  } else {
1500
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1402
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1501
1403
  }
1502
1404
  }
1503
1405
 
1504
1406
  private async createCurtain(device: device & devicesConfig) {
1505
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1407
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1506
1408
 
1507
1409
  // see if an accessory with the same uuid has already been registered and restored from
1508
1410
  // the cached devices we stored in the `configureAccessory` method above
1509
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1411
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1510
1412
 
1511
1413
  if (existingAccessory) {
1512
1414
  // the accessory already exists
1513
1415
  if (await this.registerDevice(device)) {
1514
1416
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1515
- existingAccessory.context.device = device;
1516
- existingAccessory.context.deviceId = device.deviceId;
1517
- existingAccessory.context.deviceType = device.deviceType;
1518
- existingAccessory.context.model = device.deviceType === 'Curtain3' ? SwitchBotModel.Curtain3 : SwitchBotModel.Curtain;
1417
+ existingAccessory.context.device = device
1418
+ existingAccessory.context.deviceId = device.deviceId
1419
+ existingAccessory.context.deviceType = device.deviceType
1420
+ existingAccessory.context.model = device.deviceType === 'Curtain3' ? SwitchBotModel.Curtain3 : SwitchBotModel.Curtain
1519
1421
  existingAccessory.displayName = device.configDeviceName
1520
1422
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1521
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1522
- existingAccessory.context.connectionType = await this.connectionType(device);
1523
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1524
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1525
- this.api.updatePlatformAccessories([existingAccessory]);
1423
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1424
+ existingAccessory.context.connectionType = await this.connectionType(device)
1425
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1426
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1427
+ this.api.updatePlatformAccessories([existingAccessory])
1526
1428
  // create the accessory handler for the restored accessory
1527
1429
  // this is imported from `platformAccessory.ts`
1528
- new Curtain(this, existingAccessory, device);
1529
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1430
+ new Curtain(this, existingAccessory, device)
1431
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1530
1432
  } else {
1531
- this.unregisterPlatformAccessories(existingAccessory);
1433
+ this.unregisterPlatformAccessories(existingAccessory)
1532
1434
  }
1533
1435
  } else if (await this.registerDevice(device)) {
1534
1436
  if (isCurtainDevice(device)) {
1535
1437
  if (device.group && !device.curtain?.disable_group) {
1536
1438
  this.debugLog(
1537
- 'Your Curtains are grouped, ' +
1538
- `, Secondary curtain automatically hidden. Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1439
+ 'Your Curtains are grouped, '
1440
+ + `, Secondary curtain automatically hidden. Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`,
1441
+ )
1539
1442
  } else {
1540
1443
  if (device.master) {
1541
- this.warnLog(`Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1444
+ this.warnLog(`Main Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`)
1542
1445
  } else {
1543
- this.errorLog(`Secondary Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`);
1446
+ this.errorLog(`Secondary Curtain: ${device.deviceName}, deviceId: ${device.deviceId}`)
1544
1447
  }
1545
1448
  }
1546
1449
  }
@@ -1548,1409 +1451,1318 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
1548
1451
  // create a new accessory
1549
1452
  const accessory = new this.api.platformAccessory(device.configDeviceName
1550
1453
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1551
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1454
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1552
1455
 
1553
1456
  // store a copy of the device object in the `accessory.context`
1554
1457
  // the `context` property can be used to store any data about the accessory you may need
1555
- accessory.context.device = device;
1556
- accessory.context.deviceId = device.deviceId;
1557
- accessory.context.deviceType = device.deviceType;
1558
- accessory.context.model = device.deviceType === 'Curtain3' ? SwitchBotModel.Curtain3 : SwitchBotModel.Curtain;
1458
+ accessory.context.device = device
1459
+ accessory.context.deviceId = device.deviceId
1460
+ accessory.context.deviceType = device.deviceType
1461
+ accessory.context.model = device.deviceType === 'Curtain3' ? SwitchBotModel.Curtain3 : SwitchBotModel.Curtain
1559
1462
  accessory.displayName = device.configDeviceName
1560
1463
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1561
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1562
- accessory.context.connectionType = await this.connectionType(device);
1563
- accessory.context.connectionType = await this.connectionType(device);
1564
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1565
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1566
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1464
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1465
+ accessory.context.connectionType = await this.connectionType(device)
1466
+ accessory.context.connectionType = await this.connectionType(device)
1467
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1468
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1469
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1567
1470
  // create the accessory handler for the newly create accessory
1568
1471
  // this is imported from `platformAccessory.ts`
1569
- new Curtain(this, accessory, device);
1570
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1472
+ new Curtain(this, accessory, device)
1473
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1571
1474
 
1572
1475
  // publish device externally or link the accessory to your platform
1573
- this.externalOrPlatform(device, accessory);
1574
- this.accessories.push(accessory);
1476
+ this.externalOrPlatform(device, accessory)
1477
+ this.accessories.push(accessory)
1575
1478
  } else {
1576
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1479
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1577
1480
  }
1578
1481
  }
1579
1482
 
1580
1483
  private async createPlug(device: device & devicesConfig) {
1581
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1484
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1582
1485
 
1583
1486
  // see if an accessory with the same uuid has already been registered and restored from
1584
1487
  // the cached devices we stored in the `configureAccessory` method above
1585
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1488
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1586
1489
 
1587
1490
  if (existingAccessory) {
1588
1491
  // the accessory already exists
1589
1492
  if (await this.registerDevice(device)) {
1590
1493
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1591
- existingAccessory.context.device = device;
1592
- existingAccessory.context.deviceId = device.deviceId;
1593
- existingAccessory.context.deviceType = device.deviceType;
1594
- existingAccessory.context.model = device.deviceType === 'Plug Mini (US)' ? SwitchBotModel.PlugMiniUS : device.deviceType === 'Plug Mini (JP)'
1595
- ? SwitchBotModel.PlugMiniJP : SwitchBotModel.Plug;
1494
+ existingAccessory.context.device = device
1495
+ existingAccessory.context.deviceId = device.deviceId
1496
+ existingAccessory.context.deviceType = device.deviceType
1497
+ existingAccessory.context.model = device.deviceType === 'Plug Mini (US)'
1498
+ ? SwitchBotModel.PlugMiniUS
1499
+ : device.deviceType === 'Plug Mini (JP)'
1500
+ ? SwitchBotModel.PlugMiniJP
1501
+ : SwitchBotModel.Plug
1596
1502
  existingAccessory.displayName = device.configDeviceName
1597
1503
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1598
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1599
- existingAccessory.context.connectionType = await this.connectionType(device);
1600
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1601
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1602
- this.api.updatePlatformAccessories([existingAccessory]);
1504
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1505
+ existingAccessory.context.connectionType = await this.connectionType(device)
1506
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1507
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1508
+ this.api.updatePlatformAccessories([existingAccessory])
1603
1509
  // create the accessory handler for the restored accessory
1604
1510
  // this is imported from `platformAccessory.ts`
1605
- new Plug(this, existingAccessory, device);
1606
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1511
+ new Plug(this, existingAccessory, device)
1512
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1607
1513
  } else {
1608
- this.unregisterPlatformAccessories(existingAccessory);
1514
+ this.unregisterPlatformAccessories(existingAccessory)
1609
1515
  }
1610
1516
  } else if (await this.registerDevice(device)) {
1611
1517
  // create a new accessory
1612
1518
  const accessory = new this.api.platformAccessory(device.configDeviceName
1613
1519
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1614
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1520
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1615
1521
 
1616
1522
  // store a copy of the device object in the `accessory.context`
1617
1523
  // the `context` property can be used to store any data about the accessory you may need
1618
- accessory.context.device = device;
1619
- accessory.context.deviceId = device.deviceId;
1620
- accessory.context.deviceType = device.deviceType;
1621
- accessory.context.model = device.deviceType === 'Plug Mini (US)' ? SwitchBotModel.PlugMiniUS : device.deviceType === 'Plug Mini (JP)'
1622
- ? SwitchBotModel.PlugMiniJP : SwitchBotModel.Plug;
1524
+ accessory.context.device = device
1525
+ accessory.context.deviceId = device.deviceId
1526
+ accessory.context.deviceType = device.deviceType
1527
+ accessory.context.model = device.deviceType === 'Plug Mini (US)'
1528
+ ? SwitchBotModel.PlugMiniUS
1529
+ : device.deviceType === 'Plug Mini (JP)'
1530
+ ? SwitchBotModel.PlugMiniJP
1531
+ : SwitchBotModel.Plug
1623
1532
  accessory.displayName = device.configDeviceName
1624
1533
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1625
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1626
- accessory.context.connectionType = await this.connectionType(device);
1627
- accessory.context.connectionType = await this.connectionType(device);
1628
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1629
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1630
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1534
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1535
+ accessory.context.connectionType = await this.connectionType(device)
1536
+ accessory.context.connectionType = await this.connectionType(device)
1537
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1538
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1539
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1631
1540
  // create the accessory handler for the newly create accessory
1632
1541
  // this is imported from `platformAccessory.ts`
1633
- new Plug(this, accessory, device);
1634
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1542
+ new Plug(this, accessory, device)
1543
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1635
1544
 
1636
1545
  // publish device externally or link the accessory to your platform
1637
- this.externalOrPlatform(device, accessory);
1638
- this.accessories.push(accessory);
1546
+ this.externalOrPlatform(device, accessory)
1547
+ this.accessories.push(accessory)
1639
1548
  } else {
1640
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1549
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1641
1550
  }
1642
1551
  }
1643
1552
 
1644
1553
  private async createLock(device: device & devicesConfig) {
1645
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1554
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1646
1555
 
1647
1556
  // see if an accessory with the same uuid has already been registered and restored from
1648
1557
  // the cached devices we stored in the `configureAccessory` method above
1649
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1558
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1650
1559
 
1651
1560
  if (existingAccessory) {
1652
1561
  // the accessory already exists
1653
1562
  if (await this.registerDevice(device)) {
1654
1563
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1655
- existingAccessory.context.device = device;
1656
- existingAccessory.context.deviceId = device.deviceId;
1657
- existingAccessory.context.deviceType = device.deviceType;
1658
- existingAccessory.context.model = device.deviceType === 'Smart Lock Pro' ? SwitchBotModel.LockPro : SwitchBotModel.Lock;
1564
+ existingAccessory.context.device = device
1565
+ existingAccessory.context.deviceId = device.deviceId
1566
+ existingAccessory.context.deviceType = device.deviceType
1567
+ existingAccessory.context.model = device.deviceType === 'Smart Lock Pro' ? SwitchBotModel.LockPro : SwitchBotModel.Lock
1659
1568
  existingAccessory.displayName = device.configDeviceName
1660
1569
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1661
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1662
- existingAccessory.context.connectionType = await this.connectionType(device);
1663
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1664
- this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1665
- this.api.updatePlatformAccessories([existingAccessory]);
1570
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1571
+ existingAccessory.context.connectionType = await this.connectionType(device)
1572
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1573
+ this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1574
+ this.api.updatePlatformAccessories([existingAccessory])
1666
1575
  // create the accessory handler for the restored accessory
1667
1576
  // this is imported from `platformAccessory.ts`
1668
- new Lock(this, existingAccessory, device);
1669
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1577
+ new Lock(this, existingAccessory, device)
1578
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1670
1579
  } else {
1671
- this.unregisterPlatformAccessories(existingAccessory);
1580
+ this.unregisterPlatformAccessories(existingAccessory)
1672
1581
  }
1673
1582
  } else if (await this.registerDevice(device)) {
1674
1583
  // create a new accessory
1675
1584
  const accessory = new this.api.platformAccessory(device.configDeviceName
1676
1585
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1677
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1586
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1678
1587
 
1679
1588
  // store a copy of the device object in the `accessory.context`
1680
1589
  // the `context` property can be used to store any data about the accessory you may need
1681
- accessory.context.device = device;
1682
- accessory.context.deviceId = device.deviceId;
1683
- accessory.context.deviceType = device.deviceType;
1684
- accessory.context.model = device.deviceType === 'Smart Lock Pro' ? SwitchBotModel.LockPro : SwitchBotModel.Lock;
1590
+ accessory.context.device = device
1591
+ accessory.context.deviceId = device.deviceId
1592
+ accessory.context.deviceType = device.deviceType
1593
+ accessory.context.model = device.deviceType === 'Smart Lock Pro' ? SwitchBotModel.LockPro : SwitchBotModel.Lock
1685
1594
  accessory.displayName = device.configDeviceName
1686
1595
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1687
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1688
- accessory.context.connectionType = await this.connectionType(device);
1689
- accessory.context.connectionType = await this.connectionType(device);
1690
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1691
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1692
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1596
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1597
+ accessory.context.connectionType = await this.connectionType(device)
1598
+ accessory.context.connectionType = await this.connectionType(device)
1599
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1600
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1601
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1693
1602
  // create the accessory handler for the newly create accessory
1694
1603
  // this is imported from `platformAccessory.ts`
1695
- new Lock(this, accessory, device);
1696
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1604
+ new Lock(this, accessory, device)
1605
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1697
1606
 
1698
1607
  // publish device externally or link the accessory to your platform
1699
- this.externalOrPlatform(device, accessory);
1700
- this.accessories.push(accessory);
1608
+ this.externalOrPlatform(device, accessory)
1609
+ this.accessories.push(accessory)
1701
1610
  } else {
1702
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1611
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1703
1612
  }
1704
1613
  }
1705
1614
 
1706
1615
  private async createColorBulb(device: device & devicesConfig) {
1707
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1616
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1708
1617
 
1709
1618
  // see if an accessory with the same uuid has already been registered and restored from
1710
1619
  // the cached devices we stored in the `configureAccessory` method above
1711
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1620
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1712
1621
 
1713
1622
  if (existingAccessory) {
1714
1623
  // the accessory already exists
1715
1624
  if (await this.registerDevice(device)) {
1716
1625
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1717
- existingAccessory.context.device = device;
1718
- existingAccessory.context.deviceId = device.deviceId;
1719
- existingAccessory.context.deviceType = device.deviceType;
1720
- existingAccessory.context.model = SwitchBotModel.ColorBulb;
1626
+ existingAccessory.context.device = device
1627
+ existingAccessory.context.deviceId = device.deviceId
1628
+ existingAccessory.context.deviceType = device.deviceType
1629
+ existingAccessory.context.model = SwitchBotModel.ColorBulb
1721
1630
  existingAccessory.displayName = device.configDeviceName
1722
1631
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1723
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1724
- existingAccessory.context.connectionType = await this.connectionType(device);
1725
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1726
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1727
- this.api.updatePlatformAccessories([existingAccessory]);
1632
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1633
+ existingAccessory.context.connectionType = await this.connectionType(device)
1634
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1635
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1636
+ this.api.updatePlatformAccessories([existingAccessory])
1728
1637
  // create the accessory handler for the restored accessory
1729
1638
  // this is imported from `platformAccessory.ts`
1730
- new ColorBulb(this, existingAccessory, device);
1731
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1639
+ new ColorBulb(this, existingAccessory, device)
1640
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1732
1641
  } else {
1733
- this.unregisterPlatformAccessories(existingAccessory);
1642
+ this.unregisterPlatformAccessories(existingAccessory)
1734
1643
  }
1735
1644
  } else if (await this.registerDevice(device)) {
1736
1645
  // create a new accessory
1737
1646
  const accessory = new this.api.platformAccessory(device.configDeviceName
1738
1647
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1739
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1648
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1740
1649
 
1741
1650
  // store a copy of the device object in the `accessory.context`
1742
1651
  // the `context` property can be used to store any data about the accessory you may need
1743
- accessory.context.device = device;
1744
- accessory.context.deviceId = device.deviceId;
1745
- accessory.context.deviceType = device.deviceType;
1746
- accessory.context.model = SwitchBotModel.ColorBulb;
1652
+ accessory.context.device = device
1653
+ accessory.context.deviceId = device.deviceId
1654
+ accessory.context.deviceType = device.deviceType
1655
+ accessory.context.model = SwitchBotModel.ColorBulb
1747
1656
  accessory.displayName = device.configDeviceName
1748
1657
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1749
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1750
- accessory.context.connectionType = await this.connectionType(device);
1751
- accessory.context.connectionType = await this.connectionType(device);
1752
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1753
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1754
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1658
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1659
+ accessory.context.connectionType = await this.connectionType(device)
1660
+ accessory.context.connectionType = await this.connectionType(device)
1661
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1662
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1663
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1755
1664
  // create the accessory handler for the newly create accessory
1756
1665
  // this is imported from `platformAccessory.ts`
1757
- new ColorBulb(this, accessory, device);
1758
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1666
+ new ColorBulb(this, accessory, device)
1667
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1759
1668
 
1760
1669
  // publish device externally or link the accessory to your platform
1761
- this.externalOrPlatform(device, accessory);
1762
- this.accessories.push(accessory);
1670
+ this.externalOrPlatform(device, accessory)
1671
+ this.accessories.push(accessory)
1763
1672
  } else {
1764
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1673
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1765
1674
  }
1766
1675
  }
1767
1676
 
1768
1677
  private async createCeilingLight(device: device & devicesConfig) {
1769
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1678
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1770
1679
 
1771
1680
  // see if an accessory with the same uuid has already been registered and restored from
1772
1681
  // the cached devices we stored in the `configureAccessory` method above
1773
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1682
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1774
1683
 
1775
1684
  if (existingAccessory) {
1776
1685
  // the accessory already exists
1777
1686
  if (await this.registerDevice(device)) {
1778
1687
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1779
- existingAccessory.context.device = device;
1780
- existingAccessory.context.deviceId = device.deviceId;
1781
- existingAccessory.context.deviceType = device.deviceType;
1782
- existingAccessory.context.model = device.deviceType === 'Ceiling Light Pro' ? SwitchBotModel.CeilingLightPro : SwitchBotModel.CeilingLight;
1688
+ existingAccessory.context.device = device
1689
+ existingAccessory.context.deviceId = device.deviceId
1690
+ existingAccessory.context.deviceType = device.deviceType
1691
+ existingAccessory.context.model = device.deviceType === 'Ceiling Light Pro' ? SwitchBotModel.CeilingLightPro : SwitchBotModel.CeilingLight
1783
1692
  existingAccessory.displayName = device.configDeviceName
1784
1693
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1785
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1786
- existingAccessory.context.connectionType = await this.connectionType(device);
1787
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1788
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1789
- this.api.updatePlatformAccessories([existingAccessory]);
1694
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1695
+ existingAccessory.context.connectionType = await this.connectionType(device)
1696
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1697
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1698
+ this.api.updatePlatformAccessories([existingAccessory])
1790
1699
  // create the accessory handler for the restored accessory
1791
1700
  // this is imported from `platformAccessory.ts`
1792
- new CeilingLight(this, existingAccessory, device);
1793
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1701
+ new CeilingLight(this, existingAccessory, device)
1702
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1794
1703
  } else {
1795
- this.unregisterPlatformAccessories(existingAccessory);
1704
+ this.unregisterPlatformAccessories(existingAccessory)
1796
1705
  }
1797
1706
  } else if (await this.registerDevice(device)) {
1798
1707
  // create a new accessory
1799
1708
  const accessory = new this.api.platformAccessory(device.configDeviceName
1800
1709
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1801
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1710
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1802
1711
 
1803
1712
  // store a copy of the device object in the `accessory.context`
1804
1713
  // the `context` property can be used to store any data about the accessory you may need
1805
- accessory.context.device = device;
1806
- accessory.context.deviceId = device.deviceId;
1807
- accessory.context.deviceType = device.deviceType;
1808
- accessory.context.model = device.deviceType === 'Ceiling Light Pro' ? SwitchBotModel.CeilingLightPro : SwitchBotModel.CeilingLight;
1714
+ accessory.context.device = device
1715
+ accessory.context.deviceId = device.deviceId
1716
+ accessory.context.deviceType = device.deviceType
1717
+ accessory.context.model = device.deviceType === 'Ceiling Light Pro' ? SwitchBotModel.CeilingLightPro : SwitchBotModel.CeilingLight
1809
1718
  accessory.displayName = device.configDeviceName
1810
1719
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1811
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1812
- accessory.context.connectionType = await this.connectionType(device);
1813
- accessory.context.connectionType = await this.connectionType(device);
1814
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1815
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1816
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1720
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1721
+ accessory.context.connectionType = await this.connectionType(device)
1722
+ accessory.context.connectionType = await this.connectionType(device)
1723
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1724
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1725
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1817
1726
  // create the accessory handler for the newly create accessory
1818
1727
  // this is imported from `platformAccessory.ts`
1819
- new CeilingLight(this, accessory, device);
1820
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1728
+ new CeilingLight(this, accessory, device)
1729
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1821
1730
 
1822
1731
  // publish device externally or link the accessory to your platform
1823
- this.externalOrPlatform(device, accessory);
1824
- this.accessories.push(accessory);
1732
+ this.externalOrPlatform(device, accessory)
1733
+ this.accessories.push(accessory)
1825
1734
  } else {
1826
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1735
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1827
1736
  }
1828
1737
  }
1829
1738
 
1830
1739
  private async createStripLight(device: device & devicesConfig) {
1831
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1740
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1832
1741
 
1833
1742
  // see if an accessory with the same uuid has already been registered and restored from
1834
1743
  // the cached devices we stored in the `configureAccessory` method above
1835
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1744
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1836
1745
 
1837
1746
  if (existingAccessory) {
1838
1747
  // the accessory already exists
1839
1748
  if (await this.registerDevice(device)) {
1840
1749
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1841
- existingAccessory.context.device = device;
1842
- existingAccessory.context.deviceId = device.deviceId;
1843
- existingAccessory.context.deviceType = device.deviceType;
1844
- existingAccessory.context.model = SwitchBotModel.StripLight;
1750
+ existingAccessory.context.device = device
1751
+ existingAccessory.context.deviceId = device.deviceId
1752
+ existingAccessory.context.deviceType = device.deviceType
1753
+ existingAccessory.context.model = SwitchBotModel.StripLight
1845
1754
  existingAccessory.displayName = device.configDeviceName
1846
1755
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1847
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1848
- existingAccessory.context.connectionType = await this.connectionType(device);
1849
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1850
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1851
- this.api.updatePlatformAccessories([existingAccessory]);
1756
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1757
+ existingAccessory.context.connectionType = await this.connectionType(device)
1758
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1759
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1760
+ this.api.updatePlatformAccessories([existingAccessory])
1852
1761
  // create the accessory handler for the restored accessory
1853
1762
  // this is imported from `platformAccessory.ts`
1854
- new StripLight(this, existingAccessory, device);
1855
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1763
+ new StripLight(this, existingAccessory, device)
1764
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1856
1765
  } else {
1857
- this.unregisterPlatformAccessories(existingAccessory);
1766
+ this.unregisterPlatformAccessories(existingAccessory)
1858
1767
  }
1859
1768
  } else if (await this.registerDevice(device)) {
1860
1769
  // create a new accessory
1861
1770
  const accessory = new this.api.platformAccessory(device.configDeviceName
1862
1771
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1863
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1772
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1864
1773
 
1865
1774
  // store a copy of the device object in the `accessory.context`
1866
1775
  // the `context` property can be used to store any data about the accessory you may need
1867
- accessory.context.device = device;
1868
- accessory.context.deviceId = device.deviceId;
1869
- accessory.context.deviceType = device.deviceType;
1870
- accessory.context.model = SwitchBotModel.StripLight;
1776
+ accessory.context.device = device
1777
+ accessory.context.deviceId = device.deviceId
1778
+ accessory.context.deviceType = device.deviceType
1779
+ accessory.context.model = SwitchBotModel.StripLight
1871
1780
  accessory.displayName = device.configDeviceName
1872
1781
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1873
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1874
- accessory.context.connectionType = await this.connectionType(device);
1875
- accessory.context.connectionType = await this.connectionType(device);
1876
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1877
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1878
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1782
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1783
+ accessory.context.connectionType = await this.connectionType(device)
1784
+ accessory.context.connectionType = await this.connectionType(device)
1785
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1786
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1787
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1879
1788
  // create the accessory handler for the newly create accessory
1880
1789
  // this is imported from `platformAccessory.ts`
1881
- new StripLight(this, accessory, device);
1882
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1790
+ new StripLight(this, accessory, device)
1791
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1883
1792
 
1884
1793
  // publish device externally or link the accessory to your platform
1885
- this.externalOrPlatform(device, accessory);
1886
- this.accessories.push(accessory);
1794
+ this.externalOrPlatform(device, accessory)
1795
+ this.accessories.push(accessory)
1887
1796
  } else {
1888
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1797
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1889
1798
  }
1890
1799
  }
1891
1800
 
1892
1801
  private async createFan(device: device & devicesConfig) {
1893
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1802
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1894
1803
 
1895
1804
  // see if an accessory with the same uuid has already been registered and restored from
1896
1805
  // the cached devices we stored in the `configureAccessory` method above
1897
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1806
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1898
1807
 
1899
1808
  if (existingAccessory) {
1900
1809
  // the accessory already exists
1901
1810
  if (await this.registerDevice(device)) {
1902
1811
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1903
- existingAccessory.context.device = device;
1904
- existingAccessory.context.deviceId = device.deviceId;
1905
- existingAccessory.context.deviceType = device.deviceType;
1906
- existingAccessory.context.model = SwitchBotModel.BatteryCirculatorFan;
1812
+ existingAccessory.context.device = device
1813
+ existingAccessory.context.deviceId = device.deviceId
1814
+ existingAccessory.context.deviceType = device.deviceType
1815
+ existingAccessory.context.model = SwitchBotModel.BatteryCirculatorFan
1907
1816
  existingAccessory.displayName = device.configDeviceName
1908
1817
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1909
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1910
- existingAccessory.context.connectionType = await this.connectionType(device);
1911
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1912
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1913
- this.api.updatePlatformAccessories([existingAccessory]);
1818
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1819
+ existingAccessory.context.connectionType = await this.connectionType(device)
1820
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1821
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1822
+ this.api.updatePlatformAccessories([existingAccessory])
1914
1823
  // create the accessory handler for the restored accessory
1915
1824
  // this is imported from `platformAccessory.ts`
1916
- new Fan(this, existingAccessory, device);
1917
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1825
+ new Fan(this, existingAccessory, device)
1826
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1918
1827
  } else {
1919
- this.unregisterPlatformAccessories(existingAccessory);
1828
+ this.unregisterPlatformAccessories(existingAccessory)
1920
1829
  }
1921
1830
  } else if (await this.registerDevice(device)) {
1922
1831
  // create a new accessory
1923
1832
  const accessory = new this.api.platformAccessory(device.configDeviceName
1924
1833
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1925
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1834
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1926
1835
 
1927
1836
  // store a copy of the device object in the `accessory.context`
1928
1837
  // the `context` property can be used to store any data about the accessory you may need
1929
- accessory.context.device = device;
1930
- accessory.context.deviceId = device.deviceId;
1931
- accessory.context.deviceType = device.deviceType;
1932
- accessory.context.model = SwitchBotModel.BatteryCirculatorFan;
1838
+ accessory.context.device = device
1839
+ accessory.context.deviceId = device.deviceId
1840
+ accessory.context.deviceType = device.deviceType
1841
+ accessory.context.model = SwitchBotModel.BatteryCirculatorFan
1933
1842
  accessory.displayName = device.configDeviceName
1934
1843
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1935
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1936
- accessory.context.connectionType = await this.connectionType(device);
1937
- accessory.context.connectionType = await this.connectionType(device);
1938
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1939
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
1940
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1844
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1845
+ accessory.context.connectionType = await this.connectionType(device)
1846
+ accessory.context.connectionType = await this.connectionType(device)
1847
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1848
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1849
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
1941
1850
  // create the accessory handler for the newly create accessory
1942
1851
  // this is imported from `platformAccessory.ts`
1943
- new Fan(this, accessory, device);
1944
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1852
+ new Fan(this, accessory, device)
1853
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
1945
1854
 
1946
1855
  // publish device externally or link the accessory to your platform
1947
- this.externalOrPlatform(device, accessory);
1948
- this.accessories.push(accessory);
1856
+ this.externalOrPlatform(device, accessory)
1857
+ this.accessories.push(accessory)
1949
1858
  } else {
1950
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1859
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
1951
1860
  }
1952
1861
  }
1953
1862
 
1954
1863
  private async createRobotVacuumCleaner(device: device & devicesConfig) {
1955
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`);
1864
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.deviceType}`)
1956
1865
 
1957
1866
  // see if an accessory with the same uuid has already been registered and restored from
1958
1867
  // the cached devices we stored in the `configureAccessory` method above
1959
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1868
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
1960
1869
 
1961
1870
  if (existingAccessory) {
1962
1871
  // the accessory already exists
1963
1872
  if (await this.registerDevice(device)) {
1964
1873
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
1965
- existingAccessory.context.device = device;
1966
- existingAccessory.context.deviceId = device.deviceId;
1967
- existingAccessory.context.deviceType = device.deviceType;
1968
- existingAccessory.context.model = device.deviceType === 'Robot Vacuum Cleaner S1' ? SwitchBotModel.RobotVacuumCleanerS1
1969
- : device.deviceType === 'Robot Vacuum Cleaner S1 Plus' ? SwitchBotModel.RobotVacuumCleanerS1Plus
1970
- : device.deviceType === 'Robot Vacuum Cleaner S10' ? SwitchBotModel.RobotVacuumCleanerS10
1971
- : device.deviceType === 'WoSweeper' ? SwitchBotModel.WoSweeper : device.deviceType === 'WoSweeperMini' ? SwitchBotModel.WoSweeperMini
1972
- : SwitchBotModel.Unknown;
1874
+ existingAccessory.context.device = device
1875
+ existingAccessory.context.deviceId = device.deviceId
1876
+ existingAccessory.context.deviceType = device.deviceType
1877
+ existingAccessory.context.model = device.deviceType === 'Robot Vacuum Cleaner S1'
1878
+ ? SwitchBotModel.RobotVacuumCleanerS1
1879
+ : device.deviceType === 'Robot Vacuum Cleaner S1 Plus'
1880
+ ? SwitchBotModel.RobotVacuumCleanerS1Plus
1881
+ : device.deviceType === 'Robot Vacuum Cleaner S10'
1882
+ ? SwitchBotModel.RobotVacuumCleanerS10
1883
+ : device.deviceType === 'WoSweeper'
1884
+ ? SwitchBotModel.WoSweeper
1885
+ : device.deviceType === 'WoSweeperMini'
1886
+ ? SwitchBotModel.WoSweeperMini
1887
+ : SwitchBotModel.Unknown
1973
1888
  existingAccessory.displayName = device.configDeviceName
1974
1889
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1975
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
1976
- existingAccessory.context.connectionType = await this.connectionType(device);
1977
- existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
1978
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
1979
- this.api.updatePlatformAccessories([existingAccessory]);
1890
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1891
+ existingAccessory.context.connectionType = await this.connectionType(device)
1892
+ existingAccessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1893
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1894
+ this.api.updatePlatformAccessories([existingAccessory])
1980
1895
  // create the accessory handler for the restored accessory
1981
1896
  // this is imported from `platformAccessory.ts`
1982
- new RobotVacuumCleaner(this, existingAccessory, device);
1983
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`);
1897
+ new RobotVacuumCleaner(this, existingAccessory, device)
1898
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${existingAccessory.UUID})`)
1984
1899
  } else {
1985
- this.unregisterPlatformAccessories(existingAccessory);
1900
+ this.unregisterPlatformAccessories(existingAccessory)
1986
1901
  }
1987
1902
  } else if (await this.registerDevice(device)) {
1988
1903
  // create a new accessory
1989
1904
  const accessory = new this.api.platformAccessory(device.configDeviceName
1990
1905
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
1991
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1906
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
1992
1907
 
1993
1908
  // store a copy of the device object in the `accessory.context`
1994
1909
  // the `context` property can be used to store any data about the accessory you may need
1995
- accessory.context.device = device;
1996
- accessory.context.deviceId = device.deviceId;
1997
- accessory.context.deviceType = device.deviceType;
1998
- accessory.context.model = device.deviceType === 'Robot Vacuum Cleaner S1' ? SwitchBotModel.RobotVacuumCleanerS1
1999
- : device.deviceType === 'Robot Vacuum Cleaner S1 Plus' ? SwitchBotModel.RobotVacuumCleanerS1Plus
2000
- : device.deviceType === 'Robot Vacuum Cleaner S10' ? SwitchBotModel.RobotVacuumCleanerS10
2001
- : device.deviceType === 'WoSweeper' ? SwitchBotModel.WoSweeper : device.deviceType === 'WoSweeperMini' ? SwitchBotModel.WoSweeperMini
2002
- : SwitchBotModel.Unknown;
1910
+ accessory.context.device = device
1911
+ accessory.context.deviceId = device.deviceId
1912
+ accessory.context.deviceType = device.deviceType
1913
+ accessory.context.model = device.deviceType === 'Robot Vacuum Cleaner S1'
1914
+ ? SwitchBotModel.RobotVacuumCleanerS1
1915
+ : device.deviceType === 'Robot Vacuum Cleaner S1 Plus'
1916
+ ? SwitchBotModel.RobotVacuumCleanerS1Plus
1917
+ : device.deviceType === 'Robot Vacuum Cleaner S10'
1918
+ ? SwitchBotModel.RobotVacuumCleanerS10
1919
+ : device.deviceType === 'WoSweeper'
1920
+ ? SwitchBotModel.WoSweeper
1921
+ : device.deviceType === 'WoSweeperMini'
1922
+ ? SwitchBotModel.WoSweeperMini
1923
+ : SwitchBotModel.Unknown
2003
1924
  accessory.displayName = device.configDeviceName
2004
1925
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2005
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2006
- accessory.context.connectionType = await this.connectionType(device);
2007
- accessory.context.connectionType = await this.connectionType(device);
2008
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2009
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2010
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1926
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1927
+ accessory.context.connectionType = await this.connectionType(device)
1928
+ accessory.context.connectionType = await this.connectionType(device)
1929
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1930
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1931
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2011
1932
  // create the accessory handler for the newly create accessory
2012
1933
  // this is imported from `platformAccessory.ts`
2013
- new RobotVacuumCleaner(this, accessory, device);
2014
- await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`);
1934
+ new RobotVacuumCleaner(this, accessory, device)
1935
+ await this.debugLog(`${device.deviceType} uuid: ${device.deviceId}-${device.deviceType}, (${accessory.UUID})`)
2015
1936
 
2016
1937
  // publish device externally or link the accessory to your platform
2017
- this.externalOrPlatform(device, accessory);
2018
- this.accessories.push(accessory);
1938
+ this.externalOrPlatform(device, accessory)
1939
+ this.accessories.push(accessory)
2019
1940
  } else {
2020
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`);
1941
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.deviceType} deviceId: ${device.deviceId}`)
2021
1942
  }
2022
1943
  }
2023
1944
 
2024
1945
  private async createTV(device: irdevice & devicesConfig) {
2025
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
1946
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2026
1947
 
2027
1948
  // see if an accessory with the same uuid has already been registered and restored from
2028
1949
  // the cached devices we stored in the `configureAccessory` method above
2029
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
1950
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2030
1951
 
2031
1952
  if (!device.hide_device && existingAccessory) {
2032
1953
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2033
- existingAccessory.context.device = device;
2034
- existingAccessory.context.deviceId = device.deviceId;
2035
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2036
- existingAccessory.context.model = device.remoteType;
1954
+ existingAccessory.context.device = device
1955
+ existingAccessory.context.deviceId = device.deviceId
1956
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
1957
+ existingAccessory.context.model = device.remoteType
2037
1958
  existingAccessory.displayName = device.configDeviceName
2038
1959
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2039
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2040
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2041
- existingAccessory.context.connectionType = device.connectionType;
2042
- this.api.updatePlatformAccessories([existingAccessory]);
1960
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1961
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
1962
+ existingAccessory.context.connectionType = device.connectionType
1963
+ this.api.updatePlatformAccessories([existingAccessory])
2043
1964
  // create the accessory handler for the restored accessory
2044
1965
  // this is imported from `platformAccessory.ts`
2045
- new TV(this, existingAccessory, device);
2046
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
1966
+ new TV(this, existingAccessory, device)
1967
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2047
1968
  } else if (!device.hide_device && device.hubDeviceId) {
2048
1969
  // create a new accessory
2049
1970
  const accessory = new this.api.platformAccessory(device.configDeviceName
2050
1971
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2051
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
1972
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2052
1973
 
2053
1974
  // store a copy of the device object in the `accessory.context`
2054
1975
  // the `context` property can be used to store any data about the accessory you may need
2055
- accessory.context.device = device;
2056
- accessory.context.deviceId = device.deviceId;
2057
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2058
- accessory.context.model = device.remoteType;
1976
+ accessory.context.device = device
1977
+ accessory.context.deviceId = device.deviceId
1978
+ accessory.context.deviceType = `IR: ${device.remoteType}`
1979
+ accessory.context.model = device.remoteType
2059
1980
  accessory.displayName = device.configDeviceName
2060
1981
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2061
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2062
- accessory.context.connectionType = await this.connectionType(device);
2063
- accessory.context.connectionType = device.connectionType;
2064
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2065
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2066
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
1982
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
1983
+ accessory.context.connectionType = await this.connectionType(device)
1984
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
1985
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
1986
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2067
1987
  // create the accessory handler for the newly create accessory
2068
1988
  // this is imported from `platformAccessory.ts`
2069
- new TV(this, accessory, device);
2070
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
1989
+ new TV(this, accessory, device)
1990
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2071
1991
 
2072
- this.externalOrPlatformIR(device, accessory);
2073
- this.accessories.push(accessory);
1992
+ this.externalOrPlatform(device, accessory)
1993
+ this.accessories.push(accessory)
2074
1994
  } else {
2075
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
1995
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2076
1996
  }
2077
1997
  }
2078
1998
 
2079
1999
  private async createIRFan(device: irdevice & devicesConfig) {
2080
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2000
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2081
2001
 
2082
2002
  // see if an accessory with the same uuid has already been registered and restored from
2083
2003
  // the cached devices we stored in the `configureAccessory` method above
2084
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2004
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2085
2005
 
2086
2006
  if (existingAccessory) {
2087
2007
  // the accessory already exists
2088
2008
  if (!device.hide_device && device.hubDeviceId) {
2089
2009
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2090
- existingAccessory.context.device = device;
2091
- existingAccessory.context.deviceId = device.deviceId;
2092
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2093
- existingAccessory.context.model = device.remoteType;
2010
+ existingAccessory.context.device = device
2011
+ existingAccessory.context.deviceId = device.deviceId
2012
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2013
+ existingAccessory.context.model = device.remoteType
2094
2014
  existingAccessory.displayName = device.configDeviceName
2095
2015
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2096
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2097
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2098
- existingAccessory.context.connectionType = device.connectionType;
2099
- this.api.updatePlatformAccessories([existingAccessory]);
2016
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2017
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2018
+ existingAccessory.context.connectionType = device.connectionType
2019
+ this.api.updatePlatformAccessories([existingAccessory])
2100
2020
  // create the accessory handler for the restored accessory
2101
2021
  // this is imported from `platformAccessory.ts`
2102
- new IRFan(this, existingAccessory, device);
2103
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2022
+ new IRFan(this, existingAccessory, device)
2023
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2104
2024
  } else {
2105
- this.unregisterPlatformAccessories(existingAccessory);
2025
+ this.unregisterPlatformAccessories(existingAccessory)
2106
2026
  }
2107
2027
  } else if (!device.hide_device && device.hubDeviceId) {
2108
2028
  // create a new accessory
2109
2029
  const accessory = new this.api.platformAccessory(device.configDeviceName
2110
2030
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2111
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2031
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2112
2032
 
2113
2033
  // store a copy of the device object in the `accessory.context`
2114
2034
  // the `context` property can be used to store any data about the accessory you may need
2115
- accessory.context.device = device;
2116
- accessory.context.deviceId = device.deviceId;
2117
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2118
- accessory.context.model = device.remoteType;
2035
+ accessory.context.device = device
2036
+ accessory.context.deviceId = device.deviceId
2037
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2038
+ accessory.context.model = device.remoteType
2119
2039
  accessory.displayName = device.configDeviceName
2120
2040
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2121
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2122
- accessory.context.connectionType = await this.connectionType(device);
2123
- accessory.context.connectionType = device.connectionType;
2124
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2125
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2126
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2041
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2042
+ accessory.context.connectionType = await this.connectionType(device)
2043
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2044
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2045
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2127
2046
  // create the accessory handler for the newly create accessory
2128
2047
  // this is imported from `platformAccessory.ts`
2129
- new IRFan(this, accessory, device);
2130
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2048
+ new IRFan(this, accessory, device)
2049
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2131
2050
 
2132
2051
  // publish device externally or link the accessory to your platform
2133
- this.externalOrPlatformIR(device, accessory);
2134
- this.accessories.push(accessory);
2052
+ this.externalOrPlatform(device, accessory)
2053
+ this.accessories.push(accessory)
2135
2054
  } else {
2136
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2055
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2137
2056
  }
2138
2057
  }
2139
2058
 
2140
2059
  private async createLight(device: irdevice & devicesConfig) {
2141
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2060
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2142
2061
 
2143
2062
  // see if an accessory with the same uuid has already been registered and restored from
2144
2063
  // the cached devices we stored in the `configureAccessory` method above
2145
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2064
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2146
2065
 
2147
2066
  if (existingAccessory) {
2148
2067
  // the accessory already exists
2149
2068
  if (!device.hide_device && device.hubDeviceId) {
2150
2069
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2151
- existingAccessory.context.device = device;
2152
- existingAccessory.context.deviceId = device.deviceId;
2153
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2154
- existingAccessory.context.model = device.remoteType;
2070
+ existingAccessory.context.device = device
2071
+ existingAccessory.context.deviceId = device.deviceId
2072
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2073
+ existingAccessory.context.model = device.remoteType
2155
2074
  existingAccessory.displayName = device.configDeviceName
2156
2075
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2157
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2158
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2159
- existingAccessory.context.connectionType = device.connectionType;
2160
- this.api.updatePlatformAccessories([existingAccessory]);
2076
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2077
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2078
+ existingAccessory.context.connectionType = device.connectionType
2079
+ this.api.updatePlatformAccessories([existingAccessory])
2161
2080
  // create the accessory handler for the restored accessory
2162
2081
  // this is imported from `platformAccessory.ts`
2163
- new Light(this, existingAccessory, device);
2164
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2082
+ new Light(this, existingAccessory, device)
2083
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2165
2084
  } else {
2166
- this.unregisterPlatformAccessories(existingAccessory);
2085
+ this.unregisterPlatformAccessories(existingAccessory)
2167
2086
  }
2168
2087
  } else if (!device.hide_device && device.hubDeviceId) {
2169
2088
  // create a new accessory
2170
2089
  const accessory = new this.api.platformAccessory(device.configDeviceName
2171
2090
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2172
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2091
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2173
2092
 
2174
2093
  // store a copy of the device object in the `accessory.context`
2175
2094
  // the `context` property can be used to store any data about the accessory you may need
2176
- accessory.context.device = device;
2177
- accessory.context.deviceId = device.deviceId;
2178
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2179
- accessory.context.model = device.remoteType;
2095
+ accessory.context.device = device
2096
+ accessory.context.deviceId = device.deviceId
2097
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2098
+ accessory.context.model = device.remoteType
2180
2099
  accessory.displayName = device.configDeviceName
2181
2100
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2182
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2183
- accessory.context.connectionType = await this.connectionType(device);
2184
- accessory.context.connectionType = device.connectionType;
2185
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2186
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2187
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2101
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2102
+ accessory.context.connectionType = await this.connectionType(device)
2103
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2104
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2105
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2188
2106
  // create the accessory handler for the newly create accessory
2189
2107
  // this is imported from `platformAccessory.ts`
2190
- new Light(this, accessory, device);
2191
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2108
+ new Light(this, accessory, device)
2109
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2192
2110
 
2193
2111
  // publish device externally or link the accessory to your platform
2194
- this.externalOrPlatformIR(device, accessory);
2195
- this.accessories.push(accessory);
2112
+ this.externalOrPlatform(device, accessory)
2113
+ this.accessories.push(accessory)
2196
2114
  } else {
2197
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2115
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2198
2116
  }
2199
2117
  }
2200
2118
 
2201
2119
  private async createAirConditioner(device: irdevice & devicesConfig & irDevicesConfig) {
2202
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2120
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2203
2121
 
2204
2122
  // see if an accessory with the same uuid has already been registered and restored from
2205
2123
  // the cached devices we stored in the `configureAccessory` method above
2206
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2124
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2207
2125
 
2208
2126
  if (existingAccessory) {
2209
2127
  // the accessory already exists
2210
2128
  if (!device.hide_device && device.hubDeviceId) {
2211
2129
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2212
- existingAccessory.context.device = device;
2213
- existingAccessory.context.deviceId = device.deviceId;
2214
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2215
- existingAccessory.context.model = device.remoteType;
2130
+ existingAccessory.context.device = device
2131
+ existingAccessory.context.deviceId = device.deviceId
2132
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2133
+ existingAccessory.context.model = device.remoteType
2216
2134
  existingAccessory.displayName = device.configDeviceName
2217
2135
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2218
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2219
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2220
- existingAccessory.context.connectionType = device.connectionType;
2221
- this.api.updatePlatformAccessories([existingAccessory]);
2136
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2137
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2138
+ existingAccessory.context.connectionType = device.connectionType
2139
+ this.api.updatePlatformAccessories([existingAccessory])
2222
2140
  // create the accessory handler for the restored accessory
2223
2141
  // this is imported from `platformAccessory.ts`
2224
- new AirConditioner(this, existingAccessory, device);
2225
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2142
+ new AirConditioner(this, existingAccessory, device)
2143
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2226
2144
  } else {
2227
- this.unregisterPlatformAccessories(existingAccessory);
2145
+ this.unregisterPlatformAccessories(existingAccessory)
2228
2146
  }
2229
2147
  } else if (!device.hide_device && device.hubDeviceId) {
2230
2148
  // create a new accessory
2231
2149
  const accessory = new this.api.platformAccessory(device.configDeviceName
2232
2150
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2233
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2151
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2234
2152
 
2235
2153
  // store a copy of the device object in the `accessory.context`
2236
2154
  // the `context` property can be used to store any data about the accessory you may need
2237
- accessory.context.device = device;
2238
- accessory.context.deviceId = device.deviceId;
2239
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2240
- accessory.context.model = device.remoteType;
2155
+ accessory.context.device = device
2156
+ accessory.context.deviceId = device.deviceId
2157
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2158
+ accessory.context.model = device.remoteType
2241
2159
  accessory.displayName = device.configDeviceName
2242
2160
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2243
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2244
- accessory.context.connectionType = await this.connectionType(device);
2245
- accessory.context.connectionType = device.connectionType;
2246
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2247
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2248
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2161
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2162
+ accessory.context.connectionType = await this.connectionType(device)
2163
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2164
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2165
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2249
2166
  // create the accessory handler for the newly create accessory
2250
2167
  // this is imported from `platformAccessory.ts`
2251
- new AirConditioner(this, accessory, device);
2252
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2168
+ new AirConditioner(this, accessory, device)
2169
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2253
2170
 
2254
2171
  // publish device externally or link the accessory to your platform
2255
- this.externalOrPlatformIR(device, accessory);
2256
- this.accessories.push(accessory);
2172
+ this.externalOrPlatform(device, accessory)
2173
+ this.accessories.push(accessory)
2257
2174
  } else {
2258
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2175
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2259
2176
  }
2260
2177
  }
2261
2178
 
2262
2179
  private async createAirPurifier(device: irdevice & devicesConfig) {
2263
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2180
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2264
2181
 
2265
2182
  // see if an accessory with the same uuid has already been registered and restored from
2266
2183
  // the cached devices we stored in the `configureAccessory` method above
2267
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2184
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2268
2185
 
2269
2186
  if (existingAccessory) {
2270
2187
  // the accessory already exists
2271
2188
  if (!device.hide_device && device.hubDeviceId) {
2272
2189
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2273
- existingAccessory.context.device = device;
2274
- existingAccessory.context.deviceId = device.deviceId;
2275
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2276
- existingAccessory.context.model = device.remoteType;
2190
+ existingAccessory.context.device = device
2191
+ existingAccessory.context.deviceId = device.deviceId
2192
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2193
+ existingAccessory.context.model = device.remoteType
2277
2194
  existingAccessory.displayName = device.configDeviceName
2278
2195
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2279
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2280
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2281
- existingAccessory.context.connectionType = device.connectionType;
2282
- this.api.updatePlatformAccessories([existingAccessory]);
2196
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2197
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2198
+ existingAccessory.context.connectionType = device.connectionType
2199
+ this.api.updatePlatformAccessories([existingAccessory])
2283
2200
  // create the accessory handler for the restored accessory
2284
2201
  // this is imported from `platformAccessory.ts`
2285
- new AirPurifier(this, existingAccessory, device);
2286
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2202
+ new AirPurifier(this, existingAccessory, device)
2203
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2287
2204
  } else {
2288
- this.unregisterPlatformAccessories(existingAccessory);
2205
+ this.unregisterPlatformAccessories(existingAccessory)
2289
2206
  }
2290
2207
  } else if (!device.hide_device && device.hubDeviceId) {
2291
2208
  // create a new accessory
2292
2209
  const accessory = new this.api.platformAccessory(device.configDeviceName
2293
2210
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2294
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2211
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2295
2212
 
2296
2213
  // store a copy of the device object in the `accessory.context`
2297
2214
  // the `context` property can be used to store any data about the accessory you may need
2298
- accessory.context.device = device;
2299
- accessory.context.deviceId = device.deviceId;
2300
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2301
- accessory.context.model = device.remoteType;
2215
+ accessory.context.device = device
2216
+ accessory.context.deviceId = device.deviceId
2217
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2218
+ accessory.context.model = device.remoteType
2302
2219
  accessory.displayName = device.configDeviceName
2303
2220
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2304
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2305
- accessory.context.connectionType = await this.connectionType(device);
2306
- accessory.context.connectionType = device.connectionType;
2307
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2308
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2309
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2221
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2222
+ accessory.context.connectionType = await this.connectionType(device)
2223
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2224
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2225
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2310
2226
  // create the accessory handler for the newly create accessory
2311
2227
  // this is imported from `platformAccessory.ts`
2312
- new AirPurifier(this, accessory, device);
2313
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2228
+ new AirPurifier(this, accessory, device)
2229
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2314
2230
 
2315
2231
  // publish device externally or link the accessory to your platform
2316
- this.externalOrPlatformIR(device, accessory);
2317
- this.accessories.push(accessory);
2232
+ this.externalOrPlatform(device, accessory)
2233
+ this.accessories.push(accessory)
2318
2234
  } else {
2319
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2235
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2320
2236
  }
2321
2237
  }
2322
2238
 
2323
2239
  private async createWaterHeater(device: irdevice & devicesConfig) {
2324
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2240
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2325
2241
 
2326
2242
  // see if an accessory with the same uuid has already been registered and restored from
2327
2243
  // the cached devices we stored in the `configureAccessory` method above
2328
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2244
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2329
2245
 
2330
2246
  if (existingAccessory) {
2331
2247
  // the accessory already exists
2332
2248
  if (!device.hide_device && device.hubDeviceId) {
2333
2249
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2334
- existingAccessory.context.device = device;
2335
- existingAccessory.context.deviceId = device.deviceId;
2336
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2337
- existingAccessory.context.model = device.remoteType;
2250
+ existingAccessory.context.device = device
2251
+ existingAccessory.context.deviceId = device.deviceId
2252
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2253
+ existingAccessory.context.model = device.remoteType
2338
2254
  existingAccessory.displayName = device.configDeviceName
2339
2255
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2340
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2341
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2342
- existingAccessory.context.connectionType = device.connectionType;
2343
- this.api.updatePlatformAccessories([existingAccessory]);
2256
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2257
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2258
+ existingAccessory.context.connectionType = device.connectionType
2259
+ this.api.updatePlatformAccessories([existingAccessory])
2344
2260
  // create the accessory handler for the restored accessory
2345
2261
  // this is imported from `platformAccessory.ts`
2346
- new WaterHeater(this, existingAccessory, device);
2347
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2262
+ new WaterHeater(this, existingAccessory, device)
2263
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2348
2264
  } else {
2349
- this.unregisterPlatformAccessories(existingAccessory);
2265
+ this.unregisterPlatformAccessories(existingAccessory)
2350
2266
  }
2351
2267
  } else if (!device.hide_device && device.hubDeviceId) {
2352
2268
  // create a new accessory
2353
2269
  const accessory = new this.api.platformAccessory(device.configDeviceName
2354
2270
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2355
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2271
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2356
2272
 
2357
2273
  // store a copy of the device object in the `accessory.context`
2358
2274
  // the `context` property can be used to store any data about the accessory you may need
2359
- accessory.context.device = device;
2360
- accessory.context.deviceId = device.deviceId;
2361
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2362
- accessory.context.model = device.remoteType;
2275
+ accessory.context.device = device
2276
+ accessory.context.deviceId = device.deviceId
2277
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2278
+ accessory.context.model = device.remoteType
2363
2279
  accessory.displayName = device.configDeviceName
2364
2280
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2365
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2366
- accessory.context.connectionType = await this.connectionType(device);
2367
- accessory.context.connectionType = device.connectionType;
2368
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2369
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2370
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2281
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2282
+ accessory.context.connectionType = await this.connectionType(device)
2283
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2284
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2285
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2371
2286
  // create the accessory handler for the newly create accessory
2372
2287
  // this is imported from `platformAccessory.ts`
2373
- new WaterHeater(this, accessory, device);
2374
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2288
+ new WaterHeater(this, accessory, device)
2289
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2375
2290
 
2376
2291
  // publish device externally or link the accessory to your platform
2377
- this.externalOrPlatformIR(device, accessory);
2378
- this.accessories.push(accessory);
2292
+ this.externalOrPlatform(device, accessory)
2293
+ this.accessories.push(accessory)
2379
2294
  } else {
2380
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2295
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2381
2296
  }
2382
2297
  }
2383
2298
 
2384
2299
  private async createVacuumCleaner(device: irdevice & devicesConfig) {
2385
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2300
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2386
2301
 
2387
2302
  // see if an accessory with the same uuid has already been registered and restored from
2388
2303
  // the cached devices we stored in the `configureAccessory` method above
2389
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2304
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2390
2305
 
2391
2306
  if (existingAccessory) {
2392
2307
  // the accessory already exists
2393
2308
  if (!device.hide_device && device.hubDeviceId) {
2394
2309
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2395
- existingAccessory.context.device = device;
2396
- existingAccessory.context.deviceId = device.deviceId;
2397
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2398
- existingAccessory.context.model = device.remoteType;
2310
+ existingAccessory.context.device = device
2311
+ existingAccessory.context.deviceId = device.deviceId
2312
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2313
+ existingAccessory.context.model = device.remoteType
2399
2314
  existingAccessory.displayName = device.configDeviceName
2400
2315
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2401
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2402
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2403
- existingAccessory.context.connectionType = device.connectionType;
2404
- this.api.updatePlatformAccessories([existingAccessory]);
2316
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2317
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2318
+ existingAccessory.context.connectionType = device.connectionType
2319
+ this.api.updatePlatformAccessories([existingAccessory])
2405
2320
  // create the accessory handler for the restored accessory
2406
2321
  // this is imported from `platformAccessory.ts`
2407
- new VacuumCleaner(this, existingAccessory, device);
2408
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2322
+ new VacuumCleaner(this, existingAccessory, device)
2323
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2409
2324
  } else {
2410
- this.unregisterPlatformAccessories(existingAccessory);
2325
+ this.unregisterPlatformAccessories(existingAccessory)
2411
2326
  }
2412
2327
  } else if (!device.hide_device && device.hubDeviceId) {
2413
2328
  // create a new accessory
2414
2329
  const accessory = new this.api.platformAccessory(device.configDeviceName
2415
2330
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2416
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2331
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2417
2332
 
2418
2333
  // store a copy of the device object in the `accessory.context`
2419
2334
  // the `context` property can be used to store any data about the accessory you may need
2420
- accessory.context.device = device;
2421
- accessory.context.deviceId = device.deviceId;
2422
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2423
- accessory.context.model = device.remoteType;
2335
+ accessory.context.device = device
2336
+ accessory.context.deviceId = device.deviceId
2337
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2338
+ accessory.context.model = device.remoteType
2424
2339
  accessory.displayName = device.configDeviceName
2425
2340
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2426
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2427
- accessory.context.connectionType = await this.connectionType(device);
2428
- accessory.context.connectionType = device.connectionType;
2429
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2430
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2431
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2341
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2342
+ accessory.context.connectionType = await this.connectionType(device)
2343
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2344
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2345
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2432
2346
  // create the accessory handler for the newly create accessory
2433
2347
  // this is imported from `platformAccessory.ts`
2434
- new VacuumCleaner(this, accessory, device);
2435
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2348
+ new VacuumCleaner(this, accessory, device)
2349
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2436
2350
 
2437
2351
  // publish device externally or link the accessory to your platform
2438
- this.externalOrPlatformIR(device, accessory);
2439
- this.accessories.push(accessory);
2352
+ this.externalOrPlatform(device, accessory)
2353
+ this.accessories.push(accessory)
2440
2354
  } else {
2441
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2355
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2442
2356
  }
2443
2357
  }
2444
2358
 
2445
2359
  private async createCamera(device: irdevice & devicesConfig) {
2446
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2360
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2447
2361
 
2448
2362
  // see if an accessory with the same uuid has already been registered and restored from
2449
2363
  // the cached devices we stored in the `configureAccessory` method above
2450
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2364
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2451
2365
 
2452
2366
  if (existingAccessory) {
2453
2367
  // the accessory already exists
2454
2368
  if (!device.hide_device && device.hubDeviceId) {
2455
2369
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2456
- existingAccessory.context.device = device;
2457
- existingAccessory.context.deviceId = device.deviceId;
2458
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2459
- existingAccessory.context.model = device.remoteType;
2370
+ existingAccessory.context.device = device
2371
+ existingAccessory.context.deviceId = device.deviceId
2372
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2373
+ existingAccessory.context.model = device.remoteType
2460
2374
  existingAccessory.displayName = device.configDeviceName
2461
2375
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2462
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2463
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2464
- existingAccessory.context.connectionType = device.connectionType;
2465
- this.api.updatePlatformAccessories([existingAccessory]);
2376
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2377
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2378
+ existingAccessory.context.connectionType = device.connectionType
2379
+ this.api.updatePlatformAccessories([existingAccessory])
2466
2380
  // create the accessory handler for the restored accessory
2467
2381
  // this is imported from `platformAccessory.ts`
2468
- new Camera(this, existingAccessory, device);
2469
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2382
+ new Camera(this, existingAccessory, device)
2383
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2470
2384
  } else {
2471
- this.unregisterPlatformAccessories(existingAccessory);
2385
+ this.unregisterPlatformAccessories(existingAccessory)
2472
2386
  }
2473
2387
  } else if (!device.hide_device && device.hubDeviceId) {
2474
2388
  // create a new accessory
2475
2389
  const accessory = new this.api.platformAccessory(device.configDeviceName
2476
2390
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2477
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2391
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2478
2392
 
2479
2393
  // store a copy of the device object in the `accessory.context`
2480
2394
  // the `context` property can be used to store any data about the accessory you may need
2481
- accessory.context.device = device;
2482
- accessory.context.deviceId = device.deviceId;
2483
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2484
- accessory.context.model = device.remoteType;
2395
+ accessory.context.device = device
2396
+ accessory.context.deviceId = device.deviceId
2397
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2398
+ accessory.context.model = device.remoteType
2485
2399
  accessory.displayName = device.configDeviceName
2486
2400
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2487
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2488
- accessory.context.connectionType = await this.connectionType(device);
2489
- accessory.context.connectionType = device.connectionType;
2490
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2491
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2492
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2401
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2402
+ accessory.context.connectionType = await this.connectionType(device)
2403
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2404
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2405
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2493
2406
  // create the accessory handler for the newly create accessory
2494
2407
  // this is imported from `platformAccessory.ts`
2495
- new Camera(this, accessory, device);
2496
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2408
+ new Camera(this, accessory, device)
2409
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2497
2410
 
2498
2411
  // publish device externally or link the accessory to your platform
2499
- this.externalOrPlatformIR(device, accessory);
2500
- this.accessories.push(accessory);
2412
+ this.externalOrPlatform(device, accessory)
2413
+ this.accessories.push(accessory)
2501
2414
  } else {
2502
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2415
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2503
2416
  }
2504
2417
  }
2505
2418
 
2506
2419
  private async createOthers(device: irdevice & devicesConfig) {
2507
- const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`);
2420
+ const uuid = this.api.hap.uuid.generate(`${device.deviceId}-${device.remoteType}`)
2508
2421
 
2509
2422
  // see if an accessory with the same uuid has already been registered and restored from
2510
2423
  // the cached devices we stored in the `configureAccessory` method above
2511
- const existingAccessory = this.accessories.find((accessory) => accessory.UUID === uuid);
2424
+ const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid)
2512
2425
 
2513
2426
  if (existingAccessory) {
2514
2427
  // the accessory already exists
2515
2428
  if (!device.hide_device && device.hubDeviceId) {
2516
2429
  // if you need to update the accessory.context then you should run `api.updatePlatformAccessories`. eg.:
2517
- existingAccessory.context.device = device;
2518
- existingAccessory.context.deviceId = device.deviceId;
2519
- existingAccessory.context.deviceType = `IR: ${device.remoteType}`;
2520
- existingAccessory.context.model = device.remoteType;
2430
+ existingAccessory.context.device = device
2431
+ existingAccessory.context.deviceId = device.deviceId
2432
+ existingAccessory.context.deviceType = `IR: ${device.remoteType}`
2433
+ existingAccessory.context.model = device.remoteType
2521
2434
  existingAccessory.displayName = device.configDeviceName
2522
2435
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2523
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2524
- await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`);
2525
- existingAccessory.context.connectionType = device.connectionType;
2526
- this.api.updatePlatformAccessories([existingAccessory]);
2436
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2437
+ await this.infoLog(`Restoring existing accessory from cache: ${existingAccessory.displayName} deviceId: ${device.deviceId}`)
2438
+ existingAccessory.context.connectionType = device.connectionType
2439
+ this.api.updatePlatformAccessories([existingAccessory])
2527
2440
  // create the accessory handler for the restored accessory
2528
2441
  // this is imported from `platformAccessory.ts`
2529
- new Others(this, existingAccessory, device);
2530
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`);
2442
+ new Others(this, existingAccessory, device)
2443
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${existingAccessory.UUID})`)
2531
2444
  } else {
2532
- this.unregisterPlatformAccessories(existingAccessory);
2445
+ this.unregisterPlatformAccessories(existingAccessory)
2533
2446
  }
2534
2447
  } else if (!device.hide_device && device.hubDeviceId) {
2535
2448
  // create a new accessory
2536
2449
  const accessory = new this.api.platformAccessory(device.configDeviceName
2537
2450
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2538
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid);
2451
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName), uuid)
2539
2452
 
2540
2453
  // store a copy of the device object in the `accessory.context`
2541
2454
  // the `context` property can be used to store any data about the accessory you may need
2542
- accessory.context.device = device;
2543
- accessory.context.deviceId = device.deviceId;
2544
- accessory.context.deviceType = `IR: ${device.remoteType}`;
2545
- accessory.context.model = device.remoteType;
2455
+ accessory.context.device = device
2456
+ accessory.context.deviceId = device.deviceId
2457
+ accessory.context.deviceType = `IR: ${device.remoteType}`
2458
+ accessory.context.model = device.remoteType
2546
2459
  accessory.displayName = device.configDeviceName
2547
2460
  ? await this.validateAndCleanDisplayName(device.configDeviceName, 'configDeviceName', device.configDeviceName)
2548
- : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName);
2549
- accessory.context.connectionType = await this.connectionType(device);
2550
- accessory.context.connectionType = device.connectionType;
2551
- accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0';
2552
- const newOrExternal = !device.external ? 'Adding new' : 'Loading external';
2553
- await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`);
2461
+ : await this.validateAndCleanDisplayName(device.deviceName, 'deviceName', device.deviceName)
2462
+ accessory.context.connectionType = await this.connectionType(device)
2463
+ accessory.context.version = device.firmware ?? device.version ?? this.version ?? '0.0.0'
2464
+ const newOrExternal = !device.external ? 'Adding new' : 'Loading external'
2465
+ await this.infoLog(`${newOrExternal} accessory: ${accessory.displayName} deviceId: ${device.deviceId}`)
2554
2466
  // create the accessory handler for the newly create accessory
2555
2467
  // this is imported from `platformAccessory.ts`
2556
- new Others(this, accessory, device);
2557
- await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`);
2468
+ new Others(this, accessory, device)
2469
+ await this.debugLog(`${device.remoteType} uuid: ${device.deviceId}-${device.remoteType}, (${accessory.UUID})`)
2558
2470
 
2559
2471
  // publish device externally or link the accessory to your platform
2560
- this.externalOrPlatformIR(device, accessory);
2561
- this.accessories.push(accessory);
2472
+ this.externalOrPlatform(device, accessory)
2473
+ this.accessories.push(accessory)
2562
2474
  } else {
2563
- await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`);
2475
+ await this.debugLog(`Device not registered: ${device.deviceName} ${device.remoteType} deviceId: ${device.deviceId}`)
2564
2476
  }
2565
2477
  }
2566
2478
 
2567
2479
  async registerCurtains(device: device & devicesConfig): Promise<boolean> {
2568
- /*function isCurtainDevice(device: device & devicesConfig): device is (curtain | curtain3) & devicesConfig {
2569
- return device.deviceType === 'Curtain' || device.deviceType === 'Curtain3';
2570
- }
2571
-
2572
- function isBlindTiltDevice(device: device & devicesConfig): device is blindTilt & devicesConfig {
2573
- return device.deviceType === 'Blind Tilt';
2574
- }*/
2575
- let registerWindowCovering: boolean;
2480
+ let registerWindowCovering: boolean
2576
2481
  if (isCurtainDevice(device)) {
2577
- await this.debugWarnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds},`
2578
- + ` master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group},`
2579
- + ` connectionType: ${device.connectionType}`);
2580
- registerWindowCovering = await this.registerWindowCovering(device);
2482
+ await this.debugWarnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, curtainDevicesIds: ${device.curtainDevicesIds},x master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}, connectionType: ${device.connectionType}`)
2483
+ registerWindowCovering = await this.registerWindowCovering(device)
2581
2484
  } else if (isBlindTiltDevice(device)) {
2582
- await this.debugWarnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, blindTiltDevicesIds: ${device.blindTiltDevicesIds},`
2583
- + ` master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group},`
2584
- + ` connectionType: ${device.connectionType}`);
2585
- registerWindowCovering = await this.registerWindowCovering(device);
2485
+ await this.debugWarnLog(`deviceName: ${device.deviceName} deviceId: ${device.deviceId}, blindTiltDevicesIds: ${device.blindTiltDevicesIds}, master: ${device.master}, group: ${device.group}, disable_group: ${device.curtain?.disable_group}, connectionType: ${device.connectionType}`)
2486
+ registerWindowCovering = await this.registerWindowCovering(device)
2586
2487
  } else {
2587
- registerWindowCovering = false;
2488
+ registerWindowCovering = false
2588
2489
  }
2589
- return registerWindowCovering;
2490
+ return registerWindowCovering
2590
2491
  }
2591
2492
 
2592
2493
  async registerWindowCovering(device: ((curtain | curtain3) & devicesConfig) | (blindTilt & devicesConfig)) {
2593
- await this.debugLog(`master: ${device.master}`);
2594
- let registerCurtain: boolean;
2494
+ await this.debugLog(`master: ${device.master}`)
2495
+ let registerCurtain: boolean
2595
2496
  if (device.master && device.group) {
2596
2497
  // OpenAPI: Master Curtains/Blind Tilt in Group
2597
- registerCurtain = true;
2598
- await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master},`
2599
- + ` device.group: ${device.group} connectionType; ${device.connectionType}`);
2600
- await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
2498
+ registerCurtain = true
2499
+ await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master}, device.group: ${device.group} connectionType; ${device.connectionType}`)
2500
+ await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`)
2601
2501
  } else if (!device.master && device.curtain?.disable_group) {
2602
- //!device.group && device.connectionType === 'BLE'
2502
+ // !device.group && device.connectionType === 'BLE'
2603
2503
  // OpenAPI: Non-Master Curtains/Blind Tilts that has Disable Grouping Checked
2604
- registerCurtain = true;
2605
- await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master}, disable_group: `
2606
- + `${device.curtain?.disable_group}, connectionType; ${device.connectionType}`);
2607
- await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
2504
+ registerCurtain = true
2505
+ await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master}, disable_group: ${device.curtain?.disable_group}, connectionType; ${device.connectionType}`)
2506
+ await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`)
2608
2507
  } else if (device.master && !device.group) {
2609
2508
  // OpenAPI: Master Curtains/Blind Tilts not in Group
2610
- registerCurtain = true;
2611
- await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master},`
2612
- + ` device.group: ${device.group} connectionType; ${device.connectionType}`);
2613
- await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
2509
+ registerCurtain = true
2510
+ await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] device.master: ${device.master}, device.group: ${device.group} connectionType; ${device.connectionType}`)
2511
+ await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`)
2614
2512
  } else if (device.connectionType === 'BLE') {
2615
2513
  // BLE: Curtains/Blind Tilt
2616
- registerCurtain = true;
2617
- await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] connectionType: ${device.connectionType}, `
2618
- + ` group: ${device.group}`);
2619
- await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
2514
+ registerCurtain = true
2515
+ await this.debugLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] connectionType: ${device.connectionType}, group: ${device.group}`)
2516
+ await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`)
2620
2517
  } else {
2621
- registerCurtain = false;
2622
- await this.debugErrorLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] disable_group: ${device.curtain?.disable_group},`
2623
- + ` device.master: ${device.master}, device.group: ${device.group}`);
2624
- await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}, device.connectionType: ${device.connectionType}`);
2518
+ registerCurtain = false
2519
+ await this.debugErrorLog(`deviceName: ${device.deviceName} [${device.deviceType} Config] disable_group: ${device.curtain?.disable_group}, device.master: ${device.master}, device.group: ${device.group}`)
2520
+ await this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}, device.connectionType: ${device.connectionType}`)
2625
2521
  }
2626
- return registerCurtain;
2522
+ return registerCurtain
2627
2523
  }
2628
2524
 
2629
2525
  async connectionType(device: device & devicesConfig): Promise<any> {
2630
- let connectionType: string;
2526
+ let connectionType: string
2631
2527
  if (!device.connectionType && this.config.credentials?.token && this.config.credentials.secret) {
2632
- connectionType = 'OpenAPI';
2528
+ connectionType = 'OpenAPI'
2633
2529
  } else {
2634
- connectionType = device.connectionType!;
2530
+ connectionType = device.connectionType!
2635
2531
  }
2636
- return connectionType;
2532
+ return connectionType
2637
2533
  }
2638
2534
 
2639
2535
  async registerDevice(device: device & devicesConfig) {
2640
- device.connectionType = await this.connectionType(device);
2641
- let registerDevice: boolean;
2642
- if (!device.hide_device && device.connectionType === 'BLE/OpenAPI') {
2643
- switch (device.deviceType) {
2644
- case 'Curtain':
2645
- case 'Curtain3':
2646
- case 'Blind Tilt':
2647
- registerDevice = await this.registerCurtains(device);
2648
- await this.debugWarnLog(`Device: ${device.deviceName} ${device.deviceType} registerDevice: ${registerDevice}`);
2649
- break;
2650
- default:
2651
- registerDevice = true;
2652
- await this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
2653
- }
2654
- if (registerDevice === true) {
2655
- await this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
2656
- } else {
2657
- await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
2658
- }
2659
- } else if (!device.hide_device && device.deviceId && device.configDeviceType && device.configDeviceName && device.connectionType === 'BLE') {
2660
- switch (device.deviceType) {
2661
- case 'Curtain':
2662
- case 'Curtain3':
2663
- case 'Blind Tilt':
2664
- registerDevice = await this.registerCurtains(device);
2665
- await this.debugWarnLog(`Device: ${device.deviceName} ${device.deviceType} registerDevice: ${registerDevice}`);
2666
- break;
2667
- default:
2668
- registerDevice = true;
2669
- await this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
2670
- }
2671
- if (registerDevice === true) {
2672
- await this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
2673
- } else {
2674
- await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
2675
- }
2676
- } else if (!device.hide_device && device.connectionType === 'OpenAPI') {
2677
- switch (device.deviceType) {
2678
- case 'Curtain':
2679
- case 'Curtain3':
2680
- case 'Blind Tilt':
2681
- registerDevice = await this.registerCurtains(device);
2682
- await this.debugWarnLog(`Device: ${device.deviceName} ${device.deviceType} registerDevice: ${registerDevice}`);
2683
- break;
2684
- default:
2685
- registerDevice = true;
2686
- await this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
2687
- }
2688
- if (registerDevice === true) {
2689
- await this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
2690
- } else {
2691
- await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
2692
- }
2693
- } else if (!device.hide_device && device.connectionType === 'Disabled') {
2694
- switch (device.deviceType) {
2695
- case 'Curtain':
2696
- case 'Curtain3':
2697
- case 'Blind Tilt':
2698
- registerDevice = await this.registerCurtains(device);
2699
- await this.debugWarnLog(`Device: ${device.deviceName} ${device.deviceType} registerDevice: ${registerDevice}`);
2700
- break;
2701
- default:
2702
- registerDevice = true;
2703
- await this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
2704
- }
2705
- await this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will continue to display in HomeKit`);
2706
- } else if (!device.connectionType && !device.hide_device) {
2707
- registerDevice = false;
2708
- await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`);
2709
- } else if (device.hide_device) {
2710
- registerDevice = false;
2711
- await this.debugErrorLog(`Device: ${device.deviceName} hide_device: ${device.hide_device}, will not display in HomeKit`);
2712
- } else {
2713
- registerDevice = false;
2714
- await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, hide_device: `
2715
- + `${device.hide_device}, will not display in HomeKit`);
2716
- }
2717
- return registerDevice;
2718
- }
2719
-
2720
- public async externalOrPlatformIR(device: device & irDevicesConfig, accessory: PlatformAccessory) {
2721
- /**
2722
- * Publish as external accessory
2723
- * Only one TV can exist per bridge, to bypass this limitation, you should
2724
- * publish your TV as an external accessory.
2725
- */
2726
- if (device.external) {
2727
- await this.debugWarnLog(`${accessory.displayName} External Accessory Mode`);
2728
- this.externalAccessory(accessory);
2536
+ device.connectionType = await this.connectionType(device)
2537
+ let registerDevice: boolean
2538
+
2539
+ const shouldRegister = !device.hide_device && (device.connectionType === 'BLE/OpenAPI' || (device.deviceId && device.configDeviceType && device.configDeviceName && device.connectionType === 'BLE') || device.connectionType === 'OpenAPI' || device.connectionType === 'Disabled')
2540
+
2541
+ if (shouldRegister) {
2542
+ registerDevice = await this.handleDeviceRegistration(device)
2729
2543
  } else {
2730
- await this.debugLog(`${accessory.displayName} External Accessory Mode: ${device.external}`);
2731
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
2544
+ registerDevice = false
2545
+ await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, hide_device: ${device.hide_device}, will not display in HomeKit`)
2732
2546
  }
2547
+
2548
+ return registerDevice
2733
2549
  }
2734
2550
 
2735
- public async externalOrPlatform(device: device & devicesConfig, accessory: PlatformAccessory) {
2736
- if (device.external) {
2737
- await this.debugWarnLog(`${accessory.displayName} External Accessory Mode`);
2738
- await this.externalAccessory(accessory);
2551
+ async handleDeviceRegistration(device: device & devicesConfig): Promise<boolean> {
2552
+ let registerDevice: boolean
2553
+
2554
+ switch (device.deviceType) {
2555
+ case 'Curtain':
2556
+ case 'Curtain3':
2557
+ case 'Blind Tilt':
2558
+ registerDevice = await this.registerCurtains(device)
2559
+ await this.debugWarnLog(`Device: ${device.deviceName} ${device.deviceType} registerDevice: ${registerDevice}`)
2560
+ break
2561
+ default:
2562
+ registerDevice = true
2563
+ await this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`)
2564
+ }
2565
+
2566
+ if (registerDevice) {
2567
+ await this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`)
2739
2568
  } else {
2740
- await this.debugLog(`${accessory.displayName} External Accessory Mode: ${device.external}`);
2741
- this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
2569
+ await this.debugErrorLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will not display in HomeKit`)
2742
2570
  }
2571
+
2572
+ return registerDevice
2743
2573
  }
2744
2574
 
2745
- public async externalAccessory(accessory: PlatformAccessory) {
2746
- this.api.publishExternalAccessories(PLUGIN_NAME, [accessory]);
2575
+ public async externalOrPlatform(device: device & (irDevicesConfig | devicesConfig), accessory: PlatformAccessory) {
2576
+ const { displayName } = accessory
2577
+ const isExternal = device.external ?? false
2578
+
2579
+ if (isExternal) {
2580
+ await this.debugWarnLog(`${displayName} External Accessory Mode`)
2581
+ this.api.publishExternalAccessories(PLUGIN_NAME, [accessory])
2582
+ } else {
2583
+ await this.debugLog(`${displayName} External Accessory Mode: ${isExternal}`)
2584
+ this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory])
2585
+ }
2747
2586
  }
2748
2587
 
2749
2588
  public unregisterPlatformAccessories(existingAccessory: PlatformAccessory) {
2589
+ const { displayName } = existingAccessory
2750
2590
  // remove platform accessories when no longer present
2751
- this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory]);
2752
- this.warnLog(`Removing existing accessory from cache: ${existingAccessory.displayName}`);
2591
+ this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [existingAccessory])
2592
+ this.warnLog(`Removing existing accessory from cache: ${displayName}`)
2753
2593
  }
2754
2594
 
2595
+ /**
2596
+ * Handles the status codes returned by the device and logs appropriate messages.
2597
+ *
2598
+ * @param statusCode - The status code returned by the device.
2599
+ * @returns A promise that resolves when the logging is complete.
2600
+ */
2755
2601
  async statusCode(statusCode: number): Promise<void> {
2756
- switch (statusCode) {
2757
- case 151:
2758
- await this.errorLog(`Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here: ` +
2759
- 'https://tinyurl.com/SwitchBotFeatureRequest');
2760
- break;
2761
- case 152:
2762
- await this.errorLog(`Device not found, statusCode: ${statusCode}`);
2763
- break;
2764
- case 160:
2765
- await this.errorLog(`Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`);
2766
- break;
2767
- case 161:
2768
- await this.errorLog(`Device is offline, statusCode: ${statusCode}`);
2769
- break;
2770
- case 171:
2771
- await this.errorLog(`is offline, statusCode: ${statusCode}`);
2772
- break;
2773
- case 190:
2774
- await this.errorLog(`Requests reached the daily limit, statusCode: ${statusCode}`);
2775
- break;
2776
- case 100:
2777
- await this.debugLog(`Command successfully sent, statusCode: ${statusCode}`);
2778
- break;
2779
- case 200:
2780
- await this.debugLog(`Request successful, statusCode: ${statusCode}`);
2781
- break;
2782
- case 400:
2783
- await this.errorLog('Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request '
2784
- + `payload, statusCode: ${statusCode}`);
2785
- break;
2786
- case 401:
2787
- await this.errorLog('Unauthorized, Authorization for the API is required, but the request has not been authenticated, '
2788
- + `statusCode: ${statusCode}`);
2789
- break;
2790
- case 403:
2791
- await this.errorLog('Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not '
2792
- + `found, statusCode: ${statusCode}`);
2793
- break;
2794
- case 404:
2795
- await this.errorLog(`Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`);
2796
- break;
2797
- case 406:
2798
- await this.errorLog('Not Acceptable, The client has requested a MIME type via the Accept header for a value not supported by the server, '
2799
- + `statusCode: ${statusCode}`);
2800
- break;
2801
- case 415:
2802
- await this.errorLog('Unsupported Media Type, The client has defined a contentType header that is not supported by the server, '
2803
- + `statusCode: ${statusCode}`);
2804
- break;
2805
- case 422:
2806
- await this.errorLog('Unprocessable Entity, The client has made a valid request, but the server cannot process it. This is often used for '
2807
- + `APIs for which certain limits have been exceeded, statusCode: ${statusCode}`);
2808
- break;
2809
- case 429:
2810
- await this.errorLog('Too Many Requests, The client has exceeded the number of requests allowed for a given time window, '
2811
- + `statusCode: ${statusCode}`);
2812
- break;
2813
- case 500:
2814
- await this.errorLog('Internal Server Error, An unexpected error on the SmartThings servers has occurred. These errors should be rare, '
2815
- + `statusCode: ${statusCode}`);
2816
- break;
2817
- default:
2818
- await this.errorLog(`Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: ' + 'https://tinyurl.com/SwitchBotBug`);
2602
+ const messages: { [key: number]: string } = {
2603
+ 151: `Command not supported by this device type, statusCode: ${statusCode}, Submit Feature Request Here:
2604
+ https://tinyurl.com/SwitchBotFeatureRequest`,
2605
+ 152: `Device not found, statusCode: ${statusCode}`,
2606
+ 160: `Command is not supported, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`,
2607
+ 161: `Device is offline, statusCode: ${statusCode}`,
2608
+ 171: `is offline, statusCode: ${statusCode}`,
2609
+ 190: `Requests reached the daily limit, statusCode: ${statusCode}`,
2610
+ 100: `Command successfully sent, statusCode: ${statusCode}`,
2611
+ 200: `Request successful, statusCode: ${statusCode}`,
2612
+ 400: `Bad Request, The client has issued an invalid request. This is commonly used to specify validation errors in a request payload,
2613
+ statusCode: ${statusCode}`,
2614
+ 401: `Unauthorized, Authorization for the API is required, but the request has not been authenticated, statusCode: ${statusCode}`,
2615
+ 403: `Forbidden, The request has been authenticated but does not have appropriate permissions, or a requested resource is not found,
2616
+ statusCode: ${statusCode}`,
2617
+ 404: `Not Found, Specifies the requested path does not exist, statusCode: ${statusCode}`,
2618
+ 406: `Not Acceptable, The client has requested a MIME type via the Accept header for a value not supported by the server,
2619
+ statusCode: ${statusCode}`,
2620
+ 415: `Unsupported Media Type, The client has defined a contentType header that is not supported by the server, statusCode: ${statusCode}`,
2621
+ 422: `Unprocessable Entity, The client has made a valid request, but the server cannot process it. This is often used for APIs for which
2622
+ certain limits have been exceeded, statusCode: ${statusCode}`,
2623
+ 429: `Too Many Requests, The client has exceeded the number of requests allowed for a given time window, statusCode: ${statusCode}`,
2624
+ 500: `Internal Server Error, An unexpected error on the SmartThings servers has occurred. These errors should be rare,
2625
+ statusCode: ${statusCode}`,
2626
+ }
2627
+
2628
+ const message = messages[statusCode] ?? `Unknown statusCode, statusCode: ${statusCode}, Submit Bugs Here: https://tinyurl.com/SwitchBotBug`
2629
+
2630
+ if ([100, 200].includes(statusCode)) {
2631
+ await this.debugLog(message)
2632
+ } else {
2633
+ await this.errorLog(message)
2819
2634
  }
2820
2635
  }
2821
2636
 
2822
- async retryRequest(deviceMaxRetries: number, deviceDelayBetweenRetries: number, url: string | URL | UrlObject,
2823
- options?: { dispatcher?: Dispatcher } & Omit<Dispatcher.RequestOptions,
2824
- 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>): Promise<{ body: any; statusCode: number }> {
2825
- let retryCount = 0;
2826
- const maxRetries = deviceMaxRetries;
2827
- const delayBetweenRetries = deviceDelayBetweenRetries;
2637
+ async retryRequest(deviceMaxRetries: number, deviceDelayBetweenRetries: number, url: string | URL | UrlObject, options?: { dispatcher?: Dispatcher } & Omit<Dispatcher.RequestOptions, 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>): Promise<{ body: any, statusCode: number }> {
2638
+ let retryCount = 0
2639
+ const maxRetries = deviceMaxRetries
2640
+ const delayBetweenRetries = deviceDelayBetweenRetries
2828
2641
  while (retryCount < maxRetries) {
2829
2642
  try {
2830
- const { body, statusCode } = await request(url, options);
2643
+ const { body, statusCode } = await request(url, options)
2831
2644
  if (statusCode === 200 || statusCode === 100) {
2832
- return { body, statusCode };
2645
+ return { body, statusCode }
2833
2646
  } else {
2834
- await this.debugLog(`Received status code: ${statusCode}`);
2647
+ await this.debugLog(`Received status code: ${statusCode}`)
2835
2648
  }
2836
2649
  } catch (error: any) {
2837
- await this.errorLog(`Error making request: ${error.message}`);
2650
+ await this.errorLog(`Error making request: ${error.message}`)
2838
2651
  }
2839
- retryCount++;
2840
- await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`);
2841
- await sleep(delayBetweenRetries);
2652
+ retryCount++
2653
+ await this.debugLog(`Retry attempt ${retryCount} of ${maxRetries}`)
2654
+ await sleep(delayBetweenRetries)
2842
2655
  }
2843
- return { body: null, statusCode: -1 };
2656
+ return { body: null, statusCode: -1 }
2844
2657
  }
2845
2658
 
2846
2659
  // BLE Connection
2847
- async connectBLE(accessory: PlatformAccessory, device: device & devicesConfig) {
2848
- let switchbot: any;
2660
+ async connectBLE(accessory: PlatformAccessory, device: device & devicesConfig): Promise<any> {
2849
2661
  try {
2850
- const SwitchBot = (await import('node-switchbot')).SwitchBot;
2851
- queueScheduler.schedule(() => (switchbot = new SwitchBot()));
2852
- await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`);
2662
+ const { SwitchBot } = await import('node-switchbot')
2663
+ const switchbot = new SwitchBot()
2664
+ queueScheduler.schedule(async () => switchbot)
2665
+ await this.debugLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}`)
2666
+ return switchbot
2853
2667
  } catch (e: any) {
2854
- switchbot = false;
2855
- await this.errorLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' found: ${switchbot}, Error: ${e}`);
2668
+ await this.errorLog(`${device.deviceType}: ${accessory.displayName} 'node-switchbot' not found, Error: ${e}`)
2669
+ return false
2856
2670
  }
2857
- return switchbot;
2858
2671
  }
2859
2672
 
2860
- async getVersion() {
2673
+ async getVersion(): Promise<string> {
2861
2674
  const json = JSON.parse(
2862
2675
  readFileSync(
2863
2676
  new URL('../package.json', import.meta.url),
2864
2677
  'utf-8',
2865
2678
  ),
2866
- );
2867
- await this.debugLog(`Plugin Version: ${json.version}`);
2868
- this.version = json.version;
2679
+ )
2680
+ await this.debugLog(`Plugin Version: ${json.version}`)
2681
+ this.version = json.version
2682
+ return json.version
2869
2683
  }
2870
2684
 
2871
2685
  async getPlatformConfigSettings() {
2872
- const platformConfig: SwitchBotPlatformConfig['options'] = {};
2873
- if (this.config.options) {
2874
- if (this.config.options.logging) {
2875
- platformConfig.logging = this.config.options.logging;
2876
- }
2877
- if (this.config.options.refreshRate) {
2878
- platformConfig.refreshRate = this.config.options.refreshRate;
2879
- }
2880
- if (this.config.options.updateRate) {
2881
- platformConfig.updateRate = this.config.options.updateRate;
2882
- }
2883
- if (this.config.options.pushRate) {
2884
- platformConfig.pushRate = this.config.options.pushRate;
2885
- }
2886
- if (this.config.options.maxRetries) {
2887
- this.maxRetries = this.config.options.maxRetries;
2888
- platformConfig.maxRetries = this.config.options.maxRetries;
2889
- } else {
2890
- this.maxRetries = 3;
2891
- this.debugWarnLog('Using Default Max Retries');
2892
- platformConfig.maxRetries = this.maxRetries;
2686
+ const { options } = this.config
2687
+ const platformConfig: SwitchBotPlatformConfig['options'] = {}
2688
+
2689
+ if (options) {
2690
+ platformConfig.logging = options.logging
2691
+ platformConfig.refreshRate = options.refreshRate
2692
+ platformConfig.updateRate = options.updateRate
2693
+ platformConfig.pushRate = options.pushRate
2694
+
2695
+ this.maxRetries = options.maxRetries || 3
2696
+ platformConfig.maxRetries = this.maxRetries
2697
+ if (!options.maxRetries) {
2698
+ await this.debugWarnLog('Using Default Max Retries')
2893
2699
  }
2894
- if (this.config.options.delayBetweenRetries) {
2895
- this.delayBetweenRetries = this.config.options.delayBetweenRetries * 1000;
2896
- platformConfig.delayBetweenRetries = this.config.options.delayBetweenRetries;
2897
- } else {
2898
- this.delayBetweenRetries = 3000;
2899
- this.debugWarnLog('Using Default Delay Between Retries');
2900
- platformConfig.delayBetweenRetries = this.delayBetweenRetries / 1000;
2700
+
2701
+ this.delayBetweenRetries = (options.delayBetweenRetries || 3) * 1000
2702
+ platformConfig.delayBetweenRetries = this.delayBetweenRetries / 1000
2703
+ if (!options.delayBetweenRetries) {
2704
+ await this.debugWarnLog('Using Default Delay Between Retries')
2901
2705
  }
2902
- if (Object.entries(platformConfig).length !== 0) {
2903
- await this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`);
2706
+
2707
+ if (Object.keys(platformConfig).length) {
2708
+ await this.debugLog(`Platform Config: ${JSON.stringify(platformConfig)}`)
2904
2709
  }
2905
- this.platformConfig = platformConfig;
2710
+
2711
+ this.platformConfig = platformConfig
2906
2712
  }
2907
2713
  }
2908
2714
 
2909
2715
  async getPlatformLogSettings() {
2910
- this.debugMode = process.argv.includes('-D') ?? process.argv.includes('--debug');
2716
+ this.debugMode = process.argv.includes('-D') ?? process.argv.includes('--debug')
2911
2717
  if (this.config.options?.logging === 'debug' || this.config.options?.logging === 'standard' || this.config.options?.logging === 'none') {
2912
- this.platformLogging = this.config.options.logging;
2913
- await this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`);
2718
+ this.platformLogging = this.config.options.logging
2719
+ await this.debugWarnLog(`Using Config Logging: ${this.platformLogging}`)
2914
2720
  } else if (this.debugMode) {
2915
- this.platformLogging = 'debugMode';
2916
- await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2721
+ this.platformLogging = 'debugMode'
2722
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`)
2917
2723
  } else {
2918
- this.platformLogging = 'standard';
2919
- await this.debugWarnLog(`Using ${this.platformLogging} Logging`);
2724
+ this.platformLogging = 'standard'
2725
+ await this.debugWarnLog(`Using ${this.platformLogging} Logging`)
2920
2726
  }
2921
2727
  }
2922
2728
 
2923
2729
  /**
2924
2730
  * Validate and clean a string value for a Name Characteristic.
2925
- * @param displayName
2926
- * @param name
2927
- * @param value
2928
- * @returns string value
2929
- */
2731
+ * @param displayName - The display name of the accessory.
2732
+ * @param name - The name of the characteristic.
2733
+ * @param value - The value to be validated and cleaned.
2734
+ * @returns The cleaned string value.
2735
+ */
2930
2736
  async validateAndCleanDisplayName(displayName: string, name: string, value: string): Promise<string> {
2931
- const validPattern = /^[a-zA-Z0-9][a-zA-Z0-9 ']*[a-zA-Z0-9]$/;
2932
- const invalidCharsPattern = /[^a-zA-Z0-9 ']/g;
2933
- const invalidStartEndPattern = /^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$/g;
2934
-
2935
- if (!validPattern.test(value)) {
2936
- this.warnLog(`WARNING: The accessory '${displayName}' has an invalid '${name}' characteristic ('${value}'). Please use only alphanumeric,`
2937
- + ' space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may'
2938
- + ' prevent the accessory from being added in the Home App or cause unresponsiveness.');
2939
-
2940
- // Remove invalid characters
2941
- if (invalidCharsPattern.test(value)) {
2942
- this.warnLog(`Removing invalid characters from '${name}' characteristic`);
2943
- value = value.replace(invalidCharsPattern, '');
2944
- }
2737
+ if (this.config.options?.allowInvalidCharacters) {
2738
+ return value
2739
+ } else {
2740
+ const validPattern = /^[\p{L}\p{N}][\p{L}\p{N} ']*[\p{L}\p{N}]$/u
2741
+ const invalidCharsPattern = /[^\p{L}\p{N} ']/gu
2742
+ const invalidStartEndPattern = /^[^\p{L}\p{N}]+|[^\p{L}\p{N}]+$/gu
2743
+
2744
+ if (typeof value === 'string' && !validPattern.test(value)) {
2745
+ await this.warnLog(`WARNING: The accessory '${displayName}' has an invalid '${name}' characteristic ('${value}'). Please use only alphanumeric, space, and apostrophe characters. Ensure it starts and ends with an alphabetic or numeric character, and avoid emojis. This may prevent the accessory from being added in the Home App or cause unresponsiveness.`)
2746
+
2747
+ // Remove invalid characters
2748
+ if (invalidCharsPattern.test(value)) {
2749
+ const before = value
2750
+ await this.warnLog(`Removing invalid characters from '${name}' characteristic, if you feel this is incorrect, please enable \'allowInvalidCharacter\' in the config to allow all characters`)
2751
+ value = value.replace(invalidCharsPattern, '')
2752
+ await this.warnLog(`${name} Before: '${before}' After: '${value}'`)
2753
+ }
2945
2754
 
2946
- // Ensure it starts and ends with an alphanumeric character
2947
- if (invalidStartEndPattern.test(value)) {
2948
- this.warnLog(`Removing invalid starting or ending characters from '${name}' characteristic`);
2949
- value = value.replace(invalidStartEndPattern, '');
2755
+ // Ensure it starts and ends with an alphanumeric character
2756
+ if (invalidStartEndPattern.test(value)) {
2757
+ const before = value
2758
+ await this.warnLog(`Removing invalid starting or ending characters from '${name}' characteristic, if you feel this is incorrect, please enable \'allowInvalidCharacter\' in the config to allow all characters`)
2759
+ value = value.replace(invalidStartEndPattern, '')
2760
+ await this.warnLog(`${name} Before: '${before}' After: '${value}'`)
2761
+ }
2950
2762
  }
2951
- }
2952
2763
 
2953
- return value;
2764
+ return value
2765
+ }
2954
2766
  }
2955
2767
 
2956
2768
  /**
@@ -2959,48 +2771,48 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
2959
2771
  */
2960
2772
  async infoLog(...log: any[]): Promise<void> {
2961
2773
  if (await this.enablingPlatformLogging()) {
2962
- this.log.info(String(...log));
2774
+ this.log.info(String(...log))
2963
2775
  }
2964
2776
  }
2965
2777
 
2966
2778
  async successLog(...log: any[]): Promise<void> {
2967
2779
  if (await this.enablingPlatformLogging()) {
2968
- this.log.success(String(...log));
2780
+ this.log.success(String(...log))
2969
2781
  }
2970
2782
  }
2971
2783
 
2972
2784
  async debugSuccessLog(...log: any[]): Promise<void> {
2973
2785
  if (await this.enablingPlatformLogging()) {
2974
2786
  if (await this.loggingIsDebug()) {
2975
- this.log.success('[DEBUG]', String(...log));
2787
+ this.log.success('[DEBUG]', String(...log))
2976
2788
  }
2977
2789
  }
2978
2790
  }
2979
2791
 
2980
2792
  async warnLog(...log: any[]): Promise<void> {
2981
2793
  if (await this.enablingPlatformLogging()) {
2982
- this.log.warn(String(...log));
2794
+ this.log.warn(String(...log))
2983
2795
  }
2984
2796
  }
2985
2797
 
2986
2798
  async debugWarnLog(...log: any[]): Promise<void> {
2987
2799
  if (await this.enablingPlatformLogging()) {
2988
2800
  if (await this.loggingIsDebug()) {
2989
- this.log.warn('[DEBUG]', String(...log));
2801
+ this.log.warn('[DEBUG]', String(...log))
2990
2802
  }
2991
2803
  }
2992
2804
  }
2993
2805
 
2994
2806
  async errorLog(...log: any[]): Promise<void> {
2995
2807
  if (await this.enablingPlatformLogging()) {
2996
- this.log.error(String(...log));
2808
+ this.log.error(String(...log))
2997
2809
  }
2998
2810
  }
2999
2811
 
3000
2812
  async debugErrorLog(...log: any[]): Promise<void> {
3001
2813
  if (await this.enablingPlatformLogging()) {
3002
2814
  if (await this.loggingIsDebug()) {
3003
- this.log.error('[DEBUG]', String(...log));
2815
+ this.log.error('[DEBUG]', String(...log))
3004
2816
  }
3005
2817
  }
3006
2818
  }
@@ -3008,18 +2820,18 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
3008
2820
  async debugLog(...log: any[]): Promise<void> {
3009
2821
  if (await this.enablingPlatformLogging()) {
3010
2822
  if (this.platformLogging === 'debug') {
3011
- this.log.info('[DEBUG]', String(...log));
2823
+ this.log.info('[DEBUG]', String(...log))
3012
2824
  } else if (this.platformLogging === 'debugMode') {
3013
- this.log.debug(String(...log));
2825
+ this.log.debug(String(...log))
3014
2826
  }
3015
2827
  }
3016
2828
  }
3017
2829
 
3018
2830
  async loggingIsDebug(): Promise<boolean> {
3019
- return this.platformLogging === 'debugMode' || this.platformLogging === 'debug';
2831
+ return this.platformLogging === 'debugMode' || this.platformLogging === 'debug'
3020
2832
  }
3021
2833
 
3022
2834
  async enablingPlatformLogging(): Promise<boolean> {
3023
- return this.platformLogging === 'debugMode' || this.platformLogging === 'debug' || this.platformLogging === 'standard';
2835
+ return this.platformLogging === 'debugMode' || this.platformLogging === 'debug' || this.platformLogging === 'standard'
3024
2836
  }
3025
2837
  }