Skip to main content

A Python client library for SwitchBot API.

Project description


PyPI - Python Version PyPI - Library Version PyPI - Downloads License Code style: black

An unofficial Python client implementation of the SwitchBot API.

Table of Contents


Before you start using this client, you need to get an open token. Please follow the instructions in the official documentation below.

Once you have the token, use one of the following methods to pass the information to the client.

Environment variables

If the environment variable SWITCHBOT_OPEN_TOKEN is present, this client will automatically use this value.

export SWITCHBOT_OPEN_TOKEN=your_switchbot_open_token
from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()

Constructor Arguments

It is also possible to initialize the client by passing a token directly as an argument.

from switchbot_client import SwitchBotAPIClient

your_token = "your_switchbot_open_token"
client = SwitchBotAPIClient(token=your_token)

Config file

If ~/.config/switchbot-client/config.yml exists and has a token entry, this client will automatically use the value.

mkdir -p ~/.config/switchbot-client
echo "token: your_switchbot_open_token" >>  ~/.config/switchbot-client/config.yml
from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()


Get Device List

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
result = client.devices()
{'deviceList': [{'deviceId': 'ABCDEFG', 'deviceName': 'Meter 0A', 'deviceType': 'Meter', 'enableCloudService': True, 'hubDeviceId': 'ABCDE'}, {'deviceId': 'ABCDE', 'deviceName': 'Hub Mini 0', 'deviceType': 'Hub Mini', 'hubDeviceId': 'ABCDE'}], 'infraredRemoteList': [{'deviceId': '12345', 'deviceName': 'My Light', 'remoteType': 'Light', 'hubDeviceId': 'ABCDE'}, {'deviceId': '12345, 'deviceName': 'My Air Conditioner', 'remoteType': 'Air Conditioner', 'hubDeviceId': 'ABCDE'}]}

If you run the above code, you will get a list of all the devices associated with your SwitchBot account. You can perform operations on the acquired deviceId, such as manipulating it or getting its status.

Get Device Status

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
device_id = "YOUR_DEVICE_ID"
SwitchBotAPIResponse(status_code=100, message='success', body={'deviceId': 'ABCDE', 'deviceType': 'Meter', 'hubDeviceId': 'ABCDE', 'humidity': 50, 'temperature': 25.0})

This function allows you to get the status of a device. Note that only physical devices can be targeted, not virtual infrared remote devices.

Please refer to the following official document to know what kind of information can be obtained from each device.

Control Device

from switchbot_client import SwitchBotAPIClient, ControlCommand

client = SwitchBotAPIClient()
device_id = "12345" # My Light(virtual infrared remote devices)
print(client.devices_commands(device_id, ControlCommand.VirtualInfrared.TURN_ON))
SwitchBotAPIResponse(status_code=100, message='success', body={})

It allows you to control the specified device. The ControlCommand class and the following documents define the commands that can be executed.

Get Scene List

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
SwitchBotAPIResponse(status_code=100, message='success', body=[{'sceneId': '12345', 'sceneName': 'My Scene'}])

You can get a list of all the scenes associated with your SwitchBot account. Note that only manual scenes are returned from this api.

Execute Scene

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
SwitchBotAPIResponse(status_code=100, message='success', body={})

The specified scene can be executed immediately.

Object interface

Devices can be manipulated via an easy-to-use object wrapped API.

from switchbot_client import SwitchBotAPIClient
from switchbot_client.devices import Light, AirConditioner

client = SwitchBotAPIClient()

# You can get your Lights and Air Conditioners device ids by
# print(client.devices().body["infraredRemoteList"])

light = Light(client, device_id="my_light_device_id")

air_conditioner = AirConditioner(client, device_id="my_air_conditioner_device_id")


from switchbot_client.enums import ControlCommand
from switchbot_client import SwitchBotAPIClient

def control_all_infrared_remotes_by_type(type: str, command: str):
    client = SwitchBotAPIClient()
    devices = client.devices()
    infrared_remotes = devices.body["infraredRemoteList"]
    devices = filter(lambda d: d["remoteType"] == type, infrared_remotes)

    for d in devices:
        client.devices_commands(d["deviceId"], command)

def call_this_function_when_i_go_out():
    print("turn off all lights and air conditioners...")
        "Light", ControlCommand.VirtualInfrared.TURN_OFF
        "Air Conditioner", ControlCommand.VirtualInfrared.TURN_OFF


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

switchbot-client-0.1.2.tar.gz (14.3 kB view hashes)

Uploaded Source

Built Distribution

switchbot_client-0.1.2-py3-none-any.whl (8.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page