node-switchbot
    Preparing search index...

    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
    Index

    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.

      • Optionalhostname: string

      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 ID of the device to control.

      • command: string

        The command to send to the device.

      • parameter: string

        The parameter for the command.

      • commandType: commandType = 'command'

        The type of the command (default is 'command').

      • Optionaltoken: string

        (Optional) The token used for authentication. If not provided, the instance token will be used.

      • Optionalsecret: string

        (Optional) The secret used for authentication. If not provided, the instance secret will be used.

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

      A promise that resolves to an object containing the response body and status code.

      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.

      • Optionaltoken: string

        (Optional) The token used for authentication. If not provided, the instance token will be used.

      • Optionalsecret: string

        (Optional) The secret used for authentication. If not provided, the instance secret will be used.

      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.

      Parameters

      • Optionaltoken: string

        (Optional) The token used for authentication. If not provided, the instance token will be used.

      • Optionalsecret: string

        (Optional) The secret used for authentication. If not provided, the instance secret will be used.

      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.

      • Optionaltoken: string

        (Optional) The token used for authentication. If not provided, the instance token will be used.

      • Optionalsecret: string

        (Optional) The secret used for authentication. If not provided, the instance secret will be used.

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

      A promise that resolves to an object containing the device status and the status code of the request.

      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.

      • Optionaltoken: string

        (Optional) The token used for authentication. If not provided, the instance token will be used.

      • Optionalsecret: string

        (Optional) The secret used for authentication. If not provided, the instance secret will be used.

      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.