core: Switchbot Lock IndexError

The problem

After upgrading to home assistant 2023.3 the the Switchbot lock works after restart, but after some time will generate an error of “IndexError: bytearray index out of range”. This lock is connected via an esp32 running the ESPhome Home assistant bluetooth proxy.

What version of Home Assistant Core has the issue?

2023.3

What was the last working version of Home Assistant Core?

2023.2

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Switchbot

Link to integration documentation on our website

https://www.home-assistant.io/integrations/switchbot/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant.components.websocket_api.http.connection
Source: components/switchbot/lock.py:54 
Integration: Home Assistant WebSocket API (documentation, issues) 
First occurred: 2:41:26 PM (5 occurrences) 
Last logged: 2:43:03 PM

[140336955975392] bytearray index out of range
[140336996131280] bytearray index out of range
[140336906908960] bytearray index out of range
[140337048416464] bytearray index out of range
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 198, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1808, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1845, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 213, in handle_service
    await service.entity_service_call(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 686, in entity_service_call
    future.result()  # pop exception if have
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 961, in async_request_call
    await coro
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 726, in _handle_entity_call
    await result
  File "/usr/src/homeassistant/homeassistant/components/lock/__init__.py", line 106, in _async_unlock
    await entity.async_unlock(**remove_entity_service_fields(service_call))
  File "/usr/src/homeassistant/homeassistant/components/switchbot/lock.py", line 54, in async_unlock
    self._last_run_success = await self._device.unlock()
  File "/usr/local/lib/python3.10/site-packages/switchbot/devices/lock.py", line 168, in unlock
    return await self._lock_unlock(
  File "/usr/local/lib/python3.10/site-packages/switchbot/devices/lock.py", line 196, in _lock_unlock
    self._update_parsed_data(self._parse_basic_data(basic_data))
  File "/usr/local/lib/python3.10/site-packages/switchbot/devices/lock.py", line 175, in _parse_basic_data
    "battery": basic_data[1],
IndexError: bytearray index out of range

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 61 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Should be fixed in 2023.3.6 thanks to the person who didn’t wanted to be named that mailed me a lock that had the issue.

Facing the same error also in .1 update.

Hi @federicotravaini I was looking to the diff from the old February release and the first of march but I didn’t see any changes on Switchbot files. I think it is an issue on the new python version.

In this weekend if I have time I’ll try to take some debugs for investigate.

Btw, does someone has it installed not as container but with python? In case, with python 10 it has the same error?

Edit: I read better the log, it is python10, but the error coming from the switchbot lib, not from hass

Edit 2: the only changes from 2022.2.5 is the update of the PySwitchbot lib from 0.36.4 to 0.37.3 so I think this issue is opened to the wrong repository.

Tomorrow I’ll open it to the correct one if no other do it before me (if you’ll do, please tag me)

@Danielhiversen @bdraco

is some progress with this issue? 3 weeks gone…

@federicotravaini for the sake of curiosity, would you share such integration?

actually it’s more than one automation.

This one check if the door goes offline, then tries to reload the integration and if it’s still offline it sends a message

alias: SWITCHBOT - Se offline ricarico porta
description: ""
trigger:
  - platform: state
    entity_id:
      - lock.lock_0457
    to: unavailable
    for:
      hours: 0
      minutes: 0
      seconds: 30
condition: []
action:
  - service: homeassistant.reload_config_entry
    data: {}
    target:
      entity_id: lock.lock_0457
  - if:
      - condition: state
        entity_id: lock.lock_0457
        state: unavailable
    then: []
    else:
      - stop: è tornato online
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - repeat:
      while:
        - condition: state
          entity_id: lock.lock_0457
          state: unavailable
      sequence:
        - service: notify.mobile_app_iphone_fede
          data:
            title: SWITCHBOT
            message: La porta è ancora offline
        - delay:
            hours: 0
            minutes: 30
            seconds: 0
            milliseconds: 0
mode: single

Then i have a button on the lovelace that just reloads the integration

alias: Reload Switchbot Ingresso
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.reload_switchbot_ingresso
condition: []
action:
  - service: homeassistant.reload_config_entry
    data: {}
    target:
      device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxx
mode: single

Ya - a few people reporting this as a problem that continues in HA 2023.3.3, with no updates on a fix in the thread, is not spamming.

We got it folks, everyone is affected by this issue. Please stop spamming comments and just wait for the fix.

So far appears to be fixed for my lock and HA setup using 2023.3.6.

I do not think this is fixed. I upgraded to 2023.3.6 because I was experiencing the same symptoms as described above. After upgrading the symptoms actually seem more severe. The Lock becomes completely unavailable and just reloading the switchbot integration is not enough anymore. HA can’t find the Lock anymore. To fix it, I have to restart the Bluetooth proxy and then the switchbot integration.

great idea! but my lock doesn’t show as ‘unavailable’ when I get this error. Still shows online like its working

That is when I use the button that reloads the integration, but you can also create a button that first reload the automation, and then open/close the switchbot, so it should be working all the time

In my case, the SwitchBot Lock also does not show “unavailable” in HA just prior to failing - the lock appears working normal, showing battery level, etc. And the SwitchBot Integration appears OK as well. Recyling the Integration immediately causes the lock to run I on again.

Appreciate the automations posted here. This should help for now until a fix is available in the future. 😃

i too. 2 weeks gone and critical upgrade no exsist. I think abut change lock and all switchbot devices to another brand

I don’t see anything in the changelog indicating the error has been fixed.

That is when I use the button that reloads the integration, but you can also create a button that first reload the automation, and then open/close the switchbot, so it should be working all the time

Ah yes - I used made a script that locks the door - if it fails it then loads the integration and tries again every 5 minutes - It doesn’t normally wait the 5 minutes because the door locks as soon as the integration comes back online. I just use this script for all locking purposes. My unlock script looks the same:

alias: Front Door - Lock
mode: restart
sequence:
  - condition: or
    conditions:
      - condition: state
        entity_id: lock.front_door_lock
        state: unlocked
      - condition: state
        entity_id: lock.front_door_lock
        state: unavailable
  - data: {}
    entity_id: lock.front_door_lock
    service: lock.lock
  - wait_for_trigger:
      - platform: state
        entity_id:
          - lock.front_door_lock
        to: locked
    continue_on_timeout: true
    timeout:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - condition: not
    conditions:
      - condition: state
        entity_id: lock.front_door_lock
        state: locked
  - service: homeassistant.reload_config_entry
    data: {}
    target:
      entity_id: lock.front_door_lock
  - repeat:
      until:
        - condition: state
          entity_id: lock.front_door_lock
          state: locked
      sequence:
        - data: {}
          entity_id: lock.front_door_lock
          service: lock.lock
        - delay:
            hours: 0
            minutes: 5
            seconds: 0
            milliseconds: 0

@federicotravaini for the sake of curiosity, would you share such integration?

actually it’s more than one automation.

This one check if the door goes offline, then tries to reload the integration and if it’s still offline it sends a message

alias: SWITCHBOT - Se offline ricarico porta

description: “”

trigger:

  • platform: state
entity_id:
  - lock.lock_0457
to: unavailable
for:
  hours: 0
  minutes: 0
  seconds: 30

condition: []

action:

  • service: homeassistant.reload_config_entry
data: {}
target:
  entity_id: lock.lock_0457
  • if:
  - condition: state
    entity_id: lock.lock_0457
    state: unavailable
then: []
else:
  - stop: è tornato online
  • delay:
  hours: 0
  minutes: 0
  seconds: 30
  milliseconds: 0
  • repeat:
  while:
    - condition: state
      entity_id: lock.lock_0457
      state: unavailable
  sequence:
    - service: notify.mobile_app_iphone_fede
      data:
        title: SWITCHBOT
        message: La porta è ancora offline
    - delay:
        hours: 0
        minutes: 30
        seconds: 0
        milliseconds: 0

mode: single

Then i have a button on the lovelace that just reloads the integration

alias: Reload Switchbot Ingresso

description: “”

trigger:

  • platform: state
entity_id:
  - input_button.reload_switchbot_ingresso

condition: []

action:

  • service: homeassistant.reload_config_entry
data: {}
target:
  device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxxx

mode: single

great idea! but my lock doesn’t show as ‘unavailable’ when I get this error. Still shows online like its working

That is when I use the button that reloads the integration, but you can also create a button that first reload the automation, and then open/close the switchbot, so it should be working all the time

It is randomic and reloading the integration fixes the issue temporarily I just made an automation that every now and then reloads the integration so it seems to be working a little bit better but still it’s something absolutely unreliable.

Ya - a few people reporting this as a problem that continues in HA 2023.3.3, with no updates on a fix in the thread, is not spamming.

We got it folks, everyone is affected by this issue. Please stop spamming comments and just wait for the fix.

Valid point. Adding that since today (2023.3.3) it seems that the problem is fixed (at least I was able to open/close it without an error once).

Was a fix added somewhere? It failed for me again last night just after midnight with 2023.3.3 and I had to use the SwitchBot iOS app to open the lock. HA

It seems it’s a bit of randomic problem. I retried just now and it failed again with the same error…

Ya - a few people reporting this as a problem that continues in HA 2023.3.3, with no updates on a fix in the thread, is not spamming.

We got it folks, everyone is affected by this issue. Please stop spamming comments and just wait for the fix.

Valid point. Adding that since today (2023.3.3) it seems that the problem is fixed (at least I was able to open/close it without an error once).