2021-12-26 06:12:57 +00:00
|
|
|
"""Test the UniFi Protect setup flow."""
|
2022-05-17 03:51:13 +00:00
|
|
|
# pylint: disable=protected-access
|
2021-12-26 06:12:57 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
from collections.abc import Awaitable, Callable
|
2022-01-10 21:04:53 +00:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
import aiohttp
|
2022-06-25 15:15:38 +00:00
|
|
|
from pyunifiprotect import NotAuthorized, NvrError, ProtectApiClient
|
2022-08-29 01:35:45 +00:00
|
|
|
from pyunifiprotect.data import NVR, Bootstrap, Doorlock, Light, Sensor
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-07-16 22:28:17 +00:00
|
|
|
from homeassistant.components.unifiprotect.const import (
|
|
|
|
CONF_DISABLE_RTSP,
|
2022-08-29 01:35:45 +00:00
|
|
|
CONF_IGNORED,
|
2022-07-16 22:28:17 +00:00
|
|
|
DEFAULT_SCAN_INTERVAL,
|
|
|
|
DOMAIN,
|
|
|
|
)
|
2022-01-18 18:40:29 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
|
2021-12-26 06:12:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-29 01:35:45 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2022-06-02 15:26:08 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-01-18 18:40:29 +00:00
|
|
|
from . import _patch_discovery
|
2022-08-29 01:35:45 +00:00
|
|
|
from .utils import MockUFPFixture, get_device_from_ufp_device, init_entry, time_changed
|
2022-01-10 21:04:53 +00:00
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
async def remove_device(
|
|
|
|
ws_client: aiohttp.ClientWebSocketResponse, device_id: str, config_entry_id: str
|
|
|
|
) -> bool:
|
|
|
|
"""Remove config entry from a device."""
|
|
|
|
await ws_client.send_json(
|
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"type": "config/device_registry/remove_config_entry",
|
|
|
|
"config_entry_id": config_entry_id,
|
|
|
|
"device_id": device_id,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
response = await ws_client.receive_json()
|
|
|
|
return response["success"]
|
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test working setup of unifiprotect entry."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
|
|
|
assert ufp.api.update.called
|
|
|
|
assert ufp.entry.unique_id == ufp.api.bootstrap.nvr.mac
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-01-10 21:04:53 +00:00
|
|
|
async def test_setup_multiple(
|
|
|
|
hass: HomeAssistant,
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp: MockUFPFixture,
|
|
|
|
bootstrap: Bootstrap,
|
2022-01-10 21:04:53 +00:00
|
|
|
):
|
|
|
|
"""Test working setup of unifiprotect entry."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2022-01-10 21:04:53 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
|
|
|
assert ufp.api.update.called
|
|
|
|
assert ufp.entry.unique_id == ufp.api.bootstrap.nvr.mac
|
2022-01-10 21:04:53 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
nvr = bootstrap.nvr
|
|
|
|
nvr._api = ufp.api
|
2022-01-10 21:04:53 +00:00
|
|
|
nvr.mac = "A1E00C826983"
|
|
|
|
nvr.id
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.get_nvr = AsyncMock(return_value=nvr)
|
2022-01-10 21:04:53 +00:00
|
|
|
|
|
|
|
with patch("homeassistant.components.unifiprotect.ProtectApiClient") as mock_api:
|
|
|
|
mock_config = MockConfigEntry(
|
|
|
|
domain=DOMAIN,
|
|
|
|
data={
|
|
|
|
"host": "1.1.1.1",
|
|
|
|
"username": "test-username",
|
|
|
|
"password": "test-password",
|
|
|
|
"id": "UnifiProtect",
|
|
|
|
"port": 443,
|
|
|
|
"verify_ssl": False,
|
|
|
|
},
|
|
|
|
version=2,
|
|
|
|
)
|
|
|
|
mock_config.add_to_hass(hass)
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
mock_api.return_value = ufp.api
|
2022-01-10 21:04:53 +00:00
|
|
|
|
|
|
|
await hass.config_entries.async_setup(mock_config.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert mock_config.state == ConfigEntryState.LOADED
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.api.update.called
|
|
|
|
assert mock_config.unique_id == ufp.api.bootstrap.nvr.mac
|
2022-01-10 21:04:53 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_reload(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test updating entry reload entry."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
options = dict(ufp.entry.options)
|
2021-12-26 06:12:57 +00:00
|
|
|
options[CONF_DISABLE_RTSP] = True
|
2022-06-25 15:15:38 +00:00
|
|
|
hass.config_entries.async_update_entry(ufp.entry, options=options)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
|
|
|
assert ufp.api.async_disconnect_ws.called
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-29 03:00:26 +00:00
|
|
|
async def test_unload(hass: HomeAssistant, ufp: MockUFPFixture, light: Light):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test unloading of unifiprotect entry."""
|
|
|
|
|
2022-06-29 03:00:26 +00:00
|
|
|
await init_entry(hass, ufp, [light])
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_unload(ufp.entry.entry_id)
|
|
|
|
assert ufp.entry.state == ConfigEntryState.NOT_LOADED
|
|
|
|
assert ufp.api.async_disconnect_ws.called
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup_too_old(hass: HomeAssistant, ufp: MockUFPFixture, old_nvr: NVR):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test setup of unifiprotect entry with too old of version of UniFi Protect."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.get_nvr.return_value = old_nvr
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.SETUP_ERROR
|
|
|
|
assert not ufp.api.update.called
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup_failed_update(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test setup of unifiprotect entry with failed update."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.update = AsyncMock(side_effect=NvrError)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.SETUP_RETRY
|
|
|
|
assert ufp.api.update.called
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup_failed_update_reauth(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test setup of unifiprotect entry with update that gives unauthroized error."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-07-16 22:28:17 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
# reauth should not be triggered until there are 10 auth failures in a row
|
|
|
|
# to verify it is not transient
|
|
|
|
ufp.api.update = AsyncMock(side_effect=NotAuthorized)
|
|
|
|
for _ in range(10):
|
|
|
|
await time_changed(hass, DEFAULT_SCAN_INTERVAL)
|
|
|
|
assert len(hass.config_entries.flow._progress) == 0
|
|
|
|
|
|
|
|
assert ufp.api.update.call_count == 10
|
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
|
|
|
|
|
|
|
await time_changed(hass, DEFAULT_SCAN_INTERVAL)
|
|
|
|
assert ufp.api.update.call_count == 11
|
|
|
|
assert len(hass.config_entries.flow._progress) == 1
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup_failed_error(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test setup of unifiprotect entry with generic error."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.get_nvr = AsyncMock(side_effect=NvrError)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2021-12-26 06:12:57 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.SETUP_RETRY
|
|
|
|
assert not ufp.api.update.called
|
2021-12-26 06:12:57 +00:00
|
|
|
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
async def test_setup_failed_auth(hass: HomeAssistant, ufp: MockUFPFixture):
|
2021-12-26 06:12:57 +00:00
|
|
|
"""Test setup of unifiprotect entry with unauthorized error."""
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.get_nvr = AsyncMock(side_effect=NotAuthorized)
|
2021-12-26 06:12:57 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
|
|
|
assert ufp.entry.state == ConfigEntryState.SETUP_ERROR
|
|
|
|
assert not ufp.api.update.called
|
2022-01-18 18:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_setup_starts_discovery(
|
2022-06-25 15:15:38 +00:00
|
|
|
hass: HomeAssistant, ufp_config_entry: ConfigEntry, ufp_client: ProtectApiClient
|
2022-01-18 18:40:29 +00:00
|
|
|
):
|
|
|
|
"""Test setting up will start discovery."""
|
|
|
|
with _patch_discovery(), patch(
|
|
|
|
"homeassistant.components.unifiprotect.ProtectApiClient"
|
|
|
|
) as mock_api:
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp_config_entry.add_to_hass(hass)
|
|
|
|
mock_api.return_value = ufp_client
|
|
|
|
ufp = MockUFPFixture(ufp_config_entry, ufp_client)
|
2022-01-18 18:40:29 +00:00
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2022-01-18 18:40:29 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
assert ufp.entry.state == ConfigEntryState.LOADED
|
2022-01-18 18:40:29 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.config_entries.flow.async_progress_by_handler(DOMAIN)) == 1
|
2022-05-17 03:51:13 +00:00
|
|
|
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
async def test_device_remove_devices(
|
|
|
|
hass: HomeAssistant,
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp: MockUFPFixture,
|
|
|
|
light: Light,
|
2022-08-29 01:35:45 +00:00
|
|
|
doorlock: Doorlock,
|
|
|
|
sensor: Sensor,
|
2022-06-02 15:26:08 +00:00
|
|
|
hass_ws_client: Callable[
|
|
|
|
[HomeAssistant], Awaitable[aiohttp.ClientWebSocketResponse]
|
|
|
|
],
|
|
|
|
) -> None:
|
|
|
|
"""Test we can only remove a device that no longer exists."""
|
|
|
|
|
2022-08-29 01:35:45 +00:00
|
|
|
sensor.mac = "FFFFFFFFFFFF"
|
|
|
|
|
|
|
|
await init_entry(hass, ufp, [light, doorlock, sensor], regenerate_ids=False)
|
2022-06-25 15:15:38 +00:00
|
|
|
assert await async_setup_component(hass, "config", {})
|
2022-06-02 15:26:08 +00:00
|
|
|
|
2022-08-29 01:35:45 +00:00
|
|
|
entry_id = ufp.entry.entry_id
|
2022-06-02 15:26:08 +00:00
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
|
2022-08-29 01:35:45 +00:00
|
|
|
light_device = get_device_from_ufp_device(hass, light)
|
|
|
|
assert light_device is not None
|
2022-06-02 15:26:08 +00:00
|
|
|
assert (
|
2022-08-29 01:35:45 +00:00
|
|
|
await remove_device(await hass_ws_client(hass), light_device.id, entry_id)
|
|
|
|
is True
|
2022-06-02 15:26:08 +00:00
|
|
|
)
|
|
|
|
|
2022-08-29 01:35:45 +00:00
|
|
|
doorlock_device = get_device_from_ufp_device(hass, doorlock)
|
|
|
|
assert (
|
|
|
|
await remove_device(await hass_ws_client(hass), doorlock_device.id, entry_id)
|
|
|
|
is True
|
|
|
|
)
|
|
|
|
|
|
|
|
sensor_device = get_device_from_ufp_device(hass, sensor)
|
|
|
|
assert sensor_device is None
|
|
|
|
|
2022-06-02 15:26:08 +00:00
|
|
|
dead_device_entry = device_registry.async_get_or_create(
|
|
|
|
config_entry_id=entry_id,
|
|
|
|
connections={(dr.CONNECTION_NETWORK_MAC, "e9:88:e7:b8:b4:40")},
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
await remove_device(await hass_ws_client(hass), dead_device_entry.id, entry_id)
|
|
|
|
is True
|
|
|
|
)
|
2022-06-22 20:57:21 +00:00
|
|
|
|
2022-08-29 01:35:45 +00:00
|
|
|
await time_changed(hass, 60)
|
|
|
|
entry = hass.config_entries.async_get_entry(entry_id)
|
|
|
|
entry.options[CONF_IGNORED] == f"{light.mac},{doorlock.mac}"
|
|
|
|
|
2022-06-22 20:57:21 +00:00
|
|
|
|
|
|
|
async def test_device_remove_devices_nvr(
|
|
|
|
hass: HomeAssistant,
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp: MockUFPFixture,
|
2022-06-22 20:57:21 +00:00
|
|
|
hass_ws_client: Callable[
|
|
|
|
[HomeAssistant], Awaitable[aiohttp.ClientWebSocketResponse]
|
|
|
|
],
|
|
|
|
) -> None:
|
|
|
|
"""Test we can only remove a NVR device that no longer exists."""
|
|
|
|
assert await async_setup_component(hass, "config", {})
|
|
|
|
|
2022-06-25 15:15:38 +00:00
|
|
|
ufp.api.get_bootstrap = AsyncMock(return_value=ufp.api.bootstrap)
|
|
|
|
await hass.config_entries.async_setup(ufp.entry.entry_id)
|
2022-06-22 20:57:21 +00:00
|
|
|
await hass.async_block_till_done()
|
2022-06-25 15:15:38 +00:00
|
|
|
entry_id = ufp.entry.entry_id
|
2022-06-22 20:57:21 +00:00
|
|
|
|
|
|
|
device_registry = dr.async_get(hass)
|
|
|
|
|
|
|
|
live_device_entry = list(device_registry.devices.values())[0]
|
|
|
|
assert (
|
|
|
|
await remove_device(await hass_ws_client(hass), live_device_entry.id, entry_id)
|
|
|
|
is False
|
|
|
|
)
|