Skip to content

Commit cf011aa

Browse files
authored
Merge pull request #1 from kzosabe/basic-implementation
Add basic implementation and README
2 parents 3621b9e + 1c25039 commit cf011aa

7 files changed

Lines changed: 367 additions & 8 deletions

File tree

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
/.venv
2-
*.pyc
1+
__pycache__/
2+
*.py[cod]
3+
*$py.class
34
!.keep

README.md

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,143 @@
11
# switchbot-client
22

3-
[![GitHub Build](https://github.com/kzosabe/switchbot-client/workflows/CI%20Build/badge.svg)](https://github.com/kzosabe/switchbot-client/actions)
4-
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-informational?style=flat-square)](README.md#License)
53
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/switchbot-client.svg)](https://pypi.org/project/switchbot-client/)
64
[![PyPI - Downloads](https://img.shields.io/pypi/dm/switchbot-client)](https://pypi.org/project/switchbot-client)
5+
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-informational?style=flat-square)](README.md#License)
76
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
87

98

109
## Table of Contents
1110

12-
- [Install](#install)
11+
- [Authentication](#authentication)
1312
- [Usage](#usage)
14-
- [Contributing](#contributing)
1513
- [License](#license)
1614

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+
```
1852

1953

2054
## Usage
2155

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+
22141

23142
## License
24143

poetry.lock

Lines changed: 79 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = [
88

99
[tool.poetry.dependencies]
1010
python = "~3.9.6"
11+
requests = "^2.26.0"
1112

1213
[tool.poetry.dev-dependencies]
1314
pytest = "^6.2.1"

switchbot_client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from switchbot_client.client import SwitchBotAPIClient, SwitchBotAPIResponse
2+
from switchbot_client.enums import ControlCommand

0 commit comments

Comments
 (0)