Xiaomi Gateway issue "invalid start byte"

Tags: #<Tag:0x00007f7393cef0a0> #<Tag:0x00007f7393ceef10>


I had 2 xiaomi V2 gateway with 51 devices (sensors, switches, …)
The sensors were evenly distributed among them.
Everything worked fine but one of them had burn (litterally!)

So, even if I had read that gateways devices are limited to 32, I’ve tested by myself and it work perfectly with Mii App and another domotic solution (jeedom).

But, since, it’s impossible to add this unique gateway to HA.

Autodiscovery work fine.
I’ve also try to configure it manualy: same error.
Here the log message

2020-08-20 16:10:29 DEBUG (SyncWorker_13) [xiaomi_gateway] _send_cmd >> b'{"cmd":"read","sid":"f0b429cc3e9f"}'
2020-08-20 16:10:29 DEBUG (SyncWorker_13) [xiaomi_gateway] _send_cmd resp << {'cmd': 'read_ack', 'model': 'gateway', 'sid': 'f0b429cc3e9f', 'short_id': 0, 'data': '{"rgb":0,"illumination":378,"proto_version":"1.1.2"}'}
2020-08-20 16:10:29 INFO (SyncWorker_13) [xiaomi_gateway] Discovering Xiaomi Devices
2020-08-20 16:10:29 DEBUG (SyncWorker_13) [xiaomi_gateway] _send_cmd >> b'{"cmd" : "get_id_list"}'
2020-08-20 16:10:30 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_protocol.py", line 418, in start
    resp = await task
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_app.py", line 458, in _handle
    resp = await handler(request)
  File "/usr/local/lib/python3.8/site-packages/aiohttp/web_middlewares.py", line 119, in impl
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/real_ip.py", line 39, in real_ip_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/ban.py", line 73, in ban_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/auth.py", line 127, in auth_middleware
    return await handler(request)
  File "/usr/src/homeassistant/homeassistant/components/http/view.py", line 129, in handle
    result = await result
  File "/usr/src/homeassistant/homeassistant/components/config/config_entries.py", line 145, in post
    return await super().post(request, flow_id)
  File "/usr/src/homeassistant/homeassistant/components/http/data_validator.py", line 60, in wrapper
    result = await method(view, request, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/data_entry_flow.py", line 106, in post
    result = await self._flow_mgr.async_configure(flow_id, data)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 153, in async_configure
    result = await self._async_handle_step(flow, cur_step["step_id"], user_input)
  File "/usr/src/homeassistant/homeassistant/data_entry_flow.py", line 201, in _async_handle_step
    result: Dict = await getattr(flow, method)(user_input)
  File "/usr/src/homeassistant/homeassistant/components/xiaomi_aqara/config_flow.py", line 90, in async_step_user
    self.selected_gateway = await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.8/site-packages/xiaomi_gateway/__init__.py", line 234, in __init__
    if self._discover_devices():
  File "/usr/local/lib/python3.8/site-packages/xiaomi_gateway/__init__.py", line 241, in _discover_devices
    resp = self._send_cmd(cmd, "get_id_list_ack") if int(self.proto[0:1]) == 1 \
  File "/usr/local/lib/python3.8/site-packages/xiaomi_gateway/__init__.py", line 341, in _send_cmd
    resp = json.loads(data.decode())
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 1008: invalid continuation byte

Anyone else has encountered this problem ?
Is ther a solution ?

Thank by advance for your help.

Edit: Adding the complete xiaomi_aqara log

I am now having this same issue. Not sure if it was an update or what caused it.

Any solutions?


After a long time search an hypothetical bug, I’ve bought another gateway, and splited my devices between both of them.
And… there is no problem anymore.
So, I conclude that the bug is internal at the gateway firmware, who seems to truncate at 1.000 chars the answer at the “get_id_list” request.
Moreover, I’ve observed the same issues on my Jeedom installation.

So, to conclude:

  • There is no devices count limitation on Mi App, but …
  • The API is crappy ^^