Add Ruckus Unleashed integration (#40002)

* Add Ruckus Unleashed integration

* Tweak catches to better rely on CoordinatorEntity error handling, rename LoginError to AuthenticationError

* Make router_update be a callback function

* Make name property return hint match the newer value

* Add entity to tracked set when restoring on boot

* Add a device's MAC to the attributes
This commit is contained in:
Gabe Cook authored and GitHub committed 2020-10-10 13:01:53 +02:00
1 parent de300ddd56
commit 4b617db669
16 files changed
+690

No files matched your search

@@ -0,0 +1,61 @@
"""Tests for the Ruckus Unleashed integration."""
from homeassistant.components.ruckus_unleashed import DOMAIN
from homeassistant.const import (
CONF_HOST,
CONF_IP_ADDRESS,
CONF_MAC,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
)
from tests.async_mock import patch
from tests.common import MockConfigEntry
DEFAULT_TITLE = "Ruckus Mesh"
CONFIG = {
CONF_HOST: "1.1.1.1",
CONF_USERNAME: "test-username",
CONF_PASSWORD: "test-password",
}
TEST_CLIENT_ENTITY_ID = "device_tracker.ruckus_test_device"
TEST_CLIENT = {
CONF_IP_ADDRESS: "1.1.1.2",
CONF_MAC: "AA:BB:CC:DD:EE:FF",
CONF_NAME: "Ruckus Test Device",
}
def mock_config_entry() -> MockConfigEntry:
"""Return a Ruckus Unleashed mock config entry."""
return MockConfigEntry(
domain=DOMAIN,
title=DEFAULT_TITLE,
unique_id="1.1.1.1",
data=CONFIG,
options=None,
)
async def init_integration(hass) -> MockConfigEntry:
"""Set up the Ruckus Unleashed integration in Home Assistant."""
entry = mock_config_entry()
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
return_value=None,
), patch(
"homeassistant.components.ruckus_unleashed.Ruckus.mesh_name",
return_value=DEFAULT_TITLE,
), patch(
"homeassistant.components.ruckus_unleashed.Ruckus.clients",
return_value={
TEST_CLIENT[CONF_MAC]: TEST_CLIENT,
},
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
return entry
@@ -0,0 +1,99 @@
"""Test the Ruckus Unleashed config flow."""
from pyruckus.exceptions import AuthenticationError
from homeassistant import config_entries, setup
from homeassistant.components.ruckus_unleashed.const import DOMAIN
from tests.async_mock import patch
from tests.components.ruckus_unleashed import CONFIG, DEFAULT_TITLE
async def test_form(hass):
"""Test we get the form."""
await setup.async_setup_component(hass, "persistent_notification", {})
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] == "form"
assert result["errors"] == {}
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
return_value=None,
), patch(
"homeassistant.components.ruckus_unleashed.Ruckus.mesh_name",
return_value=DEFAULT_TITLE,
), patch(
"homeassistant.components.ruckus_unleashed.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.ruckus_unleashed.async_setup_entry",
return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG,
)
assert result2["type"] == "create_entry"
assert result2["title"] == DEFAULT_TITLE
assert result2["data"] == CONFIG
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
async def test_form_invalid_auth(hass):
"""Test we handle invalid auth."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
side_effect=AuthenticationError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG,
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "invalid_auth"}
async def test_form_cannot_connect(hass):
"""Test we handle cannot connect error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
side_effect=ConnectionError,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG,
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_unknown_error(hass):
"""Test we handle unknown error."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
side_effect=Exception,
):
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"],
CONFIG,
)
assert result2["type"] == "form"
assert result2["errors"] == {"base": "unknown"}
@@ -0,0 +1,103 @@
"""The sensor tests for the Ruckus Unleashed platform."""
from datetime import timedelta
from homeassistant.components.ruckus_unleashed import DOMAIN
from homeassistant.const import CONF_MAC, STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
from homeassistant.helpers import entity_registry
from homeassistant.util import utcnow
from tests.async_mock import patch
from tests.common import async_fire_time_changed
from tests.components.ruckus_unleashed import (
DEFAULT_TITLE,
TEST_CLIENT,
TEST_CLIENT_ENTITY_ID,
init_integration,
mock_config_entry,
)
async def test_client_connected(hass):
"""Test client connected."""
await init_integration(hass)
future = utcnow() + timedelta(minutes=60)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.clients",
return_value={
TEST_CLIENT[CONF_MAC]: TEST_CLIENT,
},
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_HOME
async def test_client_disconnected(hass):
"""Test client disconnected."""
await init_integration(hass)
future = utcnow() + timedelta(minutes=60)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.clients",
return_value={},
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_NOT_HOME
async def test_clients_update_failed(hass):
"""Test failed update."""
await init_integration(hass)
future = utcnow() + timedelta(minutes=60)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.clients",
side_effect=ConnectionError,
):
async_fire_time_changed(hass, future)
await hass.async_block_till_done()
await hass.helpers.entity_component.async_update_entity(TEST_CLIENT_ENTITY_ID)
test_client = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert test_client.state == STATE_UNAVAILABLE
async def test_restoring_clients(hass):
"""Test restoring existing device_tracker entities if not detected on startup."""
entry = mock_config_entry()
entry.add_to_hass(hass)
registry = await entity_registry.async_get_registry(hass)
registry.async_get_or_create(
"device_tracker",
DOMAIN,
TEST_CLIENT[CONF_MAC],
suggested_object_id="ruckus_test_device",
config_entry=entry,
)
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus.connect",
return_value=None,
), patch(
"homeassistant.components.ruckus_unleashed.Ruckus.mesh_name",
return_value=DEFAULT_TITLE,
), patch(
"homeassistant.components.ruckus_unleashed.Ruckus.clients",
return_value={},
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
device = hass.states.get(TEST_CLIENT_ENTITY_ID)
assert device is not None
assert device.state == STATE_NOT_HOME
@@ -0,0 +1,54 @@
"""Test the Ruckus Unleashed config flow."""
from pyruckus.exceptions import AuthenticationError
from homeassistant.components.ruckus_unleashed import DOMAIN
from homeassistant.config_entries import (
ENTRY_STATE_LOADED,
ENTRY_STATE_NOT_LOADED,
ENTRY_STATE_SETUP_RETRY,
)
from tests.async_mock import patch
from tests.components.ruckus_unleashed import init_integration, mock_config_entry
async def test_setup_entry_login_error(hass):
"""Test entry setup failed due to login error."""
entry = mock_config_entry()
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus",
side_effect=AuthenticationError,
):
entry.add_to_hass(hass)
result = await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert result is False
async def test_setup_entry_connection_error(hass):
"""Test entry setup failed due to connection error."""
entry = mock_config_entry()
with patch(
"homeassistant.components.ruckus_unleashed.Ruckus",
side_effect=ConnectionError,
):
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ENTRY_STATE_SETUP_RETRY
async def test_unload_entry(hass):
"""Test successful unload of entry."""
entry = await init_integration(hass)
assert len(hass.config_entries.async_entries(DOMAIN)) == 1
assert entry.state == ENTRY_STATE_LOADED
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
assert entry.state == ENTRY_STATE_NOT_LOADED
assert not hass.data.get(DOMAIN)