Skip to main content

MQTT client controlling SwitchBot button automators, compatible with home-assistant.io's MQTT Switch platform

Project description

SwitchBot MQTT client

Code style: black CI Pipeline Status Coverage Status Last Release Compatible Python Versions

MQTT client controlling SwitchBot button automators

Compatible with Home Assistant's MQTT Switch platform.

Setup

$ pip3 install --user --upgrade switchbot-mqtt
$ switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS

Use sudo hcitool lescan or select device settings > 3 dots on top right in SwitchBot app to determine your SwitchBot's mac address.

Send ON or OFF to topic homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set.

$ mosquitto_pub -h MQTT_BROKER -t homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set -m ON

Home Assistant 🏡

Rationale

Why not use the official SwitchBot integration?

I prefer not to share the host's network stack with home assistant (more complicated network setup and additional netfilter rules required for isolation).

Sadly, docker run --network host even requires --userns host:

docker: Error response from daemon: cannot share the host's network namespace when user namespaces are enabled.

The docker image built from this repository works around this limitation by explicitly running as an unprivileged user.

The official home assistant image runs as root. This imposes an unnecessary security risk, especially when disabling user namespace remapping (--userns host). See https://github.com/fphammerle/docker-home-assistant for an alternative.

Setup

# https://www.home-assistant.io/docs/mqtt/broker/#configuration-variables
mqtt:
  broker: BROKER_HOSTNAME_OR_IP_ADDRESS
  # credentials, additional options…

# https://www.home-assistant.io/integrations/switch.mqtt/#configuration-variables
switch:
- platform: mqtt
  name: some_name
  command_topic: homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/set
  state_topic: homeassistant/switch/switchbot/aa:bb:cc:dd:ee:ff/state
  # http://materialdesignicons.com/
  icon: mdi:light-switch

Docker 🐳

Pre-built docker images are available at https://hub.docker.com/r/fphammerle/switchbot-mqtt/tags

Annotation of signed tags docker/* contains docker image digests: https://github.com/fphammerle/switchbot-mqtt/tags

$ docker build -t switchbot-mqtt .
$ docker run --name spelunca_switchbot \
    --userns host --network host \
    switchbot-mqtt:latest \
    switchbot-mqtt --mqtt-host HOSTNAME_OR_IP_ADDRESS

Alternatively, you can use docker-compose:

version: '3.8'

services:
  switchbot-mqtt:
    image: switchbot-mqtt
    container_name: switchbot-mqtt
    network_mode: host
    userns_mode: host
    environment:
    - MQTT_HOST=localhost
    - MQTT_PORT=1883
    #- MQTT_USERNAME=username
    #- MQTT_PASSWORD=password
    restart: unless-stopped

MQTT Authentication

switchbot-mqtt --mqtt-username me --mqtt-password secret # or
switchbot-mqtt --mqtt-username me --mqtt-password-file /var/lib/secrets/mqtt/password 

⚠️ --mqtt-password leaks the password to other users on the same machine, if /proc is mounted with hidepid=0 (default).

Alternatives

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

switchbot-mqtt-0.5.0.tar.gz (36.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

switchbot_mqtt-0.5.0-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file switchbot-mqtt-0.5.0.tar.gz.

File metadata

  • Download URL: switchbot-mqtt-0.5.0.tar.gz
  • Upload date:
  • Size: 36.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for switchbot-mqtt-0.5.0.tar.gz
Algorithm Hash digest
SHA256 ec4e99a049d5d8ddbd3c1c1d7749f21085479d2d68e71cd0d6a3b56ac6531d9a
MD5 89f3bec04be6a166aa90c086f2494d74
BLAKE2b-256 7c71fa8196d3cd95be41d7cad081e407821ed3f7a51409637d86d795023e2773

See more details on using hashes here.

File details

Details for the file switchbot_mqtt-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: switchbot_mqtt-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.8.5

File hashes

Hashes for switchbot_mqtt-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04f879cbcff46ca74b5eb7163eb11986da6c05bc172b395cda788f85badb2b36
MD5 fb9fd4ac771bbd982634f2eed7d80544
BLAKE2b-256 f686d700fb358e0de153a3240cf6b1c7b900132e7e243beda297769e5fce4ae7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page