From 62835f0536e323207d48b6257f01a47f86a1e1cf Mon Sep 17 00:00:00 2001 From: ollo69 <60491700+ollo69@users.noreply.github.com> Date: Fri, 3 Apr 2020 15:02:48 +0200 Subject: [PATCH] Fix asuswrt network failure startup (#33485) * Fix network failure startup Fix for issue ##33284 - Asuswrt component fail at startup after power failure * Removed comment * Removed bare except * is_connected moved out try-catch * Removed pointless-string-statement * Raise PlatformNotReady on "not is_connected" * Removed unnecessary check * Revert "Removed unnecessary check" This reverts commit a2ccddab2c4b1ba441f1d7482d802d9774527a26. * Implemented custom retry mechanism * Fix new line missing * Fix formatting * Fix indent * Reviewed check * Recoded based on tibber implementation * Formatting review * Changes requested * Fix tests for setup retry * Updated missing test * Fixed check on Tests * Return false if not exception * Format correction --- homeassistant/components/asuswrt/__init__.py | 30 +++++++++++++++++-- .../components/asuswrt/test_device_tracker.py | 12 ++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/asuswrt/__init__.py b/homeassistant/components/asuswrt/__init__.py index a0eee38c3f8..446fe898aaa 100644 --- a/homeassistant/components/asuswrt/__init__.py +++ b/homeassistant/components/asuswrt/__init__.py @@ -14,6 +14,7 @@ from homeassistant.const import ( ) from homeassistant.helpers import config_validation as cv from homeassistant.helpers.discovery import async_load_platform +from homeassistant.helpers.event import async_call_later _LOGGER = logging.getLogger(__name__) @@ -31,6 +32,9 @@ DEFAULT_SSH_PORT = 22 DEFAULT_INTERFACE = "eth0" DEFAULT_DNSMASQ = "/var/lib/misc" +FIRST_RETRY_TIME = 60 +MAX_RETRY_TIME = 900 + SECRET_GROUP = "Password or SSH Key" SENSOR_TYPES = ["upload_speed", "download_speed", "download", "upload"] @@ -59,7 +63,7 @@ CONFIG_SCHEMA = vol.Schema( ) -async def async_setup(hass, config): +async def async_setup(hass, config, retry_delay=FIRST_RETRY_TIME): """Set up the asuswrt component.""" conf = config[DOMAIN] @@ -77,9 +81,29 @@ async def async_setup(hass, config): dnsmasq=conf[CONF_DNSMASQ], ) - await api.connection.async_connect() + try: + await api.connection.async_connect() + except OSError as ex: + _LOGGER.warning( + "Error [%s] connecting %s to %s. Will retry in %s seconds...", + str(ex), + DOMAIN, + conf[CONF_HOST], + retry_delay, + ) + + async def retry_setup(now): + """Retry setup if a error happens on asuswrt API.""" + await async_setup( + hass, config, retry_delay=min(2 * retry_delay, MAX_RETRY_TIME) + ) + + async_call_later(hass, retry_delay, retry_setup) + + return True + if not api.is_connected: - _LOGGER.error("Unable to setup component") + _LOGGER.error("Error connecting %s to %s.", DOMAIN, conf[CONF_HOST]) return False hass.data[DATA_ASUSWRT] = api diff --git a/tests/components/asuswrt/test_device_tracker.py b/tests/components/asuswrt/test_device_tracker.py index b91b815d58e..62e5ed891ff 100644 --- a/tests/components/asuswrt/test_device_tracker.py +++ b/tests/components/asuswrt/test_device_tracker.py @@ -24,6 +24,18 @@ async def test_password_or_pub_key_required(hass): assert not result +async def test_network_unreachable(hass): + """Test creating an AsusWRT scanner without a pass or pubkey.""" + with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt: + AsusWrt().connection.async_connect = mock_coro_func(exception=OSError) + AsusWrt().is_connected = False + result = await async_setup_component( + hass, DOMAIN, {DOMAIN: {CONF_HOST: "fake_host", CONF_USERNAME: "fake_user"}} + ) + assert result + assert hass.data.get(DATA_ASUSWRT, None) is None + + async def test_get_scanner_with_password_no_pubkey(hass): """Test creating an AsusWRT scanner with a password and no pubkey.""" with patch("homeassistant.components.asuswrt.AsusWrt") as AsusWrt: