Browse Source

send command to switchbot

Fabian Peter Hammerle 4 years ago
parent
commit
623fc50691
3 changed files with 21 additions and 1 deletions
  1. 2 0
      Dockerfile
  2. 10 0
      README.md
  3. 9 1
      switchbot_mqtt/__init__.py

+ 2 - 0
Dockerfile

@@ -21,3 +21,5 @@ RUN SETUPTOOLS_SCM_PRETEND_VERSION=$SWITCHBOT_MQTT_VERSION pipenv install --depl
 ENV PATH=$SOURCE_DIR_PATH/.venv/bin:$PATH
 ENTRYPOINT ["tini", "--"]
 CMD ["switchbot-mqtt"]
+
+#RUN apk add bluez-deprecated `# hcitool`

+ 10 - 0
README.md

@@ -1,3 +1,13 @@
+## Docker
+
+```sh
+$ docker build -t switchbot-mqtt .
+$ docker run --name spelunca_switchbot \
+    --userns host --network host \
+    switchbot-mqtt:latest \
+    switchbot-mqtt --help
+```
+
 ## Alternatives
 
 * https://github.com/binsentsu/switchbot-ctrl

+ 9 - 1
switchbot_mqtt/__init__.py

@@ -21,6 +21,7 @@ import logging
 import typing
 
 import paho.mqtt.client
+import switchbot
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -75,7 +76,14 @@ def _mqtt_on_message(
             _LOGGER.warning("unexpected topic %s", message.topic)
             return
     assert switchbot_mac_address
-    print("TODO", switchbot_mac_address, message.payload)
+    # TODO validate mac address
+    switchbot_device = switchbot.Switchbot(mac=switchbot_mac_address)
+    if message.payload.lower() == b"on":
+        print("TODO", switchbot_device.turn_on())
+    elif message.payload.lower() == b"off":
+        print("TODO", switchbot_device.turn_off())
+    else:
+        _LOGGER.warning("unexpected payload %r", message.payload)
 
 
 def _main() -> None: