|
1 | 1 | # switchbot-client |
2 | 2 |
|
3 | | -[](https://github.com/kzosabe/switchbot-client/actions) |
4 | | -[](README.md#License) |
5 | 3 | [](https://pypi.org/project/switchbot-client/) |
6 | 4 | [](https://pypi.org/project/switchbot-client) |
| 5 | +[](README.md#License) |
7 | 6 | [](https://github.com/psf/black) |
8 | 7 |
|
9 | 8 |
|
10 | 9 | ## Table of Contents |
11 | 10 |
|
12 | | -- [Install](#install) |
| 11 | +- [Authentication](#authentication) |
13 | 12 | - [Usage](#usage) |
14 | | -- [Contributing](#contributing) |
15 | 13 | - [License](#license) |
16 | 14 |
|
17 | | -## Install |
| 15 | + |
| 16 | +## Authentication |
| 17 | + |
| 18 | +Before you start using this client, you need to get an open token. |
| 19 | +Please follow the instructions in the official documentation below. |
| 20 | + |
| 21 | +https://github.com/OpenWonderLabs/SwitchBotAPI#authentication |
| 22 | + |
| 23 | +Once you have the token, use one of the following methods to pass the information to the client. |
| 24 | + |
| 25 | +### Environment variables |
| 26 | + |
| 27 | +If the environment variable `SWITCHBOT_OPEN_TOKEN` is present, |
| 28 | +this client will automatically use this value. |
| 29 | + |
| 30 | +```shell |
| 31 | +export SWITCHBOT_OPEN_TOKEN=yourswitchbotopentoken |
| 32 | +python3 your_script_using_switchbot_client.py |
| 33 | +``` |
| 34 | + |
| 35 | +```python |
| 36 | +# your_script_using_switchbot_client.py |
| 37 | +client = SwitchBotAPIClient() |
| 38 | +result = client.devices() |
| 39 | +``` |
| 40 | + |
| 41 | +### Constructor Arguments |
| 42 | + |
| 43 | +It is also possible to initialize the client by passing a token directly as an argument. |
| 44 | + |
| 45 | +```python |
| 46 | +from switchbot_client import SwitchBotAPIClient |
| 47 | + |
| 48 | +your_token = "yourswitchbotopentoken" |
| 49 | +client = SwitchBotAPIClient(token=your_token) |
| 50 | +result = client.devices() |
| 51 | +``` |
18 | 52 |
|
19 | 53 |
|
20 | 54 | ## Usage |
21 | 55 |
|
| 56 | +### Get Device List |
| 57 | + |
| 58 | +```python |
| 59 | +from switchbot_client import SwitchBotAPIClient |
| 60 | + |
| 61 | +client = SwitchBotAPIClient() |
| 62 | +result = client.devices() |
| 63 | +print(result.body) |
| 64 | +``` |
| 65 | + |
| 66 | +``` |
| 67 | +{'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'}]} |
| 68 | +``` |
| 69 | + |
| 70 | +If you run the above code, you will get a list of all the devices associated with your SwitchBot account. |
| 71 | +You can perform operations on the acquired `deviceId`, such as manipulating it or getting its status. |
| 72 | + |
| 73 | +### Get Device Status |
| 74 | + |
| 75 | +```python |
| 76 | +from switchbot_client import SwitchBotAPIClient |
| 77 | + |
| 78 | +client = SwitchBotAPIClient() |
| 79 | +device_id = "YOURDEVICEID" |
| 80 | +print(client.devices_status(device_id)) |
| 81 | +``` |
| 82 | + |
| 83 | +``` |
| 84 | +SwitchBotAPIResponse(status_code=100, message='success', body={'deviceId': 'ABCDE', 'deviceType': 'Meter', 'hubDeviceId': 'ABCDE', 'humidity': 50, 'temperature': 25.0}) |
| 85 | +``` |
| 86 | + |
| 87 | +This function allows you to get the status of a device. |
| 88 | +Note that only physical devices can be targeted, not virtual infrared remote devices. |
| 89 | + |
| 90 | +Please refer to the following official document to know what kind of information can be obtained from each device. |
| 91 | + |
| 92 | +https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-status |
| 93 | + |
| 94 | +### Control Device |
| 95 | + |
| 96 | +```python |
| 97 | +from switchbot_client import SwitchBotAPIClient, ControlCommand |
| 98 | + |
| 99 | +client = SwitchBotAPIClient() |
| 100 | +device_id = "12345" # My Light(virtual infrared remote devices) |
| 101 | +print(client.devices_control(device_id, ControlCommand.VirtualInfrared.TURN_ON)) |
| 102 | +``` |
| 103 | + |
| 104 | +``` |
| 105 | +SwitchBotAPIResponse(status_code=100, message='success', body={}) |
| 106 | +``` |
| 107 | + |
| 108 | +It allows you to control the specified device. |
| 109 | +The `ControlCommand` class and the following documents define the commands that can be executed. |
| 110 | + |
| 111 | +https://github.com/OpenWonderLabs/SwitchBotAPI#send-device-control-commands |
| 112 | + |
| 113 | +### Get Scene List |
| 114 | + |
| 115 | +```python |
| 116 | +from switchbot_client import SwitchBotAPIClient |
| 117 | + |
| 118 | +client = SwitchBotAPIClient() |
| 119 | +print(client.scenes()) |
| 120 | +``` |
| 121 | + |
| 122 | +``` |
| 123 | +SwitchBotAPIResponse(status_code=100, message='success', body=[{'sceneId': '12345', 'sceneName': 'My Scene'}]) |
| 124 | +``` |
| 125 | + |
| 126 | +You can get a list of all the scenes associated with your SwitchBot account. |
| 127 | + |
| 128 | +### Execute Scene |
| 129 | +```python |
| 130 | +from switchbot_client import SwitchBotAPIClient |
| 131 | + |
| 132 | +client = SwitchBotAPIClient() |
| 133 | +print(client.scenes_execute("12345")) |
| 134 | +``` |
| 135 | + |
| 136 | +``` |
| 137 | +SwitchBotAPIResponse(status_code=100, message='success', body={}) |
| 138 | +``` |
| 139 | +The specified scene can be executed immediately. |
| 140 | + |
22 | 141 |
|
23 | 142 | ## License |
24 | 143 |
|
|
0 commit comments