2021-11-26 21:44:49 +00:00
|
|
|
"""Fixtures for UniFi Network methods."""
|
2021-03-18 14:13:22 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-01 21:31:56 +00:00
|
|
|
from unittest.mock import patch
|
2020-01-30 22:06:43 +00:00
|
|
|
|
2022-10-20 17:37:13 +00:00
|
|
|
from aiounifi.models.message import MessageKey
|
|
|
|
from aiounifi.websocket import WebsocketSignal, WebsocketState
|
2021-01-01 21:31:56 +00:00
|
|
|
import pytest
|
2020-04-30 20:29:50 +00:00
|
|
|
|
2022-01-05 07:16:43 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
|
|
|
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
2020-01-30 22:06:43 +00:00
|
|
|
|
2021-03-05 20:28:41 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_unifi_websocket():
|
|
|
|
"""No real websocket allowed."""
|
|
|
|
with patch("aiounifi.controller.WSClient") as mock:
|
|
|
|
|
2022-10-20 17:37:13 +00:00
|
|
|
def make_websocket_call(
|
|
|
|
*,
|
|
|
|
message: MessageKey | None = None,
|
|
|
|
data: list[dict] | dict | None = None,
|
|
|
|
state: WebsocketState | None = None,
|
|
|
|
):
|
2021-03-05 20:28:41 +00:00
|
|
|
"""Generate a websocket call."""
|
2022-10-20 17:37:13 +00:00
|
|
|
if data and not message:
|
2021-03-05 20:28:41 +00:00
|
|
|
mock.return_value.data = data
|
2022-10-19 21:10:01 +00:00
|
|
|
mock.call_args[1]["callback"](WebsocketSignal.DATA)
|
2022-10-20 17:37:13 +00:00
|
|
|
elif data and message:
|
|
|
|
if not isinstance(data, list):
|
|
|
|
data = [data]
|
|
|
|
mock.return_value.data = {
|
|
|
|
"meta": {"message": message.value},
|
|
|
|
"data": data,
|
|
|
|
}
|
|
|
|
mock.call_args[1]["callback"](WebsocketSignal.DATA)
|
2021-03-05 20:28:41 +00:00
|
|
|
elif state:
|
|
|
|
mock.return_value.state = state
|
2022-10-19 21:10:01 +00:00
|
|
|
mock.call_args[1]["callback"](WebsocketSignal.CONNECTION_STATE)
|
2021-03-05 20:28:41 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
yield make_websocket_call
|
|
|
|
|
|
|
|
|
2020-01-30 22:06:43 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def mock_discovery():
|
|
|
|
"""No real network traffic allowed."""
|
|
|
|
with patch(
|
2022-05-24 19:42:11 +00:00
|
|
|
"homeassistant.components.unifi.config_flow._async_discover_unifi",
|
2020-01-30 22:06:43 +00:00
|
|
|
return_value=None,
|
|
|
|
) as mock:
|
|
|
|
yield mock
|
2022-01-05 07:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def mock_device_registry(hass):
|
|
|
|
"""Mock device registry."""
|
|
|
|
dev_reg = dr.async_get(hass)
|
|
|
|
config_entry = MockConfigEntry(domain="something_else")
|
|
|
|
|
|
|
|
for idx, device in enumerate(
|
|
|
|
(
|
|
|
|
"00:00:00:00:00:01",
|
|
|
|
"00:00:00:00:00:02",
|
|
|
|
"00:00:00:00:00:03",
|
|
|
|
"00:00:00:00:00:04",
|
|
|
|
"00:00:00:00:00:05",
|
|
|
|
"00:00:00:00:01:01",
|
|
|
|
"00:00:00:00:02:02",
|
|
|
|
)
|
|
|
|
):
|
|
|
|
dev_reg.async_get_or_create(
|
|
|
|
name=f"Device {idx}",
|
|
|
|
config_entry_id=config_entry.entry_id,
|
|
|
|
connections={(dr.CONNECTION_NETWORK_MAC, device)},
|
|
|
|
)
|