2021-11-26 16:44:49 -05:00
|
|
|
"""Test UniFi Network integration setup process."""
|
2022-07-18 17:39:38 +02:00
|
|
|
from unittest.mock import patch
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
from homeassistant.components import unifi
|
2021-02-06 21:32:18 +01:00
|
|
|
from homeassistant.components.unifi import async_flatten_entry_data
|
|
|
|
from homeassistant.components.unifi.const import CONF_CONTROLLER, DOMAIN as UNIFI_DOMAIN
|
2022-07-18 17:39:38 +02:00
|
|
|
from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect
|
2018-10-16 10:35:35 +02:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-10-08 23:44:33 +02:00
|
|
|
|
2021-03-05 21:28:41 +01:00
|
|
|
from .test_controller import (
|
|
|
|
CONTROLLER_DATA,
|
|
|
|
DEFAULT_CONFIG_ENTRY_ID,
|
|
|
|
ENTRY_CONFIG,
|
|
|
|
setup_unifi_integration,
|
|
|
|
)
|
2020-01-31 20:23:25 +01:00
|
|
|
|
2021-11-18 15:56:22 -08:00
|
|
|
from tests.common import MockConfigEntry, flush_store
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_with_no_config(hass):
|
2021-02-05 16:31:47 +01:00
|
|
|
"""Test that we do not discover anything or try to set up a controller."""
|
2020-04-23 16:48:24 +02:00
|
|
|
assert await async_setup_component(hass, UNIFI_DOMAIN, {}) is True
|
|
|
|
assert UNIFI_DOMAIN not in hass.data
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
|
2021-02-05 16:31:47 +01:00
|
|
|
async def test_successful_config_entry(hass, aioclient_mock):
|
2018-10-16 10:35:35 +02:00
|
|
|
"""Test that configured options for a host are loaded via config entry."""
|
2021-03-05 21:28:41 +01:00
|
|
|
await setup_unifi_integration(hass, aioclient_mock, unique_id=None)
|
2020-04-23 16:48:24 +02:00
|
|
|
assert hass.data[UNIFI_DOMAIN]
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
|
2022-07-18 17:39:38 +02:00
|
|
|
async def test_setup_entry_fails_config_entry_not_ready(hass):
|
|
|
|
"""Failed authentication trigger a reauthentication flow."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.get_unifi_controller",
|
|
|
|
side_effect=CannotConnect,
|
|
|
|
):
|
2020-01-31 20:23:25 +01:00
|
|
|
await setup_unifi_integration(hass)
|
2018-10-16 10:35:35 +02:00
|
|
|
|
2020-04-23 16:48:24 +02:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
|
2022-07-18 17:39:38 +02:00
|
|
|
async def test_setup_entry_fails_trigger_reauth_flow(hass):
|
|
|
|
"""Failed authentication trigger a reauthentication flow."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.get_unifi_controller",
|
|
|
|
side_effect=AuthenticationRequired,
|
|
|
|
), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init:
|
|
|
|
await setup_unifi_integration(hass)
|
|
|
|
mock_flow_init.assert_called_once()
|
2018-10-16 10:35:35 +02:00
|
|
|
|
2022-07-18 17:39:38 +02:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2018-10-16 10:35:35 +02:00
|
|
|
|
|
|
|
|
2021-02-06 21:32:18 +01:00
|
|
|
async def test_flatten_entry_data(hass):
|
|
|
|
"""Verify entry data can be flattened."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=UNIFI_DOMAIN,
|
|
|
|
data={CONF_CONTROLLER: CONTROLLER_DATA},
|
|
|
|
)
|
|
|
|
await async_flatten_entry_data(hass, entry)
|
|
|
|
|
|
|
|
assert entry.data == ENTRY_CONFIG
|
|
|
|
|
|
|
|
|
2021-02-05 16:31:47 +01:00
|
|
|
async def test_unload_entry(hass, aioclient_mock):
|
2018-10-16 10:35:35 +02:00
|
|
|
"""Test being able to unload an entry."""
|
2021-02-05 16:31:47 +01:00
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
2020-04-23 16:48:24 +02:00
|
|
|
assert hass.data[UNIFI_DOMAIN]
|
2018-10-16 10:35:35 +02:00
|
|
|
|
2021-03-05 21:28:41 +01:00
|
|
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
2020-04-23 16:48:24 +02:00
|
|
|
assert not hass.data[UNIFI_DOMAIN]
|
2021-03-05 21:28:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_wireless_clients(hass, hass_storage, aioclient_mock):
|
|
|
|
"""Verify wireless clients class."""
|
|
|
|
hass_storage[unifi.STORAGE_KEY] = {
|
|
|
|
"version": unifi.STORAGE_VERSION,
|
|
|
|
"data": {
|
|
|
|
DEFAULT_CONFIG_ENTRY_ID: {
|
|
|
|
"wireless_devices": ["00:00:00:00:00:00", "00:00:00:00:00:01"]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client_1 = {
|
|
|
|
"hostname": "client_1",
|
|
|
|
"ip": "10.0.0.1",
|
|
|
|
"is_wired": False,
|
|
|
|
"mac": "00:00:00:00:00:01",
|
|
|
|
}
|
|
|
|
client_2 = {
|
|
|
|
"hostname": "client_2",
|
|
|
|
"ip": "10.0.0.2",
|
|
|
|
"is_wired": False,
|
|
|
|
"mac": "00:00:00:00:00:02",
|
|
|
|
}
|
|
|
|
config_entry = await setup_unifi_integration(
|
|
|
|
hass, aioclient_mock, clients_response=[client_1, client_2]
|
|
|
|
)
|
2021-11-18 15:56:22 -08:00
|
|
|
await flush_store(hass.data[unifi.UNIFI_WIRELESS_CLIENTS]._store)
|
2021-03-05 21:28:41 +01:00
|
|
|
|
|
|
|
for mac in [
|
|
|
|
"00:00:00:00:00:00",
|
|
|
|
"00:00:00:00:00:01",
|
|
|
|
"00:00:00:00:00:02",
|
|
|
|
]:
|
|
|
|
assert (
|
|
|
|
mac
|
|
|
|
in hass_storage[unifi.STORAGE_KEY]["data"][config_entry.entry_id][
|
|
|
|
"wireless_devices"
|
|
|
|
]
|
|
|
|
)
|