Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions homeassistant/components/asuswrt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
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__)

Expand All @@ -31,6 +32,9 @@
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"]

Expand Down Expand Up @@ -59,7 +63,7 @@
)


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]
Expand All @@ -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:
Comment thread
MartinHjelmare marked this conversation as resolved.
_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
Expand Down
12 changes: 12 additions & 0 deletions tests/components/asuswrt/test_device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
MartinHjelmare marked this conversation as resolved.
assert hass.data.get(DATA_ASUSWRT, None) is None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better if we tested the existence of states in the state machine. When there's an error there shouldn't be any states. After the error clears, the setup should complete and states be created. That way we don't rely on the details of our implementation in the tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will keeep in consideration for next PR. Thx



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:
Expand Down