2019-10-08 21:44:33 +00:00
|
|
|
"""Test UniFi config flow."""
|
2019-12-09 11:19:34 +00:00
|
|
|
import aiounifi
|
2019-10-08 21:44:33 +00:00
|
|
|
from asynctest import patch
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
from homeassistant import data_entry_flow
|
2019-10-08 21:44:33 +00:00
|
|
|
from homeassistant.components import unifi
|
|
|
|
from homeassistant.components.unifi import config_flow
|
2020-03-05 05:55:56 +00:00
|
|
|
from homeassistant.components.unifi.config_flow import CONF_NEW_CLIENT
|
|
|
|
from homeassistant.components.unifi.const import (
|
|
|
|
CONF_ALLOW_BANDWIDTH_SENSORS,
|
|
|
|
CONF_BLOCK_CLIENT,
|
|
|
|
CONF_CONTROLLER,
|
|
|
|
CONF_DETECTION_TIME,
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_IGNORE_WIRED_BUG,
|
2020-04-02 15:53:33 +00:00
|
|
|
CONF_POE_CLIENTS,
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_SITE_ID,
|
|
|
|
CONF_SSID_FILTER,
|
|
|
|
CONF_TRACK_CLIENTS,
|
|
|
|
CONF_TRACK_DEVICES,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS,
|
|
|
|
)
|
2019-10-08 21:44:33 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
from .test_controller import setup_unifi_integration
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
CLIENTS = [{"mac": "00:00:00:00:00:01"}]
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
WLANS = [{"name": "SSID 1"}, {"name": "SSID 2"}]
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2020-01-30 22:06:43 +00:00
|
|
|
async def test_flow_works(hass, aioclient_mock, mock_discovery):
|
2019-10-08 21:44:33 +00:00
|
|
|
"""Test config flow."""
|
2020-01-30 22:06:43 +00:00
|
|
|
mock_discovery.return_value = "1"
|
2019-10-08 21:44:33 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
2020-01-30 22:06:43 +00:00
|
|
|
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
|
|
|
|
CONF_HOST: "unifi",
|
|
|
|
CONF_USERNAME: "",
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_PORT: 8443,
|
|
|
|
CONF_VERIFY_SSL: False,
|
|
|
|
}
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
aioclient_mock.post(
|
|
|
|
"https://1.2.3.4:1234/api/login",
|
|
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
|
|
json={
|
|
|
|
"data": [{"desc": "Site name", "name": "site_id", "role": "admin"}],
|
|
|
|
"meta": {"rc": "ok"},
|
|
|
|
},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["title"] == "Site name"
|
|
|
|
assert result["data"] == {
|
|
|
|
CONF_CONTROLLER: {
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_SITE_ID: "site_id",
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_works_multiple_sites(hass, aioclient_mock):
|
|
|
|
"""Test config flow works when finding multiple sites."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
aioclient_mock.post(
|
|
|
|
"https://1.2.3.4:1234/api/login",
|
|
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
|
|
json={
|
|
|
|
"data": [
|
|
|
|
{"name": "default", "role": "admin", "desc": "site name"},
|
|
|
|
{"name": "site2", "role": "admin", "desc": "site2 name"},
|
|
|
|
],
|
|
|
|
"meta": {"rc": "ok"},
|
|
|
|
},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "site"
|
|
|
|
assert result["data_schema"]({"site": "site name"})
|
|
|
|
assert result["data_schema"]({"site": "site2 name"})
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_fails_site_already_configured(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=unifi.DOMAIN, data={"controller": {"host": "1.2.3.4", "site": "site_id"}}
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
aioclient_mock.post(
|
|
|
|
"https://1.2.3.4:1234/api/login",
|
|
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
"https://1.2.3.4:1234/api/self/sites",
|
|
|
|
json={
|
|
|
|
"data": [{"desc": "Site name", "name": "site_id", "role": "admin"}],
|
|
|
|
"meta": {"rc": "ok"},
|
|
|
|
},
|
|
|
|
headers={"content-type": "application/json"},
|
|
|
|
)
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2019-10-08 21:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.Unauthorized):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["errors"] == {"base": "faulty_credentials"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_fails_controller_unavailable(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["errors"] == {"base": "service_unavailable"}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_flow_fails_unknown_problem(hass, aioclient_mock):
|
|
|
|
"""Test config flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
config_flow.DOMAIN, context={"source": "user"}
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2020-04-08 21:19:39 +00:00
|
|
|
aioclient_mock.get("https://1.2.3.4:1234", status=302)
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
with patch("aiounifi.Controller.login", side_effect=Exception):
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
|
2019-10-08 21:44:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_option_flow(hass):
|
|
|
|
"""Test config flow options."""
|
2020-03-05 05:55:56 +00:00
|
|
|
controller = await setup_unifi_integration(
|
|
|
|
hass, clients_response=CLIENTS, wlans_response=WLANS
|
|
|
|
)
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
result = await hass.config_entries.options.async_init(
|
|
|
|
controller.config_entry.entry_id
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "device_tracker"
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
2019-10-08 21:44:33 +00:00
|
|
|
user_input={
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_TRACK_CLIENTS: False,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS: False,
|
|
|
|
CONF_TRACK_DEVICES: False,
|
|
|
|
CONF_SSID_FILTER: ["SSID 1"],
|
|
|
|
CONF_DETECTION_TIME: 100,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "client_control"
|
|
|
|
|
|
|
|
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
|
|
|
|
CONF_BLOCK_CLIENT
|
|
|
|
]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_BLOCK_CLIENT: clients_to_block,
|
|
|
|
CONF_NEW_CLIENT: "00:00:00:00:00:01",
|
2020-04-02 15:53:33 +00:00
|
|
|
CONF_POE_CLIENTS: False,
|
2020-02-18 22:24:21 +00:00
|
|
|
},
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
2020-02-18 22:24:21 +00:00
|
|
|
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "client_control"
|
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_BLOCK_CLIENT: clients_to_block,
|
|
|
|
CONF_NEW_CLIENT: "00:00:00:00:00:02",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
|
|
|
assert result["step_id"] == "client_control"
|
|
|
|
assert result["errors"] == {"base": "unknown_client_mac"}
|
|
|
|
|
|
|
|
clients_to_block = hass.config_entries.options._progress[result["flow_id"]].options[
|
|
|
|
CONF_BLOCK_CLIENT
|
|
|
|
]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"], user_input={CONF_BLOCK_CLIENT: clients_to_block},
|
|
|
|
)
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "statistics_sensors"
|
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
2020-03-05 05:55:56 +00:00
|
|
|
result["flow_id"], user_input={CONF_ALLOW_BANDWIDTH_SENSORS: True}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
2020-02-18 22:24:21 +00:00
|
|
|
|
|
|
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["data"] == {
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_TRACK_CLIENTS: False,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS: False,
|
|
|
|
CONF_TRACK_DEVICES: False,
|
|
|
|
CONF_SSID_FILTER: ["SSID 1"],
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_DETECTION_TIME: 100,
|
|
|
|
CONF_IGNORE_WIRED_BUG: False,
|
2020-04-02 15:53:33 +00:00
|
|
|
CONF_POE_CLIENTS: False,
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_BLOCK_CLIENT: ["00:00:00:00:00:01"],
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_ALLOW_BANDWIDTH_SENSORS: True,
|
2019-10-08 21:44:33 +00:00
|
|
|
}
|