AlexxIT / XiaomiGateway3

Home Assistant custom component for control Xiaomi Multimode Gateway (aka Gateway 3), Xiaomi Multimode Gateway 2, Aqara Hub E1 on default firmwares over LAN
https://github.com/AlexxIT/Blog
MIT License
2.46k stars 343 forks source link

Can control gateway but ZHA cannot communicate with it "Can't update gateway stats" #1072

Closed acseven closed 1 year ago

acseven commented 1 year ago

I've setup a Xiaomi 3 gateway some months ago and successfully added the single zigbee device I added so far, but after some time it just stops working. I'm using custom ZHA quircks to be able to use the device but as far as I can understand the main issue is communication with the gateway which can work for quite some time and then stop working at all for no aparent reason. Making it work is also something I can't pinpoint, it's just after trying to reset / reload / reboot a bunch of times and one of those it comes back.

Gateway: gateway ZNDMWG03LM by Xiaomi Firmware: 1.5.0_0102

Device: MOES Presence Detector (TS0601 by _TZE200_ikvncluo)

HA can communicate and operate the gateway (e.g. send a Reset command works fine):

image

The relevant logs are as follows:

Logger: custom_components.xiaomi_gateway3.core.gateway
Source: custom_components/xiaomi_gateway3/core/shell/__init__.py:37
Integration: Xiaomi Gateway 3 ([documentation](https://github.com/AlexxIT/XiaomiGateway3), [issues](https://github.com/AlexxIT/XiaomiGateway3/issues))
First occurred: 17:38:35 (1 occurrences)
Last logged: 17:38:35

192.168.11.90 | Can't update gateway stats
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/asyncio/streams.py", line 48, in open_connection
    transport, _ = await loop.create_connection(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1060, in create_connection
    sock = await self._connect_sock(
  File "/usr/local/lib/python3.10/asyncio/base_events.py", line 969, in _connect_sock
    await self.sock_connect(sock, address)
  File "/usr/local/lib/python3.10/asyncio/selector_events.py", line 501, in sock_connect
    return await fut
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 456, in wait_for
    return fut.result()
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/config/custom_components/xiaomi_gateway3/core/gateway/gate_mgw.py", line 105, in gw3_update_serial_stats
    async with shell.Session(self.host) as sh:
  File "/config/custom_components/xiaomi_gateway3/core/shell/__init__.py", line 30, in __aenter__
    await self.connect()
  File "/config/custom_components/xiaomi_gateway3/core/shell/__init__.py", line 37, in connect
    self.reader, self.writer = await asyncio.wait_for(self.coro, 5)
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 458, in wait_for
    raise exceptions.TimeoutError() from exc
asyncio.exceptions.TimeoutError
Logger: homeassistant.config_entries
Source: components/zha/core/gateway.py:205
First occurred: 17:33:50 (1 occurrences)
Last logged: 17:33:50

Error setting up entry socket://192.168.11.90:8888 for zha
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 268, in reset
    return await self._reset_future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/config_entries.py", line 387, in async_setup
    result = await component.async_setup_entry(hass, self)
  File "/usr/src/homeassistant/homeassistant/components/zha/__init__.py", line 122, in async_setup_entry
    await zha_gateway.async_initialize()
  File "/usr/src/homeassistant/homeassistant/components/zha/core/gateway.py", line 220, in async_initialize
    raise exc
  File "/usr/src/homeassistant/homeassistant/components/zha/core/gateway.py", line 205, in async_initialize
    self.application_controller = await app_controller_cls.new(
  File "/usr/local/lib/python3.10/site-packages/zigpy/application.py", line 219, in new
    await app.startup(auto_form=auto_form)
  File "/usr/local/lib/python3.10/site-packages/zigpy/application.py", line 193, in startup
    await self.connect()
  File "/usr/local/lib/python3.10/site-packages/bellows/zigbee/application.py", line 131, in connect
    self._ezsp = await bellows.ezsp.EZSP.initialize(self.config)
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 150, in initialize
    await ezsp._startup_reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 141, in _startup_reset
    await self.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 170, in reset
    await self._gw.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 267, in reset
    async with asyncio_timeout(RESET_TIMEOUT):
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
Logger: homeassistant.components.zha.core.gateway
Source: components/zha/core/gateway.py:205
Integration: Zigbee Home Automation ([documentation](https://www.home-assistant.io/integrations/zha), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+zha%22))
First occurred: 17:33:31 (3 occurrences)
Last logged: 17:33:50

Couldn't start EZSP = Silicon Labs EmberZNet protocol: Elelabs, HUSBZB-1, Telegesis coordinator (attempt 1 of 3)
Couldn't start EZSP = Silicon Labs EmberZNet protocol: Elelabs, HUSBZB-1, Telegesis coordinator (attempt 2 of 3)
Couldn't start EZSP = Silicon Labs EmberZNet protocol: Elelabs, HUSBZB-1, Telegesis coordinator (attempt 3 of 3)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 268, in reset
    return await self._reset_future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/zha/core/gateway.py", line 205, in async_initialize
    self.application_controller = await app_controller_cls.new(
  File "/usr/local/lib/python3.10/site-packages/zigpy/application.py", line 219, in new
    await app.startup(auto_form=auto_form)
  File "/usr/local/lib/python3.10/site-packages/zigpy/application.py", line 193, in startup
    await self.connect()
  File "/usr/local/lib/python3.10/site-packages/bellows/zigbee/application.py", line 131, in connect
    self._ezsp = await bellows.ezsp.EZSP.initialize(self.config)
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 150, in initialize
    await ezsp._startup_reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 141, in _startup_reset
    await self.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 170, in reset
    await self._gw.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 267, in reset
    async with asyncio_timeout(RESET_TIMEOUT):
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
Logger: zigpy.application
Source: /usr/local/lib/python3.10/site-packages/zigpy/application.py:196
First occurred: 17:33:31 (3 occurrences)
Last logged: 17:33:49

Couldn't start application
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 268, in reset
    return await self._reset_future
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/zigpy/application.py", line 193, in startup
    await self.connect()
  File "/usr/local/lib/python3.10/site-packages/bellows/zigbee/application.py", line 131, in connect
    self._ezsp = await bellows.ezsp.EZSP.initialize(self.config)
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 150, in initialize
    await ezsp._startup_reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 141, in _startup_reset
    await self.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/ezsp/__init__.py", line 170, in reset
    await self._gw.reset()
  File "/usr/local/lib/python3.10/site-packages/bellows/uart.py", line 267, in reset
    async with asyncio_timeout(RESET_TIMEOUT):
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 129, in __aexit__
    self._do_exit(exc_type)
  File "/usr/local/lib/python3.10/site-packages/async_timeout/__init__.py", line 212, in _do_exit
    raise asyncio.TimeoutError
asyncio.exceptions.TimeoutError
AlexxIT commented 1 year ago

Gateway stats fixed in latest version https://github.com/AlexxIT/XiaomiGateway3/releases/tag/v3.3.0

Also ZHA has not best reconnection logic. I've done a lot of tests, it can't always reconnect successfully after a connection failure. This should be fixed in the ZHA itself.