Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xiaomi Gateway discovery isn't handling network outages well #11415

Closed
papaiatis opened this issue Jan 1, 2018 · 3 comments
Closed

Xiaomi Gateway discovery isn't handling network outages well #11415

papaiatis opened this issue Jan 1, 2018 · 3 comments

Comments

@papaiatis
Copy link

Home Assistant release (hass --version): 0.60.0

Python release (python3 --version): 3.4.2

Component/platform: xiaomi_aqara

Description of problem: In case of network is unreachable Xiaomi Gateway discovery throws an exception (OSError: [Errno 101] Network is unreachable) and stops discovering devices. This happens at me in case of a power outage and after power comes back HASS is booting up faster than my router is able to finish setting up the local network.

Expected: I expect this component to better handle any exception and keep continuing device discovery.

Traceback (if applicable):

Error during setup of component xiaomi_aqara
Traceback (most recent call last):
File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/setup.py", line 193, in _async_setup_component
component.setup, hass, processed_config)
File "/usr/lib/python3.4/asyncio/futures.py", line 388, in __iter__
yield self # This tells Task to wait for completion.
File "/usr/lib/python3.4/asyncio/tasks.py", line 286, in _wakeup
value = future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 277, in result
raise self._exception
File "/usr/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "/srv/homeassistant/lib/python3.4/site-packages/homeassistant/components/xiaomi_aqara.py", line 115, in setup
xiaomi.discover_gateways()
File "/srv/homeassistant/lib/python3.4/site-packages/PyXiaomiGateway/__init__.py", line 70, in discover_gateways
(self.MULTICAST_ADDRESS, self.GATEWAY_DISCOVERY_PORT))

Additional info:
I managed to fix this issue in home-assistant/homeassistant/components/xiaomi_aqara.py around line 113 where the discovery happens in a for loop. What I did is to encapsulate the xiaomi.discover_gateways() call in a try..except block as follows:

    for k in range(discovery_retry):
        _LOGGER.info('Discovering Xiaomi Gateways (Try %s)', k + 1)
        try:
            xiaomi.discover_gateways()
            if len(xiaomi.gateways) >= len(gateways):
                break
        except Exception as e:
            _LOGGER.error('Unexpected error occured while discovering Xiaomi Gateways: ' + str(e))
            time.sleep(5)

I believe the sleep() call is necessary because this exception occurs immediately and the loop would finish instantly. Maybe the number of seconds to sleep could be extracted as a config option.
For my case I had to set discovery_retry: 50 so this component has enough time until the network is reachable again.
But IMHO the best would be to wait/sleep until the network is reachable again and not incrementing the retry counter. Without network HASS is useless anyways.

I'm happy to create a pull request with this fix if you accept it.

@syssi
Copy link
Member

syssi commented Jan 9, 2018

Please provide a pull request!

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@balloobbot
Copy link

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem 👍

@home-assistant home-assistant locked and limited conversation to collaborators Oct 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants