Skip to main content

pySwitchbee

A Python module library to control SwitchBee smart home devices.

PyPI PyPI - Downloads

alt text

Example code usage:

from asyncio import get_event_loop

from aiohttp import ClientSession, ClientTimeout, TCPConnector

from switchbee.api import CentralUnitAPI
from switchbee.device import ApiStateCommand, DeviceType


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 Name: {cu.name}")
    print(f"Central Unit MAC: {cu.mac}")
    print(f"Central Unit 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 devices 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

Metadata

Release files for aioswitchbee 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioswitchbee 0.1.0
File Size Uploaded
aioswitchbee-0.1.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aioswitchbee 0.1.0
File Interpreter ABI Platform
aioswitchbee-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 23.9 kB

Release files / aioswitchbee-0.1.0.tar.gz

Download URL aioswitchbee-0.1.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6058e5da88c8a77cea93f2ff93d5b5ed65e360d9b811169371d7ecedf2979fc9
BLAKE2b-256 checksum
How to use checksums
29bee9252b318e77dc44d7d0e5e5960eb5030bfb5a8f64f32a6c5db97d022395
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / aioswitchbee-0.1.0-py3-none-any.whl

Download URL aioswitchbee-0.1.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bcfb7d252b3a72c045a1cabf0c5a76987ee3479f8f80d5fd81400e5a41dc833
BLAKE2b-256 checksum
How to use checksums
6f009c44fdaea28b1c8cab4dae75bd0af39b751f70bc04e68841ca69dbc9c621
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page