A Python utility for controlling SwitchBot Smart Plugs using the SwitchBot API.
This project provides a set of Python scripts to interact with SwitchBot Smart Plugs. It allows users to:
- List all SwitchBot devices associated with your account
- Control smart plugs (turn on/off)
- Check the current status of smart plugs
- Python 3.12 or higher
- SwitchBot account with API access
- SwitchBot Smart Plug device(s)
- SwitchBot API token and secret
-
Clone this repository:
git clone https://github.com/yourusername/switchbot_smartplug_control.git cd switchbot_smartplug_control -
Install dependencies using pipenv:
pipenv install -
Activate the virtual environment:
pipenv shell
-
Copy the example environment file:
cp .env.example .env -
Edit the
.envfile and update it with your own SwitchBot API credentials:TOKEN=your_switchbot_token_here SECRET=your_switchbot_secret_here DEVICE_ID=your_device_id_here
Note: If you don't know your device ID, you can run the
get_device_id.pyscript to list all your devices.
To list all available SwitchBot devices and their IDs:
python get_device_id.py
To control a smart plug, use the plug_control.py script:
# Get current status
python plug_control.py status
# Turn on the plug
python plug_control.py on
# Turn off the plug
python plug_control.py off
To view detailed information about your SwitchBot device:
python check_device.py
get_device_id.py- Script to list all your SwitchBot devices and their IDsplug_control.py- Main script for controlling the smart plugcheck_device.py- Utility script to check device details and available methodsPipfile&Pipfile.lock- Dependency management with pipenv.env- Environment variables file (contains your API credentials).env.example- Example environment variables file.gitignore- Git ignore file to prevent committing sensitive files
This project uses the python-switchbot library. For more details on the SwitchBot API, refer to:
This project now uses environment variables to store sensitive API credentials:
- Your actual credentials are stored in the
.envfile - The
.envfile is included in.gitignoreto prevent accidental commits - Use the
.env.examplefile as a template for setting up your credentials - Never commit your actual API tokens to version control
This project is available under the MIT License.
June 13, 2025