Skip to content

Single data byte causing index out of range #369

@eseverson

Description

@eseverson

I have a Switch 1PM and I am seeing this error when toggling the state of the device on/off (currently just in a home assistant devcontainer, I will try to reproduce it in a prod deployment later). I haven't found specific steps yet to consistently reproduce it.

I am able to see that raw_data is a single byte \x02

I'm not sure of the source of the byte but am wondering if it should be added to the list here (and other places) or if the logic should be changed to not rely on a specific list of bytes to reject.

if _data in (b"\x07", b"\x00"):

This was on the latest commit of https://github.com/home-assistant/core as of writing.

Log output:

2025-07-05 03:36:05.951 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None):   File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module>
    cli.main()
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main
    run()
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 384, in run_module
    run_module_as_main(options.target, alter_argv=True)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 228, in _run_module_as_main
    return _run_code(code, main_globals, None, "__main__", mod_spec)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "/workspaces/hass-core/homeassistant/__main__.py", line 223, in <module>
    sys.exit(main())
  File "/workspaces/hass-core/homeassistant/__main__.py", line 209, in main
    exit_code = runner.run(runtime_conf)
  File "/workspaces/hass-core/homeassistant/runner.py", line 154, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 712, in run_until_complete
    self.run_forever()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 683, in run_forever
    self._run_once()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 2034, in _run_once
    handle._run()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/events.py", line 89, in _run
    self._context.run(self._callback, *self._args)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/manager.py", line 997, in _parse_msg
    self._run_advertisement_callbacks(
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/manager.py", line 1040, in _run_advertisement_callbacks
    callback(device_path, device.copy())
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/scanner.py", line 273, in _handle_advertising_data
    self.call_detection_callbacks(device, advertisement_data)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/scanner.py", line 247, in call_detection_callbacks
    callback(device, advertisement_data)
  File "/workspaces/hass-core/homeassistant/components/bluetooth/manager.py", line 124, in _discover_service_info
    callback(service_info, BluetoothChange.ADVERTISEMENT)
  File "/workspaces/hass-core/homeassistant/components/switchbot/coordinator.py", line 115, in _async_handle_bluetooth_event
    self.device.update_from_advertisement(adv)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 144, in update_from_advertisement
    super().update_from_advertisement(advertisement)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/device.py", line 995, in update_from_advertisement
    create_background_task(self.update())
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/helpers.py", line 19, in create_background_task
    task = asyncio.create_task(target)
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/tasks.py", line 410, in create_task
    task = loop.create_task(coro, name=name)
Traceback (most recent call last):
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/device.py", line 623, in update
    if info := await self.get_basic_info():
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 176, in get_basic_info
    common_data = self._parse_common_data(_data)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 97, in _parse_common_data
    "sequence_number": raw_data[1],
                       ~~~~~~~~^^^
IndexError: bytearray index out of range

This is a separate error, but seemingly related, that I've only seen one instance of:

2025-07-05 03:51:35.056 ERROR (MainThread) [homeassistant] Error doing job: Task exception was never retrieved (None):   File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module>
    cli.main()
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main
    run()
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 384, in run_module
    run_module_as_main(options.target, alter_argv=True)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 228, in _run_module_as_main
    return _run_code(code, main_globals, None, "__main__", mod_spec)
  File "/home/vscode/.cursor-server/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "/workspaces/hass-core/homeassistant/__main__.py", line 223, in <module>
    sys.exit(main())
  File "/workspaces/hass-core/homeassistant/__main__.py", line 209, in main
    exit_code = runner.run(runtime_conf)
  File "/workspaces/hass-core/homeassistant/runner.py", line 154, in run
    return loop.run_until_complete(setup_and_run_hass(runtime_config))
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 712, in run_until_complete
    self.run_forever()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 683, in run_forever
    self._run_once()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/base_events.py", line 2034, in _run_once
    handle._run()
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/events.py", line 89, in _run
    self._context.run(self._callback, *self._args)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/manager.py", line 997, in _parse_msg
    self._run_advertisement_callbacks(
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/manager.py", line 1040, in _run_advertisement_callbacks
    callback(device_path, device.copy())
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/bluezdbus/scanner.py", line 273, in _handle_advertising_data
    self.call_detection_callbacks(device, advertisement_data)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/bleak/backends/scanner.py", line 247, in call_detection_callbacks
    callback(device, advertisement_data)
  File "/workspaces/hass-core/homeassistant/components/bluetooth/manager.py", line 124, in _discover_service_info
    callback(service_info, BluetoothChange.ADVERTISEMENT)
  File "/workspaces/hass-core/homeassistant/components/switchbot/coordinator.py", line 115, in _async_handle_bluetooth_event
    self.device.update_from_advertisement(adv)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 144, in update_from_advertisement
    super().update_from_advertisement(advertisement)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/device.py", line 995, in update_from_advertisement
    create_background_task(self.update())
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/helpers.py", line 19, in create_background_task
    task = asyncio.create_task(target)
  File "/home/vscode/.local/share/uv/python/cpython-3.13.5-linux-x86_64-gnu/lib/python3.13/asyncio/tasks.py", line 410, in create_task
    task = loop.create_task(coro, name=name)
Traceback (most recent call last):
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/device.py", line 623, in update
    if info := await self.get_basic_info():
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 177, in get_basic_info
    user_data = self._parse_user_data(_channel1_data)
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/devices/relay_switch.py", line 105, in _parse_user_data
    _energy = parse_uint24_be(raw_data, 1) / 60000
              ~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/home/vscode/.local/ha-venv/lib/python3.13/site-packages/switchbot/helpers.py", line 65, in parse_uint24_be
    raise ValueError(
        f"Insufficient data: need at least {offset + 3} bytes, got {len(data)}"
    )
ValueError: Insufficient data: need at least 4 bytes, got 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions