Skip to content
This repository was archived by the owner on Jun 18, 2026. It is now read-only.

Commit e50c9ef

Browse files
authored
Merge pull request #23 from kzosabe/develop
Release 0.2.0
2 parents 502c689 + 843c472 commit e50c9ef

27 files changed

Lines changed: 1626 additions & 662 deletions

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: [3.6, 3.7, 3.8, 3.9]
17+
python-version: ["3.7", "3.8", "3.9", "3.10"]
1818
steps:
1919
- name: Checkout
2020
uses: actions/checkout@v2
2121

2222
- name: Set up Python ${{ matrix.python-version }}
23-
uses: actions/setup-python@v1
23+
uses: actions/setup-python@v2
2424
with:
2525
python-version: ${{ matrix.python-version }}
2626

@@ -42,4 +42,4 @@ jobs:
4242
run: poetry install --no-interaction --no-root
4343

4444
- name: Run Tests
45-
run: ./scripts/run_tests.sh
45+
run: poetry run tox

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ __pycache__/
33
*$py.class
44
build/
55
dist/
6+
/.tox
67
.idea
78
!.keep

CHANGELOG.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
0.2.0, 2021-10-30
2+
-------------------------
3+
4+
- API Interface Change
5+
- Use object interface as the main one
6+
- It is recommended to use SwitchBotClient instead of SwitchBotAPIClient
7+
- Add new devices(Motion Sensor, Contact Sensor, Color Bulb, Remote)
8+
- Use `switchbot-client/{version}` as the user agent when requesting the SwitchBot API
9+
- Remove Python 3.6.x support and add Python 3.10.x support
10+
111
0.1.2, 2021-09-11
212
-------------------------
313

414
- Support ~/.config/switchbot-client config file
515
- Add object interface for all devices
6-
- Add Python >=3.6.0 support
716
- Rename devices_control to devices_commands
817

918
0.1.1, 2021-09-05
1019
-------------------------
1120

12-
- Add [object interface](https://github.com/kzosabe/switchbot-client#object-interface)
13-
- Add Python >=3.6.2 support
21+
- Add object interface
1422

1523
0.1.0, 2021-09-04
1624
-------------------------

README.md

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ python3 your_script.py
3636

3737
```python
3838
# your_script.py
39-
from switchbot_client import SwitchBotAPIClient
39+
from switchbot_client import SwitchBotClient
4040

41-
client = SwitchBotAPIClient()
41+
client = SwitchBotClient()
4242
print(client.devices())
4343
```
4444

@@ -47,10 +47,10 @@ print(client.devices())
4747
It is also possible to initialize the client by passing a token directly as an argument.
4848

4949
```python
50-
from switchbot_client import SwitchBotAPIClient
50+
from switchbot_client import SwitchBotClient
5151

5252
your_token = "your_switchbot_open_token"
53-
client = SwitchBotAPIClient(token=your_token)
53+
client = SwitchBotClient(token=your_token)
5454
print(client.devices())
5555
```
5656

@@ -67,9 +67,9 @@ python3 your_script.py
6767

6868
```python
6969
# your_script.py
70-
from switchbot_client import SwitchBotAPIClient
70+
from switchbot_client import SwitchBotClient
7171

72-
client = SwitchBotAPIClient()
72+
client = SwitchBotClient()
7373
print(client.devices())
7474
```
7575

@@ -78,32 +78,32 @@ print(client.devices())
7878
### Get Device List
7979

8080
```python
81-
from switchbot_client import SwitchBotAPIClient
81+
from switchbot_client import SwitchBotClient
8282

83-
client = SwitchBotAPIClient()
83+
client = SwitchBotClient()
8484
result = client.devices()
85-
print(result.body)
85+
print(result)
8686
```
8787

8888
```
89-
{'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'}]}
89+
[Meter({'device_id': 'ABCDEFG', 'device_type': 'Meter', 'device_name': 'Meter 0A', 'hub_device_id': 'ABCDE', 'is_virtual_infrared': False}), HubMini({'device_id': 'ABCDEFG', 'device_type': 'Hub Mini', 'device_name': 'Hub Mini 0', 'hub_device_id': None, 'is_virtual_infrared': False}), Light({'device_id': '12345', 'device_type': 'Light', 'device_name': 'My Light', 'hub_device_id': 'ABCDE', 'is_virtual_infrared': True}), AirConditioner({'device_id': '12345', 'device_type': 'Air Conditioner', 'device_name': 'My Air Conditioner', 'hub_device_id': 'ABCDE', 'is_virtual_infrared': True})]
9090
```
9191

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

9595
### Get Device Status
9696

9797
```python
98-
from switchbot_client import SwitchBotAPIClient
98+
from switchbot_client import SwitchBotClient
9999

100-
client = SwitchBotAPIClient()
100+
client = SwitchBotClient()
101101
device_id = "YOUR_DEVICE_ID"
102-
print(client.devices_status(device_id))
102+
print(client.device(device_id).status())
103103
```
104104

105105
```
106-
SwitchBotAPIResponse(status_code=100, message='success', body={'deviceId': 'ABCDE', 'deviceType': 'Meter', 'hubDeviceId': 'ABCDE', 'humidity': 50, 'temperature': 25.0})
106+
DeviceStatus(device_id='ABCDE', device_type='Meter', device_name='Meter 0', hub_device_id='ABCDE', data={'deviceId': 'ABCDE', 'deviceType': 'Meter', 'hubDeviceId': 'ABCDE', 'humidity': 50, 'temperature': 25.0})
107107
```
108108

109109
This function allows you to get the status of a device.
@@ -116,102 +116,119 @@ https://github.com/OpenWonderLabs/SwitchBotAPI#get-device-status
116116
### Control Device
117117

118118
```python
119-
from switchbot_client import SwitchBotAPIClient, ControlCommand
119+
from switchbot_client import SwitchBotAPIClient
120+
from switchbot_client.devices import Light
120121

121122
client = SwitchBotAPIClient()
122-
device_id = "12345" # My Light(virtual infrared remote devices)
123-
print(client.devices_commands(device_id, ControlCommand.VirtualInfrared.TURN_ON))
123+
device_id = "12345" # My Light(virtual infrared remote devices)
124+
device = Light.create_by_id(client, device_id)
125+
print(device.turn_on())
124126
```
125127

126128
```
127-
SwitchBotAPIResponse(status_code=100, message='success', body={})
129+
SwitchBotCommandResult(status_code=100, message='success', response_body={})
128130
```
129131

130132
It allows you to control the specified device.
131-
The `ControlCommand` class and the following documents define the commands that can be executed.
133+
The following documents define the commands that can be executed.
132134

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

135137
### Get Scene List
136138

137139
```python
138-
from switchbot_client import SwitchBotAPIClient
140+
from switchbot_client import SwitchBotClient
139141

140-
client = SwitchBotAPIClient()
142+
client = SwitchBotClient()
141143
print(client.scenes())
142144
```
143145

144146
```
145-
SwitchBotAPIResponse(status_code=100, message='success', body=[{'sceneId': '12345', 'sceneName': 'My Scene'}])
147+
[SwitchBotScene({'scene_id': '12345', 'scene_name': 'My Scene1'}), SwitchBotScene({'scene_id': '23456', 'scene_name': 'My Scene2'})]
146148
```
147149

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

151153
### Execute Scene
152154
```python
153-
from switchbot_client import SwitchBotAPIClient
155+
from switchbot_client import SwitchBotClient
154156

155-
client = SwitchBotAPIClient()
156-
print(client.scenes_execute("12345"))
157+
client = SwitchBotClient()
158+
print(client.scene("12345").execute())
157159
```
158160

159161
```
160-
SwitchBotAPIResponse(status_code=100, message='success', body={})
162+
SwitchBotCommandResult(status_code=100, message='success', response_body={})
161163
```
162164
The specified scene can be executed immediately.
163165

164-
### Object interface
166+
### Raw API interface
165167

166-
Devices can be manipulated via an easy-to-use object wrapped API.
168+
Devices and scenes also can be manipulated via the low-level raw API client.
169+
The `SwitchBotAPIClient` class has methods for each endpoints of SwitchBot API.
167170

168-
```python
169-
from switchbot_client import SwitchBotAPIClient
170-
from switchbot_client.devices import Light, AirConditioner
171-
172-
client = SwitchBotAPIClient()
173-
174-
# You can get your Lights and Air Conditioners device ids by
175-
# print(client.devices().body["infraredRemoteList"])
171+
For example the `/v1.0/devices` endpoint is implemented as `SwitchBotAPIClient.devices()`,
172+
the `/v1.0/devices/{device_id}/status"` endpoint is implemented as `SwitchBotAPIClient.devices_status(device_id: str)`.
176173

177-
light = Light(client, device_id="my_light_device_id")
178-
light.turn_on()
179-
180-
air_conditioner = AirConditioner(client, device_id="my_air_conditioner_device_id")
181-
air_conditioner.set_all(
182-
temperature=25,
183-
mode=AirConditioner.Parameters.MODE_DRY,
184-
fan_speed=AirConditioner.Parameters.FAN_SPEED_AUTO,
185-
power=AirConditioner.Parameters.POWER_ON
186-
)
187-
```
188174

189175
### Examples
190176

191177
```python
192-
from switchbot_client.enums import ControlCommand
193-
from switchbot_client import SwitchBotAPIClient
194-
195-
196-
def control_all_infrared_remotes_by_type(type: str, command: str):
197-
client = SwitchBotAPIClient()
198-
devices = client.devices()
199-
infrared_remotes = devices.body["infraredRemoteList"]
200-
devices = filter(lambda d: d["remoteType"] == type, infrared_remotes)
201-
202-
for d in devices:
203-
client.devices_commands(d["deviceId"], command)
178+
from switchbot_client import devices
179+
from switchbot_client import SwitchBotClient
204180

205181

206182
def call_this_function_when_i_go_out():
183+
client = SwitchBotClient()
207184
print("turn off all lights and air conditioners...")
208-
control_all_infrared_remotes_by_type(
209-
"Light", ControlCommand.VirtualInfrared.TURN_OFF
210-
)
211-
control_all_infrared_remotes_by_type(
212-
"Air Conditioner", ControlCommand.VirtualInfrared.TURN_OFF
213-
)
185+
for d in client.devices():
186+
if isinstance(d, devices.Light):
187+
d.turn_off()
188+
189+
if isinstance(d, devices.ColorBulb):
190+
d.turn_off()
191+
192+
if isinstance(d, devices.AirConditioner):
193+
d.turn_off()
214194
print("done")
195+
196+
197+
def control_devices_by_temperature():
198+
client = SwitchBotClient()
199+
all_devices = client.devices()
200+
201+
temperatures = [d.temperature() for d in all_devices if isinstance(d, devices.Meter)]
202+
temperature = min(temperatures)
203+
204+
color_bulbs = [d for d in all_devices if isinstance(d, devices.ColorBulb)]
205+
air_conditioners = [d for d in all_devices if isinstance(d, devices.AirConditioner)]
206+
207+
if temperature > 25.0:
208+
print("hot!")
209+
for d in color_bulbs:
210+
d.set_color("#FF0000")
211+
212+
for d in air_conditioners:
213+
d.set_all(
214+
temperature=20.0,
215+
mode=devices.AirConditioner.Parameters.MODE_COOL,
216+
fan_speed=devices.AirConditioner.Parameters.FAN_SPEED_HIGH,
217+
power=devices.AirConditioner.Parameters.POWER_ON
218+
)
219+
220+
elif temperature < 15.0:
221+
print("cold!")
222+
for d in color_bulbs:
223+
d.set_color("#0000FF")
224+
225+
for d in air_conditioners:
226+
d.set_all(
227+
temperature=20.0,
228+
mode=devices.AirConditioner.Parameters.MODE_HEAT,
229+
fan_speed=devices.AirConditioner.Parameters.FAN_SPEED_HIGH,
230+
power=devices.AirConditioner.Parameters.POWER_ON
231+
)
215232
```
216233

217234
## License

0 commit comments

Comments
 (0)