SwitchBee Python Integration.
Project description
pySwitchbee
A Python module library to control SwitchBee smart home devices.
Example code usage:
from asyncio import get_event_loop
from aiohttp import ClientSession, ClientTimeout, TCPConnector
from switchbee.api import CentralUnitAPI
from switchbee.device import DeviceType, ApiStateCommand
async def main():
session = ClientSession(
connector=TCPConnector(ssl=False),
timeout=ClientTimeout(total=3),
)
cu = CentralUnitAPI("192.168.50.2", "user", "pass", session)
await cu.connect()
print(f"Central Unit: {cu.name}")
print(f"Central MAC: {cu.mac}")
print(f"Central Version: {cu.version}")
devices = await cu.devices
for device in devices:
# set the dimmer lights to 50% brightness
if device.type == DeviceType.Dimmer:
print(
"Discovered Dimmer device called {device.name}"
" current brightness is {device.brigt}"
)
await cu.set_state(device.id, 50)
# set the shutters position to 30% opened
if device.type == DeviceType.Shutter:
print(
"Discovered Shutter device called {device.name}"
" current position is {device.position}"
)
await cu.set_state(device.id, 30)
# turn off switches
if device.type == DeviceType.Switch:
print(
"Discovered Switch device called {device.name}"
" current state is {device.state}"
)
await cu.set_state(device.id, ApiStateCommand.OFF)
# set timer switch on for 10 minutes
if device.type == DeviceType.TimedPower:
print(
"Discovered Timed Power device called {device.name}"
" current state is {device.state} with {device.minutes_left} "
"minutes left until shutdown"
)
await cu.set_state(device.id, 10)
session.close()
if __name__ == "__main__":
get_event_loop().run_until_complete(main())
exit()
Using the CLI tool:
Alternatively, it is possible to control SwitchBee using the cli tool switchbee_cli.py
as following:
To list devices that currently on:
python switchbee_cli.py -i 192.168.50.2 -u USERNAME -p PASSWORD get_states --only-on
'_state': 'ON',
'hardware': <HardwareType.Switch: 'DIMMABLE_SWITCH'>,
'id': 311,
'name': 'Ceiling',
'type': <DeviceType.Switch: 'SWITCH'>,
'zone': 'Outdoo Storage'}
{ '_state': 'ON',
'hardware': <HardwareType.Switch: 'DIMMABLE_SWITCH'>,
'id': 142,
'name': 'Spotlights',
'type': <DeviceType.Switch: 'SWITCH'>,
'zone': 'Porch'}
To set shutter with device id 392 position 50%:
python switchbee_cli.py -i 192.168.50.2 -u USERNAME -p PASSWORD set_state --device-id 392 --state 50
To turn on Power Timed Switch with device id 450 for 30 minutes:
python switchbee_cli.py -i 192.168.50.2 -u USERNAME -p PASSWORD set_state --device-id 450 --state 30
To turn off light with device id 122:
python switchbee_cli.py -i 192.168.50.2 -u USERNAME -p PASSWORD set_state --device-id 122 --state OFF
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pyswitchbee-1.2.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 002833096ec0b6607f5b4ede17dec38a0856b9fc34b8af7a69e890c99fe9fb75 |
|
MD5 | 943e1f64d2c95ff29a186a388425216e |
|
BLAKE2b-256 | 406e65ccbfe51589f844307805c3d22b5e27fab0449e36f76501a9ce61369e28 |