iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔘

Controlling SwitchBot with Stream Deck (via SwitchBot API)

に公開
2022/05/21

Introduction

Recently, I wrote an article about how to control SwitchBot from a Stream Deck using IFTTT.
https://zenn.dev/07jp27/articles/streamdeck-ifttt-integration

Although I wrote the following in that article, I was wondering, "Couldn't I call it directly if a SwitchBot API exists?"

Since IFTTT has a limit of three Applets for free users, I plan to subscribe to a paid plan and integrate all my SwitchBots if it seems useful after trying it out for a while.

Actually, when I first bought a SwitchBot in June 2020, I looked into the availability of an API, but it wasn't provided at that time, so I wasn't expecting much.
However, upon checking, I found that an API documentation page was released in December 2020 🎉!

https://github.com/OpenWonderLabs/SwitchBotAPI

So this time, I will try to control devices connected to SwitchBot by calling the SwitchBot API directly from the Stream Deck without using IFTTT.

Implementation

Obtaining the SwitchBot API Token

You can obtain the SwitchBot API token from the SwitchBot app.

  1. Open "Settings" in the SwitchBot app and tap "App Version" 10 times.

  2. Tap the "Developer Options" menu that appears after the 10 taps.

  3. The token will be displayed, so make a note of it.

Obtaining the Device ID of the Target Device

Call the API by setting the URL to https://api.switch-bot.com/v1.0/devices and the Header to Authorization: {Token obtained from the SwitchBot app} using Postman or Insomnia. (Of course, using curl from the CLI is also fine.)

Once the request is successful, you will receive a list of devices linked to the token's account. Make a note of the deviceId for the device you wish to control.

Stream Deck Settings

Installing the API Ninja Plugin

  1. Open the Stream Deck Store.
  2. Go to Plugins -> Developer Tools and click "Install" for API Ninja.

API Ninja Settings

  1. Drag and drop API Ninja from the BarRaider category.

  2. Configure the API call according to the SwitchBot API documentation.
    In this example, we will set up the command to turn on a Plug.

Item Setting
Request Type POST
API URL https://api.switch-bot.com/v1.0/devices/{Obtained Plug deviceId}/commands
Content Type application/json
Headers Authorization:{Token obtained from the SwitchBot app}
Data {"command": "turnOff","parameter": "default","commandType": "command"}


Complete!

Set your favorite icon, and you're all set. Try pressing it to test.

Conclusion

With this, I can now control SwitchBot devices without using IFTTT, successfully avoiding the need for a paid plan.

Since the API Ninja plugin used here is only released for Windows, the method in this article can only be configured on Windows. However, since the internal process is just accessing a URL, it should be possible on Mac as well.

While waiting (without much expectation) for an official Stream Deck plugin from SwitchBot, I’m thinking of trying to build one myself (it seems there’s no SDK yet, so I might try implementing that as well).

GitHubで編集を提案

Discussion