2021-11-26 21:44:49 +00:00
|
|
|
"""Test UniFi Network."""
|
2024-03-08 13:44:56 +00:00
|
|
|
|
2024-05-26 14:30:22 +00:00
|
|
|
from collections.abc import Callable
|
2021-10-24 09:27:17 +00:00
|
|
|
from http import HTTPStatus
|
2024-06-08 20:44:24 +00:00
|
|
|
from types import MappingProxyType
|
2024-06-08 15:58:47 +00:00
|
|
|
from typing import Any
|
2024-05-26 14:30:22 +00:00
|
|
|
from unittest.mock import 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
|
|
|
|
|
2024-06-08 20:44:24 +00:00
|
|
|
from homeassistant.components.unifi.const import DOMAIN as UNIFI_DOMAIN
|
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
|
2024-05-26 14:30:22 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
2024-06-08 20:44:24 +00:00
|
|
|
from homeassistant.const import CONF_HOST, Platform
|
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
|
|
|
import homeassistant.util.dt as dt_util
|
2020-01-03 18:23:17 +00:00
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
2019-10-07 19:55:35 +00:00
|
|
|
|
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
|
|
|
device_registry: dr.DeviceRegistry,
|
2024-05-31 22:27:53 +00:00
|
|
|
config_entry_factory: Callable[[], ConfigEntry],
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Successful setup."""
|
|
|
|
with patch(
|
2024-03-23 19:26:38 +00:00
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_setups",
|
2019-10-07 19:55:35 +00:00
|
|
|
return_value=True,
|
|
|
|
) as forward_entry_setup:
|
2024-05-31 22:27:53 +00:00
|
|
|
config_entry = await config_entry_factory()
|
2019-10-07 19:55:35 +00:00
|
|
|
|
2024-03-23 19:26:38 +00:00
|
|
|
assert len(forward_entry_setup.mock_calls) == 1
|
|
|
|
assert forward_entry_setup.mock_calls[0][1] == (
|
2024-06-08 20:44:24 +00:00
|
|
|
config_entry,
|
2024-03-23 19:26:38 +00:00
|
|
|
[
|
2024-06-08 20:44:24 +00:00
|
|
|
Platform.BUTTON,
|
|
|
|
Platform.DEVICE_TRACKER,
|
|
|
|
Platform.IMAGE,
|
|
|
|
Platform.SENSOR,
|
|
|
|
Platform.SWITCH,
|
|
|
|
Platform.UPDATE,
|
2024-03-23 19:26:38 +00:00
|
|
|
],
|
|
|
|
)
|
2019-10-07 19:55: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
|
|
|
|
2023-02-08 18:10:53 +00:00
|
|
|
async def test_reset_after_successful_setup(
|
2024-05-31 22:27:53 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2019-10-07 19:55:35 +00:00
|
|
|
"""Calling reset when the entry has been setup."""
|
2024-06-08 20:44:24 +00:00
|
|
|
assert config_entry_setup.state is ConfigEntryState.LOADED
|
2018-10-16 08:35:35 +00:00
|
|
|
|
2024-06-08 20:44:24 +00:00
|
|
|
assert await hass.config_entries.async_unload(config_entry_setup.entry_id)
|
|
|
|
assert config_entry_setup.state is ConfigEntryState.NOT_LOADED
|
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(
|
2024-05-31 22:27:53 +00:00
|
|
|
hass: HomeAssistant, config_entry_setup: ConfigEntry
|
2023-02-08 18:10:53 +00:00
|
|
|
) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Calling reset when the entry has been setup can return false."""
|
2024-06-08 20:44:24 +00:00
|
|
|
assert config_entry_setup.state is ConfigEntryState.LOADED
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
with patch(
|
|
|
|
"homeassistant.config_entries.ConfigEntries.async_forward_entry_unload",
|
|
|
|
return_value=False,
|
|
|
|
):
|
2024-06-08 20:44:24 +00:00
|
|
|
assert not await hass.config_entries.async_unload(config_entry_setup.entry_id)
|
|
|
|
assert config_entry_setup.state is ConfigEntryState.LOADED
|
2021-03-05 20:28:41 +00:00
|
|
|
|
|
|
|
|
2022-01-05 07:16:43 +00:00
|
|
|
async def test_connection_state_signalling(
|
2023-02-17 15:40:46 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
mock_device_registry,
|
2023-09-27 08:56:24 +00:00
|
|
|
websocket_mock,
|
2024-06-08 15:58:47 +00:00
|
|
|
config_entry_factory: Callable[[], ConfigEntry],
|
|
|
|
client_payload: list[dict[str, Any]],
|
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."""
|
2024-06-08 15:58:47 +00:00
|
|
|
client_payload.append(
|
|
|
|
{
|
|
|
|
"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 config_entry_factory()
|
|
|
|
|
2021-03-05 20:28:41 +00:00
|
|
|
# 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(
|
2024-06-08 20:44:24 +00:00
|
|
|
aioclient_mock: AiohttpClientMocker, websocket_mock, config_entry_setup: ConfigEntry
|
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."""
|
|
|
|
aioclient_mock.clear_requests()
|
2024-06-08 20:44:24 +00:00
|
|
|
aioclient_mock.get(
|
|
|
|
f"https://{config_entry_setup.data[CONF_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,
|
|
|
|
],
|
|
|
|
)
|
2024-06-08 20:44:24 +00:00
|
|
|
@pytest.mark.usefixtures("config_entry_setup")
|
|
|
|
async def test_reconnect_mechanism_exceptions(websocket_mock, exception) -> None:
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Verify async_reconnect calls expected methods."""
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("aiounifi.Controller.login", side_effect=exception),
|
|
|
|
patch(
|
|
|
|
"homeassistant.components.unifi.hub.hub.UnifiWebsocket.reconnect"
|
|
|
|
) 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()
|
|
|
|
|
|
|
|
|
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(
|
2024-06-08 20:44:24 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
side_effect,
|
|
|
|
raised_exception,
|
|
|
|
config_entry_data: MappingProxyType[str, Any],
|
2023-02-17 15:40:46 +00:00
|
|
|
) -> None:
|
2024-02-20 07:51:22 +00:00
|
|
|
"""Check that get_unifi_api can handle UniFi Network being unavailable."""
|
2024-03-25 23:02:16 +00:00
|
|
|
with (
|
|
|
|
patch("aiounifi.Controller.login", side_effect=side_effect),
|
|
|
|
pytest.raises(raised_exception),
|
2023-08-26 23:27:45 +00:00
|
|
|
):
|
2024-06-08 20:44:24 +00:00
|
|
|
await get_unifi_api(hass, config_entry_data)
|