Skip to content
RoButtonPublic

About

REST API to control multiple Switchbots

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

21 Commits

Folders and files

Repository files navigation

RoButton

A REST API for SwitchBot Bots: press, switch, settings and timers over HTTPS.

Running on a Raspberry Pi next to the bots, it lets you control them from anywhere, without a SwitchBot Hub, e.g. from your phone with HTTP Shortcuts. Bluetooth (BLE) is handled by switchbotpy.

API

All endpoints are under /switchbot/api/v1. Log in with {"name": "<any client name>", "password": "<login password>"} to get a bearer token, then send it as Authorization: Bearer <token>.

Method Endpoint Description
POST /login Get a token
GET /bots Scan for bots in range (each gets a stable id)
GET /bot/{id} Settings (battery, firmware, mode, ...)
PATCH /bot/{id} Update settings (name, mode, hold time, ...)
PUT /bot/{id}/password Set or change the password on the bot
POST /bot/{id}/actions press, turn_on, turn_off
GET /bot/{id}/timers All timers
POST /bot/{id}/timers Add a timer
PATCH /bot/{id}/timers Update several timers
PATCH /bot/{id}/timer/{timer_id} Update a timer
DELETE /bot/{id}/timer/{timer_id} Delete a timer

The full OpenAPI specification is served at /doc/openapi.json.

Deployment

The ansible playbook sets up a Raspberry Pi (Raspberry Pi OS) completely: nginx with TLS (self-signed certificate) and login rate limit, gunicorn in a sandboxed systemd service, firewall and automatic updates. The code is deployed from this repository.

cd ansible
cp inventory/example.yml inventory/home.yml   # adjust: hosts, ssh user / key, options (all documented there)
ansible-playbook -i inventory/home.yml install.yml -K

The playbook asks for the API login password. Real inventories are gitignored.

Development

Requires uv and Linux with bluetooth.

uv sync
uv run robutton-login-password   # set the login password (stored in ./secrets.json)
uv run python -m robutton        # dev server on http://localhost:5000
uv run ruff check src tests

End-to-end tests against a deployed pi (login, scan, and with --bot a real bot, interactive), see tests/conftest.py:

uv run pytest -v --url=https://raspberrypi.local --cert=~/.robutton/raspberrypi.local.crt
uv run pytest -v -s --url=https://raspberrypi.local --cert=~/.robutton/raspberrypi.local.crt --bot   # lists the bots in range to choose from

Authors

About

REST API to control multiple Switchbots

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages