Switchbot HUB integration

I’m also very interested in this as the hub can control various devices over IR, and it would be great to have them in HA.

There seem to be a python library already in progress to use this API: https://github.com/jonghwanhyeon/python-switchbot this could be used for the integration, as directly interacting with the API from a HA integration is generally discouraged

1 Like

I’d love to have Switchbot hub integrated in HA. Currently I trigger IR commands by calling Alexa (who can talk to the hub) routines from HA.

4 Likes

Has there been any update on this? Love to see it integrated. :slight_smile:

1 Like

Any update?

definitely a push! hopefully someone will take care of it :smiley: I am unfortunately not able to…

I did some tinkering around and found a quite straight forward way to use the Switchbox hub within Home Assistant. It uses Python-switchbot which is then called through pyscript. This will give you a service that then switches the switchbot and you can use the service in any automation.

Switchbot hub integration instructions:

  1. Download the SwitchBot app on App Store or Google Play Store
  2. Register a SwitchBot account and log in into your account
  3. Generate an Open Token within the app
    a) Go to Profile > Preference
    b) Tap App Version 10 times. Developer Options will show up
    c) Tap Developer Options
    d) Tap Get Token
  4. Now you have the token to use with the switchbot API (see below)
  5. Install python-switchbot (you might want to have this also install on every startup of HA in case you are running it in Docker)
    pip install python-switchbot
  6. Install Pyscript integration
  7. Create a python file with the following contents in folder <config>/pyscript, i called it switch.py:
    (to get your device id see documentation of python-switchbot)
from switchbot import SwitchBot

@pyscript_executor
def switchfunction():
	your_switch_bot_token = '<COPY YOUR TOKEN HERE>'
	switchbot = SwitchBot(token=your_switch_bot_token)
	device = switchbot.device(id='deviceID')
	device.command('press')

@service
def switch1():
    switchfunction()
  1. Restart HA (perhaps not needed, but I did)
  2. You will now have a service called pyscript.switch1 that you can use to make the switchbot press once.

From here you can tinker around to add more functions as you need.

Sources used:

3 Likes

Is it also possible to connect a HA thermostat to this?

Isn’t the API limit a huge problem?

no idea, at this moment my switchbot is in the “useless gadget” drawer. What a crap piece of hardware. Completely unreliable. Sometimes it will switch (using their own app) and sometimes it will just fail.

Complete garbage.

1 Like

Hi do you think you could maybe explain further how to get this working

i got the token from the app, the deviceid too (deviceid=BLE MAC without “:” right?)
installed python-switchbot by ssh to my HA
installed psycript from HASS and then from integration on HA , made the switch.py file, placed it in the psycript folder
used this content

‘’’
from switchbot import SwitchBot

@pyscript_executor
def switchfunction():
your_switch_bot_token = ‘’
switchbot = SwitchBot(token=your_switch_bot_token)
device = switchbot.device(id=‘deviceID’)
device.command(‘press’)

@service
def switch1():
switchfunction()
‘’’

inserted the token between the ’ ’
same goes for the deviceID
and saved the file restarted everything
on the switchbot app i also enabled cloud services on the switchbot.
i can only see two services added, one of “reload” and one of “start juniper kernel”

dont see the service im supposed to call

Log showing me this
Exception in </config/pyscript/switch.py> line 1: from switchbot import SwitchBot ^ AttributeError: module ‘switchbot’ has no attribute ‘SwitchBot’

8:01:13 PM – (ERROR) Pyscript Python scripting (custom integration)

I think you need to install switchbot:

pip install python-switchbot

yes of course i did, just like your instructions said

GOT IT RESOLVED
im on HA OS
i SSH before and then did the pip install
i had to SSH and then go into the actual docker
by entering
docker exec -it homeassistant /bin/bash
and then do the pip install from there
that did the trick and got things working
hope this helps someone one day

7 Likes

Thanks for the additional info Ron, definitely helping me here. I’m still having trouble with the install python-switchbot command, also running HA OS. Outside of the Docker command, was there anything else you did to execute the installation?

Hello, I’m blocked on the step to retreive Switchbot device ID. I don’t understand what I have to do… I installed python-switchbot with ssh. Someone could help me ?
I’m on python-switchbot site.
Thanks in advance :slight_smile:

You can either use the aforementioned CURL command or you can use your switchbot app on your smart phone to find the MAC address. Both work.

If using the app on your phone, the MAC addresses you want will be from the front menu, click the item (e.g. door sensor, light bulb) and then select the gear icon (top right of screen) and look for the menu item “Device Info” - click that and you’re presented with the MAC address. HTH.

Doing this seems to break the official switchbot integration. It overwrites /usr/local/lib/python3.9/site-packages/switchbot/__init__.py with the one used by python-switchbot, so if you try to uninstall and install the official switchbot component again, it will fail.

See this: Switchbot intergration not working after 2022.4.7

1 Like

I stopped with the integration I described here as it was way to unreliable. I switched to SwitchBot bot/curtain/meter/contact/motion MQTT ESP32 bridge - Local control - Share your Projects! - Home Assistant Community (home-assistant.io) and that one works so much more reliable.

How was it unreliable? I have a hub mini as part of the a recent combo sale and would prefer to take advantage of that rather than building an esp32 to mqtt bridge. I would expect their cloud API to be what they also use so it should be as reliable as their cloud options?

The actual click would be more or less a 50% chance to work. So if I told Home Assistant to click, it sometimes worked and sometimes didn’t.