mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
Use fixtures in UniFi switch tests (#118831)
This commit is contained in:
parent
c2e245f9d4
commit
b4f6325278
@ -35,9 +35,9 @@ from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.entity_registry import RegistryEntryDisabler
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .test_hub import CONTROLLER_HOST, ENTRY_CONFIG, SITE, setup_unifi_integration
|
||||
from .test_hub import CONTROLLER_HOST
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
from tests.common import async_fire_time_changed
|
||||
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||
|
||||
CLIENT_1 = {
|
||||
@ -760,77 +760,50 @@ WLAN = {
|
||||
}
|
||||
|
||||
|
||||
async def test_no_clients(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test the update_clients function when no clients are found."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
@pytest.mark.parametrize("client_payload", [[CONTROLLER_HOST]])
|
||||
@pytest.mark.parametrize("device_payload", [[DEVICE_1]])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_hub_not_client(hass: HomeAssistant) -> None:
|
||||
"""Test that the cloud key doesn't become a switch."""
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
assert hass.states.get("switch.cloud_key") is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("client_payload", [[CLIENT_1]])
|
||||
@pytest.mark.parametrize("device_payload", [[DEVICE_1]])
|
||||
@pytest.mark.parametrize(
|
||||
"site_payload",
|
||||
[[{"desc": "Site name", "name": "site_id", "role": "not admin", "_id": "1"}]],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_not_admin(hass: HomeAssistant) -> None:
|
||||
"""Test that switch platform only work on an admin account."""
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options",
|
||||
[
|
||||
{
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
CONF_DPI_RESTRICTIONS: False,
|
||||
},
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_hub_not_client(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that the cloud key doesn't become a switch."""
|
||||
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(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: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
"""Test that switch platform only work on an admin account."""
|
||||
site = deepcopy(SITE)
|
||||
site[0]["role"] = "not admin"
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False},
|
||||
sites=site,
|
||||
clients_response=[CLIENT_1],
|
||||
devices_response=[DEVICE_1],
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
}
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("client_payload", [[CLIENT_4]])
|
||||
@pytest.mark.parametrize("clients_all_payload", [[BLOCKED, UNBLOCKED, CLIENT_1]])
|
||||
@pytest.mark.parametrize("dpi_app_payload", [DPI_APPS])
|
||||
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_switches(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
config_entry_setup,
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
},
|
||||
clients_response=[CLIENT_4],
|
||||
clients_all_response=[BLOCKED, UNBLOCKED, CLIENT_1],
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 3
|
||||
|
||||
@ -906,19 +879,15 @@ async def test_switches(
|
||||
assert aioclient_mock.mock_calls[1][2] == {"enabled": True}
|
||||
|
||||
|
||||
async def test_remove_switches(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_unifi_websocket
|
||||
) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options", [{CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]}]
|
||||
)
|
||||
@pytest.mark.parametrize("client_payload", [[UNBLOCKED]])
|
||||
@pytest.mark.parametrize("dpi_app_payload", [DPI_APPS])
|
||||
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_remove_switches(hass: HomeAssistant, mock_unifi_websocket) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||
clients_response=[UNBLOCKED],
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
assert hass.states.get("switch.block_client_2") is not None
|
||||
@ -939,21 +908,26 @@ async def test_remove_switches(
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_block_switches(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_unifi_websocket
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options",
|
||||
[
|
||||
{
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
},
|
||||
clients_response=[UNBLOCKED],
|
||||
clients_all_response=[BLOCKED],
|
||||
)
|
||||
}
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("client_payload", [[UNBLOCKED]])
|
||||
@pytest.mark.parametrize("clients_all_payload", [[BLOCKED]])
|
||||
async def test_block_switches(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
config_entry_setup,
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
|
||||
@ -1006,20 +980,13 @@ async def test_block_switches(
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dpi_app_payload", [DPI_APPS])
|
||||
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_dpi_switches(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
websocket_mock,
|
||||
hass: HomeAssistant, mock_unifi_websocket, websocket_mock
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
dpi_switch = hass.states.get("switch.block_media_streaming")
|
||||
@ -1050,17 +1017,13 @@ async def test_dpi_switches(
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("dpi_app_payload", [DPI_APPS])
|
||||
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_dpi_switches_add_second_app(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_unifi_websocket
|
||||
hass: HomeAssistant, mock_unifi_websocket
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
assert hass.states.get("switch.block_media_streaming").state == STATE_ON
|
||||
|
||||
@ -1109,43 +1072,29 @@ async def test_dpi_switches_add_second_app(
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "test_data", "outlet_index", "expected_switches"),
|
||||
("device_payload", "entity_id", "outlet_index", "expected_switches"),
|
||||
[
|
||||
(
|
||||
"plug_outlet_1",
|
||||
OUTLET_UP1,
|
||||
1,
|
||||
1,
|
||||
),
|
||||
(
|
||||
"dummy_usp_pdu_pro_usb_outlet_1",
|
||||
PDU_DEVICE_1,
|
||||
1,
|
||||
2,
|
||||
),
|
||||
(
|
||||
"dummy_usp_pdu_pro_outlet_2",
|
||||
PDU_DEVICE_1,
|
||||
2,
|
||||
2,
|
||||
),
|
||||
([OUTLET_UP1], "plug_outlet_1", 1, 1),
|
||||
([PDU_DEVICE_1], "dummy_usp_pdu_pro_usb_outlet_1", 1, 2),
|
||||
([PDU_DEVICE_1], "dummy_usp_pdu_pro_outlet_2", 2, 2),
|
||||
],
|
||||
)
|
||||
async def test_outlet_switches(
|
||||
hass: HomeAssistant,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
config_entry_setup,
|
||||
device_payload,
|
||||
websocket_mock,
|
||||
entity_id: str,
|
||||
test_data: any,
|
||||
outlet_index: int,
|
||||
expected_switches: int,
|
||||
) -> None:
|
||||
"""Test the outlet entities."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass, aioclient_mock, devices_response=[test_data]
|
||||
)
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == expected_switches
|
||||
|
||||
# Validate state object
|
||||
switch_1 = hass.states.get(f"switch.{entity_id}")
|
||||
assert switch_1 is not None
|
||||
@ -1153,14 +1102,14 @@ async def test_outlet_switches(
|
||||
assert switch_1.attributes.get(ATTR_DEVICE_CLASS) == SwitchDeviceClass.OUTLET
|
||||
|
||||
# Update state object
|
||||
device_1 = deepcopy(test_data)
|
||||
device_1 = deepcopy(device_payload[0])
|
||||
device_1["outlet_table"][outlet_index - 1]["relay_state"] = False
|
||||
mock_unifi_websocket(message=MessageKey.DEVICE, data=device_1)
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(f"switch.{entity_id}").state == STATE_OFF
|
||||
|
||||
# Turn off outlet
|
||||
device_id = test_data["device_id"]
|
||||
device_id = device_payload[0]["device_id"]
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.put(
|
||||
f"https://{config_entry.data[CONF_HOST]}:1234"
|
||||
@ -1229,21 +1178,22 @@ async def test_outlet_switches(
|
||||
assert hass.states.get(f"switch.{entity_id}") is None
|
||||
|
||||
|
||||
async def test_new_client_discovered_on_block_control(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, mock_unifi_websocket
|
||||
) -> None:
|
||||
"""Test if 2nd update has a new client."""
|
||||
await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options",
|
||||
[
|
||||
{
|
||||
CONF_BLOCK_CLIENT: [BLOCKED["mac"]],
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
CONF_DPI_RESTRICTIONS: False,
|
||||
},
|
||||
)
|
||||
|
||||
}
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("config_entry_setup")
|
||||
async def test_new_client_discovered_on_block_control(
|
||||
hass: HomeAssistant, mock_unifi_websocket
|
||||
) -> None:
|
||||
"""Test if 2nd update has a new client."""
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
assert hass.states.get("switch.block_client_1") is None
|
||||
|
||||
@ -1254,22 +1204,27 @@ async def test_new_client_discovered_on_block_control(
|
||||
assert hass.states.get("switch.block_client_1") is not None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options", [{CONF_BLOCK_CLIENT: [BLOCKED["mac"]]}]
|
||||
)
|
||||
@pytest.mark.parametrize("clients_all_payload", [[BLOCKED, UNBLOCKED]])
|
||||
async def test_option_block_clients(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
hass: HomeAssistant, config_entry_setup, clients_all_payload
|
||||
) -> None:
|
||||
"""Test the changes to option reflects accordingly."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
clients_all_response=[BLOCKED, UNBLOCKED],
|
||||
)
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Add a second switch
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"], UNBLOCKED["mac"]]},
|
||||
options={
|
||||
CONF_BLOCK_CLIENT: [
|
||||
clients_all_payload[0]["mac"],
|
||||
clients_all_payload[1]["mac"],
|
||||
]
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
@ -1277,15 +1232,15 @@ async def test_option_block_clients(
|
||||
# Remove the second switch again
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [BLOCKED["mac"]]},
|
||||
options={CONF_BLOCK_CLIENT: [clients_all_payload[0]["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Enable one and remove another one
|
||||
# Enable one and remove the other one
|
||||
hass.config_entries.async_update_entry(
|
||||
config_entry,
|
||||
options={CONF_BLOCK_CLIENT: [UNBLOCKED["mac"]]},
|
||||
options={CONF_BLOCK_CLIENT: [clients_all_payload[1]["mac"]]},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
@ -1299,21 +1254,17 @@ async def test_option_block_clients(
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
async def test_option_remove_switches(
|
||||
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
|
||||
) -> None:
|
||||
@pytest.mark.parametrize(
|
||||
"config_entry_options",
|
||||
[{CONF_TRACK_CLIENTS: False, CONF_TRACK_DEVICES: False}],
|
||||
)
|
||||
@pytest.mark.parametrize("client_payload", [[CLIENT_1]])
|
||||
@pytest.mark.parametrize("dpi_app_payload", [DPI_APPS])
|
||||
@pytest.mark.parametrize("dpi_group_payload", [DPI_GROUPS])
|
||||
async def test_option_remove_switches(hass: HomeAssistant, config_entry_setup) -> None:
|
||||
"""Test removal of DPI switch when options updated."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass,
|
||||
aioclient_mock,
|
||||
options={
|
||||
CONF_TRACK_CLIENTS: False,
|
||||
CONF_TRACK_DEVICES: False,
|
||||
},
|
||||
clients_response=[CLIENT_1],
|
||||
dpigroup_response=DPI_GROUPS,
|
||||
dpiapp_response=DPI_APPS,
|
||||
)
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
# Disable DPI Switches
|
||||
@ -1325,17 +1276,18 @@ async def test_option_remove_switches(
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_payload", [[DEVICE_1]])
|
||||
async def test_poe_port_switches(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
websocket_mock,
|
||||
config_entry_setup,
|
||||
device_payload,
|
||||
) -> None:
|
||||
"""Test the update_items function with some clients."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass, aioclient_mock, devices_response=[DEVICE_1]
|
||||
)
|
||||
"""Test PoE port entities work."""
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
@ -1350,7 +1302,7 @@ async def test_poe_port_switches(
|
||||
entity_registry.async_update_entity(
|
||||
entity_id="switch.mock_name_port_2_poe", disabled_by=None
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
# await hass.async_block_till_done()
|
||||
|
||||
async_fire_time_changed(
|
||||
hass,
|
||||
@ -1365,7 +1317,7 @@ async def test_poe_port_switches(
|
||||
assert switch_1.attributes.get(ATTR_DEVICE_CLASS) == SwitchDeviceClass.OUTLET
|
||||
|
||||
# Update state object
|
||||
device_1 = deepcopy(DEVICE_1)
|
||||
device_1 = deepcopy(device_payload[0])
|
||||
device_1["port_table"][0]["poe_mode"] = "off"
|
||||
mock_unifi_websocket(message=MessageKey.DEVICE, data=device_1)
|
||||
await hass.async_block_till_done()
|
||||
@ -1437,17 +1389,18 @@ async def test_poe_port_switches(
|
||||
assert hass.states.get("switch.mock_name_port_1_poe").state == STATE_OFF
|
||||
|
||||
|
||||
@pytest.mark.parametrize("wlan_payload", [[WLAN]])
|
||||
async def test_wlan_switches(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
websocket_mock,
|
||||
config_entry_setup,
|
||||
wlan_payload,
|
||||
) -> None:
|
||||
"""Test control of UniFi WLAN availability."""
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass, aioclient_mock, wlans_response=[WLAN]
|
||||
)
|
||||
config_entry = config_entry_setup
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
@ -1462,7 +1415,7 @@ async def test_wlan_switches(
|
||||
assert switch_1.attributes.get(ATTR_DEVICE_CLASS) == SwitchDeviceClass.SWITCH
|
||||
|
||||
# Update state object
|
||||
wlan = deepcopy(WLAN)
|
||||
wlan = deepcopy(wlan_payload[0])
|
||||
wlan["enabled"] = False
|
||||
mock_unifi_websocket(message=MessageKey.WLAN_CONF_UPDATED, data=wlan)
|
||||
await hass.async_block_till_done()
|
||||
@ -1472,7 +1425,7 @@ async def test_wlan_switches(
|
||||
aioclient_mock.clear_requests()
|
||||
aioclient_mock.put(
|
||||
f"https://{config_entry.data[CONF_HOST]}:1234"
|
||||
f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/wlanconf/{WLAN['_id']}",
|
||||
f"/api/s/{config_entry.data[CONF_SITE_ID]}/rest/wlanconf/{wlan['_id']}",
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
@ -1505,30 +1458,36 @@ async def test_wlan_switches(
|
||||
assert hass.states.get("switch.ssid_1").state == STATE_OFF
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"port_forward_payload",
|
||||
[
|
||||
[
|
||||
{
|
||||
"_id": "5a32aa4ee4b0412345678911",
|
||||
"dst_port": "12345",
|
||||
"enabled": True,
|
||||
"fwd_port": "23456",
|
||||
"fwd": "10.0.0.2",
|
||||
"name": "plex",
|
||||
"pfwd_interface": "wan",
|
||||
"proto": "tcp_udp",
|
||||
"site_id": "5a32aa4ee4b0412345678910",
|
||||
"src": "any",
|
||||
}
|
||||
]
|
||||
],
|
||||
)
|
||||
async def test_port_forwarding_switches(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
mock_unifi_websocket,
|
||||
websocket_mock,
|
||||
config_entry_setup,
|
||||
port_forward_payload,
|
||||
) -> None:
|
||||
"""Test control of UniFi port forwarding."""
|
||||
_data = {
|
||||
"_id": "5a32aa4ee4b0412345678911",
|
||||
"dst_port": "12345",
|
||||
"enabled": True,
|
||||
"fwd_port": "23456",
|
||||
"fwd": "10.0.0.2",
|
||||
"name": "plex",
|
||||
"pfwd_interface": "wan",
|
||||
"proto": "tcp_udp",
|
||||
"site_id": "5a32aa4ee4b0412345678910",
|
||||
"src": "any",
|
||||
}
|
||||
config_entry = await setup_unifi_integration(
|
||||
hass, aioclient_mock, port_forward_response=[_data.copy()]
|
||||
)
|
||||
|
||||
config_entry = config_entry_setup
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
|
||||
|
||||
ent_reg_entry = entity_registry.async_get("switch.unifi_network_plex")
|
||||
@ -1542,7 +1501,7 @@ async def test_port_forwarding_switches(
|
||||
assert switch_1.attributes.get(ATTR_DEVICE_CLASS) == SwitchDeviceClass.SWITCH
|
||||
|
||||
# Update state object
|
||||
data = _data.copy()
|
||||
data = port_forward_payload[0].copy()
|
||||
data["enabled"] = False
|
||||
mock_unifi_websocket(message=MessageKey.PORT_FORWARD_UPDATED, data=data)
|
||||
await hass.async_block_till_done()
|
||||
@ -1562,7 +1521,7 @@ async def test_port_forwarding_switches(
|
||||
blocking=True,
|
||||
)
|
||||
assert aioclient_mock.call_count == 1
|
||||
data = _data.copy()
|
||||
data = port_forward_payload[0].copy()
|
||||
data["enabled"] = False
|
||||
assert aioclient_mock.mock_calls[0][2] == data
|
||||
|
||||
@ -1574,7 +1533,7 @@ async def test_port_forwarding_switches(
|
||||
blocking=True,
|
||||
)
|
||||
assert aioclient_mock.call_count == 2
|
||||
assert aioclient_mock.mock_calls[1][2] == _data
|
||||
assert aioclient_mock.mock_calls[1][2] == port_forward_payload[0]
|
||||
|
||||
# Availability signalling
|
||||
|
||||
@ -1587,72 +1546,74 @@ async def test_port_forwarding_switches(
|
||||
assert hass.states.get("switch.unifi_network_plex").state == STATE_OFF
|
||||
|
||||
# Remove entity on deleted message
|
||||
mock_unifi_websocket(message=MessageKey.PORT_FORWARD_DELETED, data=_data)
|
||||
mock_unifi_websocket(
|
||||
message=MessageKey.PORT_FORWARD_DELETED, data=port_forward_payload[0]
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"device_payload",
|
||||
[
|
||||
[
|
||||
OUTLET_UP1,
|
||||
{
|
||||
"board_rev": 3,
|
||||
"device_id": "mock-id",
|
||||
"ip": "10.0.0.1",
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"model": "US16P150",
|
||||
"name": "switch",
|
||||
"state": 1,
|
||||
"type": "usw",
|
||||
"version": "4.0.42.10433",
|
||||
"port_table": [
|
||||
{
|
||||
"media": "GE",
|
||||
"name": "Port 1",
|
||||
"port_idx": 1,
|
||||
"poe_caps": 7,
|
||||
"poe_class": "Class 4",
|
||||
"poe_enable": True,
|
||||
"poe_mode": "auto",
|
||||
"poe_power": "2.56",
|
||||
"poe_voltage": "53.40",
|
||||
"portconf_id": "1a1",
|
||||
"port_poe": True,
|
||||
"up": True,
|
||||
},
|
||||
],
|
||||
},
|
||||
]
|
||||
],
|
||||
)
|
||||
async def test_updating_unique_id(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
config_entry_factory,
|
||||
config_entry,
|
||||
device_payload,
|
||||
) -> None:
|
||||
"""Verify outlet control and poe control unique ID update works."""
|
||||
poe_device = {
|
||||
"board_rev": 3,
|
||||
"device_id": "mock-id",
|
||||
"ip": "10.0.0.1",
|
||||
"last_seen": 1562600145,
|
||||
"mac": "00:00:00:00:01:01",
|
||||
"model": "US16P150",
|
||||
"name": "switch",
|
||||
"state": 1,
|
||||
"type": "usw",
|
||||
"version": "4.0.42.10433",
|
||||
"port_table": [
|
||||
{
|
||||
"media": "GE",
|
||||
"name": "Port 1",
|
||||
"port_idx": 1,
|
||||
"poe_caps": 7,
|
||||
"poe_class": "Class 4",
|
||||
"poe_enable": True,
|
||||
"poe_mode": "auto",
|
||||
"poe_power": "2.56",
|
||||
"poe_voltage": "53.40",
|
||||
"portconf_id": "1a1",
|
||||
"port_poe": True,
|
||||
"up": True,
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
config_entry = MockConfigEntry(
|
||||
domain=UNIFI_DOMAIN,
|
||||
data=ENTRY_CONFIG,
|
||||
source="test",
|
||||
options={},
|
||||
entry_id="1",
|
||||
)
|
||||
|
||||
entity_registry.async_get_or_create(
|
||||
SWITCH_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{poe_device["mac"]}-poe-1',
|
||||
suggested_object_id="switch_port_1_poe",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
entity_registry.async_get_or_create(
|
||||
SWITCH_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{OUTLET_UP1["mac"]}-outlet-1',
|
||||
f'{device_payload[0]["mac"]}-outlet-1',
|
||||
suggested_object_id="plug_outlet_1",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
await setup_unifi_integration(
|
||||
hass, aioclient_mock, devices_response=[poe_device, OUTLET_UP1]
|
||||
entity_registry.async_get_or_create(
|
||||
SWITCH_DOMAIN,
|
||||
UNIFI_DOMAIN,
|
||||
f'{device_payload[1]["mac"]}-poe-1',
|
||||
suggested_object_id="switch_port_1_poe",
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
await config_entry_factory()
|
||||
|
||||
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2
|
||||
assert hass.states.get("switch.switch_port_1_poe")
|
||||
assert hass.states.get("switch.plug_outlet_1")
|
||||
assert hass.states.get("switch.switch_port_1_poe")
|
||||
|
Loading…
x
Reference in New Issue
Block a user