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: