2018-10-16 08:35:35 +00:00
|
|
|
"""Test UniFi setup process."""
|
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
|
|
from homeassistant.components import unifi
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant.components.unifi import config_flow
|
2018-10-16 08:35:35 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2019-09-01 15:57:25 +00:00
|
|
|
from homeassistant.components.unifi.const import (
|
|
|
|
CONF_CONTROLLER,
|
|
|
|
CONF_SITE_ID,
|
|
|
|
CONTROLLER_ID as CONF_CONTROLLER_ID,
|
|
|
|
)
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
from tests.common import mock_coro, MockConfigEntry
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_with_no_config(hass):
|
|
|
|
"""Test that we do not discover anything or try to set up a bridge."""
|
|
|
|
assert await async_setup_component(hass, unifi.DOMAIN, {}) is True
|
|
|
|
assert unifi.DOMAIN not in hass.data
|
2019-07-14 19:57:09 +00:00
|
|
|
assert hass.data[unifi.UNIFI_CONFIG] == []
|
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_with_config(hass):
|
|
|
|
"""Test that we do not discover anything or try to set up a bridge."""
|
|
|
|
config = {
|
|
|
|
unifi.DOMAIN: {
|
|
|
|
unifi.CONF_CONTROLLERS: {
|
2019-07-31 19:25:30 +00:00
|
|
|
unifi.CONF_HOST: "1.2.3.4",
|
|
|
|
unifi.CONF_SITE_ID: "My site",
|
|
|
|
unifi.CONF_BLOCK_CLIENT: ["12:34:56:78:90:AB"],
|
2019-07-14 19:57:09 +00:00
|
|
|
unifi.CONF_DETECTION_TIME: 3,
|
2019-07-31 19:25:30 +00:00
|
|
|
unifi.CONF_SSID_FILTER: ["ssid"],
|
2019-07-14 19:57:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert await async_setup_component(hass, unifi.DOMAIN, config) is True
|
|
|
|
assert unifi.DOMAIN not in hass.data
|
2019-07-31 19:25:30 +00:00
|
|
|
assert hass.data[unifi.UNIFI_CONFIG] == [
|
|
|
|
{
|
|
|
|
unifi.CONF_HOST: "1.2.3.4",
|
|
|
|
unifi.CONF_SITE_ID: "My site",
|
|
|
|
unifi.CONF_BLOCK_CLIENT: ["12:34:56:78:90:AB"],
|
2019-08-21 20:22:42 +00:00
|
|
|
unifi.CONF_DETECTION_TIME: 3,
|
2019-07-31 19:25:30 +00:00
|
|
|
unifi.CONF_SSID_FILTER: ["ssid"],
|
|
|
|
}
|
|
|
|
]
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_successful_config_entry(hass):
|
|
|
|
"""Test that configured options for a host are loaded via config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN,
|
|
|
|
data={
|
|
|
|
"controller": {
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"username": "user",
|
|
|
|
"password": "pass",
|
|
|
|
"port": 80,
|
|
|
|
"site": "default",
|
|
|
|
"verify_ssl": True,
|
|
|
|
},
|
|
|
|
"poe_control": True,
|
2018-10-16 08:35:35 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_registry = Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
|
|
|
"homeassistant.helpers.device_registry.async_get_registry",
|
|
|
|
return_value=mock_coro(mock_registry),
|
|
|
|
):
|
2018-10-16 08:35:35 +00:00
|
|
|
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_controller.return_value.mac = "00:11:22:33:44:55"
|
2018-10-16 08:35:35 +00:00
|
|
|
assert await unifi.async_setup_entry(hass, entry) is True
|
|
|
|
|
|
|
|
assert len(mock_controller.mock_calls) == 2
|
|
|
|
p_hass, p_entry = mock_controller.mock_calls[0][1]
|
|
|
|
|
|
|
|
assert p_hass is hass
|
|
|
|
assert p_entry is entry
|
|
|
|
|
|
|
|
assert len(mock_registry.mock_calls) == 1
|
|
|
|
assert mock_registry.mock_calls[0][2] == {
|
2019-07-31 19:25:30 +00:00
|
|
|
"config_entry_id": entry.entry_id,
|
|
|
|
"connections": {("mac", "00:11:22:33:44:55")},
|
|
|
|
"manufacturer": unifi.ATTR_MANUFACTURER,
|
|
|
|
"model": "UniFi Controller",
|
|
|
|
"name": "UniFi Controller",
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_fail_setup(hass):
|
2018-10-29 18:09:54 +00:00
|
|
|
"""Test that a failed setup still stores controller."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN,
|
|
|
|
data={
|
|
|
|
"controller": {
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"username": "user",
|
|
|
|
"password": "pass",
|
|
|
|
"port": 80,
|
|
|
|
"site": "default",
|
|
|
|
"verify_ssl": True,
|
|
|
|
},
|
|
|
|
"poe_control": True,
|
2018-10-16 08:35:35 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(unifi, "UniFiController") as mock_cntrlr:
|
2018-10-16 08:35:35 +00:00
|
|
|
mock_cntrlr.return_value.async_setup.return_value = mock_coro(False)
|
|
|
|
assert await unifi.async_setup_entry(hass, entry) is False
|
|
|
|
|
2019-09-01 15:57:25 +00:00
|
|
|
controller_id = CONF_CONTROLLER_ID.format(host="0.0.0.0", site="default")
|
2018-10-29 18:09:54 +00:00
|
|
|
assert controller_id in hass.data[unifi.DOMAIN]
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_no_mac(hass):
|
|
|
|
"""Test that configured options for a host are loaded via config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN,
|
|
|
|
data={
|
|
|
|
"controller": {
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"username": "user",
|
|
|
|
"password": "pass",
|
|
|
|
"port": 80,
|
|
|
|
"site": "default",
|
|
|
|
"verify_ssl": True,
|
|
|
|
},
|
|
|
|
"poe_control": True,
|
2018-10-16 08:35:35 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
mock_registry = Mock()
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
|
|
|
"homeassistant.helpers.device_registry.async_get_registry",
|
|
|
|
return_value=mock_coro(mock_registry),
|
|
|
|
):
|
2018-10-16 08:35:35 +00:00
|
|
|
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
|
|
|
mock_controller.return_value.mac = None
|
|
|
|
assert await unifi.async_setup_entry(hass, entry) is True
|
|
|
|
|
|
|
|
assert len(mock_controller.mock_calls) == 2
|
|
|
|
|
|
|
|
assert len(mock_registry.mock_calls) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_entry(hass):
|
|
|
|
"""Test being able to unload an entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN,
|
|
|
|
data={
|
|
|
|
"controller": {
|
|
|
|
"host": "0.0.0.0",
|
|
|
|
"username": "user",
|
|
|
|
"password": "pass",
|
|
|
|
"port": 80,
|
|
|
|
"site": "default",
|
|
|
|
"verify_ssl": True,
|
|
|
|
},
|
|
|
|
"poe_control": True,
|
2018-10-16 08:35:35 +00:00
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(unifi, "UniFiController") as mock_controller, patch(
|
|
|
|
"homeassistant.helpers.device_registry.async_get_registry",
|
|
|
|
return_value=mock_coro(Mock()),
|
|
|
|
):
|
2018-10-16 08:35:35 +00:00
|
|
|
mock_controller.return_value.async_setup.return_value = mock_coro(True)
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_controller.return_value.mac = "00:11:22:33:44:55"
|
2018-10-16 08:35:35 +00:00
|
|
|
assert await unifi.async_setup_entry(hass, entry) is True
|
|
|
|
|
|
|
|
assert len(mock_controller.return_value.mock_calls) == 1
|
|
|
|
|
|
|
|
mock_controller.return_value.async_reset.return_value = mock_coro(True)
|
|
|
|
assert await unifi.async_unload_entry(hass, entry)
|
|
|
|
assert len(mock_controller.return_value.async_reset.mock_calls) == 1
|
|
|
|
assert hass.data[unifi.DOMAIN] == {}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_works(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch("aiounifi.Controller") as mock_controller:
|
|
|
|
|
2019-06-02 16:24:13 +00:00
|
|
|
def mock_constructor(
|
2019-07-31 19:25:30 +00:00
|
|
|
host, username, password, port, site, websession, sslcontext
|
|
|
|
):
|
2018-10-16 08:35:35 +00:00
|
|
|
"""Fake the controller constructor."""
|
|
|
|
mock_controller.host = host
|
|
|
|
mock_controller.username = username
|
|
|
|
mock_controller.password = password
|
|
|
|
mock_controller.port = port
|
|
|
|
mock_controller.site = site
|
|
|
|
return mock_controller
|
|
|
|
|
|
|
|
mock_controller.side_effect = mock_constructor
|
|
|
|
mock_controller.login.return_value = mock_coro()
|
2019-07-31 19:25:30 +00:00
|
|
|
mock_controller.sites.return_value = mock_coro(
|
|
|
|
{"site1": {"name": "default", "role": "admin", "desc": "site name"}}
|
|
|
|
)
|
|
|
|
|
|
|
|
await flow.async_step_user(
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
}
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
result = await flow.async_step_site(user_input={})
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert mock_controller.host == "1.2.3.4"
|
2018-10-16 08:35:35 +00:00
|
|
|
assert len(mock_controller.login.mock_calls) == 1
|
|
|
|
assert len(mock_controller.sites.mock_calls) == 1
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "create_entry"
|
|
|
|
assert result["title"] == "site name"
|
|
|
|
assert result["data"] == {
|
2019-05-13 08:16:55 +00:00
|
|
|
CONF_CONTROLLER: {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
2019-05-13 08:16:55 +00:00
|
|
|
CONF_PORT: 1234,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SITE_ID: "default",
|
|
|
|
CONF_VERIFY_SSL: True,
|
2019-06-15 15:38:22 +00:00
|
|
|
}
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_multiple_sites(hass):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
|
|
|
flow.config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
|
|
|
flow.sites = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"site1": {"name": "default", "role": "admin", "desc": "site name"},
|
|
|
|
"site2": {"name": "site2", "role": "admin", "desc": "site2 name"},
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = await flow.async_step_site()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["step_id"] == "site"
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["data_schema"]({"site": "site name"})
|
|
|
|
assert result["data_schema"]({"site": "site2 name"})
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_site_already_configured(hass):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN, data={"controller": {"host": "1.2.3.4", "site": "default"}}
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
flow.config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
flow.desc = "site name"
|
|
|
|
flow.sites = {"site1": {"name": "default", "role": "admin", "desc": "site name"}}
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
result = await flow.async_step_site()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "abort"
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_user_credentials_faulty(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(
|
|
|
|
config_flow, "get_controller", side_effect=unifi.errors.AuthenticationRequired
|
|
|
|
):
|
|
|
|
result = await flow.async_step_user(
|
|
|
|
{
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_SITE_ID: "default",
|
|
|
|
}
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {"base": "faulty_credentials"}
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_is_unavailable(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(
|
|
|
|
config_flow, "get_controller", side_effect=unifi.errors.CannotConnect
|
|
|
|
):
|
|
|
|
result = await flow.async_step_user(
|
|
|
|
{
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_SITE_ID: "default",
|
|
|
|
}
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "form"
|
|
|
|
assert result["errors"] == {"base": "service_unavailable"}
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_controller_unkown_problem(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
2019-05-13 08:16:55 +00:00
|
|
|
flow = config_flow.UnifiFlowHandler()
|
2018-10-16 08:35:35 +00:00
|
|
|
flow.hass = hass
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
with patch.object(config_flow, "get_controller", side_effect=Exception):
|
|
|
|
result = await flow.async_step_user(
|
|
|
|
{
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_SITE_ID: "default",
|
|
|
|
}
|
|
|
|
)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
assert result["type"] == "abort"
|