2021-11-26 21:44:49 +00:00
|
|
|
"""Test UniFi Network config flow."""
|
2024-03-08 18:16:21 +00:00
|
|
|
|
2021-03-05 20:28:41 +00:00
|
|
|
import socket
|
2024-05-31 22:27:53 +00:00
|
|
|
from unittest.mock import PropertyMock, patch
|
2021-01-01 21:31:56 +00:00
|
|
|
|
2019-12-09 11:19:34 +00:00
|
|
|
import aiounifi
|
2024-05-31 22:27:53 +00:00
|
|
|
import pytest
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
from homeassistant import config_entries
|
2021-11-25 13:35:19 +00:00
|
|
|
from homeassistant.components import ssdp
|
2022-05-24 19:42:11 +00:00
|
|
|
from homeassistant.components.unifi.config_flow import _async_discover_unifi
|
2020-03-05 05:55:56 +00:00
|
|
|
from homeassistant.components.unifi.const import (
|
|
|
|
CONF_ALLOW_BANDWIDTH_SENSORS,
|
2020-09-18 17:33:37 +00:00
|
|
|
CONF_ALLOW_UPTIME_SENSORS,
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_BLOCK_CLIENT,
|
2023-10-22 21:39:54 +00:00
|
|
|
CONF_CLIENT_SOURCE,
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_DETECTION_TIME,
|
2020-11-03 07:36:37 +00:00
|
|
|
CONF_DPI_RESTRICTIONS,
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_IGNORE_WIRED_BUG,
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_SITE_ID,
|
|
|
|
CONF_SSID_FILTER,
|
|
|
|
CONF_TRACK_CLIENTS,
|
|
|
|
CONF_TRACK_DEVICES,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS,
|
2020-04-23 14:48:24 +00:00
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
2020-03-05 05:55:56 +00:00
|
|
|
)
|
2019-10-08 21:44:33 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_VERIFY_SSL,
|
|
|
|
)
|
2023-02-08 18:10:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-04-02 21:09:56 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResultType
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2024-07-30 17:33:25 +00:00
|
|
|
from .conftest import ConfigEntryFactoryType
|
|
|
|
|
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-05-06 22:35:04 +00:00
|
|
|
DEVICES = [
|
|
|
|
{
|
|
|
|
"board_rev": 21,
|
|
|
|
"device_id": "mock-id",
|
|
|
|
"ip": "10.0.1.1",
|
|
|
|
"last_seen": 0,
|
|
|
|
"mac": "00:00:00:00:01:01",
|
|
|
|
"model": "U7PG2",
|
|
|
|
"name": "access_point",
|
|
|
|
"state": 1,
|
|
|
|
"type": "uap",
|
|
|
|
"version": "4.0.80.10875",
|
|
|
|
"wlan_overrides": [
|
|
|
|
{
|
|
|
|
"name": "SSID 3",
|
|
|
|
"radio": "na",
|
|
|
|
"radio_name": "wifi1",
|
|
|
|
"wlan_id": "012345678910111213141516",
|
|
|
|
},
|
2020-05-08 16:51:10 +00:00
|
|
|
{
|
|
|
|
"name": "",
|
|
|
|
"radio": "na",
|
|
|
|
"radio_name": "wifi1",
|
|
|
|
"wlan_id": "012345678910111213141516",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"radio": "na",
|
|
|
|
"radio_name": "wifi1",
|
|
|
|
"wlan_id": "012345678910111213141516",
|
|
|
|
},
|
2020-05-06 22:35:04 +00:00
|
|
|
],
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2020-05-02 21:16:18 +00:00
|
|
|
WLANS = [
|
2024-03-09 08:18:23 +00:00
|
|
|
{"_id": "1", "name": "SSID 1", "enabled": True},
|
2023-07-25 08:11:48 +00:00
|
|
|
{
|
|
|
|
"_id": "2",
|
|
|
|
"name": "SSID 2",
|
|
|
|
"name_combine_enabled": False,
|
|
|
|
"name_combine_suffix": "_IOT",
|
2024-03-09 08:18:23 +00:00
|
|
|
"enabled": True,
|
2023-07-25 08:11:48 +00:00
|
|
|
},
|
2024-03-09 08:18:23 +00:00
|
|
|
{"_id": "3", "name": "SSID 4", "name_combine_enabled": False, "enabled": True},
|
2020-05-02 21:16:18 +00:00
|
|
|
]
|
2020-02-18 22:24:21 +00:00
|
|
|
|
2020-11-03 07:36:37 +00:00
|
|
|
DPI_GROUPS = [
|
|
|
|
{
|
|
|
|
"_id": "5ba29dd8e3c58f026e9d7c4a",
|
|
|
|
"name": "Default",
|
|
|
|
"site_id": "5ba29dd4e3c58f026e9d7c38",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("mock_default_requests")
|
|
|
|
async def test_flow_works(hass: HomeAssistant, mock_discovery) -> None:
|
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(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.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: "",
|
2021-01-29 17:14:39 +00:00
|
|
|
CONF_PORT: 443,
|
2020-01-30 22:06:43 +00:00
|
|
|
CONF_VERIFY_SSL: False,
|
|
|
|
}
|
2019-10-08 21:44:33 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["title"] == "Site name"
|
|
|
|
assert result["data"] == {
|
2021-02-06 20:32:18 +00:00
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_SITE_ID: "site_id",
|
|
|
|
CONF_VERIFY_SSL: True,
|
2019-10-08 21:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-26 07:48:12 +00:00
|
|
|
async def test_flow_works_negative_discovery(hass: HomeAssistant) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Test config flow with a negative outcome of async_discovery_unifi."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2021-03-05 20:28:41 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-03-05 20:28:41 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
|
|
|
|
CONF_HOST: "",
|
|
|
|
CONF_USERNAME: "",
|
|
|
|
CONF_PASSWORD: "",
|
|
|
|
CONF_PORT: 443,
|
|
|
|
CONF_VERIFY_SSL: False,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-31 22:27:53 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"site_payload",
|
|
|
|
[
|
|
|
|
[
|
|
|
|
{"name": "default", "role": "admin", "desc": "site name", "_id": "1"},
|
|
|
|
{"name": "site2", "role": "admin", "desc": "site2 name", "_id": "2"},
|
|
|
|
]
|
|
|
|
],
|
|
|
|
)
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("mock_default_requests")
|
|
|
|
async def test_flow_multiple_sites(hass: HomeAssistant) -> None:
|
2019-10-08 21:44:33 +00:00
|
|
|
"""Test config flow works when finding multiple sites."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "site"
|
2021-02-05 18:38:08 +00:00
|
|
|
assert result["data_schema"]({"site": "1"})
|
|
|
|
assert result["data_schema"]({"site": "2"})
|
2019-10-08 21:44:33 +00:00
|
|
|
|
|
|
|
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("config_entry_setup")
|
|
|
|
async def test_flow_raise_already_configured(hass: HomeAssistant) -> None:
|
2021-01-29 17:14:39 +00:00
|
|
|
"""Test config flow aborts since a connected config entry already exists."""
|
2019-10-08 21:44:33 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2020-05-02 21:16:45 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("config_entry_setup")
|
|
|
|
async def test_flow_aborts_configuration_updated(hass: HomeAssistant) -> None:
|
2021-01-29 17:14:39 +00:00
|
|
|
"""Test config flow aborts since a connected config entry already exists."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2021-01-29 17:14:39 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-01-29 17:14:39 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
2024-05-31 22:27:53 +00:00
|
|
|
with patch("homeassistant.components.unifi.async_setup_entry") and patch(
|
|
|
|
"homeassistant.components.unifi.UnifiHub.available", new_callable=PropertyMock
|
|
|
|
) as ws_mock:
|
|
|
|
ws_mock.return_value = False
|
2021-01-29 17:14:39 +00:00
|
|
|
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",
|
2024-05-31 22:27:53 +00:00
|
|
|
CONF_PORT: 12345,
|
2021-01-29 17:14:39 +00:00
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-01-29 17:14:39 +00:00
|
|
|
assert result["reason"] == "configuration_updated"
|
|
|
|
|
|
|
|
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("mock_default_requests")
|
|
|
|
async def test_flow_fails_user_credentials_faulty(hass: HomeAssistant) -> None:
|
2019-10-08 21:44:33 +00:00
|
|
|
"""Test config flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["errors"] == {"base": "faulty_credentials"}
|
|
|
|
|
|
|
|
|
2024-06-09 20:07:36 +00:00
|
|
|
@pytest.mark.usefixtures("mock_default_requests")
|
|
|
|
async def test_flow_fails_hub_unavailable(hass: HomeAssistant) -> None:
|
2019-10-08 21:44:33 +00:00
|
|
|
"""Test config flow."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
2021-04-25 09:27:40 +00:00
|
|
|
UNIFI_DOMAIN, context={"source": config_entries.SOURCE_USER}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["errors"] == {"base": "service_unavailable"}
|
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_reauth_flow_update_configuration(
|
2024-07-30 17:33:25 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2024-02-20 07:51:22 +00:00
|
|
|
"""Verify reauth flow can update hub configuration."""
|
2024-05-31 22:27:53 +00:00
|
|
|
config_entry = config_entry_setup
|
2021-01-20 21:10:40 +00:00
|
|
|
|
2024-08-28 14:51:16 +00:00
|
|
|
result = await config_entry.start_reauth_flow(hass)
|
2021-01-20 21:10:40 +00:00
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-04-11 08:00:17 +00:00
|
|
|
assert result["step_id"] == "user"
|
2021-01-20 21:10:40 +00:00
|
|
|
|
2024-05-31 22:27:53 +00:00
|
|
|
with patch(
|
|
|
|
"homeassistant.components.unifi.UnifiHub.available", new_callable=PropertyMock
|
|
|
|
) as ws_mock:
|
|
|
|
ws_mock.return_value = False
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "new_name",
|
|
|
|
CONF_PASSWORD: "new_pass",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
2021-01-20 21:10:40 +00:00
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-01-20 21:10:40 +00:00
|
|
|
assert result["reason"] == "reauth_successful"
|
2021-02-06 20:32:18 +00:00
|
|
|
assert config_entry.data[CONF_HOST] == "1.2.3.4"
|
|
|
|
assert config_entry.data[CONF_USERNAME] == "new_name"
|
|
|
|
assert config_entry.data[CONF_PASSWORD] == "new_pass"
|
2021-01-20 21:10:40 +00:00
|
|
|
|
|
|
|
|
2024-07-10 21:53:11 +00:00
|
|
|
async def test_reauth_flow_update_configuration_on_not_loaded_entry(
|
2024-07-30 17:33:25 +00:00
|
|
|
hass: HomeAssistant, config_entry_factory: ConfigEntryFactoryType
|
2024-07-10 21:53:11 +00:00
|
|
|
) -> None:
|
|
|
|
"""Verify reauth flow can update hub configuration on a not loaded entry."""
|
|
|
|
with patch("aiounifi.Controller.login", side_effect=aiounifi.errors.RequestError):
|
|
|
|
config_entry = await config_entry_factory()
|
|
|
|
|
2024-08-28 14:51:16 +00:00
|
|
|
result = await config_entry.start_reauth_flow(hass)
|
2024-07-10 21:53:11 +00:00
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.FORM
|
|
|
|
assert result["step_id"] == "user"
|
|
|
|
|
|
|
|
result = await hass.config_entries.flow.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_HOST: "1.2.3.4",
|
|
|
|
CONF_USERNAME: "new_name",
|
|
|
|
CONF_PASSWORD: "new_pass",
|
|
|
|
CONF_PORT: 1234,
|
|
|
|
CONF_VERIFY_SSL: True,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
assert result["type"] is FlowResultType.ABORT
|
|
|
|
assert result["reason"] == "reauth_successful"
|
|
|
|
assert config_entry.data[CONF_HOST] == "1.2.3.4"
|
|
|
|
assert config_entry.data[CONF_USERNAME] == "new_name"
|
|
|
|
assert config_entry.data[CONF_PASSWORD] == "new_pass"
|
|
|
|
|
|
|
|
|
2024-05-31 22:27:53 +00:00
|
|
|
@pytest.mark.parametrize("client_payload", [CLIENTS])
|
|
|
|
@pytest.mark.parametrize("device_payload", [DEVICES])
|
|
|
|
@pytest.mark.parametrize("wlan_payload", [WLANS])
|
|
|
|
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_advanced_option_flow(
|
2024-07-30 17:33:25 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2020-05-01 05:33:22 +00:00
|
|
|
"""Test advanced config flow options."""
|
2024-05-31 22:27:53 +00:00
|
|
|
config_entry = config_entry_setup
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
result = await hass.config_entries.options.async_init(
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry.entry_id, context={"show_advanced_options": True}
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2023-10-22 21:39:54 +00:00
|
|
|
assert result["step_id"] == "configure_entity_sources"
|
|
|
|
assert not result["last_step"]
|
|
|
|
assert list(result["data_schema"].schema[CONF_CLIENT_SOURCE].options.keys()) == [
|
|
|
|
"00:00:00:00:00:01"
|
|
|
|
]
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={CONF_CLIENT_SOURCE: ["00:00:00:00:00:01"]},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "device_tracker"
|
2021-05-04 06:54:45 +00:00
|
|
|
assert not result["last_step"]
|
2023-04-03 07:47:08 +00:00
|
|
|
assert list(result["data_schema"].schema[CONF_SSID_FILTER].options.keys()) == [
|
|
|
|
"",
|
|
|
|
"SSID 1",
|
|
|
|
"SSID 2",
|
|
|
|
"SSID 2_IOT",
|
|
|
|
"SSID 3",
|
|
|
|
"SSID 4",
|
|
|
|
]
|
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,
|
2023-04-03 07:47:08 +00:00
|
|
|
CONF_SSID_FILTER: ["SSID 1", "SSID 2_IOT", "SSID 3", "SSID 4"],
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_DETECTION_TIME: 100,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-03-05 05:55:56 +00:00
|
|
|
assert result["step_id"] == "client_control"
|
2021-05-04 06:54:45 +00:00
|
|
|
assert not result["last_step"]
|
2020-03-05 05:55:56 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
2020-11-03 07:36:37 +00:00
|
|
|
user_input={
|
|
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
|
|
CONF_DPI_RESTRICTIONS: False,
|
|
|
|
},
|
2020-03-05 05:55:56 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["step_id"] == "statistics_sensors"
|
2021-05-04 06:54:45 +00:00
|
|
|
assert result["last_step"]
|
2019-10-08 21:44:33 +00:00
|
|
|
|
2020-02-18 22:24:21 +00:00
|
|
|
result = await hass.config_entries.options.async_configure(
|
2020-09-18 17:33:37 +00:00
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_ALLOW_BANDWIDTH_SENSORS: True,
|
|
|
|
CONF_ALLOW_UPTIME_SENSORS: True,
|
|
|
|
},
|
2019-10-08 21:44:33 +00:00
|
|
|
)
|
2020-02-18 22:24:21 +00:00
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2019-10-08 21:44:33 +00:00
|
|
|
assert result["data"] == {
|
2023-10-22 21:39:54 +00:00
|
|
|
CONF_CLIENT_SOURCE: ["00:00:00:00:00:01"],
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_TRACK_CLIENTS: False,
|
|
|
|
CONF_TRACK_WIRED_CLIENTS: False,
|
|
|
|
CONF_TRACK_DEVICES: False,
|
2023-04-03 07:47:08 +00:00
|
|
|
CONF_SSID_FILTER: ["SSID 1", "SSID 2_IOT", "SSID 3", "SSID 4"],
|
2020-04-17 06:39:01 +00:00
|
|
|
CONF_DETECTION_TIME: 100,
|
|
|
|
CONF_IGNORE_WIRED_BUG: False,
|
2020-11-03 07:36:37 +00:00
|
|
|
CONF_DPI_RESTRICTIONS: False,
|
2020-04-22 03:45:48 +00:00
|
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
2020-03-05 05:55:56 +00:00
|
|
|
CONF_ALLOW_BANDWIDTH_SENSORS: True,
|
2020-09-18 17:33:37 +00:00
|
|
|
CONF_ALLOW_UPTIME_SENSORS: True,
|
2019-10-08 21:44:33 +00:00
|
|
|
}
|
2020-05-01 05:33:22 +00:00
|
|
|
|
|
|
|
|
2024-05-31 22:27:53 +00:00
|
|
|
@pytest.mark.parametrize("client_payload", [CLIENTS])
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_simple_option_flow(
|
2024-07-30 17:33:25 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: MockConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2020-05-01 05:33:22 +00:00
|
|
|
"""Test simple config flow options."""
|
2024-05-31 22:27:53 +00:00
|
|
|
config_entry = config_entry_setup
|
2020-05-01 05:33:22 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_init(
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry.entry_id, context={"show_advanced_options": False}
|
2020-05-01 05:33:22 +00:00
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2020-05-01 05:33:22 +00:00
|
|
|
assert result["step_id"] == "simple_options"
|
2021-05-04 06:54:45 +00:00
|
|
|
assert result["last_step"]
|
2020-05-01 05:33:22 +00:00
|
|
|
|
|
|
|
result = await hass.config_entries.options.async_configure(
|
|
|
|
result["flow_id"],
|
|
|
|
user_input={
|
|
|
|
CONF_TRACK_CLIENTS: False,
|
|
|
|
CONF_TRACK_DEVICES: False,
|
|
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-04-02 21:09:56 +00:00
|
|
|
assert result["type"] is FlowResultType.CREATE_ENTRY
|
2020-05-01 05:33:22 +00:00
|
|
|
assert result["data"] == {
|
|
|
|
CONF_TRACK_CLIENTS: False,
|
|
|
|
CONF_TRACK_DEVICES: False,
|
|
|
|
CONF_BLOCK_CLIENT: [CLIENTS[0]["mac"]],
|
|
|
|
}
|
2021-01-21 17:03:54 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_form_ssdp(hass: HomeAssistant) -> None:
|
2021-01-21 17:03:54 +00:00
|
|
|
"""Test we get the form with ssdp source."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
2021-11-25 13:35:19 +00:00
|
|
|
data=ssdp.SsdpServiceInfo(
|
|
|
|
ssdp_usn="mock_usn",
|
|
|
|
ssdp_st="mock_st",
|
|
|
|
ssdp_location="http://192.168.208.1:41417/rootDesc.xml",
|
|
|
|
upnp={
|
|
|
|
"friendlyName": "UniFi Dream Machine",
|
|
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
|
|
},
|
|
|
|
),
|
2021-01-21 17:03:54 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-01-21 17:03:54 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
2022-01-20 08:34:54 +00:00
|
|
|
|
|
|
|
flows = hass.config_entries.flow.async_progress()
|
|
|
|
assert len(flows) == 1
|
|
|
|
|
|
|
|
assert (
|
|
|
|
flows[0].get("context", {}).get("configuration_url")
|
|
|
|
== "https://192.168.208.1:443"
|
|
|
|
)
|
|
|
|
|
2021-01-21 17:03:54 +00:00
|
|
|
context = next(
|
|
|
|
flow["context"]
|
|
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
|
|
if flow["flow_id"] == result["flow_id"]
|
|
|
|
)
|
|
|
|
assert context["title_placeholders"] == {
|
|
|
|
"host": "192.168.208.1",
|
|
|
|
"site": "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-26 07:48:12 +00:00
|
|
|
@pytest.mark.usefixtures("config_entry")
|
|
|
|
async def test_form_ssdp_aborts_if_host_already_exists(hass: HomeAssistant) -> None:
|
2021-01-21 17:03:54 +00:00
|
|
|
"""Test we abort if the host is already configured."""
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
2021-11-25 13:35:19 +00:00
|
|
|
data=ssdp.SsdpServiceInfo(
|
|
|
|
ssdp_usn="mock_usn",
|
|
|
|
ssdp_st="mock_st",
|
2024-05-31 22:27:53 +00:00
|
|
|
ssdp_location="http://1.2.3.4:1234/rootDesc.xml",
|
2021-11-25 13:35:19 +00:00
|
|
|
upnp={
|
|
|
|
"friendlyName": "UniFi Dream Machine",
|
|
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
|
|
|
"serialNumber": "e0:63:da:20:14:a9",
|
|
|
|
},
|
|
|
|
),
|
2021-01-21 17:03:54 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-01-21 17:03:54 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2024-07-26 07:48:12 +00:00
|
|
|
@pytest.mark.usefixtures("config_entry")
|
|
|
|
async def test_form_ssdp_aborts_if_serial_already_exists(hass: HomeAssistant) -> None:
|
2021-01-21 17:03:54 +00:00
|
|
|
"""Test we abort if the serial is already configured."""
|
2021-10-07 10:58:00 +00:00
|
|
|
|
2021-01-21 17:03:54 +00:00
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
2021-11-25 13:35:19 +00:00
|
|
|
data=ssdp.SsdpServiceInfo(
|
|
|
|
ssdp_usn="mock_usn",
|
|
|
|
ssdp_st="mock_st",
|
2024-05-31 22:27:53 +00:00
|
|
|
ssdp_location="http://1.2.3.4:1234/rootDesc.xml",
|
2021-11-25 13:35:19 +00:00
|
|
|
upnp={
|
|
|
|
"friendlyName": "UniFi Dream Machine",
|
|
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
2024-05-31 22:27:53 +00:00
|
|
|
"serialNumber": "1",
|
2021-11-25 13:35:19 +00:00
|
|
|
},
|
|
|
|
),
|
2021-01-21 17:03:54 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.ABORT
|
2021-01-21 17:03:54 +00:00
|
|
|
assert result["reason"] == "already_configured"
|
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_form_ssdp_gets_form_with_ignored_entry(hass: HomeAssistant) -> None:
|
2024-05-31 22:27:53 +00:00
|
|
|
"""Test we can still setup if there is an ignored never configured entry."""
|
2021-01-21 17:03:54 +00:00
|
|
|
entry = MockConfigEntry(
|
|
|
|
domain=UNIFI_DOMAIN,
|
2021-01-30 15:45:46 +00:00
|
|
|
data={"not_controller_key": None},
|
2021-01-21 17:03:54 +00:00
|
|
|
source=config_entries.SOURCE_IGNORE,
|
|
|
|
)
|
|
|
|
entry.add_to_hass(hass)
|
|
|
|
result = await hass.config_entries.flow.async_init(
|
|
|
|
UNIFI_DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_SSDP},
|
2021-11-25 13:35:19 +00:00
|
|
|
data=ssdp.SsdpServiceInfo(
|
|
|
|
ssdp_usn="mock_usn",
|
|
|
|
ssdp_st="mock_st",
|
2024-05-31 22:27:53 +00:00
|
|
|
ssdp_location="http://1.2.3.4:1234/rootDesc.xml",
|
2021-11-25 13:35:19 +00:00
|
|
|
upnp={
|
|
|
|
"friendlyName": "UniFi Dream Machine New",
|
|
|
|
"modelDescription": "UniFi Dream Machine Pro",
|
2024-05-31 22:27:53 +00:00
|
|
|
"serialNumber": "1",
|
2021-11-25 13:35:19 +00:00
|
|
|
},
|
|
|
|
),
|
2021-01-21 17:03:54 +00:00
|
|
|
)
|
2024-04-03 07:53:20 +00:00
|
|
|
assert result["type"] is FlowResultType.FORM
|
2021-01-21 17:03:54 +00:00
|
|
|
assert result["step_id"] == "user"
|
|
|
|
assert result["errors"] == {}
|
|
|
|
context = next(
|
|
|
|
flow["context"]
|
|
|
|
for flow in hass.config_entries.flow.async_progress()
|
|
|
|
if flow["flow_id"] == result["flow_id"]
|
|
|
|
)
|
|
|
|
assert context["title_placeholders"] == {
|
|
|
|
"host": "1.2.3.4",
|
|
|
|
"site": "default",
|
|
|
|
}
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_discover_unifi_positive(hass: HomeAssistant) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify positive run of UniFi discovery."""
|
|
|
|
with patch("socket.gethostbyname", return_value=True):
|
2022-05-24 19:42:11 +00:00
|
|
|
assert await _async_discover_unifi(hass)
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_discover_unifi_negative(hass: HomeAssistant) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify negative run of UniFi discovery."""
|
|
|
|
with patch("socket.gethostbyname", side_effect=socket.gaierror):
|
2022-05-24 19:42:11 +00:00
|
|
|
assert await _async_discover_unifi(hass) is None
|