Class SwitchBotOpenAPI

The SwitchBotOpenAPI class provides methods to interact with the SwitchBot OpenAPI. It allows you to retrieve device information, control devices, and manage webhooks.

const switchBotAPI = new SwitchBotOpenAPI('your-token', 'your-secret');

// Get devices
switchBotAPI.getDevices().then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});

// Control a device
switchBotAPI.controlDevice('device-id', 'turnOn', 'default').then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});

// Setup webhook
switchBotAPI.setupWebhook('http://your-webhook-url').then(() => {
console.log('Webhook setup successfully');
}).catch(error => {
console.error(error);
});

The API token used for authentication.

The secret key used for signing requests.

Hierarchy

  • EventEmitter
    • SwitchBotOpenAPI

Constructors

  • Creates an instance of the SwitchBot OpenAPI client.

    Parameters

    • token: string

      The API token used for authentication.

    • secret: string

      The secret key used for signing requests.

    Returns SwitchBotOpenAPI

Properties

webhookEventListener?: null | Server<typeof IncomingMessage, typeof ServerResponse> = null

Methods

  • Controls a device by sending a command to the SwitchBot API.

    Parameters

    • deviceId: string

      The unique identifier of the device to control.

    • command: string

      The command to send to the device.

    • parameter: string

      The parameter for the command.

    • commandType: string = 'command'

      The type of the command, defaults to 'command'.

    Returns Promise<{
        response: {
            commandId: string;
        };
        statusCode: number;
    }>

    A promise that resolves to an object containing the API response.

    An error if the device control fails.

  • Deletes a webhook by sending a request to the specified URL.

    Parameters

    • url: string

      The URL of the webhook to be deleted.

    Returns Promise<void>

    A promise that resolves when the webhook is successfully deleted.

    Will log an error if the deletion fails.

  • Retrieves the list of devices from the SwitchBot OpenAPI.

    Returns Promise<{
        response: devices;
        statusCode: number;
    }>

    A promise that resolves to an object containing the API response.

    Throws an error if the request to get devices fails.

  • Retrieves the status of a specific device.

    Parameters

    • deviceId: string

      The unique identifier of the device.

    Returns Promise<{
        response: deviceStatus;
        statusCode: number;
    }>

    A promise that resolves to the device status.

    An error if the request fails.

  • Sets up a webhook listener and configures the webhook on the server.

    This method performs the following steps:

    1. Creates a local server to listen for incoming webhook events.
    2. Sends a request to set up the webhook with the provided URL.
    3. Sends a request to update the webhook configuration.
    4. Sends a request to query the current webhook URL.

    Parameters

    • url: string

      The URL to which the webhook events will be sent.

    Returns Promise<void>

    A promise that resolves when the webhook setup is complete.

    Will log an error if any step in the webhook setup process fails.