mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 12:59:34 +00:00
Improve UniFi tests (#45871)
This commit is contained in:
@@ -17,7 +17,6 @@ from homeassistant.components.unifi.const import (
|
||||
)
|
||||
from homeassistant.components.unifi.switch import POE_SWITCH
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from .test_controller import (
|
||||
CONTROLLER_HOST,
|
||||
@@ -282,21 +281,11 @@ DPI_APPS = [
|
||||
]
|
||||
|
||||
|
||||
async def test_platform_manually_configured(hass):
|
||||
"""Test that we do not discover anything or try to set up a controller."""
|
||||
assert (
|
||||
await async_setup_component(
|
||||
hass, SWITCH_DOMAIN, {SWITCH_DOMAIN: {"platform": UNIFI_DOMAIN}}
|
||||
)
|
||||
is True
|
||||
)
|
||||
assert UNIFI_DOMAIN not in hass.data
|
||||
|
||||
|
||||
async def test_no_clients(hass):
|
||||
async def test_no_clients(hass, aioclient_mock):
|
||||
"""Test the update_clients function when no clients are found."""
|
||||
controller = await setup_unifi_integration(
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
@@ -304,45 +293,46 @@ async def test_no_clients(hass):
|
||||
},
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert aioclient_mock.call_count == 10
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_controller_not_client(hass):
|
||||
async def test_controller_not_client(hass, aioclient_mock):
|
||||
"""Test that the controller doesn't become a switch."""
|
||||
controller = await setup_unifi_integration(
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False},
|
||||
clients_response=[CONTROLLER_HOST],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
cloudkey = hass.states.get("switch.cloud_key")
|
||||
assert cloudkey is None
|
||||
|
||||
|
||||
async def test_not_admin(hass):
|
||||
async def test_not_admin(hass, aioclient_mock):
|
||||
"""Test that switch platform only work on an admin account."""
|
||||
description = deepcopy(DESCRIPTION)
|
||||
description[0]["site_role"] = "not admin"
|
||||
controller = await setup_unifi_integration(
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False},
|
||||
site_description=description,
|
||||
clients_response=[CLIENT_1],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_switches(hass):
|
||||
async def test_switches(hass, aioclient_mock):
|
||||
"""Test the update_items function with some clients."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
@@ -354,8 +344,8 @@ async def test_switches(hass):
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 4
|
||||
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
@@ -381,38 +371,44 @@ async def test_switches(hass):
|
||||
assert dpi_switch is not None
|
||||
assert dpi_switch.state == "on"
|
||||
|
||||
# Block and unblock client
|
||||
|
||||
aioclient_mock.post(
|
||||
f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_off", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 7
|
||||
assert controller.mock_requests[6] == {
|
||||
"json": {"mac": "00:00:00:00:01:01", "cmd": "block-sta"},
|
||||
"method": "post",
|
||||
"path": "/cmd/stamgr",
|
||||
assert aioclient_mock.call_count == 11
|
||||
assert aioclient_mock.mock_calls[10][2] == {
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"cmd": "block-sta",
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_on", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 8
|
||||
assert controller.mock_requests[7] == {
|
||||
"json": {"mac": "00:00:00:00:01:01", "cmd": "unblock-sta"},
|
||||
"method": "post",
|
||||
"path": "/cmd/stamgr",
|
||||
assert aioclient_mock.call_count == 12
|
||||
assert aioclient_mock.mock_calls[11][2] == {
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"cmd": "unblock-sta",
|
||||
}
|
||||
|
||||
# Enable and disable DPI
|
||||
|
||||
aioclient_mock.put(
|
||||
f"https://{controller.host}:1234/api/s/{controller.site}/rest/dpiapp/5f976f62e3c58f018ec7e17d",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
"turn_off",
|
||||
{"entity_id": "switch.block_media_streaming"},
|
||||
blocking=True,
|
||||
)
|
||||
assert len(controller.mock_requests) == 9
|
||||
assert controller.mock_requests[8] == {
|
||||
"json": {"enabled": False},
|
||||
"method": "put",
|
||||
"path": "/rest/dpiapp/5f976f62e3c58f018ec7e17d",
|
||||
}
|
||||
assert aioclient_mock.call_count == 13
|
||||
assert aioclient_mock.mock_calls[12][2] == {"enabled": False}
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN,
|
||||
@@ -420,22 +416,20 @@ async def test_switches(hass):
|
||||
{"entity_id": "switch.block_media_streaming"},
|
||||
blocking=True,
|
||||
)
|
||||
assert len(controller.mock_requests) == 10
|
||||
assert controller.mock_requests[9] == {
|
||||
"json": {"enabled": True},
|
||||
"method": "put",
|
||||
"path": "/rest/dpiapp/5f976f62e3c58f018ec7e17d",
|
||||
}
|
||||
assert aioclient_mock.call_count == 14
|
||||
assert aioclient_mock.mock_calls[13][2] == {"enabled": True}
|
||||
|
||||
|
||||
async def test_remove_switches(hass):
|
||||
async def test_remove_switches(hass, aioclient_mock):
|
||||
"""Test the update_items function with some clients."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||
clients_response=[CLIENT_1, UNBLOCKED],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
poe_switch = hass.states.get("switch.poe_client_1")
|
||||
@@ -460,10 +454,11 @@ async def test_remove_switches(hass):
|
||||
assert block_switch is None
|
||||
|
||||
|
||||
async def test_block_switches(hass):
|
||||
async def test_block_switches(hass, aioclient_mock):
|
||||
"""Test the update_items function with some clients."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
@@ -472,6 +467,7 @@ async def test_block_switches(hass):
|
||||
clients_response=[UNBLOCKED],
|
||||
clients_all_response=[BLOCKED],
|
||||
)
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
@@ -507,31 +503,34 @@ async def test_block_switches(hass):
|
||||
assert blocked is not None
|
||||
assert blocked.state == "off"
|
||||
|
||||
aioclient_mock.post(
|
||||
f"https://{controller.host}:1234/api/s/{controller.site}/cmd/stamgr",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_off", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 7
|
||||
assert controller.mock_requests[6] == {
|
||||
"json": {"mac": "00:00:00:00:01:01", "cmd": "block-sta"},
|
||||
"method": "post",
|
||||
"path": "/cmd/stamgr",
|
||||
assert aioclient_mock.call_count == 11
|
||||
assert aioclient_mock.mock_calls[10][2] == {
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"cmd": "block-sta",
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_on", {"entity_id": "switch.block_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 8
|
||||
assert controller.mock_requests[7] == {
|
||||
"json": {"mac": "00:00:00:00:01:01", "cmd": "unblock-sta"},
|
||||
"method": "post",
|
||||
"path": "/cmd/stamgr",
|
||||
assert aioclient_mock.call_count == 12
|
||||
assert aioclient_mock.mock_calls[11][2] == {
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"cmd": "unblock-sta",
|
||||
}
|
||||
|
||||
|
||||
async def test_new_client_discovered_on_block_control(hass):
|
||||
async def test_new_client_discovered_on_block_control(hass, aioclient_mock):
|
||||
"""Test if 2nd update has a new client."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
@@ -539,8 +538,8 @@ async def test_new_client_discovered_on_block_control(hass):
|
||||
CONF_DPI_RESTRICTIONS: False,
|
||||
},
|
||||
)
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
blocked = hass.states.get("switch.block_client_1")
|
||||
@@ -567,10 +566,11 @@ async def test_new_client_discovered_on_block_control(hass):
|
||||
assert blocked is not None
|
||||
|
||||
|
||||
async def test_option_block_clients(hass):
|
||||
async def test_option_block_clients(hass, aioclient_mock):
|
||||
"""Test the changes to option reflects accordingly."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
clients_all_response=[BLOCKED, UNBLOCKED],
|
||||
)
|
||||
@@ -578,7 +578,7 @@ async def test_option_block_clients(hass):
|
||||
|
||||
# Add a second switch
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry,
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -586,7 +586,7 @@ async def test_option_block_clients(hass):
|
||||
|
||||
# Remove the second switch again
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry,
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -594,7 +594,7 @@ async def test_option_block_clients(hass):
|
||||
|
||||
# Enable one and remove another one
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry,
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
@@ -602,17 +602,18 @@ async def test_option_block_clients(hass):
|
||||
|
||||
# Remove one
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry,
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: []},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_option_remove_switches(hass):
|
||||
async def test_option_remove_switches(hass, aioclient_mock):
|
||||
"""Test removal of DPI switch when options updated."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
@@ -626,23 +627,24 @@ async def test_option_remove_switches(hass):
|
||||
|
||||
# Disable DPI Switches
|
||||
hass.config_entries.async_update_entry(
|
||||
controller.config_entry,
|
||||
config_entry,
|
||||
options={CONF_DPI_RESTRICTIONS: False, CONF_POE_CLIENTS: False},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_new_client_discovered_on_poe_control(hass):
|
||||
async def test_new_client_discovered_on_poe_control(hass, aioclient_mock):
|
||||
"""Test if 2nd update has a new client."""
|
||||
controller = await setup_unifi_integration(
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False},
|
||||
clients_response=[CLIENT_1],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
controller = hass.data[UNIFI_DOMAIN][config_entry.entry_id]
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
controller.api.websocket._data = {
|
||||
@@ -665,47 +667,41 @@ async def test_new_client_discovered_on_poe_control(hass):
|
||||
switch_2 = hass.states.get("switch.poe_client_2")
|
||||
assert switch_2 is not None
|
||||
|
||||
aioclient_mock.put(
|
||||
f"https://{controller.host}:1234/api/s/{controller.site}/rest/device/mock-id",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_off", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 7
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
assert controller.mock_requests[6] == {
|
||||
"json": {
|
||||
"port_overrides": [{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "off"}]
|
||||
},
|
||||
"method": "put",
|
||||
"path": "/rest/device/mock-id",
|
||||
assert aioclient_mock.call_count == 11
|
||||
assert aioclient_mock.mock_calls[10][2] == {
|
||||
"port_overrides": [{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "off"}]
|
||||
}
|
||||
|
||||
await hass.services.async_call(
|
||||
SWITCH_DOMAIN, "turn_on", {"entity_id": "switch.poe_client_1"}, blocking=True
|
||||
)
|
||||
assert len(controller.mock_requests) == 8
|
||||
assert controller.mock_requests[7] == {
|
||||
"json": {
|
||||
"port_overrides": [
|
||||
{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "auto"}
|
||||
]
|
||||
},
|
||||
"method": "put",
|
||||
"path": "/rest/device/mock-id",
|
||||
assert aioclient_mock.call_count == 12
|
||||
assert aioclient_mock.mock_calls[11][2] == {
|
||||
"port_overrides": [{"port_idx": 1, "portconf_id": "1a1", "poe_mode": "auto"}]
|
||||
}
|
||||
|
||||
|
||||
async def test_ignore_multiple_poe_clients_on_same_port(hass):
|
||||
async def test_ignore_multiple_poe_clients_on_same_port(hass, aioclient_mock):
|
||||
"""Ignore when there are multiple POE driven clients on same port.
|
||||
|
||||
If there is a non-UniFi switch powered by POE,
|
||||
clients will be transparently marked as having POE as well.
|
||||
"""
|
||||
controller = await setup_unifi_integration(
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
clients_response=POE_SWITCH_CLIENTS,
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 3
|
||||
|
||||
switch_1 = hass.states.get("switch.poe_client_1")
|
||||
@@ -714,7 +710,7 @@ async def test_ignore_multiple_poe_clients_on_same_port(hass):
|
||||
assert switch_2 is None
|
||||
|
||||
|
||||
async def test_restoring_client(hass):
|
||||
async def test_restoring_client(hass, aioclient_mock):
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = config_entries.ConfigEntry(
|
||||
version=1,
|
||||
@@ -744,8 +740,9 @@ async def test_restoring_client(hass):
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
controller = await setup_unifi_integration(
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: ["random mac"],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
@@ -756,7 +753,6 @@ async def test_restoring_client(hass):
|
||||
clients_all_response=[CLIENT_1],
|
||||
)
|
||||
|
||||
assert len(controller.mock_requests) == 6
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
device_1 = hass.states.get("switch.client_1")
|
||||
|
||||
Reference in New Issue
Block a user