node-switchbot 4.0.0-beta.17 → 4.0.0-beta.19

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 (224) hide show
  1. package/BLE.md +215 -838
  2. package/CHANGELOG.md +96 -1371
  3. package/OpenAPI.md +169 -135
  4. package/README.md +1 -1
  5. package/dist/devices/base.d.ts.map +1 -1
  6. package/dist/devices/base.js.map +1 -1
  7. package/dist/devices/wo-ai-hub.d.ts.map +1 -1
  8. package/dist/devices/wo-ai-hub.js.map +1 -1
  9. package/dist/devices/wo-air-purifier-pm25.d.ts.map +1 -1
  10. package/dist/devices/wo-air-purifier-pm25.js.map +1 -1
  11. package/dist/devices/wo-air-purifier.d.ts.map +1 -1
  12. package/dist/devices/wo-air-purifier.js.map +1 -1
  13. package/dist/devices/wo-blind-tilt.d.ts.map +1 -1
  14. package/dist/devices/wo-blind-tilt.js.map +1 -1
  15. package/dist/devices/wo-bulb.d.ts.map +1 -1
  16. package/dist/devices/wo-bulb.js.map +1 -1
  17. package/dist/devices/wo-candle-warmer-lamp.d.ts.map +1 -1
  18. package/dist/devices/wo-candle-warmer-lamp.js.map +1 -1
  19. package/dist/devices/wo-keypad-vision-pro.d.ts.map +1 -1
  20. package/dist/devices/wo-keypad-vision-pro.js.map +1 -1
  21. package/dist/devices/wo-pan-tilt-cam-plus-3k.d.ts.map +1 -1
  22. package/dist/devices/wo-pan-tilt-cam-plus-3k.js.map +1 -1
  23. package/dist/devices/wo-relay-switch-1.d.ts.map +1 -1
  24. package/dist/devices/wo-relay-switch-1.js.map +1 -1
  25. package/dist/devices/wo-remote-with-screen.d.ts.map +1 -1
  26. package/dist/devices/wo-remote-with-screen.js.map +1 -1
  27. package/dist/devices/wo-rgbic-neon-wire-rope-light.d.ts.map +1 -1
  28. package/dist/devices/wo-rgbic-neon-wire-rope-light.js.map +1 -1
  29. package/dist/devices/wo-water-detector.d.ts.map +1 -1
  30. package/dist/devices/wo-water-detector.js.map +1 -1
  31. package/dist/index.d.ts +1 -1
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +1 -1
  34. package/dist/index.js.map +1 -1
  35. package/dist/utils/fallback-handler.js +0 -1
  36. package/dist/utils/fallback-handler.js.map +1 -1
  37. package/dist/utils/index.d.ts.map +1 -1
  38. package/dist/utils/index.js.map +1 -1
  39. package/package.json +25 -10
  40. package/.gitattributes +0 -2
  41. package/.github/copilot-instructions.md +0 -179
  42. package/.github/dependabot.yml +0 -17
  43. package/.github/labeler.yml +0 -38
  44. package/.github/npm-version-script-esm.js +0 -97
  45. package/.github/workflows/beta-release.yml +0 -52
  46. package/.github/workflows/release.yml +0 -71
  47. package/.github/workflows/stale.yml +0 -12
  48. package/branding/Node_x_SwitchBot.svg +0 -8
  49. package/branding/icon.png +0 -0
  50. package/branding/switchbot.png +0 -0
  51. package/docs/.nojekyll +0 -1
  52. package/docs/assets/hierarchy.js +0 -1
  53. package/docs/assets/highlight.css +0 -92
  54. package/docs/assets/icons.js +0 -18
  55. package/docs/assets/icons.svg +0 -1
  56. package/docs/assets/main.js +0 -60
  57. package/docs/assets/navigation.js +0 -1
  58. package/docs/assets/search.js +0 -1
  59. package/docs/assets/style.css +0 -1633
  60. package/docs/classes/APIError.html +0 -6
  61. package/docs/classes/APINotAvailableError.html +0 -4
  62. package/docs/classes/BLEConnection.html +0 -25
  63. package/docs/classes/BLENotAvailableError.html +0 -4
  64. package/docs/classes/BLEScanner.html +0 -17
  65. package/docs/classes/CommandFailedError.html +0 -6
  66. package/docs/classes/ConnectionTimeoutError.html +0 -5
  67. package/docs/classes/DeviceManager.html +0 -25
  68. package/docs/classes/DeviceNotFoundError.html +0 -4
  69. package/docs/classes/DeviceOverrideStateDuringConnection.html +0 -82
  70. package/docs/classes/DiscoveryError.html +0 -5
  71. package/docs/classes/OpenAPIClient.html +0 -66
  72. package/docs/classes/SequenceDevice.html +0 -92
  73. package/docs/classes/SwitchBot.html +0 -22
  74. package/docs/classes/SwitchBotDevice.html +0 -115
  75. package/docs/classes/SwitchBotError.html +0 -4
  76. package/docs/classes/ValidationError.html +0 -5
  77. package/docs/classes/WoAIHub.html +0 -116
  78. package/docs/classes/WoAirPurifier.html +0 -88
  79. package/docs/classes/WoAirPurifierPM25.html +0 -117
  80. package/docs/classes/WoAirPurifierTable.html +0 -89
  81. package/docs/classes/WoArtFrame.html +0 -103
  82. package/docs/classes/WoBlindTilt.html +0 -89
  83. package/docs/classes/WoBulb.html +0 -102
  84. package/docs/classes/WoCandleWarmerLamp.html +0 -122
  85. package/docs/classes/WoCeilingLight.html +0 -103
  86. package/docs/classes/WoCirculatorFan.html +0 -89
  87. package/docs/classes/WoClimatePanel.html +0 -89
  88. package/docs/classes/WoContact.html +0 -82
  89. package/docs/classes/WoCurtain.html +0 -94
  90. package/docs/classes/WoFloorLamp.html +0 -103
  91. package/docs/classes/WoGarageDoorOpener.html +0 -94
  92. package/docs/classes/WoHand.html +0 -95
  93. package/docs/classes/WoHub2.html +0 -82
  94. package/docs/classes/WoHub3.html +0 -83
  95. package/docs/classes/WoHubMiniMatter.html +0 -83
  96. package/docs/classes/WoHumi.html +0 -89
  97. package/docs/classes/WoHumi2.html +0 -90
  98. package/docs/classes/WoIOSensorTH.html +0 -83
  99. package/docs/classes/WoKeypad.html +0 -83
  100. package/docs/classes/WoKeypadVision.html +0 -83
  101. package/docs/classes/WoKeypadVisionPro.html +0 -83
  102. package/docs/classes/WoLeak.html +0 -82
  103. package/docs/classes/WoPanTiltCamPlus3K.html +0 -122
  104. package/docs/classes/WoPlugMiniJP.html +0 -89
  105. package/docs/classes/WoPlugMiniUS.html +0 -88
  106. package/docs/classes/WoPresence.html +0 -82
  107. package/docs/classes/WoRGBICBulb.html +0 -114
  108. package/docs/classes/WoRGBICNeonWireRopeLight.html +0 -126
  109. package/docs/classes/WoRGBICWWFloorLamp.html +0 -114
  110. package/docs/classes/WoRGBICWWStripLight.html +0 -114
  111. package/docs/classes/WoRelaySwitch1.html +0 -93
  112. package/docs/classes/WoRelaySwitch1PM.html +0 -94
  113. package/docs/classes/WoRelaySwitch2PM.html +0 -98
  114. package/docs/classes/WoRemote.html +0 -83
  115. package/docs/classes/WoRemoteWithScreen.html +0 -116
  116. package/docs/classes/WoRollerShade.html +0 -95
  117. package/docs/classes/WoSensorTH.html +0 -82
  118. package/docs/classes/WoSensorTHPlus.html +0 -83
  119. package/docs/classes/WoSensorTHPro.html +0 -83
  120. package/docs/classes/WoSensorTHProCO2.html +0 -83
  121. package/docs/classes/WoSmartLock.html +0 -91
  122. package/docs/classes/WoSmartLockLite.html +0 -91
  123. package/docs/classes/WoSmartLockPro.html +0 -95
  124. package/docs/classes/WoSmartLockProWiFi.html +0 -95
  125. package/docs/classes/WoSmartLockVision.html +0 -91
  126. package/docs/classes/WoSmartLockVisionPro.html +0 -95
  127. package/docs/classes/WoSmartThermostatRadiator.html +0 -89
  128. package/docs/classes/WoStrip.html +0 -103
  129. package/docs/classes/WoStripLight3.html +0 -103
  130. package/docs/classes/WoVacuum.html +0 -98
  131. package/docs/classes/WoVacuumK10Plus.html +0 -98
  132. package/docs/classes/WoVacuumK10Pro.html +0 -98
  133. package/docs/classes/WoVacuumK10ProCombo.html +0 -98
  134. package/docs/classes/WoVacuumK11Plus.html +0 -98
  135. package/docs/classes/WoVacuumK20.html +0 -98
  136. package/docs/classes/WoVacuumS10.html +0 -98
  137. package/docs/classes/WoVacuumS20.html +0 -98
  138. package/docs/classes/WoWaterDetector.html +0 -116
  139. package/docs/enums/LogLevel.html +0 -7
  140. package/docs/enums/SwitchBotBLEModel.html +0 -40
  141. package/docs/enums/SwitchBotBLEModelName.html +0 -40
  142. package/docs/functions/updateBaseURL.html +0 -3
  143. package/docs/hierarchy.html +0 -1
  144. package/docs/index.html +0 -55
  145. package/docs/interfaces/APICommandRequest.html +0 -5
  146. package/docs/interfaces/APICommandResponse.html +0 -5
  147. package/docs/interfaces/APIDevice.html +0 -18
  148. package/docs/interfaces/APIDeviceStatus.html +0 -36
  149. package/docs/interfaces/APIErrorResponse.html +0 -5
  150. package/docs/interfaces/APIResponse.html +0 -5
  151. package/docs/interfaces/AirPurifierCommands.html +0 -6
  152. package/docs/interfaces/AirPurifierServiceData.html +0 -29
  153. package/docs/interfaces/AirPurifierStatus.html +0 -17
  154. package/docs/interfaces/BLEAdvertisement.html +0 -13
  155. package/docs/interfaces/BLEScanOptions.html +0 -10
  156. package/docs/interfaces/BLEServiceData.html +0 -27
  157. package/docs/interfaces/BlindTiltCommands.html +0 -7
  158. package/docs/interfaces/BlindTiltServiceData.html +0 -30
  159. package/docs/interfaces/BlindTiltStatus.html +0 -16
  160. package/docs/interfaces/BotCommands.html +0 -8
  161. package/docs/interfaces/BotServiceData.html +0 -27
  162. package/docs/interfaces/BotStatus.html +0 -14
  163. package/docs/interfaces/BulbCommands.html +0 -10
  164. package/docs/interfaces/BulbServiceData.html +0 -35
  165. package/docs/interfaces/BulbStatus.html +0 -16
  166. package/docs/interfaces/CeilingLightCommands.html +0 -10
  167. package/docs/interfaces/CeilingLightServiceData.html +0 -35
  168. package/docs/interfaces/CeilingLightStatus.html +0 -16
  169. package/docs/interfaces/CommandResult.html +0 -12
  170. package/docs/interfaces/ContactServiceData.html +0 -30
  171. package/docs/interfaces/ContactStatus.html +0 -15
  172. package/docs/interfaces/CurtainCommands.html +0 -8
  173. package/docs/interfaces/CurtainExtendedInfo.html +0 -4
  174. package/docs/interfaces/CurtainServiceData.html +0 -31
  175. package/docs/interfaces/CurtainStatus.html +0 -17
  176. package/docs/interfaces/DeviceInfo.html +0 -36
  177. package/docs/interfaces/DeviceListResponse.html +0 -4
  178. package/docs/interfaces/DeviceStatus.html +0 -12
  179. package/docs/interfaces/DiscoveryOptions.html +0 -14
  180. package/docs/interfaces/HubServiceData.html +0 -31
  181. package/docs/interfaces/HubStatus.html +0 -15
  182. package/docs/interfaces/HumidifierCommands.html +0 -9
  183. package/docs/interfaces/HumidifierServiceData.html +0 -31
  184. package/docs/interfaces/HumidifierStatus.html +0 -18
  185. package/docs/interfaces/KeypadStatus.html +0 -13
  186. package/docs/interfaces/LeakServiceData.html +0 -28
  187. package/docs/interfaces/LeakStatus.html +0 -13
  188. package/docs/interfaces/LockCommands.html +0 -7
  189. package/docs/interfaces/LockServiceData.html +0 -29
  190. package/docs/interfaces/LockStatus.html +0 -15
  191. package/docs/interfaces/MeterServiceData.html +0 -30
  192. package/docs/interfaces/MeterStatus.html +0 -15
  193. package/docs/interfaces/MotionServiceData.html +0 -30
  194. package/docs/interfaces/MotionStatus.html +0 -14
  195. package/docs/interfaces/PlugCommands.html +0 -4
  196. package/docs/interfaces/PlugServiceData.html +0 -33
  197. package/docs/interfaces/PlugStatus.html +0 -16
  198. package/docs/interfaces/PresenceServiceData.html +0 -29
  199. package/docs/interfaces/PresenceStatus.html +0 -14
  200. package/docs/interfaces/RelaySwitchCommands.html +0 -6
  201. package/docs/interfaces/RelaySwitchServiceData.html +0 -30
  202. package/docs/interfaces/RelaySwitchStatus.html +0 -16
  203. package/docs/interfaces/RemoteStatus.html +0 -12
  204. package/docs/interfaces/SceneListResponse.html +0 -3
  205. package/docs/interfaces/StripCommands.html +0 -10
  206. package/docs/interfaces/StripServiceData.html +0 -35
  207. package/docs/interfaces/StripStatus.html +0 -16
  208. package/docs/interfaces/SwitchBotConfig.html +0 -39
  209. package/docs/interfaces/VacuumCommands.html +0 -8
  210. package/docs/interfaces/VacuumStatus.html +0 -16
  211. package/docs/interfaces/WebhookConfig.html +0 -4
  212. package/docs/interfaces/WebhookDetails.html +0 -7
  213. package/docs/interfaces/WebhookQueryResponse.html +0 -5
  214. package/docs/interfaces/WebhookSetupResponse.html +0 -5
  215. package/docs/media/BLE.md +0 -1259
  216. package/docs/media/OpenAPI.md +0 -186
  217. package/docs/modules.html +0 -1
  218. package/docs/types/ConnectionType.html +0 -2
  219. package/docs/types/PhysicalDeviceType.html +0 -2
  220. package/docs/types/VirtualDeviceType.html +0 -2
  221. package/docs/variables/urls.html +0 -1
  222. package/tmp-switchbot-scan.mjs +0 -79
  223. package/todo/todo.md +0 -15
  224. package/tsconfig.build.json +0 -17
package/docs/media/BLE.md DELETED
@@ -1,1259 +0,0 @@
1
- # SwitchBot BLE Documentation
2
-
3
- The `SwitchBot` class allows you to interact with SwitchBot devices using the SwitchBot BLE. This documentation provides an overview of how to install, set up, and use the various methods available in the `SwitchBotBLE` class.
4
-
5
- ## Table of Contents
6
-
7
- - [BLE (Bluetooth Low Energy)](#ble-bluetooth-low-energy)
8
- - [Supported OS](#supported-os)
9
- - [Dependencies](#dependencies)
10
- - [Importing and Setting Up](#importing-and-setting-up)
11
- - [`SwitchBotBLE` Object](#switchbot-object)
12
- - [`discover()` method](#discover-method)
13
- - [`ondiscover` event handler](#ondiscover-event-handler)
14
- - [`startScan()` method](#startscan-method)
15
- - [`onadvertisement` event handler](#onadvertisement-event-handler)
16
- - [`stopScan()` method](#stopscan-method)
17
- - [`wait()` method](#wait-method)
18
- - [`SwitchBotDevice` Object](#switchbotdevice-object)
19
- - [Properties](#properties)
20
- - [`getDeviceName()` method](#getdevicename-method)
21
- - [`setDeviceName()` method](#setdevicename-method)
22
- - [`connect()` method](#connect-method)
23
- - [`disconnect()` method](#disconnect-method)
24
- - [`onconnect` event handler](#onconnect-event-handler)
25
- - [`ondisconnect` event handler](#ondisconnect-event-handler)
26
- - [`WoHand` Object](#wohand-object)
27
- - [`press()` method](#press-method)
28
- - [`turnOn()` method](#turnon-method)
29
- - [`turnOff()` method](#turnoff-method)
30
- - [`down()` method](#down-method)
31
- - [`up()` method](#up-method)
32
- - [`WoCurtain` object](#wocurtain-object)
33
- - [`open()` method](#open-method)
34
- - [`close()` method](#close-method)
35
- - [`pause()` method](#pause-method)
36
- - [`runToPos()` method](#runtopos-method)
37
- - [`WoPlugMini` object](#woplugmini-object)
38
- - [`turnOn()` method](#turnon-method)
39
- - [`turnOff()` method](#turnoff-method)
40
- - [`toggle()` method](#toggle-method)
41
- - [`WoSmartLock` object](#wosmartlock-object)
42
- - [`lock()` method](#lock-method)
43
- - [`unlock()` method](#unlock-method)
44
- - [`unlock_no_unlatch()` method](#unlock_no_unlatch-method)
45
- - [`info()` method](#info-method)
46
- - [Advertisement data](#advertisement-data)
47
- - [Bot (WoHand)](#bot-wohand)
48
- - [Meter (WoSensorTH)](#meter-wosensorth)
49
- - [Curtain (WoCurtain)](#curtain-wocurtain)
50
- - [Contact (WoContact)](#contact-wocontact)
51
- - [Motion (WoMotion)](#motion-womotion)
52
- - [PlugMini (WoPlugMini)](#plugmini-woplugmini)
53
- - [Supported Devices](#supported-devices)
54
- - [Advertisement Data](#advertisement-data)
55
- - [Control Device](#control-device)
56
- - [Summary](#summary)
57
-
58
- ## BLE (Bluetooth Low Energy)
59
-
60
- ### Supported OS
61
-
62
- The node-switchbot supports Linux-based OSes (such as Raspbian, Ubuntu) and macOS. Windows is not currently supported. BLE functionality requires [@stoprocent/noble](https://github.com/stoprocent/noble#readme) which works natively on Linux and macOS.
63
-
64
- ### Dependencies
65
-
66
- - [Node.js](https://nodejs.org/en/): ^20 || ^22 || ^24
67
- - [@stoprocent/noble](https://github.com/stoprocent/noble) - Included as a dependency
68
-
69
- ### Prerequisites
70
-
71
- #### macOS
72
-
73
- - **Xcode**: Install from the App Store
74
- - **Bluetooth Permissions**: On newer versions of macOS, allow Bluetooth access for your terminal app:
75
- 1. Open "System Preferences" → "Security & Privacy" → "Privacy" → "Bluetooth"
76
- 2. Add and enable your terminal application (Terminal.app, iTerm.app, etc.)
77
-
78
- #### Linux (Ubuntu, Debian, Raspbian)
79
-
80
- - **Kernel**: Version 3.6 or above
81
- - **Required Packages**:
82
-
83
- ```bash
84
- sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
85
- ```
86
-
87
- - **Node Path**: Ensure `node` is on your PATH
88
-
89
- ```bash
90
- # If needed, symlink nodejs to node
91
- sudo ln -s /usr/bin/nodejs /usr/bin/node
92
- ```
93
-
94
- - **Running without sudo** (Recommended):
95
-
96
- ```bash
97
- sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
98
- ```
99
-
100
- This grants the `node` binary `cap_net_raw` privileges for BLE operations.
101
-
102
- Required package: `sudo apt-get install libcap2-bin`
103
-
104
- - **Raspberry Pi Specific**: If having trouble connecting to BLE devices, disable the `pnat` plugin by adding this line to `/etc/bluetooth/main.conf`:
105
- ```
106
- DisablePlugins=pnat
107
- ```
108
- Then restart the system.
109
-
110
- #### Linux (Fedora and RPM-based)
111
-
112
- ```bash
113
- sudo yum install bluez bluez-libs bluez-libs-devel
114
- ```
115
-
116
- See [@stoprocent/noble prerequisites](https://github.com/stoprocent/noble?tab=readme-ov-file#prerequisites) for more detailed platform-specific instructions.
117
-
118
- ### Importing and Setting Up
119
-
120
- To use the `SwitchBotBLE` class in `node-switchbot`, you need to import it and create an instance.
121
-
122
- ```typescript
123
- import { SwitchBotBLE } from 'node-switchbot'
124
-
125
- // Example usage
126
- const switchBotBLE = new SwitchBotBLE()
127
-
128
- try {
129
- await switchBotBLE.startScan()
130
- } catch (e: any) {
131
- console.error(`Failed to start BLE scanning, Error: ${e.message ?? e}`)
132
- }
133
- ```
134
-
135
- ### `SwitchBotBLE` Object
136
-
137
- The `SwitchBotBLE` constructor takes an argument optionally. It must be a hash object containing the properties as follows:
138
-
139
- | Property | Type | Required | Description |
140
- | :------- | :---- | :------- | :-------------------------------------------------------------------------------------- |
141
- | `noble` | Noble | option | a Noble object of the [`@stoprocent/noble`](https://github.com/stoprocent/noble) module |
142
-
143
- The node-switchbot module uses the [`@stoprocent/noble`](https://github.com/stoprocent/noble) module in order to interact with BLE devices. If you want to interact other BLE devices using the `@stoprocent/noble` module, you can create an `Noble` object by yourself, then pass it to this module. If you don't specify a `Noble` object to the `noble` property, this module automatically create a `Noble` object internally.
144
-
145
- The sample code below shows how to pass a `Noble` object to the `Switchbot` constructor.
146
-
147
- ```Typescript
148
- // Create a Noble object
149
- const noble = require('@stoprocent/noble');
150
-
151
- const switchBotBLE = new SwitchBotBLE({ 'noble': Noble })
152
- ```
153
-
154
- In the code snippet above, the variable `switchbot` is an `SwitchBotBLE` object. The `SwitchBotBLE` object has a lot of methods as described in sections below.
155
-
156
- #### `discover()` method
157
-
158
- The `discover` method finds devices. This method returns a `Promise` object. This method takes an argument which is a hash object containing parameters as follows:
159
-
160
- | Property | Type | Required | Description |
161
- | :--------- | :------ | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
162
- | `duration` | Integer | Optional | Duration for discovery process (msec). The default value is 5000 (msec). |
163
- | `model` | String | Optional | `"H"`, `"T"` or `"c"`. If `"H"` is specified, this method will discover only Bots. If `"T"` is specified, this method will discover only Meters. If `"c"` is specified, this method will discover only Curtains. |
164
- | `id` | String | Optional | If this value is set, this method will discover only a device whose ID is as same as this value. The ID is identical to the MAC address. This parameter is case-insensitive, and colons are ignored. |
165
- | `quick` | Boolean | Optional | If this value is `true`, this method finishes the discovery process when the first device is found, then calls the `resolve()` function without waiting the specified `duration`. The default value is `false`. |
166
-
167
- In the code snippet below, no parameter is passed to the method:
168
-
169
- ```Typescript
170
- switchBotBLE.discover().then((device_list) => {
171
- // Do something...
172
- }).catch((error) => {
173
- console.error(error);
174
- });
175
- ```
176
-
177
- If no parameter is passed to the method as the code above, an `Array` object will be passed to the `resolve()` function in 5 seconds. The `Array` object contains [`SwitchbotDevice`](#SwitchbotDevice-object) objects representing the found devices. See the section "[`SwitchbotDevice`](#SwitchbotDevice-object) objects" for more details.
178
-
179
- If you want a quick response, you can set the `quick` property to `true`.
180
-
181
- ```Typescript
182
- switchBotBLE.discover({
183
- duration: 5000,
184
- quick: true
185
- }).then((device_list) => {
186
- // Do something...
187
- }).catch((error) => {
188
- console.error(error);
189
- });
190
- ```
191
-
192
- As the `quick` property is set to `true`, the `resolve()` function will be called immediately after a device is found regardless the value of the `duration` property.
193
-
194
- #### `ondiscover` event handler
195
-
196
- The `ondiscover` property on the [`Switchbot`](#Switchbot-object) object is an event handler called whenever a device is newly found in the discovery process. A [`SwitchbotDevice`](#SwitchbotDevice-object) object is passed to the callback function set to the `ondiscover` property.
197
-
198
- ```Typescript
199
- switchBotBLE.ondiscover = (device) => {
200
- console.log(device.id + ' (' + device.modelName + ')');
201
- };
202
-
203
- switchBotBLE.discover().then(() => {
204
- console.log('The discovery process was finished.');
205
- }).catch((error) => {
206
- console.error(error);
207
- });
208
- ```
209
-
210
- The code snippet above will output the result as follows:
211
-
212
- ```
213
- cb4eb903c96d (WoSensorTH)
214
- c12e453e2008 (WoHand)
215
- The discovery process was finished.
216
- ```
217
-
218
- #### `startScan()` method
219
-
220
- The `startScan()` method starts to scan advertising packets coming from devices. This method takes an argument which is a hash object containing the parameters as follows:
221
-
222
- | Property | Type | Required | Description |
223
- | :------- | :----- | :------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
224
- | `model` | String | Optional | `"H"`, `"T"`, `"c"`, `"g"` or `"j"`. If `"H"` is specified, this method will discover only Bots. If `"T"` is specified, this method will discover only Meters. If `"c"` is specified, this method will discover only Curtains. If `"g"` or `"j"` is specified, this method will discover only (US/JP) Plug Minis. |
225
- | `id` | String | Optional | If this value is set, this method will discover only a device whose ID is as same as this value. The ID is identical to the MAC address. This value is case-insensitive, and colons are ignored. |
226
-
227
- Whenever a packet is received, the callback function set to the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) property of the [`Switchbot`](#Switchbot-object) object will be called. When a packet is received, a hash object representing the packet will be passed to the callback function.
228
-
229
- ```Typescript
230
- // Set a callback function called when a packet is received
231
- switchBotBLE.onadvertisement = (ad) => {
232
- console.log(ad);
233
- };
234
-
235
- // Start to scan advertising packets
236
- switchBotBLE.startScan({
237
- id: 'cb:4e:b9:03:c9:6d',
238
- }).then(() => {
239
- // Wait for 30 seconds
240
- return switchBotBLE.wait(30000);
241
- }).then(() => {
242
- // Stop to scan
243
- switchBotBLE.stopScan();
244
- process.exit();
245
- }).catch((error) => {
246
- console.error(error);
247
- });
248
- ```
249
-
250
- The code snippet above will output the result as follows:
251
-
252
- ```
253
- {
254
- id: 'cb4eb903c96d',
255
- address: 'cb:4e:b9:03:c9:6d',
256
- rssi: -65,
257
- serviceData: {
258
- model: 'T',
259
- modelName: 'WoSensorTH',
260
- celsius: 25.8,
261
- fahrenheit: 78.4,
262
- fahrenheit_mode: false,
263
- humidity: 43,
264
- battery: 100
265
- }
266
- }
267
- ...
268
- ```
269
-
270
- The `serviceData` property depends on the model of the device. See the section "[Advertisement data](#Advertisement-data)" for the details of the data format.
271
-
272
- #### `onadvertisement` event handler
273
-
274
- If a callback function is set to the `onadvertisement` property, the callback function will be called whenever an advertising packet is received from a device during the scan is active (from the moment when the [`startScan()`](#startscan-method) method is called, to the moment when the [`stopScan()`](#Switchbot-stopScan-method) method is called).
275
-
276
- ```typescript
277
- switchBotBLE.onadvertisement = async (ad: any) => {
278
- try {
279
- this.bleEventHandler[ad.address]?.(ad.serviceData)
280
- } catch (e: any) {
281
- await this.errorLog(`Failed to handle BLE event, Error: ${e.message ?? e}`)
282
- }
283
- }
284
- ```
285
-
286
- See the section "[`startScan()` method](#startscan-method)" for details.
287
-
288
- #### `stopScan()` method
289
-
290
- The `stopScan()` method stops to scan advertising packets coming from devices. This method returns nothing. Note that this method is _not_ asynchronous but synchronous unlike the other methods. See the section "[`startScan()` method](#startscan-method)" for details.
291
-
292
- ```typescript
293
- try {
294
- switchBotBLE.stopScan()
295
- console.log('Stopped BLE scanning to close listening.')
296
- } catch (e: any) {
297
- console.error(`Failed to stop BLE scanning, Error: ${e.message ?? e}`)
298
- }
299
- ```
300
-
301
- #### `wait()` method
302
-
303
- The `wait()` method waits for the specified milliseconds. This method takes an integer representing the duration (millisecond). This method returns a `Promise` object.
304
-
305
- This method has nothing to do with Switchbot devices. It's just a utility method. See the section "[Quick Start](#Quick-Start)" for details of the usage of this method.
306
-
307
- ```typescript
308
- await switchBotBLE.wait(1000)
309
- ```
310
-
311
- ### `SwitchBotDevice` Object
312
-
313
- The `SwitchbotDevice` object represents a Switchbot device (Bot, Meter, Curtain, Contact or Motion), which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.
314
-
315
- Actually, the `SwitchbotDevice` object is a super class of the [`WoHand`](#SwitchbotDeviceWoHand-object) and `WoSensorTH` objects. The [`WoHand`](#SwitchbotDeviceWoHand-object) object represents a Bot, the `WoSensorTH` object represents a Meter.
316
-
317
- You can use the properties and methods described in this section on Bot, Meter, Curtain, Contact and Motion. See the section "[`WoHand` object](#SwitchbotDeviceWoHand-object)" for the details of the functionalities available only on Bot. For now, `WoSensorTH` object has no additional functionality.
318
-
319
- #### Properties
320
-
321
- The `SwitchbotDevice` object supports the properties as follows:
322
-
323
- | Property | Type | Description |
324
- | :---------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
325
- | `id` | String | ID of the device. (e.g., `"cb4eb903c96d"`) |
326
- | `address` | String | MAC address of the device. Basically it is as same as the value of the `id` except that this value includes `:` in the string. (e.g., `"cb:4e:b9:03:c9:6d"`) |
327
- | `model` | String | This value is `"H"` "Bot (WoHand)", `"T"` "Meter (WoSensorTH)", `"c"` "Curtain (WoCurtain)", `"d"` "Contact (WoContact)" or `"s"` "Motion (WoMotion)". |
328
- | `modelName` | String | This value is `"WoHand"`, `"WoSensorTH"`, `WoCurtain`, `WoContect` or `WoMotion`. |
329
- | `connectionState` | String | This value indicates the BLE connection state. `"connecting"`, `"connected"`, `"disconnecting"`, or `"disconnected"`. |
330
- | `onconnect` | Function | See the section "[`onconnect` event handler](#SwitchbotDevice-onconnect-event-handler)" for details. |
331
- | `ondisconnect` | Function | See the section "[`ondisconnect` event handler](#SwitchbotDevice-ondisconnect-event-handler)" for details. |
332
-
333
- #### `getDeviceName()` method
334
-
335
- The `getDeviceName()` method fetches the device name saved in the device. This method returns a `Promise` object.
336
-
337
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
338
-
339
- If the device name is fetched successfully, the device name will be passed to the `resolve()`.
340
-
341
- ```Typescript
342
- switchbot
343
- .discover({ model: "H", quick: true })
344
- .then((device_list) => {
345
- return device_list[0].getDeviceName();
346
- })
347
- .then((name) => {
348
- console.log(name);
349
- process.exit();
350
- })
351
- .catch((error) => {
352
- console.error(error);
353
- process.exit();
354
- });
355
- ```
356
-
357
- The code above will output the result as follows:
358
-
359
- ```Typescript
360
- WoHand;
361
- ```
362
-
363
- #### `setDeviceName()` method
364
-
365
- The `setDeviceName()` method update the device name saved in the device with the name specified as the first argument. This method returns a `Promise` object. Nothing will be passed to the `resolve()` function.
366
-
367
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
368
-
369
- The character set of the device name saved in the device is UTF-8. The byte length of the name must be less than or equal to 20 bytes. If the name consists of only ASCII characters, up to 20 characters would be allowed. But if the name consists of multibyte characters, the upper limit of characters would be fewer than half. For example, Japanese characters could be saved at most 6 characters because most of Japanese characters consume 3 byte per each character.
370
-
371
- ```Typescript
372
- switchbot
373
- .discover({ model: "H", quick: true })
374
- .then((device_list) => {
375
- return device_list[0].setDeviceName("Bot in kitchen");
376
- })
377
- .then(() => {
378
- console.log("Done.");
379
- process.exit();
380
- })
381
- .catch((error) => {
382
- console.error(error);
383
- process.exit();
384
- });
385
- ```
386
-
387
- #### `connect()` method
388
-
389
- The `connect()` method establishes a connection with the device (i.e., pairing). This method returns a `Promise` object. If the device has already been connected, this method does nothing and calls the `resolve()` function immediately.
390
-
391
- Most of the methods implemented in the `SwitchbotDevice` object automatically connect and disconnect the device. But this mechanism would be absolutely inefficient if you want to manipulate the device repeatedly in the short time.
392
-
393
- The connection established using the `connect()` method is not disconnected automatically unless the [`disconnect()`](#SwitchbotDevice-disconnect-method) method is explicitly called.
394
-
395
- The code snippet below establishes a connection with the Bot using the `connect()` method, then puts the Bot's arm down, then waits for 5 seconds, then puts the arm down, finally disconnects the device using the [`disconnect()`](#SwitchbotDevice-disconnect-method) method:
396
-
397
- ```Typescript
398
- let device = null;
399
-
400
- switchbot
401
- .discover({ model: "H", quick: true })
402
- .then((device_list) => {
403
- device = device_list[0];
404
- if (!device) {
405
- console.log("No device was found.");
406
- process.exit();
407
- }
408
- console.log(device.modelName + " (" + device.address + ") was found.");
409
- console.log("Connecting...");
410
- return device.connect();
411
- })
412
- .then(() => {
413
- console.log("Putting the arm down...");
414
- return device.down();
415
- })
416
- .then(() => {
417
- console.log("Waiting for 5 seconds...");
418
- return switchBotBLE.wait(5000);
419
- })
420
- .then(() => {
421
- console.log("Putting the arm up...");
422
- return device.up();
423
- })
424
- .then(() => {
425
- console.log("Disconnecting...");
426
- return device.disconnect();
427
- })
428
- .then(() => {
429
- console.log("Done.");
430
- process.exit();
431
- })
432
- .catch((error) => {
433
- console.error(error);
434
- process.exit();
435
- });
436
- ```
437
-
438
- The result will be as follows:
439
-
440
- ```
441
- WoHand (c1:2e:45:3e:20:08) was found.
442
- Connecting...
443
- Putting the arm down...
444
- Waiting for 5 seconds...
445
- Putting the arm up...
446
- Disconnecting...
447
- Done.
448
- ```
449
-
450
- #### `disconnect()` method
451
-
452
- The `disconnect()` method disconnects the device. This method returns a `Promise` object. If the device has already been disconnected, this method does nothing and calls the `resolve()` function immediately.
453
-
454
- See the [previous section](#SwitchbotDevice-connect-method) for more details.
455
-
456
- #### `onconnect` event handler
457
-
458
- The `onconnect` event handler will be called when the connection with the device is established. Nothing will be passed to the handler.
459
-
460
- The code below calls the [`press()`](#SwitchbotDeviceWoHand-press-method) method, while callback functions are attached to the `onconnect` and `ondisconnect`.
461
-
462
- ```Typescript
463
- switchbot
464
- .discover({ model: "H", quick: true })
465
- .then((device_list) => {
466
- const device = device_list[0];
467
- if (!device) {
468
- console.log("No device was found.");
469
- process.exit();
470
- }
471
- console.log(device.modelName + " (" + device.address + ") was found.");
472
-
473
- // Set event handers
474
- device.onconnect = () => {
475
- console.log("Connected.");
476
- };
477
- device.ondisconnect = () => {
478
- console.log("Disconnected.");
479
- };
480
-
481
- console.log("Pressing the switch...");
482
- return device.press();
483
- })
484
- .then(() => {
485
- console.log("Done.");
486
- process.exit();
487
- })
488
- .catch((error) => {
489
- console.error(error);
490
- process.exit();
491
- });
492
- ```
493
-
494
- The code above will output the result as follows:
495
-
496
- ```
497
- WoHand (c1:2e:45:3e:20:08) was found.
498
- Pressing the switch...
499
- Connected.
500
- Disconnected.
501
- Done.
502
- ```
503
-
504
- Seeing the result, you would find the [`press()`](#SwitchbotDeviceWoHand-press-method) method automatically connects and disconnects the device.
505
-
506
- #### `ondisconnect` event handler
507
-
508
- The `ondisconnect` event handler will be called when the connection with the device is closed. Nothing will be passed to the handler. See the previous section "[`onconnect` event handler](#SwitchbotDevice-onconnect-event-handler)" for more details.
509
-
510
- ### `WoHand` Object
511
-
512
- The `WoHand` object represents a Bot, which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.
513
-
514
- Actually, the `WoHand` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
515
-
516
- #### `press()` method
517
-
518
- The `press()` method sends a press command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
519
-
520
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
521
-
522
- ```Typescript
523
- switchbot
524
- .discover({ model: "H", quick: true })
525
- .then((device_list) => {
526
- return device_list[0].press();
527
- })
528
- .then(() => {
529
- console.log("Done.");
530
- })
531
- .catch((error) => {
532
- console.error(error);
533
- });
534
- ```
535
-
536
- When the Bot receives this command, the Bot's arm will be put down (stretched), then put up (retracted) in a few seconds.
537
-
538
- #### `turnOn()` method
539
-
540
- The `turnOn()` method sends a turn-on command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
541
-
542
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
543
-
544
- When the Bot receives this command, the Bot's arm will be put down (stretched) or put up (retracted) depending on the mode setting.
545
-
546
- | Mode | Inverse the on/off direction | Physical position of the arm |
547
- | :---------- | :--------------------------- | :------------------------------------ |
548
- | Press mode | N/A | Down (stretched), then Up (retracted) |
549
- | Switch mode | Disabled | Down (stretched) |
550
- |   | Enabled | Up (retracted) |
551
-
552
- ```Typescript
553
- switchbot
554
- .discover({ model: "H", quick: true })
555
- .then((device_list) => {
556
- return device_list[0].turnOn();
557
- })
558
- .then(() => {
559
- console.log("Done.");
560
- })
561
- .catch((error) => {
562
- console.error(error);
563
- });
564
- ```
565
-
566
- #### `turnOff()` method
567
-
568
- The `turnOff()` method sends a turn-off command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
569
-
570
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
571
-
572
- When the Bot receives this command, the Bot's arm will be put down (stretched) or put up (retracted) depending on the mode setting.
573
-
574
- | Mode | Inverse the on/off direction | Physical position of the arm |
575
- | :---------- | :--------------------------- | :------------------------------------ |
576
- | Press mode | N/A | Down (stretched), then Up (retracted) |
577
- | Switch mode | Disabled | Up (retracted) |
578
- |   | Enabled | Down (stretched) |
579
-
580
- ```Typescript
581
- switchbot
582
- .discover({ model: "H", quick: true })
583
- .then((device_list) => {
584
- return device_list[0].turnOff();
585
- })
586
- .then(() => {
587
- console.log("Done.");
588
- })
589
- .catch((error) => {
590
- console.error(error);
591
- });
592
- ```
593
-
594
- #### `down()` method
595
-
596
- The `down()` method sends a down command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
597
-
598
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
599
-
600
- When the Bot receives this command, the Bot's arm will be put down (stretched) regardless of the mode setting.
601
-
602
- ```Typescript
603
- switchbot
604
- .discover({ model: "H", quick: true })
605
- .then((device_list) => {
606
- return device_list[0].down();
607
- })
608
- .then(() => {
609
- console.log("Done.");
610
- })
611
- .catch((error) => {
612
- console.error(error);
613
- });
614
- ```
615
-
616
- #### `up()` method
617
-
618
- The `up()` method sends an up command to the Bot. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
619
-
620
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
621
-
622
- When the Bot receives this command, the Bot's arm will be put up (retracted) regardless of the mode setting.
623
-
624
- ```Typescript
625
- switchbot
626
- .discover({ model: "H", quick: true })
627
- .then((device_list) => {
628
- return device_list[0].up();
629
- })
630
- .then(() => {
631
- console.log("Done.");
632
- })
633
- .catch((error) => {
634
- console.error(error);
635
- });
636
- ```
637
-
638
- #### Password Protection
639
-
640
- The Bot (WoHand) supports password protection via BLE to prevent unauthorized control. When a password is set, all BLE commands (press, turnOn, turnOff, up, down) will be automatically encrypted.
641
-
642
- **Password Requirements:**
643
-
644
- - Exactly 4 characters
645
- - Alphanumeric only (letters and numbers)
646
- - Case-sensitive
647
-
648
- **Setting a Password:**
649
-
650
- ```typescript
651
- // Using device options during construction
652
- const bot = new WoHand({
653
- id: 'c1:2e:45:3e:20:08',
654
- password: 'A1b2' // Your 4-character password
655
- })
656
-
657
- // Or set/change password on existing device
658
- await bot.setPassword('A1b2')
659
- ```
660
-
661
- **Clearing a Password:**
662
-
663
- ```typescript
664
- // Remove password protection
665
- await bot.clearPassword()
666
- ```
667
-
668
- **Checking Password Status:**
669
-
670
- ```typescript
671
- // Check if device has password configured
672
- if (bot.hasPassword()) {
673
- console.log('Device is password protected')
674
- }
675
- ```
676
-
677
- **Example with Password:**
678
-
679
- ```typescript
680
- switchbot
681
- .discover({ model: 'H', quick: true })
682
- .then((device_list) => {
683
- const bot = device_list[0]
684
- // Set password for protected Bot
685
- bot.setPassword('MyP4')
686
- return bot.press()
687
- })
688
- .then(() => {
689
- console.log('Password-protected Bot pressed successfully')
690
- })
691
- .catch((error) => {
692
- console.error(error)
693
- })
694
- ```
695
-
696
- **Notes:**
697
-
698
- - Password encryption uses CRC32 checksums for secure command transmission
699
- - All control methods (press, turnOn, turnOff, up, down) automatically use encrypted commands when password is set
700
- - Password is stored in memory only and must be set each time the application starts
701
- - If the wrong password is configured, BLE commands will fail silently (Bot will not respond)
702
-
703
- ### `WoCurtain` Object
704
-
705
- The `WoCurtain` object represents a Curtain, which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.
706
-
707
- Actually, the `WoCurtain` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
708
-
709
- #### `open()` method
710
-
711
- The `open()` method sends an open command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
712
-
713
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
714
-
715
- When the Curtain receives this command, the Curtain will open the curtain (0% position). If not calibrated, the Curtain does not move.
716
-
717
- The `open()` method receives an optional `mode` parameter. (See [`runToPos()`](#runtopos-method))
718
-
719
- ```Typescript
720
- switchbot
721
- .discover({ model: "c", quick: true })
722
- .then((device_list) => {
723
- return device_list[0].open();
724
- })
725
- .then(() => {
726
- console.log("Done.");
727
- })
728
- .catch((error) => {
729
- console.error(error);
730
- });
731
- ```
732
-
733
- #### `close()` method
734
-
735
- The `close()` method sends a close command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
736
-
737
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
738
-
739
- When the Curtain receives this command, the Curtain will close the curtain (100% position). If not calibrated, the Curtain does not move.
740
-
741
- The `close()` method receives an optional `mode` parameter. (See [`runToPos()`](#runtopos-method))
742
-
743
- ```Typescript
744
- switchbot
745
- .discover({ model: "c", quick: true })
746
- .then((device_list) => {
747
- return device_list[0].close();
748
- })
749
- .then(() => {
750
- console.log("Done.");
751
- })
752
- .catch((error) => {
753
- console.error(error);
754
- });
755
- ```
756
-
757
- #### `pause()` method
758
-
759
- The `pause()` method sends a pause command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
760
-
761
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
762
-
763
- When the Curtain receives this command, the Curtain will pause.
764
-
765
- ```Typescript
766
- switchbot
767
- .discover({ model: "c", quick: true })
768
- .then((device_list) => {
769
- return device_list[0].pause();
770
- })
771
- .then(() => {
772
- console.log("Done.");
773
- })
774
- .catch((error) => {
775
- console.error(error);
776
- });
777
- ```
778
-
779
- #### `runToPos()` method
780
-
781
- The `runToPos()` method sends a position command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
782
-
783
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
784
-
785
- When the Curtain receives this command, the Curtain will run to the percentage position. If not calibrated, the Curtain does not move.
786
-
787
- The `open()` method sends an open command to the Curtain. This method returns a `Promise` object. Nothing will be passed to the `resove()`.
788
-
789
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
790
-
791
- When the Curtain receives this command, the Curtain will open the curtain (0% position). If not calibrated, the Curtain does not move.
792
-
793
- | Property | Type | Required | Description |
794
- | :-------- | :------ | :------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |
795
- | `percent` | Integer | Required | The percentage of target position (`0-100`). (e.g., `50`) |
796
- | `mode` | Integer | Optional | The running mode of Curtain. <br/>`0x00` - Performance mode.<br/> `0x01` - Silent mode. <br/>`0xff` - Default. Unspecified, from Curtain's settings. |
797
-
798
- ```Typescript
799
- switchbot
800
- .discover({ model: "c", quick: true })
801
- .then((device_list) => {
802
- return device_list[0].runToPos(50);
803
- })
804
- .then(() => {
805
- console.log("Done.");
806
- })
807
- .catch((error) => {
808
- console.error(error);
809
- });
810
- ```
811
-
812
- ### `WoPlugMini` Object
813
-
814
- The `WoPlugMini ` object represents a PlugMini, which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.
815
-
816
- Actually, the `WoPlugMini ` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
817
-
818
- #### `turnOn()` method
819
-
820
- The `turnOn()` method sends a turn-on command to the PlugMini. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is on (`true`), is passed to the `resolve()` method of the Promise.
821
-
822
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
823
-
824
- #### `turnOff()` method
825
-
826
- The `turnOff()` method sends a turn-off command to the PlugMini. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is off (`false`), is passed to the `resolve()` method of the Promise.
827
-
828
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
829
-
830
- #### `toggle()` method
831
-
832
- The `toggle()` method sends a toggle command to the PlugMini, toggling between the on and off state. This method returns a `Promise` object. A `boolean` value indicating whether the PlugMini is on (`true`) or off (`false`), is passed to the `resolve()` method of the Promise.
833
-
834
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
835
-
836
- ### `WoSmartLock` Object
837
-
838
- The `WoSmartLock ` object represents a SmartLock, which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.
839
-
840
- Actually, the `WoSmartLock ` is an object inherited from the [`SwitchbotDevice`](#SwitchbotDevice-object). You can use not only the method described in this section but also the properties and methods implemented in the [`SwitchbotDevice`](#SwitchbotDevice-object) object.
841
-
842
- #### `setKey()` method
843
-
844
- The `setKey()` method initialises the key information required for encrypted communication with the SmartLock
845
-
846
- This must be set before any control commands are sent to the device. To obtain the key information you will need to use an external tool - see [`pySwitchbot`](https://github.com/Danielhiversen/pySwitchbot/tree/master?tab=readme-ov-file#obtaining-locks-encryption-key) project for an example script.
847
- Or, use [`switchbot-get-encryption-key`](https://www.npmjs.com/package/switchbot-get-encryption-key) npm script.
848
-
849
- | Property | Type | Description |
850
- | :-------------- | :----- | :----------------------------------------------------------------------------------------------- |
851
- | `keyId` | String | unique2 character ID for the key. (e.g., `"ff"`) returned from the SwitchBot api for your device |
852
- | `encryptionKey` | String | the unique encryption key returned from the SwitchBot api for your device |
853
-
854
- #### `lock()` method
855
-
856
- The `lock()` method sends a lock command to the SmartLock. This method returns a `Promise` object. A `boolean` value indicating whether the SmartLock is locked (`true`), is passed to the `resolve()` method of the Promise.
857
-
858
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
859
-
860
- #### `unlock()` method
861
-
862
- The `unlock()` method sends an unlock command to the SmartLock. This method returns a `Promise` object. A `boolean` value indicating whether the SmartLock is locked (`false`), is passed to the `resolve()` method of the Promise.
863
-
864
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
865
-
866
- #### `unlockNoUnlatch()` method
867
-
868
- The `unlockNoUnlatch()` method sends a partial unlock command to the SmartLock, unlocking without the full unlatch.
869
-
870
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
871
-
872
- #### `info()` method
873
-
874
- The `info()` method retreieves state information from the SmartLock, This method returns a `Promise` object. An `object` value indicating with the state infor, is passed to the `resolve()` method of the Promise.
875
-
876
- If no connection is established with the device, this method automatically establishes a connection with the device, then finally closes the connection. You don't have to call the [`connect()`](#SwitchbotDevice-connect-method) method in advance.
877
-
878
- ### Advertisement Data
879
-
880
- After the [`startScan()`](#startscan-method) method is invoked, the [`onadvertisement`](#Switchbot-onadvertisement-event-handler) event handler will be called whenever an advertising packet comes from the switchbot devices.
881
-
882
- ```Typescript
883
- // Load the node-switchbot and get a `Switchbot` constructor object
884
- import { SwitchBotBLE } from 'node-switchbot';
885
- // Create a `Switchbot` object
886
- const switchBotBLE = new SwitchBotBLE();
887
-
888
- (async () => {
889
- // Start to monitor advertisement packets
890
- await switchBotBLE.startScan();
891
- // Set an event handler
892
- switchBotBLE.onadvertisement = (ad) => {
893
- console.log(JSON.stringify(ad, null, ' '));
894
- };
895
- // Wait 10 seconds
896
- await switchBotBLE.wait(10000);
897
- // Stop to monitor
898
- switchBotBLE.stopScan();
899
- process.exit();
900
- })();
901
- ```
902
-
903
- An object containing the properties as follows will be passed to the event handler:
904
-
905
- | Property | Type | Description |
906
- | :------------ | :------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------- |
907
- | `id` | String | ID of the device. (e.g., `"cb4eb903c96d"`) |
908
- | `address` | String | MAC address of the device. Basically it is as same as the value of the `id` except that this value includes `:` in the string. (e.g., `"cb:4e:b9:03:c9:6d"`) |
909
- | `rssi` | Integer | RSSI. (e.g., `-62`) |
910
- | `serviceData` | Object | An object including the device-specific data. |
911
-
912
- The structures of the `serviceData` are described in the following sections.
913
-
914
- #### Bot (WoHand)
915
-
916
- Example of the advertisement data:
917
-
918
- ```json
919
- {
920
- "id": "c12e453e2008",
921
- "address": "c1:2e:45:3e:20:08",
922
- "rssi": -61,
923
- "serviceData": {
924
- "model": "H",
925
- "modelName": "WoHand",
926
- "mode": true,
927
- "state": false,
928
- "battery": 100
929
- }
930
- }
931
- ```
932
-
933
- Structure of the `serviceData`:
934
-
935
- | Property | Type | Description |
936
- | :---------- | :------ | :------------------------------------------------------------------------------------------------------------------------------------------- |
937
- | `model` | String | This value is always `"H"`, which means "Bot (WoHand)". |
938
- | `modelName` | String | This value is always `"WoHand"`, which means "Bot". |
939
- | `mode` | Boolean | This indicates the mode setting. When the mode is "Switch mode", this value is `true`. When the mode is "Press mode", this value is `false`. |
940
- | `state` | Boolean | This value indicates whether the switch status is ON or OFF. |
941
- | `battery` | Integer | (**experimental**) This value indicates the battery level (`%`). |
942
-
943
- The `mode` can be changed only using the official smartphone app. The node-switchbot does not support changing the mode because the BLE protocol is non-public.
944
-
945
- If the `mode` is `false`, which means the "Press mode" is selected, the `state` is always `false`. If the `mode` is `true`, which means the "Switch mode" is selected, the `state` represents the logical state (ON or OFF). Note that it does _not_ mean the physical arm position. The physical arm position depends on the setting "Inverse the on/off direction" on the official smartphone app.
946
-
947
- | "Inverse the on/off direction" | Value of the `state` | Logical state | Physical arm position |
948
- | :----------------------------- | :------------------- | :------------ | :-------------------- |
949
- | disabled | `true` | OFF | Up (retracted) |
950
- | &nbsp; | `false` | ON | Down (stretched) |
951
- | enabled | `true` | OFF | Down (stretched) |
952
- | &nbsp; | `false` | ON | Up (retracted) |
953
-
954
- The `battery` is _experimental_ for now. I'm not sure whether the value is correct or not. Never trust this value for now.
955
-
956
- #### Meter (WoSensorTH)
957
-
958
- Example of the advertisement data:
959
-
960
- ```json
961
- {
962
- "id": "cb4eb903c96d",
963
- "address": "cb:4e:b9:03:c9:6d",
964
- "rssi": -70,
965
- "serviceData": {
966
- "model": "T",
967
- "modelName": "WoSensorTH",
968
- "temperature": {
969
- "c": 25.2,
970
- "f": 77.4
971
- },
972
- "fahrenheit": false,
973
- "humidity": 43,
974
- "battery": 100
975
- }
976
- }
977
- ```
978
-
979
- Structure of the `data`:
980
-
981
- | Property | Type | Description |
982
- | :--------------- | :------ | :----------------------------------------------------------------------------------------------------------- |
983
- | `model` | String | This value is always `"T"`, which means "Meter (WoSensorTH)". |
984
- | `modelName` | String | This value is always `"WoSensorTH"`, which means "Meter". |
985
- | `temperature` | Object |
986
- | &nbsp;&nbsp; `c` | Float | Temperature (degree Celsius/°C) |
987
- | &nbsp;&nbsp; `f` | Float | Temperature (degree Fahrenheit/℉) |
988
- | `fahrenheit` | Boolean | The flag whether the Meter shows Fahrenheit (`true`) or Celsius (`false`) for the temperature on the display |
989
- | `humidity` | Integer | Humidity (`%`) |
990
- | `battery` | Integer | (**experimental**) This value indicates the battery level (`%`). |
991
-
992
- The `fahrenheit` indicates the setting on the device. Note that it does _not_ indicate the setting on the official smartphone app. The setting of the temperature unit on the device and the setting on the app are independent.
993
-
994
- The `battery` is _experimental_ for now. I'm not sure whether the value is correct or not. Never trust this value for now.
995
-
996
- #### Curtain (WoCurtain)
997
-
998
- Example of the advertisement data:
999
-
1000
- ```json
1001
- {
1002
- "id": "ec58c5d00111",
1003
- "address": "ec:58:c5:d0:01:11",
1004
- "rssi": -39,
1005
- "serviceData": {
1006
- "model": "c",
1007
- "modelName": "WoCurtain",
1008
- "calibration": true,
1009
- "battery": 91,
1010
- "position": 1,
1011
- "lightLevel": 1
1012
- }
1013
- }
1014
- ```
1015
-
1016
- Structure of the `serviceData`:
1017
-
1018
- | Property | Type | Description |
1019
- | :------------ | :------ | :--------------------------------------------------------------------------------- |
1020
- | `model` | String | This value is `"c"`, which means "Curtain (WoCurtain)". |
1021
- | | | or `"{"`, which means "Curtain 3 (WoCurtain)". |
1022
- | `modelName` | String | This value is always `"WoCurtain"`, which means "Curtain". |
1023
- | `calibration` | Boolean | This value indicates the calibration status (`true` or `false`). |
1024
- | `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
1025
- | `position` | Integer | This value indicates the percentage of current position (`0-100`, 0 is open, `%`). |
1026
- | `lightLevel` | Integer | This value indicates the light level of the light source currently set (`1-10`). |
1027
-
1028
- #### Contact (WoContact)
1029
-
1030
- Example of the advertisement data:
1031
-
1032
- ```json
1033
- {
1034
- "id": "f0cda125e3ec",
1035
- "address": "f0:cd:a1:25:e3:ec",
1036
- "rssi": -56,
1037
- "serviceData": {
1038
- "model": "d",
1039
- "modelName": "WoContact",
1040
- "movement": false,
1041
- "battery": 95,
1042
- "doorState": "close",
1043
- "lightLevel": "bright"
1044
- }
1045
- }
1046
- ```
1047
-
1048
- Structure of the `serviceData`:
1049
-
1050
- | Property | Type | Description |
1051
- | :----------- | :------ | :--------------------------------------------------------------------------- |
1052
- | `model` | String | This value is always `"c"`, which means "Contact (WoContact)". |
1053
- | `modelName` | String | This value is always `"WoContact"`, which means "Contact". |
1054
- | `movement` | Boolean | This value indicates the motion status (`true` or `false`). |
1055
- | `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
1056
- | `doorState` | String | This value indicates the door Status (`close`, `open`, `timeout no closed`). |
1057
- | `lightLevel` | String | This value indicates the light level (`dark`, `bright`). |
1058
-
1059
- #### Motion (WoMotion)
1060
-
1061
- Example of the advertisement data:
1062
-
1063
- ```json
1064
- {
1065
- "id": "e7216fa344a9",
1066
- "address": "e7:21:6f:a3:44:a9",
1067
- "rssi": -53,
1068
- "serviceData": {
1069
- "model": "s",
1070
- "modelName": "WoMotion",
1071
- "movement": false,
1072
- "battery": 96,
1073
- "lightLevel": "bright"
1074
- }
1075
- }
1076
- ```
1077
-
1078
- Structure of the `serviceData`:
1079
-
1080
- | Property | Type | Description |
1081
- | :----------- | :------ | :----------------------------------------------------------- |
1082
- | `model` | String | This value is always `"s"`, which means "Motion (WoMotion)". |
1083
- | `modelName` | String | This value is always `"WoMotion"`, which means "Motion". |
1084
- | `movement` | Boolean | This value indicates the motion status (`true` or `false`). |
1085
- | `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
1086
- | `lightLevel` | String | This value indicates the light level (`dark`, `bright`). |
1087
-
1088
- #### PlugMini (WoPlugMini)
1089
-
1090
- Example of the advertisement data:
1091
-
1092
- ```json
1093
- {
1094
- "id": "cd2409ea3e9441f87d4580e0380a62bf",
1095
- "address": "60:55:f9:35:f6:a6",
1096
- "rssi": -50,
1097
- "serviceData": {
1098
- "model": "j",
1099
- "modelName": "WoPlugMini",
1100
- "state": "off",
1101
- "delay": false,
1102
- "timer": false,
1103
- "syncUtcTime": true,
1104
- "wifiRssi": 48,
1105
- "overload": false,
1106
- "currentPower": 0
1107
- }
1108
- }
1109
- ```
1110
-
1111
- Structure of the `serviceData`:
1112
-
1113
- | Property | Type | Description |
1114
- | :------------- | :------ | :--------------------------------------------------------------------------------- |
1115
- | `model` | String | This value is always `"j"` or `"g"`, which means "PlugMini" (JP or US). |
1116
- | `modelName` | String | This value is always `"WoPlugMini"`, which means "PlugMini". |
1117
- | `state ` | Boolean | This value indicates whether the plug mini is turned on (`true`) or not (`false`). |
1118
- | `delay` | Boolean | Indicates whether a delay is present. |
1119
- | `timer` | Boolean | Indicates whether a timer is present. |
1120
- | `syncUtcTime` | boolean | Indicates whether the UTC time has been synchronized. |
1121
- | `overload` | boolean | Indicates whether the Plug Mini is overloaded, more than 15A current overload. |
1122
- | `currentPower` | Float | Current power consumption in Watts. |
1123
-
1124
- ---
1125
-
1126
- #### SmartLock (WoSmartLock)
1127
-
1128
- Example of the advertisement data:
1129
-
1130
- ```json
1131
- {
1132
- "id": "d30864110b8c",
1133
- "address": "d3:08:64:11:0b:8c",
1134
- "rssi": -52,
1135
- "serviceData": {
1136
- "model": "o",
1137
- "modelName": "WoSmartLock",
1138
- "battery": 100,
1139
- "calibration": true,
1140
- "status": "LOCKED",
1141
- "update_from_secondary_lock": false,
1142
- "door_open": false,
1143
- "double_lock_mode": false,
1144
- "unclosed_alarm": false,
1145
- "unlocked_alarm": false,
1146
- "auto_lock_paused": false
1147
- }
1148
- }
1149
- ```
1150
-
1151
- Structure of the `serviceData`:
1152
-
1153
- | Property | Type | Description |
1154
- | :--------------------------- | :------ | :---------------------------------------------------------------------------------- |
1155
- | `model` | String | This value is `"o"`, which means "Lock (WoSmartLock)". |
1156
- | `modelName` | String | This value is always `"WoSmartLock"`, which means "Lock". |
1157
- | `battery` | Integer | This value indicates the battery level (`1-100`, `%`). |
1158
- | `calibration` | Boolean | This value indicates the calibration status (`true` or `false`). |
1159
- | `status` | String | This value indicates the current locked state. Possible values: |
1160
- | | | `"LOCKED"`, `"UNLOCKED"`, `"LOCKING"`, `"UNLOCKING"` |
1161
- | | | `"LOCKING_STOP"`, `"UNLOCKING_STOP"` (stuck when locking or unlocking respectively) |
1162
- | | | `"NOT_FULLY_LOCKED"` (eu model only), `"UNKNOWN"` (fallback: must be some error) |
1163
- | `update_from_secondary_lock` | Boolean | ?? |
1164
- | `door_open` | Boolean | door open status - whether the door is not detecting the sensor magnet |
1165
- | `double_lock_mode` | Boolean | dual lock mode enabled status - two locks working simultaneously |
1166
- | `unclosed_alarm` | Boolean | enabled status for door ajar alarm function |
1167
- | `unlocked_alarm` | Boolean | whether the alarm function is enabled for door left unlocked |
1168
- | `auto_lock_paused` | Boolean | auto lock mode paused |
1169
- | `night_latch` | Boolean | night latch mode enabled (eu firmware only) |
1170
-
1171
- ### Control Device
1172
-
1173
- This sample discovers a Bot (WoHand), then put the Bot's arm down, finally put it up in 5 seconds.
1174
-
1175
- ```Typescript
1176
- // Load the node-switchbot and get a `Switchbot` constructor object
1177
- import { SwitchBotBLE } from 'node-switchbot';
1178
- // Create a `Switchbot` object
1179
- const switchBotBLE = new SwitchBotBLE();
1180
-
1181
- (async () => {
1182
- // Find a Bot (WoHand)
1183
- const bot_list = await switchBotBLE.discover({ model: "H", quick: true });
1184
- if (bot_list.length === 0) {
1185
- throw new Error("No device was found.");
1186
- }
1187
- // The `WoHand` object representing the found Bot.
1188
- const device = bot_list[0];
1189
- // Put the Bot's arm down (stretch the arm)
1190
- await device.down();
1191
- // Wait for 5 seconds
1192
- await switchBotBLE.wait(5000);
1193
- // Put the Bot's arm up (retract the arm)
1194
- await device.up();
1195
- process.exit();
1196
- })();
1197
- ```
1198
-
1199
- In order to manipulate the arm of your Bot, you have to discover your Bot using the [`discover()`](#discover-method) method. The object `{ model: 'H' }` passed to the method means that only Bots will be discovered. That is, Meters will be ignored.
1200
-
1201
- In this code, you can get a [`WoHand`](#SwitchbotDeviceWoHand-object) object representing the found Bot. Using the [`down()`](#SwitchbotDeviceWoHand-down-method) and [`up()`](#SwitchbotDeviceWoHand-up-method) methods of the object, you can move the arm. In addition to these methods, you can use the [`press()`](#SwitchbotDeviceWoHand-press-method), [`turnOn()`](#SwitchbotDeviceWoHand-turnOn-method), and [`turnOff()`](#SwitchbotDeviceWoHand-turnOff-method) methods as well.
1202
-
1203
- ### Logging
1204
-
1205
- To be able to receive logging that this module is pushing out you will need to subscribe to the events.
1206
-
1207
- ```typescript
1208
- this.switchBotBLE.on('log', (log) => {
1209
- switch (log.level) {
1210
- case LogLevel.SUCCESS:
1211
- this.successLog(log.message)
1212
- break
1213
- case LogLevel.DEBUGSUCCESS:
1214
- this.debugSuccessLog(log.message)
1215
- break
1216
- case LogLevel.WARN:
1217
- this.warnLog(log.message)
1218
- break
1219
- case LogLevel.DEBUGWARN:
1220
- this.debugWarnLog(log.message)
1221
- break
1222
- case LogLevel.ERROR:
1223
- this.errorLog(log.message)
1224
- break
1225
- case LogLevel.DEBUGERROR:
1226
- this.debugErrorLog(log.message)
1227
- break
1228
- case LogLevel.DEBUG:
1229
- this.debugLog(log.message)
1230
- break
1231
- case LogLevel.INFO:
1232
- default:
1233
- this.infoLog(log.message)
1234
- }
1235
- })
1236
- ```
1237
-
1238
- ### Supported Devices
1239
-
1240
- The following devices are supported.
1241
-
1242
- | Device | BLE Support |
1243
- | ---------------------------------------------- | ----------- |
1244
- | SwitchBot Bot | Yes |
1245
- | SwitchBot Curtain | Yes |
1246
- | SwitchBot Meter | Yes |
1247
- | SwitchBot Motion Sensor | Yes |
1248
- | SwitchBot Contact Sensor | Yes |
1249
- | SwitchBot Plug Mini | Yes |
1250
- | SwitchBot Smart Lock | Yes |
1251
- | SwitchBot Smart Lock Pro | Yes |
1252
- | SwitchBot Humidifier | Yes |
1253
- | SwitchBot Evaporative Humidifier (Auto-refill) | No |
1254
- | SwitchBot Color Bulb | Yes |
1255
- | SwitchBot LED Strip Light | Yes |
1256
-
1257
- ### Summary
1258
-
1259
- The `SwitchBotBLE` class provides a powerful way to interact with your SwitchBot devices through BLE. By following the examples provided, you can easily integrate SwitchBot device control and monitoring into your applications.