Switchbot integration in 2021.10

Hello,

I had a switchbot that worked finewith the configuration.yml

  - platform: switchbot
    mac: 'a1:a2:a3:a4:a5:a6'
    name: 'My switchbot'
    password: '12345'

Installed 2021.10 and the integration switchbot has an error
Retrying setup: ‘utf-8’ codec can’t decode byte 0xc8 in position 0: unexpected end of data

I have removed the switchbot integration, removed the config from configuration.yml restarted home assistant and adding the Switchbot integration getting just an error. I can try to get it again on a screenshot if it does help.

I had the password in the past config without quotes so tried with quotes in my last config…

Anyone facing similar problem?

Thank you!

1 Like

File "/usr/src/homeassistant/homeassistant/components/switchbot/config_flow.py", line 36, in _btle_connect
switchbot_devices = GetSwitchbotDevices().discover()
File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 138, in discover
_model = binascii.unhexlify(value[4:6]).decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc8 in position 0: unexpected end of data

I have an additional blootooth dongle to get closer to my switchbot!

ok I had to reset it and pair again from within the integration that will set the password as it integrates with the switchbot.
Just cannot figure out how to set it in press mode and not in switch mode… if I involve the app all gets disabled in HA…

I had a success but the new integration is buggy… You turn it on - does the job and in the lovelace turns off and after a minute or so it probably re-reads the status and it shows as on in lovelace too. Same when you turn it off… It does not bug me based on what I use it but if you turn the light on and off I guess you will not like it…

3 Likes

Found it. Remove the bots password from the switchbot app.

I have problems with the new integration also. The switchbot bot works strangely. Before the update, when I turned it on it stay in state on. Now when I turn on it come backs to off in a few seconds. Moreover I miss the “is state” value. Before update I could create sensor that check the state value and now I can’t do it.

4 Likes

Since the update there are connection issues.
I have an external bluetooth adapter.
After several attempts I managed to add the devices. But nevertheless it constantly loses connection.
Now the switchbots are most of the time unreachable. Before updating everythink worked as expected.

Logger: switchbot
Source: /usr/local/lib/python3.9/site-packages/switchbot/__init__.py:115
First occurred: 11:33:03 (68 occurrences)
Last logged: 12:40:05

Error scanning for switchbot devices
Traceback (most recent call last):
 File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 112, in discover
    devices = bluepy.btle.Scanner(self._interface).scan(scan_timeout)
  File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 852, in scan
    self.start(passive=passive)
  File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 797, in start
    self._mgmtCmd(self._cmd()+"end")
  File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 312, in _mgmtCmd
    raise BTLEManagementError("Failed to execute management command '%s'" % (cmd), rsp)
bluepy.btle.BTLEManagementError: Failed to execute management command 'scanend' (code: 11, error: Rejected)
2 Likes

I’m having bizarre behaviors too with this integration. It’s working now but a bit sluggishly, especially updating the UI with a state change. Here’s a couple of errors:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 283, in _sendcommand
    self._connect()
  File "/usr/local/lib/python3.9/site-packages/switchbot/__init__.py", line 232, in _connect
    self._device = bluepy.btle.Peripheral(
  File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 391, in __init__
    self._connect(deviceAddr, addrType, iface)
  File "/usr/local/lib/python3.9/site-packages/bluepy/btle.py", line 439, in _connect
    raise BTLEDisconnectError("Failed to connect to peripheral %s, addr type: %s" % (addr, addrType), rsp)
bluepy.btle.BTLEDisconnectError: Failed to connect to peripheral d#:##:##:##:##:##, addr type: random

  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 134, in _handle_refresh_interval
    await self._async_refresh(log_failures=True, scheduled=True)
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 265, in _async_refresh
    update_callback()
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 325, in _handle_coordinator_update
    self.async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 456, in async_write_ha_state
    self._async_write_ha_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 489, in _async_write_ha_state
    state = self._stringify_state()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 462, in _stringify_state
    if (state := self.state) is None:
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 869, in state
    return STATE_ON if self.is_on else STATE_OFF
  File "/usr/src/homeassistant/homeassistant/components/switchbot/switch.py", line 154, in is_on
    return self.data["data"]["isOn"]
  File "/usr/src/homeassistant/homeassistant/components/switchbot/entity.py", line 41, in data
    return self.coordinator.data[self._idx]
KeyError: 'd###########
1 Like

With 2021.10.4, switches do work correctly now, but the status will always go “off” soon after you turn it on. Hopefully to have it fixed soon.

1 Like

I just updated to the latest and it works but the status is delayed by about a minute or so, really weird.

1 Like

For those who want status back in 2021.10, I have reported here which worked, following an advice from another guy.
What you will do is to copy the old code to custom components. Bring back the old switchbot config into configuration.yaml and restart.
You will lose battery info which was new in 2021.10, but status will work again as it was.

Deployed this project by devWaves: SwitchBot-MQTT-BLE-ESP32, this is way more responsive and the switchbot devices are natively discoverable through the MQTT integration, really cool stuff! I already had a couple of D1 Mini ESP32 dev boards laying around which might not be the case for most… But the flashing and set up is really painless. Then I 3D printed an enclosure and it came out better than expected:

7 Likes

added another switchbot bot to my Home Assistant and all works great. I have followed the steps below:

  • Added the bot in the swithchbot app and set a password (numbers only - it had issues in the past with characters so I stick to numbers)

  • Home Assistant → Integrations → add integration → Switchbot

  • it detected automatically the bot mac address, just added the name and password

  • it is working great with the slight delay that is typical for Bluetooth devices

Thanks you Home Assistant for another great and rock solid integration!

I have been having issues with the integration since the update. I used to run a script that relied on the state of the switchbot to verify if the last command worked and if not to try again (i.e. when turning on the switchbot, check after 15 seconds if state is ‘on’, if not, try again) and it worked for long time without any issues. After the update the script obviously stopped working and I changed the script to check the last_run_success instead but it doesn’t work as well - physical switch is now often out of sync with the virtual switch.

I am trying to follow the instructions linked by rkjr. I managed to put the older version of switchbot in the custom components folder but I can’t delete the newer one on my Home Assistant OS installation on VM because I can’t find ‘python3.8/site-packages/homeassistant/switchbot’ directory. I tried to ssh into port 22222 but I have trouble coping the public key from the usb. As such, every time I restart the server the new switchbot integration finds the bots automatically and I can’t use the old version.

Any suggestions? Do I need to ssh to port 22222 in order to find the phython3.8 directory on my installation?

I’m too having issues since update, they are just not responding.

After updating to 2021.12 the switchbot integration stopped working completely (not responding). Went back to 2021.11.2 and it is working again but same problems as since 2021.10.

I just want to use the old integration but can’t make it work.

odd, mine is back working again. Check if your bluetooth is working. I have an external bluetooth dongle to my pi 3B and I had to re-set the default controller to my external dongle.
ssh to home assistant and use bluetoothctl to check it out

I’m using a PC with a built in bluetooth, running AH on VM. I don’t think it is hardware because it started working again when I went back to 2021.11.2.

It was working fine until 2021.10. After the 2021.10 update (when it stopped showing persistent on/off states and only changed to ‘on’ for few second) it became unreliable and after 2021.12 stopped working at all.

For what it worth, now on 2021.11.2 bluetoothctl gives:

Agent registered
[CHG] Controller A0:C5:89:01:60:8E Pairable: yes

1 Like

That enclosure looks really cool. Well done! Would you mind sharing the .stl?

Update: I managed to use the old switchbot integration (2021.9) with AH 2021.11.2 as per instructions by rkjr. I didn’t need to delete the original core integration. It previously didn’t work for me because I didn’t aad version tag to manifest.json. This link - Integration File Structure | Home Assistant Developer Docs - was helpful. All seem to be working well again. I’ll try to go back to the latest integration in few months, hopefully by then it will be working again.

1 Like