### Run TypeScript Example with Credentials Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md Execute TypeScript examples using ts-node. Ensure SWITCHBOT_TOKEN and SWITCHBOT_SECRET are set. Install ts-node globally if needed. ```bash # Install ts-node if needed npm install -g ts-node # Run TypeScript example SWITCHBOT_TOKEN=xxx SWITCHBOT_SECRET=yyy ts-node examples/typescript-usage.ts ``` -------------------------------- ### Run API-Only Example (Any Platform) Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md Execute the API-only example, which works on any platform and requires API credentials. Ensure SWITCHBOT_TOKEN and SWITCHBOT_SECRET are set. ```bash SWITCHBOT_TOKEN=xxx SWITCHBOT_SECRET=yyy node examples/api-only.js ``` -------------------------------- ### Run JavaScript Examples with Credentials Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md Execute JavaScript examples using environment variables for API credentials. Ensure SWITCHBOT_TOKEN and SWITCHBOT_SECRET are set. ```bash SWITCHBOT_TOKEN=xxx SWITCHBOT_SECRET=yyy node examples/basic-usage.js ``` -------------------------------- ### WoWaterDetector Example in a Device Subclass Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoWaterDetector.html An example demonstrating how to implement `getStatus` and `turnOn` methods using WoWaterDetector's fallback mechanisms. ```APIDOC ### Example (in a device subclass) async getStatus(): Promise { return this.getStatusWithFallback( bleData => ({ ... }), // normalize BLE data apiData => ({ ... }), // normalize API data ) } async turnOn(): Promise { const result = await this.sendCommand([0x57, 0x01, 0x01], 'turnOn') return result.success } ``` -------------------------------- ### Install Node-SwitchBot Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/README.md Install the node-switchbot module using npm. ```sh npm install --save node-switchbot ``` -------------------------------- ### Run BLE-Only Example (Linux/macOS) Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md Execute the BLE-only example, which does not require API credentials. This mode is suitable for Linux and macOS environments. ```bash node examples/ble-only.js ``` -------------------------------- ### getStatus Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/SequenceDevice.html Example of how to implement the getStatus method using the getStatusWithFallback helper for robust BLE-first, API-fallback logic. ```APIDOC ## getStatus ### Description Retrieves the device status using a BLE-first, API-fallback approach. ### Example ```typescript async getStatus(): Promise { return this.getStatusWithFallback( bleData => ({ ... }), // Process BLE data apiData => ({ ... }) // Process API data ); } ``` ``` -------------------------------- ### Example Device Discovery Output Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md This is an example of the output you might see when the script discovers SwitchBot devices. It lists the found devices with their names and types. ```text Discovering devices... Found 5 devices Device: Living Room Bot (WoHand) Device: Bedroom Curtain (WoCurtain) Device: Front Door Lock (WoSmartLock) Device: Kitchen Meter (WoSensorTH) Device: Office Plug (WoPlugMiniUS) ``` -------------------------------- ### SmartLock Advertisement Data Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md This JSON object shows an example of the advertisement data received from a WoSmartLock device. ```json { "id": "d30864110b8c", "address": "d3:08:64:11:0b:8c", "rssi": -52, "serviceData": { "model": "o", "modelName": "WoSmartLock", "battery": 100, "calibration": true, "status": "LOCKED", "update_from_secondary_lock": false, "door_open": false, "double_lock_mode": false, "unclosed_alarm": false, "unlocked_alarm": false, "auto_lock_paused": false } } ``` -------------------------------- ### Install Linux BLE Prerequisites Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md Installs necessary packages for BLE support on Ubuntu, Debian, and Raspbian. Grants raw socket capability to node for non-root access. ```bash sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev sudo apt-get install libcap2-bin sudo setcap cap_net_raw+eip $(eval readlink -f `which node`) ``` -------------------------------- ### Install RPM-Based Linux BLE Prerequisites Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md Installs required packages for BLE support on Fedora and other RPM-based Linux distributions. ```bash sudo yum install bluez bluez-libs bluez-libs-devel ``` -------------------------------- ### WoPlugMini Advertisement Data Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md Example JSON payload for a SwitchBot WoPlugMini device's advertisement data. Contains device ID, address, RSSI, and detailed service data. ```json { "id": "cd2409ea3e9441f87d4580e0380a62bf", "address": "60:55:f9:35:f6:a6", "rssi": -50, "serviceData": { "model": "j", "modelName": "WoPlugMini", "state": "off", "delay": false, "timer": false, "syncUtcTime": true, "wifiRssi": 48, "overload": false, "currentPower": 0 } } ``` -------------------------------- ### cleanUp Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoVacuumK10ProCombo.html Start cleaning (BLE-first, API-fallback). Inherited from WoVacuum.cleanUp. ```APIDOC ## cleanUp ### Description Start cleaning (BLE-first, API-fallback). ### Parameters #### Path Parameters * protocolVersion (number) - Required - The protocol version to use for cleaning. ### Returns Promise Inherited from WoVacuum.cleanUp ``` -------------------------------- ### BLEScanner Advertisement Scanning Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md Use BLEScanner for direct advertisement scanning and device discovery without the higher-level SwitchBot abstraction. This example starts a scan and logs discovered advertisements. ```typescript import { BLEScanner } from 'node-switchbot' const scanner = new BLEScanner() scanner.on('discover', (advertisement) => { console.log(advertisement) }) await scanner.startScan({ duration: 10_000, active: true }) ``` -------------------------------- ### getBasicInfo Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoContact.html Get basic device info (universal settings retrieval). Returns battery, firmware, device-specific settings, etc. Command: 0x57 0x02 (BLE), 'getBasicInfo' (API). Example usage: const info = await device.getBasicInfo(); console.log(info); Returns a CommandResult object with device info fields. Inherited from SwitchBotDevice.getBasicInfo. ```APIDOC ## getBasicInfo ### Description Get basic device info (universal settings retrieval). Returns battery, firmware, device-specific settings, etc. ### Command 0x57 0x02 (BLE), 'getBasicInfo' (API) ### Example Usage ```javascript const info = await device.getBasicInfo(); console.log(info); ``` ### Returns Promise ### Inherited From SwitchBotDevice.getBasicInfo ``` -------------------------------- ### Migrate from v3.x to v4.0.0 Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/README.md Example demonstrating the migration from separate BLE and OpenAPI classes in v3.x to the unified SwitchBot class in v4.0.0. ```javascript // v3.x (old) import { SwitchBotBLE, SwitchBotOpenAPI } from 'node-switchbot' const ble = new SwitchBotBLE() const api = new SwitchBotOpenAPI(token, secret) // v4.0.0 (new) import { SwitchBot } from 'node-switchbot' const switchbot = new SwitchBot({ token, secret, enableBLE: true }) ``` -------------------------------- ### get mac Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoSmartLockLite.html Get MAC address. This is a property accessor for convenience. ```APIDOC ## get mac ### Description Get MAC address (property accessor for convenience) ### Returns string | undefined Inherited from WoSmartLock.mac ``` -------------------------------- ### setupWebhook Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/OpenAPIClient.html Sets up a new webhook with the provided configuration. This method registers a webhook endpoint for receiving device events. ```APIDOC ## setupWebhook ### Description Sets up a webhook. ### Method setupWebhook(config: WebhookConfig): Promise[] ### Parameters #### Path Parameters - **config** (WebhookConfig) - Required - The configuration object for the webhook. ### Returns Promise[] ``` -------------------------------- ### get name Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoPlugMiniUS.html Get the device name. This is a property accessor for convenience. ```APIDOC ## get name ### Description Get the device name. ### Method GET ### Endpoint `/name` ### Returns - **name** (string) - The name of the device. ``` -------------------------------- ### SwitchBot Class Initialization Source: https://context7.com/openwonderlabs/node-switchbot/llms.txt Demonstrates how to initialize the main SwitchBot class with various configuration options, including API credentials, BLE enablement, fallback settings, and logging. ```APIDOC ## SwitchBot (main class) The top-level `SwitchBot` class is the entry point for all operations. It initialises both a `BLEScanner`/`BLEConnection` (when `enableBLE` is `true` on Linux/macOS) and an `OpenAPIClient` (when `token`+`secret` are provided), then exposes a `DeviceManager` at `switchbot.devices`. ```typescript import { SwitchBot, LogLevel } from 'node-switchbot' // Hybrid mode: BLE-first with API fallback const switchbot = new SwitchBot({ token: process.env.SWITCHBOT_TOKEN, // required for API access secret: process.env.SWITCHBOT_SECRET, // required for API access enableBLE: true, // default true on Linux/macOS enableFallback: true, // auto-fallback BLE→API on failure logLevel: LogLevel.INFO, // NONE|ERROR|WARN|INFO|DEBUG scanTimeout: 10000, // BLE scan duration in ms enableCircuitBreaker: true, enableRetry: true, maxRetryAttempts: 3, retryInitialDelayMs: 100, retryMaxDelayMs: 5000, }) console.log('BLE available:', switchbot.isBLEAvailable()) console.log('API available:', switchbot.isAPIAvailable()) // Adjust log level at runtime switchbot.setLogLevel(LogLevel.DEBUG) // Retrieve current config const config = switchbot.getConfig() // Access the raw API client const apiClient = switchbot.getAPIClient() // Clean up connections when done await switchbot.cleanup() ``` ``` -------------------------------- ### get id Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoPlugMiniUS.html Get the device ID. This is a property accessor for convenience. ```APIDOC ## get id ### Description Get the device ID. ### Method GET ### Endpoint `/id` ### Returns - **id** (string | undefined) - The unique identifier of the device. ``` -------------------------------- ### get mac Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoPlugMiniUS.html Get the MAC address of the device. This is a property accessor for convenience. ```APIDOC ## get mac ### Description Get the MAC address of the device. ### Method GET ### Endpoint `/mac` ### Returns - **mac** (string | undefined) - The MAC address of the device. ``` -------------------------------- ### WoCeilingLight Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoCeilingLight.html Initializes a new instance of the WoCeilingLight class. It accepts device information and optional configuration options for API client, BLE connection, and various connection intelligence features. ```APIDOC ## constructor ### Description Initializes a new instance of the WoCeilingLight class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Optional - Enables the circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback mechanisms. * **enableRetry** (boolean) - Optional - Enables retry mechanisms. * **logLevel** (number) - Optional - Sets the log level. * **preferredConnection** (ConnectionType) - Optional - Preferred connection type. * **retryConfig** (RetryConfig) - Optional - Configuration for retries. ### Returns * WoCeilingLight - The initialized WoCeilingLight instance. ``` -------------------------------- ### get name Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoHub3.html Get device name (property accessor for convenience). Inherited from WoHub2. ```APIDOC ## get name ### Description Get device name (property accessor for convenience). ### Returns string ### Inherited From WoHub2.name ``` -------------------------------- ### Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRGBICBulb.html Initializes a new instance of the WoRGBICBulb class. ```APIDOC ## constructor ### Description Initializes a new instance of the WoRGBICBulb class. ### Parameters * **deviceConfig** (object) - Required - Configuration object for the device. ``` -------------------------------- ### get mac Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoHub3.html Get MAC address (property accessor for convenience). Inherited from WoHub2. ```APIDOC ## get mac ### Description Get MAC address (property accessor for convenience). ### Returns string | undefined ### Inherited From WoHub2.mac ``` -------------------------------- ### Example Usage in a Device Subclass Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/SwitchbotDevice.html Demonstrates how to implement `getStatus()` and a command method like `turnOn()` within a device subclass, utilizing the `getStatusWithFallback()` and `sendCommand()` methods for hybrid connection logic. ```typescript async getStatus(): Promise { return this.getStatusWithFallback( bleData => ({ ... }), // normalize BLE data apiData => ({ ... }), // normalize API data ) } async turnOn(): Promise { const result = await this.sendCommand([0x57, 0x01, 0x01], 'turnOn') return result.success } ``` -------------------------------- ### get name Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoCirculatorFan.html Get device name (property accessor for convenience). Inherited from WoAirPurifier.name. ```APIDOC ## get name ### Description Get device name (property accessor for convenience). ### Returns string ### Source devices/base.ts:263 ``` -------------------------------- ### get mac Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoCirculatorFan.html Get MAC address (property accessor for convenience). Inherited from WoAirPurifier.mac. ```APIDOC ## get mac ### Description Get MAC address (property accessor for convenience). ### Returns string | undefined ### Source devices/base.ts:291 ``` -------------------------------- ### constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRemoteWithScreen.html Initializes a new instance of the WoRemoteWithScreen class. ```APIDOC ## constructor ### Description Initializes a new instance of the WoRemoteWithScreen class. ### Parameters * **deviceConfig** (object) - Required - Configuration object for the device. * **deviceConfig.id** (string) - Required - The unique identifier of the device. * **deviceConfig.name** (string) - Required - The name of the device. * **deviceConfig.mac** (string) - Required - The MAC address of the device. * **deviceConfig.firmwareVersion** (string) - Optional - The firmware version of the device. * **deviceConfig.model** (string) - Optional - The model of the device. * **deviceConfig.isCloud** (boolean) - Optional - Indicates if the device is cloud-connected. ``` -------------------------------- ### get id Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoCirculatorFan.html Get device ID (property accessor for convenience). Inherited from WoAirPurifier.id. ```APIDOC ## get id ### Description Get device ID (property accessor for convenience). ### Returns string | undefined ### Source devices/base.ts:249 ``` -------------------------------- ### WoPanTiltCamPlus3K Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoPanTiltCamPlus3K.html Initializes a new instance of the WoPanTiltCamPlus3K class. It accepts device information and optional configuration settings for API client, BLE connection, and connection intelligence. ```APIDOC ## constructor ### Description Initializes a new instance of the WoPanTiltCamPlus3K class. ### Parameters * **info**: DeviceInfo - Information about the device. * **options**: object - Optional configuration settings. * **apiClient**: OpenAPIClient - An optional OpenAPI client instance. * **bleConnection**: BLEConnection - An optional BLE connection instance. * **circuitBreakerConfig**: CircuitBreakerConfig - Configuration for the circuit breaker. * **enableCircuitBreaker**: boolean - Enables the circuit breaker. * **enableConnectionIntelligence**: boolean - Enables connection intelligence. * **enableFallback**: boolean - Enables fallback mechanisms. * **enableRetry**: boolean - Enables retry logic. * **logLevel**: number - The logging level. * **preferredConnection**: ConnectionType - The preferred connection type. * **retryConfig**: RetryConfig - Configuration for retries. ### Returns WoPanTiltCamPlus3K - A new instance of WoPanTiltCamPlus3K. ``` -------------------------------- ### WoRGBICWWStripLight Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRGBICWWStripLight.html Initializes a new instance of the WoRGBICWWStripLight class. It accepts device information and optional configuration options for API client, BLE connection, circuit breaker, connection intelligence, fallback, retry, log level, and preferred connection. ```APIDOC ## constructor ### Description Initializes a new instance of the WoRGBICWWStripLight class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Circuit breaker configuration. * **enableCircuitBreaker** (boolean) - Optional - Enables circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback mechanism. * **enableRetry** (boolean) - Optional - Enables retry mechanism. * **logLevel** (number) - Optional - Sets the log level. * **preferredConnection** (ConnectionType) - Optional - Sets the preferred connection type. * **retryConfig** (RetryConfig) - Optional - Retry configuration. ### Returns WoRGBICWWStripLight - The initialized WoRGBICWWStripLight instance. ``` -------------------------------- ### Hybrid Mode Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/media/OpenAPI.md Illustrates how to configure and use the SwitchBot class in hybrid mode, combining local BLE with cloud fallback. ```APIDOC ## Hybrid Mode If you want local BLE when available and cloud fallback when needed: ```typescript import { LogLevel, SwitchBot } from 'node-switchbot' const switchbot = new SwitchBot({ token: 'YOUR_TOKEN', secret: 'YOUR_SECRET', enableBLE: true, enableFallback: true, enableConnectionIntelligence: true, enableCircuitBreaker: true, enableRetry: true, logLevel: LogLevel.INFO, }) await switchbot.discover({ scanBLE: true, fetchAPI: true, timeout: 10_000, }) const lock = switchbot.devices.get('YOUR_LOCK_ID') if (lock) { await lock.lock() } await switchbot.cleanup() ``` ``` -------------------------------- ### WoSensorTHPlus Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoSensorTHPlus.html Initializes a new instance of the WoSensorTHPlus class. ```APIDOC ## constructor ### Description Initializes a new instance of the WoSensorTHPlus class. ### Parameters * `info`: DeviceInfo - Information about the device. * `options`: object - Optional configuration settings for the device. * `apiClient`: OpenAPIClient - An optional API client instance. * `bleConnection`: BLEConnection - An optional BLE connection instance. * `circuitBreakerConfig`: CircuitBreakerConfig - Configuration for the circuit breaker. * `enableCircuitBreaker`: boolean - Enables the circuit breaker. * `enableConnectionIntelligence`: boolean - Enables connection intelligence. * `enableFallback`: boolean - Enables fallback mechanisms. * `enableRetry`: boolean - Enables retry logic. * `logLevel`: number - The desired log level. * `preferredConnection`: ConnectionType - The preferred connection type. * `retryConfig`: RetryConfig - Configuration for retry logic. ### Returns WoSensorTHPlus - A new instance of the WoSensorTHPlus class. ``` -------------------------------- ### constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierTable.html Initializes a new instance of the WoAirPurifierTable class. ```APIDOC ## constructor ### Description Initializes a new instance of the WoAirPurifierTable class. ### Parameters * **device** (object) - Required - The device object representing the air purifier. ``` -------------------------------- ### WoStrip Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoStrip.html Initializes a new instance of the WoStrip class. It accepts device information and optional configuration options for API client, BLE connection, and various connection intelligence features. ```APIDOC ## constructor WoStrip ### Description Initializes a new instance of the WoStrip class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - API client instance. * **bleConnection** (BLEConnection) - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Enable circuit breaker. * **enableConnectionIntelligence** (boolean) - Enable connection intelligence. * **enableFallback** (boolean) - Enable fallback mechanism. * **enableRetry** (boolean) - Enable retry mechanism. * **logLevel** (number) - Set the log level. * **preferredConnection** (ConnectionType) - Preferred connection type. * **retryConfig** (RetryConfig) - Configuration for retries. ### Returns * WoStrip - An instance of the WoStrip class. ``` -------------------------------- ### Install Required Packages for Linux BLE Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/examples/README.md Install necessary packages for BLE functionality on Ubuntu/Debian systems. This includes bluetooth, bluez, and development libraries. ```bash sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev ``` -------------------------------- ### WoRGBICNeonWireRopeLight Class Methods Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRGBICNeonWireRopeLight.html This section outlines the various methods available for controlling and querying the WoRGBICNeonWireRopeLight device, including status retrieval, command execution, and configuration. ```APIDOC ## Class WoRGBICNeonWireRopeLight Base class for all SwitchBot devices ### BLE-first, API-fallback Logic This class provides a centralized, robust hybrid connection strategy for all SwitchBot devices: * **BLE-first, API-fallback**: By default, status and command methods attempt BLE first (if available), then fall back to OpenAPI if BLE fails or is unavailable. This is controlled by `preferredConnection` and `enableFallback`. * **Centralized Fallback**: The `getStatusWithFallback()` and `sendCommand()` methods implement this logic. Device subclasses should call these methods and provide normalization/mapping as needed. * **Connection Intelligence**: Tracks connection health and performance, automatically preferring the most reliable connection if enabled. * **Circuit Breaker & Retry**: Both BLE and API commands are protected by circuit breaker and retry logic to handle transient failures gracefully. ### Usage in Subclasses * For status: Call `await this.getStatusWithFallback(normalizeBLE, normalizeAPI)` in your `getStatus()` implementation. * For commands: Use `await this.sendCommand(bleCommand, apiCommand, apiParameter)` to automatically select the best connection and handle fallback. * For custom logic: You may override or extend these methods, but should preserve the fallback and error-handling patterns for consistency. ### Example (in a device subclass) ```typescript async getStatus(): Promise { return this.getStatusWithFallback( bleData => ({ ... }), // normalize BLE data apiData => ({ ... }), // normalize API data ) } async turnOn(): Promise { const result = await this.sendCommand([0x57, 0x01, 0x01], 'turnOn') return result.success } ``` ### Configuration * `preferredConnection`: 'ble' | 'api' (default: 'ble') * `enableFallback`: boolean (default: true) * `enableConnectionIntelligence`: boolean (default: true) * `enableCircuitBreaker`: boolean (default: true) * `enableRetry`: boolean (default: true) ### See Also * `getStatusWithFallback()` * `sendCommand()` * `hasBLE()`, `hasAPI()` * `setPreferredConnection()`, `setFallbackEnabled()` ### Methods * `getActiveConnection()`: Returns the currently active connection type. * `getBasicInfo()`: Retrieves basic device information. * `getCircuitBreakerAPI()`: Gets the circuit breaker instance for API connections. * `getCircuitBreakerBLE()`: Gets the circuit breaker instance for BLE connections. * `getConnectionTracker()`: Retrieves the connection tracking instance. * `getDeviceType()`: Returns the device type string. * `getFallbackHandlerManager()`: Gets the fallback handler manager. * `getId()`: Returns the device ID. * `getInfo()`: Retrieves detailed device information. * `getMAC()`: Returns the device MAC address. * `getName()`: Returns the device name. * `getStatus()`: Retrieves the current status of the device. This method should use `getStatusWithFallback` internally. * `hasAPI()`: Checks if the device supports API connection. * `hasBLE()`: Checks if the device supports BLE connection. * `pollIfNeeded()`: Initiates a status poll if necessary. * `pollNeeded()`: Marks that a poll is needed. * `registerFallbackHandler()`: Registers a custom fallback handler. * `sendCommandSequence()`: Sends a sequence of commands. * `sendMultipleCommands()`: Sends multiple commands to the device. * `setBrightness()`: Sets the brightness of the RGBIC Neon Wire Rope Light. * `setCircuitBreakerEnabled()`: Enables or disables the circuit breaker. * `setColor()`: Sets the color of the RGBIC Neon Wire Rope Light. * `setColorTemperature()`: Sets the color temperature of the RGBIC Neon Wire Rope Light. * `setConnectionIntelligenceEnabled()`: Enables or disables connection intelligence. * `setFallbackEnabled()`: Enables or disables the fallback mechanism. * `setMode()`: Sets the operational mode of the device. * `setPreferredConnection()`: Sets the preferred connection type. * `setRetryEnabled()`: Enables or disables the retry mechanism. * `turnOff()`: Turns the device off. * `turnOn()`: Turns the device on. * `unregisterFallbackHandler()`: Unregisters a custom fallback handler. * `updateInfo()`: Updates the device information. ``` -------------------------------- ### SwitchBotConfig Type Example Source: https://context7.com/openwonderlabs/node-switchbot/llms.txt Demonstrates the structure of the SwitchBotConfig object, used for initializing the SwitchBot client. All fields are optional. ```typescript const config: SwitchBotConfig = { token: 'abc', secret: 'xyz', enableBLE: true, enableFallback: true, logLevel: 3 /* INFO */, scanTimeout: 10000, enableCircuitBreaker: true, enableRetry: true, maxRetryAttempts: 3, retryInitialDelayMs: 100, retryMaxDelayMs: 5000, enableConnectionIntelligence: true, detailedErrors: false, } ``` -------------------------------- ### WoPlugMiniJP Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoPlugMiniJP.html Initializes a new instance of the WoPlugMiniJP class. It accepts device information and optional configuration options for API client, BLE connection, and various connection intelligence features. ```APIDOC ## constructor ### Description Initializes a new instance of the WoPlugMiniJP class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Optional - Enables the circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback connection. * **enableRetry** (boolean) - Optional - Enables retry mechanism. * **logLevel** (number) - Optional - Sets the log level. * **preferredConnection** (ConnectionType) - Optional - Preferred connection type. * **retryConfig** (RetryConfig) - Optional - Configuration for retries. ### Returns WoPlugMiniJP - A new instance of the WoPlugMiniJP class. ``` -------------------------------- ### Hybrid Mode Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/OpenAPI.md Illustrates how to configure the SwitchBot class for hybrid mode, combining local BLE discovery with OpenAPI fallback for a robust connection. ```APIDOC ## Hybrid Mode Example ### Description This example demonstrates setting up the `SwitchBot` class to utilize both BLE and OpenAPI for device communication, providing local control with cloud fallback. ### Usage ```typescript import { LogLevel, SwitchBot } from 'node-switchbot' const switchbot = new SwitchBot({ token: 'YOUR_TOKEN', secret: 'YOUR_SECRET', enableBLE: true, enableFallback: true, enableConnectionIntelligence: true, enableCircuitBreaker: true, enableRetry: true, logLevel: LogLevel.INFO, }) await switchbot.discover({ scanBLE: true, fetchAPI: true, timeout: 10_000, }) const lock = switchbot.devices.get('YOUR_LOCK_ID') if (lock) { await lock.lock() } await switchbot.cleanup() ``` ``` -------------------------------- ### WoMotion Advertisement Data Example Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/BLE.md Example JSON payload representing advertisement data for a SwitchBot WoMotion device. Includes device ID, address, RSSI, and service data. ```json { "id": "e7216fa344a9", "address": "e7:21:6f:a3:44:a9", "rssi": -53, "serviceData": { "model": "s", "modelName": "WoMotion", "movement": false, "battery": 96, "lightLevel": "bright" } } ``` -------------------------------- ### WoLeak Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoLeak.html Initializes a new instance of the WoLeak class. It takes device information and optional configuration options for API client, BLE connection, and various connection management features. ```APIDOC ## constructor ### Description Initializes a new instance of the WoLeak class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - API client instance. * **bleConnection** (BLEConnection) - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Enables the circuit breaker. * **enableConnectionIntelligence** (boolean) - Enables connection intelligence. * **enableFallback** (boolean) - Enables fallback mechanism. * **enableRetry** (boolean) - Enables retry mechanism. * **logLevel** (number) - Sets the log level. * **preferredConnection** (ConnectionType) - Preferred connection type. * **retryConfig** (RetryConfig) - Configuration for retries. ### Returns WoLeak - A new instance of the WoLeak class. ``` -------------------------------- ### getName Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRemoteWithScreen.html Gets the name of the device. ```APIDOC ## getName ### Description Gets the name of the device. ### Returns String representing the device name. ``` -------------------------------- ### Usage in Subclasses Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/SwitchbotDevice.html Examples of how to use the `getStatusWithFallback` and `sendCommand` methods within device subclasses. ```APIDOC ### Usage in Subclasses * For status: Call `await this.getStatusWithFallback(normalizeBLE, normalizeAPI)` in your `getStatus()` implementation. * For commands: Use `await this.sendCommand(bleCommand, apiCommand, apiParameter)` to automatically select the best connection and handle fallback. * For custom logic: You may override or extend these methods, but should preserve the fallback and error-handling patterns for consistency. ### Example (in a device subclass) async getStatus(): Promise { return this.getStatusWithFallback( bleData => ({ ... }), // normalize BLE data apiData => ({ ... }), // normalize API data ) } async turnOn(): Promise { const result = await this.sendCommand([0x57, 0x01, 0x01], 'turnOn') return result.success } ``` -------------------------------- ### name Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierPM25.html Gets the name of the device. ```APIDOC ## Accessors ### name ```typescript get name(): string ``` Gets the name of the device. **Returns:** * `string` - The device name. ``` -------------------------------- ### WoVacuumK10Plus Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoVacuumK10Plus.html Initializes a new instance of the WoVacuumK10Plus class. It requires device information and accepts optional configuration options for API client, BLE connection, and various connection/retry settings. ```APIDOC ## constructor ### Description Initializes a new instance of the WoVacuumK10Plus class. ### Signature ```typescript new WoVacuumK10Plus(info: DeviceInfo, options?: { apiClient?: OpenAPIClient; bleConnection?: BLEConnection; circuitBreakerConfig?: CircuitBreakerConfig; enableCircuitBreaker?: boolean; enableConnectionIntelligence?: boolean; enableFallback?: boolean; enableRetry?: boolean; logLevel?: number; preferredConnection?: ConnectionType; retryConfig?: RetryConfig; }): WoVacuumK10Plus ``` ### Parameters #### info * **info** (DeviceInfo) - Required - Device information. #### options * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Circuit breaker configuration. * **enableCircuitBreaker** (boolean) - Optional - Enables circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback mechanism. * **enableRetry** (boolean) - Optional - Enables retry mechanism. * **logLevel** (number) - Optional - Sets the log level. * **preferredConnection** (ConnectionType) - Optional - Preferred connection type. * **retryConfig** (RetryConfig) - Optional - Retry configuration. ### Returns * WoVacuumK10Plus - A new instance of the WoVacuumK10Plus class. ``` -------------------------------- ### deviceType Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierPM25.html Gets the type of the device. ```APIDOC ## Accessors ### deviceType ```typescript get deviceType(): string ``` Gets the type of the device. **Returns:** * `string` - The device type identifier. ``` -------------------------------- ### name Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierTable.html Get device name. ```APIDOC ## name ### Description Get device name (property accessor for convenience). ### Returns string ``` -------------------------------- ### WoVacuumK11Plus Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoVacuumK11Plus.html Initializes a new instance of the WoVacuumK11Plus class. It accepts device information and optional configuration options for API client, BLE connection, and various connection/retry settings. ```APIDOC ## constructor ### Description Initializes a new instance of the WoVacuumK11Plus class. ### Parameters * **info**: DeviceInfo * **options**: { apiClient?: OpenAPIClient; bleConnection?: BLEConnection; circuitBreakerConfig?: CircuitBreakerConfig; enableCircuitBreaker?: boolean; enableConnectionIntelligence?: boolean; enableFallback?: boolean; enableRetry?: boolean; logLevel?: number; preferredConnection?: ConnectionType; retryConfig?: RetryConfig; } ### Returns WoVacuumK11Plus ``` -------------------------------- ### mac Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierTable.html Get MAC address. ```APIDOC ## mac ### Description Get MAC address (property accessor for convenience). ### Returns string | undefined ``` -------------------------------- ### WoAIHub Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAIHub.html Initializes a new instance of the WoAIHub class. ```APIDOC ## constructor ### Description Initializes a new instance of the WoAIHub class. ### Parameters * **id** (string) - Required - The unique identifier for the device. * **mac** (string) - Required - The MAC address of the device. * **name** (string) - Required - The name of the device. * **deviceType** (string) - Required - The type of the device. * **options** (object) - Optional - Configuration options for the hub. ``` -------------------------------- ### id Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierTable.html Get device ID. ```APIDOC ## id ### Description Get device ID (property accessor for convenience). ### Returns string | undefined ``` -------------------------------- ### open Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/interfaces/CurtainCommands.html Opens the smart curtain. Optionally accepts a speed parameter. ```APIDOC ## open ### Description Opens the smart curtain. Optionally accepts a speed parameter. ### Method Signature `open(speed?: number) => Promise` ### Parameters * **speed** (number) - Optional - The speed at which to open the curtain. ``` -------------------------------- ### id Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifierPM25.html Gets the unique identifier of the device. ```APIDOC ## Accessors ### id ```typescript get id(): string ``` Gets the unique identifier of the device. **Returns:** * `string` - The device ID. ``` -------------------------------- ### WoRelaySwitch2PM Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRelaySwitch2PM.html Initializes a new instance of the WoRelaySwitch2PM class. It accepts device information and optional configuration options for API client, BLE connection, and various feature enablers like circuit breaker, connection intelligence, fallback, and retry mechanisms. ```APIDOC ## constructor ### Description Initializes a new instance of the WoRelaySwitch2PM class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Optional - Enables the circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback mechanism. * **enableRetry** (boolean) - Optional - Enables retry mechanism. * **logLevel** (number) - Optional - Sets the log level. * **preferredConnection** (ConnectionType) - Optional - Sets the preferred connection type. * **retryConfig** (RetryConfig) - Optional - Configuration for retry mechanism. ### Returns WoRelaySwitch2PM - A new instance of WoRelaySwitch2PM. ``` -------------------------------- ### Initialize and Discover SwitchBot Devices Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/README.md Initialize the SwitchBot module with optional OpenAPI credentials and BLE support. Then, discover all available devices. ```javascript import { SwitchBot } from 'node-switchbot' const switchbot = new SwitchBot({ token: 'YOUR_TOKEN', // OpenAPI token (optional for BLE-only) secret: 'YOUR_SECRET', // OpenAPI secret (optional for BLE-only) enableBLE: true, // Enable BLE discovery (Linux/macOS) enableFallback: true, // Auto-fallback between BLE/API }) // Discover all devices (BLE + API) const devices = await switchbot.discover() // Control devices const bot = switchbot.devices.get('YOUR_DEVICE_ID') await bot.press() // Get status const status = await bot.getStatus() console.log(status) // Cleanup await switchbot.cleanup() ``` -------------------------------- ### Get Device Type Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoSmartLockPro.html Retrieves the type of the device. ```APIDOC ## getDeviceType ### Description Get device type. ### Returns - **string** (string[]) - An array containing the device type. ``` -------------------------------- ### WoRelaySwitch1PM Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRelaySwitch1PM.html Initializes a new instance of the WoRelaySwitch1PM class. This constructor accepts device information and optional configuration options for API client, BLE connection, circuit breaker, connection intelligence, fallback, retry, log level, and preferred connection. ```APIDOC ## constructor * new WoRelaySwitch1PM(     info: DeviceInfo,     options?: {         apiClient?: OpenAPIClient;         bleConnection?: BLEConnection;         circuitBreakerConfig?: CircuitBreakerConfig;         enableCircuitBreaker?: boolean;         enableConnectionIntelligence?: boolean;         enableFallback?: boolean;         enableRetry?: boolean;         logLevel?: number;         preferredConnection?: ConnectionType;         retryConfig?: RetryConfig;     }, ): WoRelaySwitch1PM #### Parameters * info: DeviceInfo * options: {     apiClient?: OpenAPIClient;     bleConnection?: BLEConnection;     circuitBreakerConfig?: CircuitBreakerConfig;     enableCircuitBreaker?: boolean;     enableConnectionIntelligence?: boolean;     enableFallback?: boolean;     enableRetry?: boolean;     logLevel?: number;     preferredConnection?: ConnectionType;     retryConfig?: RetryConfig; } = {} #### Returns WoRelaySwitch1PM Inherited from WoRelaySwitch1.constructor * Defined in devices/wo-relay-switch-1pm.ts:12 ``` -------------------------------- ### getId Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoIOSensorTH.html Get device ID. Inherited from WoSensorTH. ```APIDOC ## getId() ### Description Get device ID. ### Returns string ### Inherited From WoSensorTH.getId ``` -------------------------------- ### getInfo Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifier.html Get device information. Inherited from SequenceDevice. ```APIDOC ## getInfo ### Description Get device information. ### Method GET ### Endpoint /devices/{deviceId}/info ### Returns [DeviceInfo](../interfaces/DeviceInfo.html)[] ``` -------------------------------- ### WoRGBICNeonWireRopeLight Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoRGBICNeonWireRopeLight.html Initializes a new instance of the WoRGBICNeonWireRopeLight class. This constructor is inherited from the base SwitchBotDevice class and accepts device information and optional connection options. ```APIDOC ## constructor WoRGBICNeonWireRopeLight ### Description Initializes a new instance of the WoRGBICNeonWireRopeLight class. ### Parameters * **info** (DeviceInfo) - Required - Information about the device. * **options** (object) - Optional - Configuration options for the device connection. * **apiClient** (OpenAPIClient) - Optional - An OpenAPI client instance. * **bleConnection** (BLEConnection) - Optional - A BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Configuration for the circuit breaker. * **enableCircuitBreaker** (boolean) - Optional - Enables the circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enables connection intelligence. * **enableFallback** (boolean) - Optional - Enables fallback mechanism. * **enableRetry** (boolean) - Optional - Enables retry mechanism. * **logLevel** (number) - Optional - The log level. * **preferredConnection** (ConnectionType) - Optional - The preferred connection type. * **retryConfig** (RetryConfig) - Optional - Configuration for retries. ### Returns WoRGBICNeonWireRopeLight - An instance of WoRGBICNeonWireRopeLight. ``` -------------------------------- ### getId Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoAirPurifier.html Get device ID. Inherited from SequenceDevice. ```APIDOC ## getId ### Description Get device ID. ### Method GET ### Endpoint /devices/{deviceId}/id ### Returns string[] ``` -------------------------------- ### get Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/DeviceManager.html Retrieves a specific device by its unique ID. ```APIDOC ## get ### Description Retrieves a specific device by its unique ID. ### Parameters * `deviceId` (string) - The ID of the device to retrieve. ### Returns * `SwitchBotDevice | undefined` - The device object if found, otherwise undefined. ``` -------------------------------- ### WoSensorTHPro Constructor Source: https://github.com/openwonderlabs/node-switchbot/blob/latest/docs/classes/WoSensorTHPro.html Initializes a new instance of the WoSensorTHPro class. It accepts device information and optional configuration options for API client, BLE connection, and various connection management features. ```APIDOC ## constructor ### Description Initializes a new instance of the WoSensorTHPro class. ### Parameters * **info** (DeviceInfo) - Required - Device information. * **options** (object) - Optional - Configuration options: * **apiClient** (OpenAPIClient) - Optional - API client instance. * **bleConnection** (BLEConnection) - Optional - BLE connection instance. * **circuitBreakerConfig** (CircuitBreakerConfig) - Optional - Circuit breaker configuration. * **enableCircuitBreaker** (boolean) - Optional - Enable circuit breaker. * **enableConnectionIntelligence** (boolean) - Optional - Enable connection intelligence. * **enableFallback** (boolean) - Optional - Enable fallback mechanism. * **enableRetry** (boolean) - Optional - Enable retry mechanism. * **logLevel** (number) - Optional - Log level. * **preferredConnection** (ConnectionType) - Optional - Preferred connection type. * **retryConfig** (RetryConfig) - Optional - Retry configuration. ### Returns WoSensorTHPro - A new instance of WoSensorTHPro. ```