Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

python-host

> 编程语言
Open source

The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.

400 stars0 likes0 views
WebsiteGitHub

About

The python code running on Raspberry Pi or other Linux based boards to control SwitchBot.

python-host

What is the SwitchBot opensource project?

SwitchBot is a smart IoT robot to mechanically control all your switches and buttons. You can control the bot by your smartphone app (iOS or Android, SwitchLink, or other platform based on our open APIs.

This project aims to provide a super light-weight solution to control your SwitchBot on Raspberry Pi/OpenWRT or any other Linux based board.

The python-host distribution is supported and tested on Python 2.7.

How to Install?

On Raspberry Pi

You will need:

  • A Raspberry Pi 3 or A Raspberry Pi 2 plugged with a Bluetooth dongle.
  • A SwitchBot.
  • An SD Card with a fresh install of Raspbian (tested against the latest build 2017-01-11 Jessie with Pixel).

Installation

  1. Boot your fresh Pi and open a command prompt.
  2. Install the require library.
bash
sudo apt-get update
sudo apt-get install python-pexpect
sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev 
sudo apt-get install libudev-dev libical-dev libreadline-dev
sudo pip install bluepy
  1. Clone this repo to the Pi.
bash
git clone https://github.com/OpenWonderLabs/python-host.git
cd python-host

Running

You can use in two ways:

  1. Scan and control by device name.
bash
sudo python switchbot.py

Follow the instruction, input the device number for SwitchBot you want to control.

  1. Control SwitchBot by MAC address. (MAC address should be retrived in advanced)
bash
sudo python switchbot.py [mac_addr action_cmd]

action_cmd :Press, Turn On, Turn Off.

eg: sudo python switchbot.py  xx:xx:xx:xx:xx:xx Press

Python 3 and new bluetooth stack support

The original switchbot.py script will work only on Python 2 and it relies on the old Bluez utils (like hciconfig and hcitool) that have been deprecated in the latest Bluez releases.

If you want to use the script on Python 3 or on a Linux distro that no longer ships Bluez with the old tools, use the switchbot_py3.py script instead.

To install the required dependencies on Debian 11 "Bullseye", Raspberry Pi OS or Ubuntu 21.10 or later:

bash
sudo apt install python3-bluez

On older versions of Ubuntu/Debian/Raspbian:

bash
sudo apt-get install python3-pip
sudo apt-get install libbluetooth-dev
pip3 install pybluez
sudo apt-get install libboost-python-dev
sudo apt-get install libboost-thread-dev
pip3 install gattlib

If for some reason the gattlib installation fails:

bash
sudo apt-get install pkg-config python3-dev
sudo apt-get install libglib2.0-dev

pip3 download gattlib
tar xvzf ./gattlib-0.20150805.tar.gz
cd gattlib-0.20150805/
sed -ie 's/boost_python-py34/boost_python-py36/' setup.py # "py36" might be "py37" (for example). Check "python3 --version"
pip3 install .

Type python3 switchbot_py3.py -h/--help for usage tips.

eg: sudo python3 switchbot_py3.py -d xx:xx:xx:xx:xx:xx -c close

Enjoy :)

References

SwitchBotAPI-BLE

  • Bot BLE open api

  • Meter BLE open api

  • Curtain BLE open api

  • Contact Sensor BLE open api

  • Motion Sensor BLE open api

Thanks to contributors

@BlackLight

@rcmdnk

@tony-wallace

Community

SwitchBot (Official website)

Facebook @SwitchBotRobot

Twitter @SwitchBot

Issues· 17 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言