Skip to content

Commit 2176af8

Browse files
committed
v4.0.0
feat: v4.0.0 - Complete rewrite with hybrid BLE/API architecture and advanced features BREAKING CHANGES: - Complete rewrite with unified hybrid BLE/API architecture - No backward compatibility with v3.x - migration required - Single `SwitchBot` class replaces `SwitchBotBLE` and `SwitchBotOpenAPI` - Device access via `switchbot.devices` manager pattern - Full TypeScript rewrite with comprehensive type definitions Major Features (v4.0.0): - **Hybrid Architecture**: Unified BLE-first approach with automatic OpenAPI fallback - **Automatic Discovery**: Combined BLE + OpenAPI device discovery in single call - **Smart Fallback**: Commands automatically retry via API when BLE fails - **Device Manager**: Centralized device management with get(), list(), clear() - **30 Device Types**: Full support for all SwitchBot devices - **Event-Driven**: EventEmitter-based architecture for discovery and commands - **TypeScript Native**: Full type safety and comprehensive type exports - **Custom Errors**: Specific error classes (BLENotAvailableError, DeviceNotFoundError, etc.) - **Flexible Modes**: BLE-only mode (Linux/macOS) or API-only mode (all platforms) Additional Features (Post-4.0.0): - **macOS BLE Support**: Enable BLE on macOS in addition to Linux * Platform detection now includes darwin (macOS) * Windows remains unsupported for BLE operations - **Bot Password Protection**: BLE password support for Bot devices * CRC32 encryption for 4-char alphanumeric passwords * Methods: setPassword(), clearPassword(), hasPassword() * Automatic encrypted command execution - **Advanced Resilience**: Enterprise-grade reliability features * Retry logic with exponential backoff and jitter * Circuit breaker pattern (CLOSED/OPEN/HALF_OPEN states) * Connection intelligence tracking per-device * Custom fallback handler system with logging, metrics, alerting Technical Improvements: - Complete ES2022 module implementation - Improved error handling with custom error classes - Better connection management and timeout handling - Enhanced logging with configurable log levels - Full JSDoc documentation coverage - Comprehensive TypeScript type definitions - Move regex patterns to module scope for performance - Fix BLE Noble initialization race conditions - Proper null checks to prevent "Cannot read properties of undefined" Dependencies: - Update @antfu/eslint-config from 5.4.1 to 7.7.0 - Update @types/node from 25.3.3 to 25.3.5 - Update eslint from 9.39.3 to 9.39.4 - Update @stoprocent/noble to ^2.3.17 Documentation: - Add comprehensive prerequisites for BLE setup (macOS, Linux, Raspberry Pi) - Update all "Linux-only" references to "Linux/macOS" - Add password protection section to BLE.md - Create bot-password.js example - Add 6 example files covering all major use cases - Generate TypeDoc API documentation Tests: - 108 comprehensive tests (up from 40 in v3.x) - 14 new password protection tests - Device manager tests - Utilities tests - Error handling tests - Full validation of CRC32 encryption and command building Files Changed: 431 files, 13,604 insertions(+), 12,376 deletions(-)
1 parent db28768 commit 2176af8

558 files changed

Lines changed: 27226 additions & 15601 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/copilot-instructions.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@ Always reference these instructions first and fallback to search or bash command
2121
- **Use lint:fix for automatic fixes**: `npm run lint:fix` to automatically fix ESLint issues
2222

2323
### Platform Requirements and Constraints
24-
- **BLE functionality requires Linux-based OS only** (Raspbian, Ubuntu, etc.)
25-
- **Windows and macOS are NOT supported** for BLE operations (use OpenAPI instead)
24+
- **BLE functionality requires Linux or macOS** (Raspbian, Ubuntu, macOS, etc.)
25+
- **Windows is NOT supported** for BLE operations (use OpenAPI instead)
2626
- **Node.js versions**: Must use ^20, ^22, or ^24 (currently using v20.19.4)
2727
- **ES Modules**: This project uses `"type": "module"` - always use ES import/export syntax
2828

29+
#### BLE Prerequisites
30+
31+
**macOS:**
32+
- Xcode installed
33+
- Bluetooth permissions enabled for terminal app in System Preferences
34+
35+
**Linux (Ubuntu/Debian/Raspbian):**
36+
- Required packages: `sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev`
37+
- For non-root access: `sudo setcap cap_net_raw+eip $(eval readlink -f \`which node\`)`
38+
- Raspberry Pi: May need to disable pnat plugin in `/etc/bluetooth/main.conf`
39+
40+
**Linux (Fedora/RPM):**
41+
- Required packages: `sudo yum install bluez bluez-libs bluez-libs-devel`
42+
2943
### Testing and Validation
3044
- **Basic functionality test**: After any changes to core classes, run this validation:
3145
```javascript
@@ -116,9 +130,9 @@ Always reference these instructions first and fallback to search or bash command
116130
## Troubleshooting Common Issues
117131

118132
### BLE Not Working
119-
- **Check OS**: BLE only works on Linux-based systems
133+
- **Check OS**: BLE works on Linux and macOS systems only
120134
- **Install noble prerequisites**: May need additional system libraries for @stoprocent/noble
121-
- **Use OpenAPI instead**: For Windows/macOS development, use SwitchBotOpenAPI class
135+
- **Use OpenAPI instead**: For Windows development, use SwitchBotOpenAPI class
122136

123137
### Build Failures
124138
- **Check Node.js version**: Must be ^20, ^22, or ^24
@@ -156,7 +170,7 @@ npm run watch # Build and link for development
156170
- **Type definitions**: All device status and response types
157171

158172
### Dependencies Summary
159-
- **@stoprocent/noble**: BLE functionality (Linux only)
173+
- **@stoprocent/noble**: BLE functionality (Linux/macOS)
160174
- **undici**: HTTP client for API requests
161175
- **async-mutex**: Concurrency control
162176
- **TypeScript**: Language and compilation

.npmignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
src
44

5+
# v4.0.0 additions
6+
test/
7+
examples/
8+
.dev-archive/
9+
vitest.config.ts
10+
typedoc.json
11+
eslint.config.js
12+
513
# ------------- Defaults -------------
614

715
# eslint

BLE.md

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,61 @@ The `SwitchBot` class allows you to interact with SwitchBot devices using the Sw
5959

6060
### Supported OS
6161

62-
The node-switchbot supports only Linux-based OSes, such as Raspbian, Ubuntu, and so on. This module does not support Windows and macOS for now. (If [@stoprocent/noble](https://github.com/stoprocent/noble#readme) is installed properly, this module might work well on such OSes.)
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.
6363

6464
### Dependencies
6565

66-
- [Node.js](https://nodejs.org/en/): ^20
67-
- [@stoprocent/noble](https://github.com/stoprocent/noble)
68-
- Included as a dependency so no need to install manually however if for some reason your OS requires addtional libaries, see `@stoprocent/noble` [prerequisites](https://github.com/stoprocent/noble?tab=readme-ov-file#prerequisites), you will then need to reinstall `node-switchbot` or the package that you have `node-swtichbot` as a dependency.
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.
69117

70118
### Importing and Setting Up
71119

@@ -587,6 +635,71 @@ switchbot
587635
});
588636
```
589637

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+
590703
### `WoCurtain` Object
591704

592705
The `WoCurtain` object represents a Curtain, which is created through the discovery process triggered by the [`switchBotBLE.discover()`](#discover-method) method.

CHANGELOG.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,95 @@
22

33
All notable changes to this project will be documented in this file. This project uses [Semantic Versioning](https://semver.org/)
44

5+
## [Unreleased]
6+
7+
### Features
8+
9+
* **macOS BLE Support**: Enabled BLE functionality on macOS in addition to Linux
10+
* Platform detection now includes macOS (darwin) using @stoprocent/noble
11+
* BLE now works on Linux and macOS systems
12+
* Windows remains unsupported for BLE operations
13+
14+
* **Bot Password Protection**: Added BLE password support for Bot (WoHand) devices
15+
* Password encryption using CRC32 checksums
16+
* Password validation (4 alphanumeric characters, case-sensitive)
17+
* Methods: `setPassword()`, `clearPassword()`, `hasPassword()`
18+
* Automatic encrypted command execution when password is set
19+
* Example: `examples/bot-password.js`
20+
* Based on [homebridge-switchbot PR #1337](https://github.com/OpenWonderLabs/homebridge-switchbot/pull/1337)
21+
22+
* **Advanced Resilience Features**: Enterprise-grade reliability enhancements
23+
* Retry logic with exponential backoff and jitter (RetryExecutor)
24+
* Circuit breaker pattern to prevent cascading failures (CLOSED/OPEN/HALF_OPEN states)
25+
* Connection intelligence tracking per-device success/failure rates
26+
* Custom fallback handler system with built-in logging, metrics, and alerting handlers
27+
* Intelligent connection selection based on historical performance
28+
29+
### Documentation
30+
31+
* Added password protection section to [BLE.md](BLE.md)
32+
* Updated [README.md](README.md) with password examples
33+
* Created comprehensive password protection example
34+
* Updated examples index with bot-password.js
35+
* Updated platform support documentation to reflect macOS BLE support
36+
* Updated all "Linux-only" references to "Linux/macOS"
37+
* **Added comprehensive prerequisites section** for BLE setup on macOS and Linux
38+
* macOS: Xcode and Bluetooth permissions requirements
39+
* Linux: System packages, non-root setup, and Raspberry Pi specific notes
40+
* Based on [@stoprocent/noble prerequisites](https://github.com/stoprocent/noble?tab=readme-ov-file#prerequisites)
41+
42+
### Tests
43+
44+
* Added 14 comprehensive password protection tests
45+
* All 54 tests passing (up from 40 in v4.0.0)
46+
* Validation of CRC32 encryption, command building, and response parsing
47+
48+
---
49+
50+
## [4.0.0](https://github.com/OpenWonderLabs/node-switchbot/compare/v3.6.6...v4.0.0) (2026-03-03)
51+
52+
### ⚠ BREAKING CHANGES
53+
54+
* Complete rewrite with unified hybrid BLE/API architecture
55+
* No backward compatibility with v3.x - migration required
56+
* Single `SwitchBot` class replaces `SwitchBotBLE` and `SwitchBotOpenAPI`
57+
* Device access via `switchbot.devices` manager pattern
58+
* Full TypeScript rewrite with comprehensive type definitions
59+
60+
### Features
61+
62+
* **Hybrid Architecture**: Unified BLE-first approach with automatic OpenAPI fallback
63+
* **Automatic Discovery**: Combined BLE + OpenAPI device discovery in single call
64+
* **Smart Fallback**: Commands automatically retry via API when BLE fails
65+
* **Device Manager**: Centralized device management with `get()`, `list()`, and `clear()` methods
66+
* **30 Device Types**: Full support for all SwitchBot devices (Bot, Curtain, Lock, Meter, Plug, Bulb, etc.)
67+
* **Event-Driven**: EventEmitter-based architecture for discovery and command events
68+
* **TypeScript Native**: Written in TypeScript with full type safety and exports
69+
* **Custom Errors**: Specific error classes for better error handling
70+
* **BLE-Only Mode**: Works without OpenAPI credentials on Linux systems
71+
* **API-Only Mode**: Works without BLE on Windows/macOS
72+
* **Comprehensive Examples**: 6 example files covering all major use cases
73+
74+
### Technical Improvements
75+
76+
* Complete ES2022 module implementation
77+
* Improved error handling with custom error classes
78+
* Better connection management and timeout handling
79+
* Enhanced logging with configurable log levels
80+
* Full JSDoc documentation coverage
81+
* Comprehensive TypeScript type definitions for all devices
82+
* Production-ready test suite with Vitest
83+
* Auto-formatting and linting with @antfu/eslint-config
84+
85+
### Documentation
86+
87+
* Updated README with v4 quick start and migration guide
88+
* 6 usage examples in `examples/` directory
89+
* Migration guide from v3.x to v4.0.0
90+
* Full API documentation via TypeDoc
91+
92+
---
93+
594
## [3.6.6](https://github.com/OpenWonderLabs/node-switchbot/compare/v3.6.5...v3.6.6) (2026-02-25)
695

796

0 commit comments

Comments
 (0)