|
| 1 | +# SwitchBot OpenAPI Documentation |
| 2 | + |
| 3 | +The `SwitchBotOpenAPI` class allows you to interact with SwitchBot devices using the SwitchBot OpenAPI. This documentation provides an overview of how to install, set up, and use the various methods available in the `SwitchBotOpenAPI` class. |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [OpenAPI](#openapi) |
| 8 | + - [Importing and Setting Up](#importing-and-setting-up) |
| 9 | + - [`SwitchBotOpenAPI` Object](#switchbotopenapi-object) |
| 10 | + - [`getDevices()` Method](#getdevices-method) |
| 11 | + - [`controlDevice()` Method](#controldevice-method) |
| 12 | + - [`getDeviceStatus()` Method](#getdevicestatus-method) |
| 13 | + - [`setupWebhook()` Method](#setupwebhook-method) |
| 14 | + - [`deleteWebhook()` Method](#deletewebhook-method) |
| 15 | + - [Logging](#logging) |
| 16 | + - [Supported Devices](#supported-devices) |
| 17 | + |
| 18 | +## OpenAPI |
| 19 | + |
| 20 | +### Importing and Setting Up |
| 21 | + |
| 22 | +To use the `SwitchBotOpenAPI` class, you need to import it and create an instance with your SwitchBot token and secret. |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { SwitchBotOpenAPI } from 'node-switchbot' |
| 26 | + |
| 27 | +const switchBotAPI = new SwitchBotOpenAPI('your-token', 'your-secret') |
| 28 | + |
| 29 | +// Example usage |
| 30 | +switchBotAPI.getDevices().then((devices) => { |
| 31 | + console.log('Devices:', devices) |
| 32 | +}).catch((error) => { |
| 33 | + console.error('Error getting devices:', error) |
| 34 | +}) |
| 35 | +``` |
| 36 | + |
| 37 | +### SwitchBotOpenAPI Object |
| 38 | + |
| 39 | +The `SwitchBotOpenAPI` object provides several methods to interact with SwitchBot devices. Below are examples of how to use each method. |
| 40 | + |
| 41 | +#### `getDevices()` Method |
| 42 | + |
| 43 | +Fetches the list of devices associated with your SwitchBot account. |
| 44 | + |
| 45 | +```typescript |
| 46 | +async function getDevices() { |
| 47 | + try { |
| 48 | + const devices = await switchBotAPI.getDevices() |
| 49 | + console.log('Devices:', devices) |
| 50 | + } catch (error) { |
| 51 | + console.error('Error getting devices:', error) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Example usage |
| 56 | +getDevices() |
| 57 | +``` |
| 58 | + |
| 59 | +#### `controlDevice()` Method |
| 60 | + |
| 61 | +Sends a command to control a specific device. |
| 62 | + |
| 63 | +```typescript |
| 64 | +async function controlDevice(deviceId, command, parameter) { |
| 65 | + try { |
| 66 | + const response = await switchBotAPI.controlDevice(deviceId, command, parameter) |
| 67 | + console.log('Control Device Response:', response) |
| 68 | + } catch (error) { |
| 69 | + console.error('Error controlling device:', error) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Example usage |
| 74 | +controlDevice('your-device-id', 'turnOn', 'default') |
| 75 | +``` |
| 76 | + |
| 77 | +#### `getDeviceStatus()` Method |
| 78 | + |
| 79 | +Fetches the current status of a specific device. |
| 80 | + |
| 81 | +```typescript |
| 82 | +async function getDeviceStatus(deviceId) { |
| 83 | + try { |
| 84 | + const status = await switchBotAPI.getDeviceStatus(deviceId) |
| 85 | + console.log('Device Status:', status) |
| 86 | + } catch (error) { |
| 87 | + console.error('Error getting device status:', error) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// Example usage |
| 92 | +getDeviceStatus('your-device-id') |
| 93 | +``` |
| 94 | + |
| 95 | +#### `setupWebhook()` Method |
| 96 | + |
| 97 | +Sets up a webhook to receive events from SwitchBot devices. |
| 98 | + |
| 99 | +```typescript |
| 100 | +async function setupWebhook(url) { |
| 101 | + try { |
| 102 | + await switchBotAPI.setupWebhook(url) |
| 103 | + console.log('Webhook setup successfully') |
| 104 | + } catch (error) { |
| 105 | + console.error('Error setting up webhook:', error) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Example usage |
| 110 | +setupWebhook('http://your-webhook-url') |
| 111 | +``` |
| 112 | + |
| 113 | +#### `deleteWebhook()` Method |
| 114 | + |
| 115 | +Deletes an existing webhook. |
| 116 | + |
| 117 | +```typescript |
| 118 | +async function deleteWebhook(url) { |
| 119 | + try { |
| 120 | + await switchBotAPI.deleteWebhook(url) |
| 121 | + console.log('Webhook deleted successfully') |
| 122 | + } catch (error) { |
| 123 | + console.error('Error deleting webhook:', error) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// Example usage |
| 128 | +deleteWebhook('http://your-webhook-url') |
| 129 | +``` |
| 130 | + |
| 131 | +### Logging |
| 132 | + |
| 133 | +To be able to receive logging that this module is pushing out you will need to subscribt to the events. |
| 134 | + |
| 135 | +```typescript |
| 136 | +this.switchBotAPI.on('log', (log) => { |
| 137 | + switch (log.level) { |
| 138 | + case LogLevel.SUCCESS: |
| 139 | + this.successLog(log.message) |
| 140 | + break |
| 141 | + case LogLevel.DEBUGSUCCESS: |
| 142 | + this.debugSuccessLog(log.message) |
| 143 | + break |
| 144 | + case LogLevel.WARN: |
| 145 | + this.warnLog(log.message) |
| 146 | + break |
| 147 | + case LogLevel.DEBUGWARN: |
| 148 | + this.debugWarnLog(log.message) |
| 149 | + break |
| 150 | + case LogLevel.ERROR: |
| 151 | + this.errorLog(log.message) |
| 152 | + break |
| 153 | + case LogLevel.DEBUGERROR: |
| 154 | + this.debugErrorLog(log.message) |
| 155 | + break |
| 156 | + case LogLevel.DEBUG: |
| 157 | + this.debugLog(log.message) |
| 158 | + break |
| 159 | + case LogLevel.INFO: |
| 160 | + default: |
| 161 | + this.infoLog(log.message) |
| 162 | + } |
| 163 | +}) |
| 164 | +``` |
| 165 | + |
| 166 | +### Supported Devices |
| 167 | + |
| 168 | +The following devices are supported. |
| 169 | + |
| 170 | +| Device | OpenAPI Support | Webhook Support | |
| 171 | +| ------------------------- | --------------- | --------------- | |
| 172 | +| SwitchBot Bot | Yes | Yes | |
| 173 | +| SwitchBot Curtain | Yes | Yes | |
| 174 | +| SwitchBot Meter | Yes | Yes | |
| 175 | +| SwitchBot Motion Sensor | Yes | Yes | |
| 176 | +| SwitchBot Contact Sensor | Yes | Yes | |
| 177 | +| SwitchBot Plug Mini | Yes | Yes | |
| 178 | +| SwitchBot Smart Lock | Yes | Yes | |
| 179 | +| SwitchBot Humidifier | Yes | Yes | |
| 180 | +| SwitchBot Color Bulb | Yes | Yes | |
| 181 | +| SwitchBot LED Strip Light | Yes | Yes | |
| 182 | + |
| 183 | +### Summary |
| 184 | + |
| 185 | +The `SwitchBotOpenAPI` class provides a powerful way to interact with your SwitchBot devices programmatically. By following the examples provided, you can easily integrate SwitchBot device control and monitoring into your applications. |
0 commit comments