node-switchbot
    Preparing search index...

    Class WoAIHub

    Base class for all SwitchBot devices

    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.
    • 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.
    async getStatus(): Promise<DeviceStatus> {
    return this.getStatusWithFallback(
    bleData => ({ ... }), // normalize BLE data
    apiData => ({ ... }), // normalize API data
    )
    }

    async turnOn(): Promise<boolean> {
    const result = await this.sendCommand([0x57, 0x01, 0x01], 'turnOn')
    return result.success
    }
    • preferredConnection: 'ble' | 'api' (default: 'ble')
    • enableFallback: boolean (default: true)
    • enableConnectionIntelligence: boolean (default: true)
    • enableCircuitBreaker: boolean (default: true)
    • enableRetry: boolean (default: true)
    • getStatusWithFallback()
    • sendCommand()
    • hasBLE(), hasAPI()
    • setPreferredConnection(), setFallbackEnabled()

    This pattern ensures all device classes benefit from robust, testable, and consistent connection logic.

    Hierarchy (View Summary)

    Index

    Constructors

    • 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 WoAIHub

    Accessors

    • get deviceType(): string

      Get device type (property accessor for convenience)

      Returns string

    • get id(): string | undefined

      Get device ID (property accessor for convenience)

      Returns string | undefined

    • get mac(): string | undefined

      Get MAC address (property accessor for convenience)

      Returns string | undefined

    • get name(): string

      Get device name (property accessor for convenience)

      Returns string

    Methods

    • 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.

      Returns Promise<CommandResult>

    • Returns true if device should be polled (passive polling interval elapsed)

      Parameters

      • interval: number = PASSIVE_POLL_INTERVAL

      Returns boolean

    • Register a custom fallback handler

      Parameters

      • handler: FallbackHandler
      • Optionaloptions: FallbackHandlerOptions

      Returns string

    • Send multiple commands in sequence (all must succeed) Used for Curtain 3, bulbs, strips, and other multi-step devices

      Parameters

      • commands: (() => Promise<boolean>)[]

      Returns Promise<boolean>

    • Send multiple commands (returns true if any succeed) Used for fallback operations with complex patterns

      Parameters

      • commands: (() => Promise<boolean>)[]

      Returns Promise<boolean>

    • Universal mode setting command BLE: 0x57 0x03 [modeByte] API: 'setMode' (if available)

      Parameters

      • mode: string | number

        Mode value (number or string, per-device enum recommended)

        Example usage: await device.setMode('auto') await device.setMode(1)

        Returns a CommandResult object indicating success and mode info.

      Returns Promise<CommandResult>