Skip to main content

A Python client library for SwitchBot API.

Project description

switchbot-client

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

Table of Contents

Authentication

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

https://github.com/OpenWonderLabs/SwitchBotAPI#authentication

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=yourswitchbotopentoken
python3 your_script_using_switchbot_client.py
# your_script_using_switchbot_client.py
client = SwitchBotAPIClient()
result = client.devices()

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 = "yourswitchbotopentoken"
client = SwitchBotAPIClient(token=your_token)
result = client.devices()

Usage

Get Device List

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
result = client.devices()
print(result.body)
{'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 = "YOURDEVICEID"
print(client.devices_status(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.

https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-status

Control Device

from switchbot_client import SwitchBotAPIClient, ControlCommand

client = SwitchBotAPIClient()
device_id = "12345" # My Light(virtual infrared remote devices)
print(client.devices_control(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.

https://github.com/OpenWonderLabs/SwitchBotAPI#send-device-control-commands

Get Scene List

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
print(client.scenes())
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.

Execute Scene

from switchbot_client import SwitchBotAPIClient

client = SwitchBotAPIClient()
print(client.scenes_execute("12345"))
SwitchBotAPIResponse(status_code=100, message='success', body={})

The specified scene can be executed immediately.

Examples

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_control(d["deviceId"], command)


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

License

Licensed under either of

at your option.

Contribution

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.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

switchbot_client-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file switchbot-client-0.1.0.tar.gz.

File metadata

  • Download URL: switchbot-client-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/21.0.0

File hashes

Hashes for switchbot-client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bc9ea9578411f507c4083d794721705f52186f947194f09017f03f88d6e80d6d
MD5 45a8cea33917128f0dd35693d39536aa
BLAKE2b-256 e91b7241fdcb0b6209059dfb880012cfe36e8e0a40d2fc50fce2047f0abb957f

See more details on using hashes here.

File details

Details for the file switchbot_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: switchbot_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.9.6 Darwin/21.0.0

File hashes

Hashes for switchbot_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3eaa101ec25058cf5a95b4dafdf9c9f9de98bc613ca8d91fb88edead133d43ea
MD5 e3a7406864bba9118599f60e38ef0b38
BLAKE2b-256 5f004df737f638c32f23a442e9b4a9362102d7311df3b1b61879648c73212947

See more details on using hashes here.

Supported by

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