2021-11-26 21:44:49 +00:00
|
|
|
"""Test UniFi Network."""
|
2020-01-03 18:23:17 +00:00
|
|
|
from copy import deepcopy
|
2019-10-07 19:55:35 +00:00
|
|
|
from datetime import timedelta
|
2021-10-24 09:27:17 +00:00
|
|
|
from http import HTTPStatus
|
2021-03-05 20:28:41 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2019-12-09 11:19:34 +00:00
|
|
|
import aiounifi
|
2019-02-14 04:36:06 +00:00
|
|
|
import pytest
|
|
|
|
|
2023-08-05 18:07:20 +00:00
|
|
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as TRACKER_DOMAIN
|
2023-07-25 08:11:48 +00:00
|
|
|
from homeassistant.components.image import DOMAIN as IMAGE_DOMAIN
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
2019-07-14 19:57:09 +00:00
|
|
|
from homeassistant.components.unifi.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SITE_ID,
|
2021-03-05 20:28:41 +00:00
|
|
|
CONF_TRACK_CLIENTS,
|
|
|
|
CONF_TRACK_DEVICES,
|
2020-04-23 14:48:24 +00:00
|
|
|
DEFAULT_ALLOW_BANDWIDTH_SENSORS,
|
2020-09-18 17:33:37 +00:00
|
|
|
DEFAULT_ALLOW_UPTIME_SENSORS,
|
2020-04-23 14:48:24 +00:00
|
|
|
DEFAULT_DETECTION_TIME,
|
|
|
|
DEFAULT_TRACK_CLIENTS,
|
|
|
|
DEFAULT_TRACK_DEVICES,
|
|
|
|
DEFAULT_TRACK_WIRED_CLIENTS,
|
|
|
|
DOMAIN as UNIFI_DOMAIN,
|
2021-03-05 20:28:41 +00:00
|
|
|
PLATFORMS,
|
2022-09-25 18:08:56 +00:00
|
|
|
UNIFI_WIRELESS_CLIENTS,
|
2021-03-05 20:28:41 +00:00
|
|
|
)
|
2020-04-23 14:48:24 +00:00
|
|
|
from homeassistant.components.unifi.errors import AuthenticationRequired, CannotConnect
|
2024-02-20 07:51:22 +00:00
|
|
|
from homeassistant.components.unifi.hub import get_unifi_api
|
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,
|
2021-02-05 15:31:47 +00:00
|
|
|
CONTENT_TYPE_JSON,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2023-02-08 18:10:53 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-22 18:04:25 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2021-03-05 20:28:41 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2020-01-03 18:23:17 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-03-05 20:28:41 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2020-01-03 18:23:17 +00:00
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
from tests.common import MockConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2021-11-18 23:56:22 +00:00
|
|
|
DEFAULT_CONFIG_ENTRY_ID = "1"
|
2021-02-05 15:31:47 +00:00
|
|
|
DEFAULT_HOST = "1.2.3.4"
|
|
|
|
DEFAULT_SITE = "site_id"
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
CONTROLLER_HOST = {
|
|
|
|
"hostname": "controller_host",
|
2021-02-05 15:31:47 +00:00
|
|
|
"ip": DEFAULT_HOST,
|
2019-10-07 19:55:35 +00:00
|
|
|
"is_wired": True,
|
|
|
|
"last_seen": 1562600145,
|
|
|
|
"mac": "10:00:00:00:00:01",
|
|
|
|
"name": "Controller host",
|
|
|
|
"oui": "Producer",
|
|
|
|
"sw_mac": "00:00:00:00:01:01",
|
|
|
|
"sw_port": 1,
|
|
|
|
"wired-rx_bytes": 1234000000,
|
|
|
|
"wired-tx_bytes": 5678000000,
|
2020-09-18 17:33:37 +00:00
|
|
|
"uptime": 1562600160,
|
2019-10-07 19:55:35 +00:00
|
|
|
}
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2023-01-01 19:03:14 +00:00
|
|
|
ENTRY_CONFIG = {
|
2021-02-05 15:31:47 +00:00
|
|
|
CONF_HOST: DEFAULT_HOST,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_USERNAME: "username",
|
|
|
|
CONF_PASSWORD: "password",
|
2019-05-13 08:16:55 +00:00
|
|
|
CONF_PORT: 1234,
|
2021-02-05 15:31:47 +00:00
|
|
|
CONF_SITE_ID: DEFAULT_SITE,
|
2019-10-07 19:55:35 +00:00
|
|
|
CONF_VERIFY_SSL: False,
|
2018-10-16 08:35:35 +00:00
|
|
|
}
|
2020-01-03 18:23:17 +00:00
|
|
|
ENTRY_OPTIONS = {}
|
|
|
|
|
|
|
|
CONFIGURATION = []
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-05 18:38:08 +00:00
|
|
|
SITE = [{"desc": "Site name", "name": "site_id", "role": "admin", "_id": "1"}]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2023-08-27 15:07:38 +00:00
|
|
|
SYSTEM_INFORMATION = [
|
|
|
|
{
|
|
|
|
"anonymous_controller_id": "24f81231-a456-4c32-abcd-f5612345385f",
|
|
|
|
"build": "atag_7.4.162_21057",
|
|
|
|
"console_display_version": "3.1.15",
|
|
|
|
"hostname": "UDMP",
|
|
|
|
"name": "UDMP",
|
|
|
|
"previous_version": "7.4.156",
|
|
|
|
"timezone": "Europe/Stockholm",
|
|
|
|
"ubnt_device_type": "UDMPRO",
|
|
|
|
"udm_version": "3.0.20.9281",
|
|
|
|
"update_available": False,
|
|
|
|
"update_downloaded": False,
|
|
|
|
"uptime": 1196290,
|
|
|
|
"version": "7.4.162",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
def mock_default_unifi_requests(
|
|
|
|
aioclient_mock,
|
|
|
|
host,
|
|
|
|
site_id,
|
|
|
|
sites=None,
|
|
|
|
clients_response=None,
|
|
|
|
clients_all_response=None,
|
|
|
|
devices_response=None,
|
|
|
|
dpiapp_response=None,
|
|
|
|
dpigroup_response=None,
|
2023-08-18 20:44:59 +00:00
|
|
|
port_forward_response=None,
|
|
|
|
system_information_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
wlans_response=None,
|
|
|
|
):
|
|
|
|
"""Mock default UniFi requests responses."""
|
|
|
|
aioclient_mock.get(f"https://{host}:1234", status=302) # Check UniFi OS
|
|
|
|
|
|
|
|
aioclient_mock.post(
|
|
|
|
f"https://{host}:1234/api/login",
|
|
|
|
json={"data": "login successful", "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/self/sites",
|
|
|
|
json={"data": sites or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/stat/sta",
|
|
|
|
json={"data": clients_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/user",
|
|
|
|
json={"data": clients_all_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/stat/device",
|
|
|
|
json={"data": devices_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/dpiapp",
|
|
|
|
json={"data": dpiapp_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/dpigroup",
|
|
|
|
json={"data": dpigroup_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
2023-08-18 20:44:59 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/portforward",
|
|
|
|
json={"data": port_forward_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/stat/sysinfo",
|
|
|
|
json={"data": system_information_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
2021-02-05 15:31:47 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/api/s/{site_id}/rest/wlanconf",
|
|
|
|
json={"data": wlans_response or [], "meta": {"rc": "ok"}},
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
2023-11-22 06:02:13 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/v2/api/site/{site_id}/trafficroutes",
|
|
|
|
json=[{}],
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
2023-10-25 01:27:42 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{host}:1234/v2/api/site/{site_id}/trafficrules",
|
|
|
|
json=[{}],
|
|
|
|
headers={"content-type": CONTENT_TYPE_JSON},
|
|
|
|
)
|
2021-02-05 15:31:47 +00:00
|
|
|
|
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
async def setup_unifi_integration(
|
|
|
|
hass,
|
2021-02-05 15:31:47 +00:00
|
|
|
aioclient_mock=None,
|
|
|
|
*,
|
2020-01-03 18:23:17 +00:00
|
|
|
config=ENTRY_CONFIG,
|
|
|
|
options=ENTRY_OPTIONS,
|
2021-02-05 15:31:47 +00:00
|
|
|
sites=SITE,
|
2020-01-03 18:23:17 +00:00
|
|
|
clients_response=None,
|
|
|
|
clients_all_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
devices_response=None,
|
2020-11-03 07:36:37 +00:00
|
|
|
dpiapp_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
dpigroup_response=None,
|
2023-08-18 20:44:59 +00:00
|
|
|
port_forward_response=None,
|
|
|
|
system_information_response=None,
|
2021-02-05 15:31:47 +00:00
|
|
|
wlans_response=None,
|
2019-12-30 18:40:52 +00:00
|
|
|
known_wireless_clients=None,
|
2021-03-05 20:28:41 +00:00
|
|
|
unique_id="1",
|
2021-10-01 13:59:29 +00:00
|
|
|
config_entry_id=DEFAULT_CONFIG_ENTRY_ID,
|
2019-10-07 19:55:35 +00:00
|
|
|
):
|
2021-11-26 21:44:49 +00:00
|
|
|
"""Create the UniFi Network instance."""
|
2020-04-23 14:48:24 +00:00
|
|
|
assert await async_setup_component(hass, UNIFI_DOMAIN, {})
|
2020-01-03 18:23:17 +00:00
|
|
|
|
|
|
|
config_entry = MockConfigEntry(
|
2020-04-23 14:48:24 +00:00
|
|
|
domain=UNIFI_DOMAIN,
|
2020-01-03 18:23:17 +00:00
|
|
|
data=deepcopy(config),
|
|
|
|
options=deepcopy(options),
|
2021-03-05 20:28:41 +00:00
|
|
|
unique_id=unique_id,
|
2021-10-01 13:59:29 +00:00
|
|
|
entry_id=config_entry_id,
|
2021-02-06 20:32:18 +00:00
|
|
|
version=1,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-03 18:23:17 +00:00
|
|
|
config_entry.add_to_hass(hass)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-12-30 18:40:52 +00:00
|
|
|
if known_wireless_clients:
|
2023-03-24 09:06:09 +00:00
|
|
|
hass.data[UNIFI_WIRELESS_CLIENTS].wireless_clients.update(
|
|
|
|
known_wireless_clients
|
2019-12-30 18:40:52 +00:00
|
|
|
)
|
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
if aioclient_mock:
|
|
|
|
mock_default_unifi_requests(
|
|
|
|
aioclient_mock,
|
2021-02-06 20:32:18 +00:00
|
|
|
host=config_entry.data[CONF_HOST],
|
|
|
|
site_id=config_entry.data[CONF_SITE_ID],
|
2021-02-05 15:31:47 +00:00
|
|
|
sites=sites,
|
|
|
|
clients_response=clients_response,
|
|
|
|
clients_all_response=clients_all_response,
|
|
|
|
devices_response=devices_response,
|
|
|
|
dpiapp_response=dpiapp_response,
|
|
|
|
dpigroup_response=dpigroup_response,
|
2023-08-18 20:44:59 +00:00
|
|
|
port_forward_response=port_forward_response,
|
|
|
|
system_information_response=system_information_response,
|
2021-02-05 15:31:47 +00:00
|
|
|
wlans_response=wlans_response,
|
|
|
|
)
|
|
|
|
|
2021-03-05 20:28:41 +00:00
|
|
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
2019-10-07 19:55:35 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-14 19:57:09 +00:00
|
|
|
|
2020-04-23 14:48:24 +00:00
|
|
|
if config_entry.entry_id not in hass.data[UNIFI_DOMAIN]:
|
2019-10-07 19:55:35 +00:00
|
|
|
return None
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2021-02-05 15:31:47 +00:00
|
|
|
return config_entry
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_hub_setup(
|
2024-02-18 22:47:10 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
device_registry: dr.DeviceRegistry,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Successful setup."""
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setup",
|
|
|
|
return_value=True,
|
|
|
|
) as forward_entry_setup:
|
2023-08-27 15:07:38 +00:00
|
|
|
config_entry = await setup_unifi_integration(
|
|
|
|
hass, aioclient_mock, system_information_response=SYSTEM_INFORMATION
|
|
|
|
)
|
2024-02-20 07:51:22 +00:00
|
|
|
hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2024-03-05 20:36:26 +00:00
|
|
|
entry = hub.config.entry
|
2021-03-02 20:43:59 +00:00
|
|
|
assert len(forward_entry_setup.mock_calls) == len(PLATFORMS)
|
2023-08-05 18:07:20 +00:00
|
|
|
assert forward_entry_setup.mock_calls[0][1] == (entry, BUTTON_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[1][1] == (entry, TRACKER_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[2][1] == (entry, IMAGE_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[3][1] == (entry, SENSOR_DOMAIN)
|
|
|
|
assert forward_entry_setup.mock_calls[4][1] == (entry, SWITCH_DOMAIN)
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2024-03-05 20:36:26 +00:00
|
|
|
assert hub.config.host == ENTRY_CONFIG[CONF_HOST]
|
2024-02-20 07:51:22 +00:00
|
|
|
assert hub.is_admin == (SITE[0]["role"] == "admin")
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2024-03-05 20:36:26 +00:00
|
|
|
assert hub.config.option_allow_bandwidth_sensors == DEFAULT_ALLOW_BANDWIDTH_SENSORS
|
|
|
|
assert hub.config.option_allow_uptime_sensors == DEFAULT_ALLOW_UPTIME_SENSORS
|
|
|
|
assert isinstance(hub.config.option_block_clients, list)
|
|
|
|
assert hub.config.option_track_clients == DEFAULT_TRACK_CLIENTS
|
|
|
|
assert hub.config.option_track_devices == DEFAULT_TRACK_DEVICES
|
|
|
|
assert hub.config.option_track_wired_clients == DEFAULT_TRACK_WIRED_CLIENTS
|
|
|
|
assert hub.config.option_detection_time == timedelta(seconds=DEFAULT_DETECTION_TIME)
|
|
|
|
assert isinstance(hub.config.option_ssid_filter, set)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
assert hub.signal_reachable == "unifi-reachable-1"
|
|
|
|
assert hub.signal_options_update == "unifi-options-1"
|
|
|
|
assert hub.signal_heartbeat_missed == "unifi-heartbeat-missed"
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2024-02-18 22:47:10 +00:00
|
|
|
device_entry = device_registry.async_get_or_create(
|
2021-10-22 18:04:25 +00:00
|
|
|
config_entry_id=config_entry.entry_id,
|
2023-08-27 15:07:38 +00:00
|
|
|
identifiers={(UNIFI_DOMAIN, config_entry.unique_id)},
|
2021-10-22 18:04:25 +00:00
|
|
|
)
|
2023-08-27 15:07:38 +00:00
|
|
|
|
|
|
|
assert device_entry.sw_version == "7.4.162"
|
2021-10-22 18:04:25 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_hub_not_accessible(hass: HomeAssistant) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Retry to login gets scheduled when connection fails."""
|
2020-04-23 14:48:24 +00:00
|
|
|
with patch(
|
2024-02-20 07:51:22 +00:00
|
|
|
"homeassistant.components.unifi.hub.get_unifi_api",
|
2020-04-23 14:48:24 +00:00
|
|
|
side_effect=CannotConnect,
|
2020-01-03 18:23:17 +00:00
|
|
|
):
|
|
|
|
await setup_unifi_integration(hass)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_hub_trigger_reauth_flow(hass: HomeAssistant) -> None:
|
2021-01-20 21:10:40 +00:00
|
|
|
"""Failed authentication trigger a reauthentication flow."""
|
|
|
|
with patch(
|
2024-02-20 07:51:22 +00:00
|
|
|
"homeassistant.components.unifi.get_unifi_api",
|
2021-01-20 21:10:40 +00:00
|
|
|
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()
|
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
|
|
|
|
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_hub_unknown_error(hass: HomeAssistant) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Unknown errors are handled."""
|
2020-04-23 14:48:24 +00:00
|
|
|
with patch(
|
2024-02-20 07:51:22 +00:00
|
|
|
"homeassistant.components.unifi.hub.get_unifi_api",
|
2020-04-23 14:48:24 +00:00
|
|
|
side_effect=Exception,
|
|
|
|
):
|
2020-01-03 18:23:17 +00:00
|
|
|
await setup_unifi_integration(hass)
|
2020-04-23 14:48:24 +00:00
|
|
|
assert hass.data[UNIFI_DOMAIN] == {}
|
2019-10-07 19:55:35 +00:00
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_config_entry_updated(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Calling reset when the entry has been setup."""
|
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
2024-02-20 07:51:22 +00:00
|
|
|
hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
event_call = Mock()
|
2024-02-20 07:51:22 +00:00
|
|
|
unsub = async_dispatcher_connect(hass, hub.signal_options_update, event_call)
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
config_entry, options={CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert config_entry.options[CONF_TRACK_CLIENTS] is False
|
|
|
|
assert config_entry.options[CONF_TRACK_DEVICES] is False
|
|
|
|
|
|
|
|
event_call.assert_called_once()
|
|
|
|
|
|
|
|
unsub()
|
|
|
|
|
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_reset_after_successful_setup(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Calling reset when the entry has been setup."""
|
2021-02-05 15:31:47 +00:00
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
2024-02-20 07:51:22 +00:00
|
|
|
hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
result = await hub.async_reset()
|
2019-10-07 19:55:35 +00:00
|
|
|
await hass.async_block_till_done()
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
assert result is True
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_reset_fails(
|
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Calling reset when the entry has been setup can return false."""
|
|
|
|
config_entry = await setup_unifi_integration(hass, aioclient_mock)
|
2024-02-20 07:51:22 +00:00
|
|
|
hub = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_unload",
|
|
|
|
return_value=False,
|
|
|
|
):
|
2024-02-20 07:51:22 +00:00
|
|
|
result = await hub.async_reset()
|
2021-03-05 20:28:41 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert result is False
|
|
|
|
|
|
|
|
|
2022-01-05 07:16:43 +00:00
|
|
|
async def test_connection_state_signalling(
|
2023-02-17 15:40:46 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
aioclient_mock: AiohttpClientMocker,
|
|
|
|
mock_device_registry,
|
2023-09-27 08:56:24 +00:00
|
|
|
websocket_mock,
|
2023-02-17 15:40:46 +00:00
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify connection statesignalling and connection state are working."""
|
|
|
|
client = {
|
|
|
|
"hostname": "client",
|
|
|
|
"ip": "10.0.0.1",
|
|
|
|
"is_wired": True,
|
|
|
|
"last_seen": dt_util.as_timestamp(dt_util.utcnow()),
|
|
|
|
"mac": "00:00:00:00:00:01",
|
|
|
|
}
|
|
|
|
await setup_unifi_integration(hass, aioclient_mock, clients_response=[client])
|
|
|
|
|
|
|
|
# Controller is connected
|
|
|
|
assert hass.states.get("device_tracker.client").state == "home"
|
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.disconnect()
|
2021-03-05 20:28:41 +00:00
|
|
|
# Controller is disconnected
|
|
|
|
assert hass.states.get("device_tracker.client").state == "unavailable"
|
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.reconnect()
|
2021-03-05 20:28:41 +00:00
|
|
|
# Controller is once again connected
|
|
|
|
assert hass.states.get("device_tracker.client").state == "home"
|
|
|
|
|
|
|
|
|
2023-02-17 15:40:46 +00:00
|
|
|
async def test_reconnect_mechanism(
|
2023-09-27 08:56:24 +00:00
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, websocket_mock
|
2023-02-17 15:40:46 +00:00
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify reconnect prints only on first reconnection try."""
|
|
|
|
await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
|
|
|
|
aioclient_mock.clear_requests()
|
2023-08-26 23:27:45 +00:00
|
|
|
aioclient_mock.get(f"https://{DEFAULT_HOST}:1234/", status=HTTPStatus.BAD_GATEWAY)
|
2021-03-05 20:28:41 +00:00
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.disconnect()
|
2021-03-05 20:28:41 +00:00
|
|
|
assert aioclient_mock.call_count == 0
|
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.reconnect(fail=True)
|
2021-03-05 20:28:41 +00:00
|
|
|
assert aioclient_mock.call_count == 1
|
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.reconnect(fail=True)
|
2021-03-05 20:28:41 +00:00
|
|
|
assert aioclient_mock.call_count == 2
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"exception",
|
|
|
|
[
|
2024-02-05 11:20:36 +00:00
|
|
|
TimeoutError,
|
2021-03-05 20:28:41 +00:00
|
|
|
aiounifi.BadGateway,
|
|
|
|
aiounifi.ServiceUnavailable,
|
|
|
|
aiounifi.AiounifiException,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_reconnect_mechanism_exceptions(
|
2023-09-27 08:56:24 +00:00
|
|
|
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, websocket_mock, exception
|
2023-02-17 15:40:46 +00:00
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify async_reconnect calls expected methods."""
|
|
|
|
await setup_unifi_integration(hass, aioclient_mock)
|
|
|
|
|
|
|
|
with patch("aiounifi.Controller.login", side_effect=exception), patch(
|
2024-02-24 21:20:59 +00:00
|
|
|
"homeassistant.components.unifi.hub.hub.UnifiWebsocket.reconnect"
|
2021-03-05 20:28:41 +00:00
|
|
|
) as mock_reconnect:
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.disconnect()
|
2021-03-05 20:28:41 +00:00
|
|
|
|
2023-09-27 08:56:24 +00:00
|
|
|
await websocket_mock.reconnect()
|
2021-03-05 20:28:41 +00:00
|
|
|
mock_reconnect.assert_called_once()
|
|
|
|
|
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_get_unifi_api(hass: HomeAssistant) -> None:
|
2018-10-16 08:35:35 +00:00
|
|
|
"""Successful call."""
|
2023-08-26 23:27:45 +00:00
|
|
|
with patch("aiounifi.Controller.login", return_value=True):
|
2024-02-20 07:51:22 +00:00
|
|
|
assert await get_unifi_api(hass, ENTRY_CONFIG)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_get_unifi_api_verify_ssl_false(hass: HomeAssistant) -> None:
|
2018-10-16 08:35:35 +00:00
|
|
|
"""Successful call with verify ssl set to false."""
|
2024-02-20 07:51:22 +00:00
|
|
|
hub_data = dict(ENTRY_CONFIG)
|
|
|
|
hub_data[CONF_VERIFY_SSL] = False
|
2023-08-26 23:27:45 +00:00
|
|
|
with patch("aiounifi.Controller.login", return_value=True):
|
2024-02-20 07:51:22 +00:00
|
|
|
assert await get_unifi_api(hass, hub_data)
|
2018-10-16 08:35:35 +00:00
|
|
|
|
|
|
|
|
2022-01-12 23:08:04 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-02-15 13:09:50 +00:00
|
|
|
("side_effect", "raised_exception"),
|
2022-01-12 23:08:04 +00:00
|
|
|
[
|
2024-02-05 11:20:36 +00:00
|
|
|
(TimeoutError, CannotConnect),
|
2022-01-12 23:08:04 +00:00
|
|
|
(aiounifi.BadGateway, CannotConnect),
|
2024-01-29 20:39:15 +00:00
|
|
|
(aiounifi.Forbidden, CannotConnect),
|
2022-01-12 23:08:04 +00:00
|
|
|
(aiounifi.ServiceUnavailable, CannotConnect),
|
|
|
|
(aiounifi.RequestError, CannotConnect),
|
|
|
|
(aiounifi.ResponseError, CannotConnect),
|
|
|
|
(aiounifi.Unauthorized, AuthenticationRequired),
|
|
|
|
(aiounifi.LoginRequired, AuthenticationRequired),
|
|
|
|
(aiounifi.AiounifiException, AuthenticationRequired),
|
|
|
|
],
|
|
|
|
)
|
2024-02-20 07:51:22 +00:00
|
|
|
async def test_get_unifi_api_fails_to_connect(
|
2023-02-17 15:40:46 +00:00
|
|
|
hass: HomeAssistant, side_effect, raised_exception
|
|
|
|
) -> None:
|
2024-02-20 07:51:22 +00:00
|
|
|
"""Check that get_unifi_api can handle UniFi Network being unavailable."""
|
2023-08-26 23:27:45 +00:00
|
|
|
with patch("aiounifi.Controller.login", side_effect=side_effect), pytest.raises(
|
|
|
|
raised_exception
|
|
|
|
):
|
2024-02-20 07:51:22 +00:00
|
|
|
await get_unifi_api(hass, ENTRY_CONFIG)
|