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 correctionpull/33817/head
parent
730d90fb8c
commit
62835f0536
|
@ -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
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue