Add Traffic Rule switches to UniFi Network (#118821)

* Add Traffic Rule switches to UniFi Network

* Retrieve Fix unifi traffic rule switches

Poll for traffic rule updates; have immediate feedback in the UI for modifying traffic rules

* Remove default values for unifi entity; Remove unnecessary code

* Begin updating traffic rule unit tests

* For the mock get request, allow for meta and data properties to not be appended to support v2 api requests

Fix traffic rule unit tests;

* inspect path to determine json response instead of passing an argument

* Remove entity id parameter from tests; remove unused code; rename traffic rule unique ID prefix

* Remove parameter with default.

* More code removal;

* Rename copy/paste variable; remove commented code; remove duplicate default code

---------

Co-authored-by: ViViDboarder <ViViDboarder@gmail.com>
This commit is contained in:
bdowden 2024-07-30 11:26:08 -04:00 committed by GitHub
parent be24475cee
commit 18a7d15d14
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 164 additions and 8 deletions

View file

@ -160,6 +160,7 @@ def fixture_request(
dpi_app_payload: list[dict[str, Any]],
dpi_group_payload: list[dict[str, Any]],
port_forward_payload: list[dict[str, Any]],
traffic_rule_payload: list[dict[str, Any]],
site_payload: list[dict[str, Any]],
system_information_payload: list[dict[str, Any]],
wlan_payload: list[dict[str, Any]],
@ -170,9 +171,16 @@ def fixture_request(
url = f"https://{host}:{DEFAULT_PORT}"
def mock_get_request(path: str, payload: list[dict[str, Any]]) -> None:
# APIV2 request respoonses have `meta` and `data` automatically appended
json = {}
if path.startswith("/v2"):
json = payload
else:
json = {"meta": {"rc": "OK"}, "data": payload}
aioclient_mock.get(
f"{url}{path}",
json={"meta": {"rc": "OK"}, "data": payload},
json=json,
headers={"content-type": CONTENT_TYPE_JSON},
)
@ -182,6 +190,7 @@ def fixture_request(
json={"data": "login successful", "meta": {"rc": "ok"}},
headers={"content-type": CONTENT_TYPE_JSON},
)
mock_get_request("/api/self/sites", site_payload)
mock_get_request(f"/api/s/{site_id}/stat/sta", client_payload)
mock_get_request(f"/api/s/{site_id}/rest/user", clients_all_payload)
@ -191,6 +200,7 @@ def fixture_request(
mock_get_request(f"/api/s/{site_id}/rest/portforward", port_forward_payload)
mock_get_request(f"/api/s/{site_id}/stat/sysinfo", system_information_payload)
mock_get_request(f"/api/s/{site_id}/rest/wlanconf", wlan_payload)
mock_get_request(f"/v2/api/site/{site_id}/trafficrules", traffic_rule_payload)
return __mock_requests
@ -262,6 +272,12 @@ def fixture_system_information_data() -> list[dict[str, Any]]:
]
@pytest.fixture(name="traffic_rule_payload")
def traffic_rule_payload_data() -> list[dict[str, Any]]:
"""Traffic rule data."""
return []
@pytest.fixture(name="wlan_payload")
def fixture_wlan_data() -> list[dict[str, Any]]:
"""WLAN data."""

View file

@ -774,6 +774,37 @@ PORT_FORWARD_PLEX = {
"src": "any",
}
TRAFFIC_RULE = {
"_id": "6452cd9b859d5b11aa002ea1",
"action": "BLOCK",
"app_category_ids": [],
"app_ids": [],
"bandwidth_limit": {
"download_limit_kbps": 1024,
"enabled": False,
"upload_limit_kbps": 1024,
},
"description": "Test Traffic Rule",
"name": "Test Traffic Rule",
"domains": [],
"enabled": True,
"ip_addresses": [],
"ip_ranges": [],
"matching_target": "INTERNET",
"network_ids": [],
"regions": [],
"schedule": {
"date_end": "2023-05-10",
"date_start": "2023-05-03",
"mode": "ALWAYS",
"repeat_on_days": [],
"time_all_day": False,
"time_range_end": "12:00",
"time_range_start": "09:00",
},
"target_devices": [{"client_mac": CLIENT_1["mac"], "type": "CLIENT"}],
}
@pytest.mark.parametrize("client_payload", [[CONTROLLER_HOST]])
@pytest.mark.parametrize("device_payload", [[DEVICE_1]])
@ -1072,6 +1103,64 @@ async def test_dpi_switches_add_second_app(
assert hass.states.get("switch.block_media_streaming").state == STATE_ON
@pytest.mark.parametrize(("traffic_rule_payload"), [([TRAFFIC_RULE])])
@pytest.mark.usefixtures("config_entry_setup")
async def test_traffic_rules(
hass: HomeAssistant,
aioclient_mock: AiohttpClientMocker,
mock_websocket_message,
config_entry_setup: ConfigEntry,
traffic_rule_payload: list[dict[str, Any]],
) -> None:
"""Test control of UniFi traffic rules."""
assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 1
# Validate state object
switch_1 = hass.states.get("switch.unifi_network_test_traffic_rule")
assert switch_1.state == STATE_ON
assert switch_1.attributes.get(ATTR_DEVICE_CLASS) == SwitchDeviceClass.SWITCH
traffic_rule = deepcopy(traffic_rule_payload[0])
# Disable traffic rule
aioclient_mock.put(
f"https://{config_entry_setup.data[CONF_HOST]}:1234"
f"/v2/api/site/{config_entry_setup.data[CONF_SITE_ID]}/trafficrules/{traffic_rule['_id']}",
)
call_count = aioclient_mock.call_count
await hass.services.async_call(
SWITCH_DOMAIN,
"turn_off",
{"entity_id": "switch.unifi_network_test_traffic_rule"},
blocking=True,
)
# Updating the value for traffic rules will make another call to retrieve the values
assert aioclient_mock.call_count == call_count + 2
expected_disable_call = deepcopy(traffic_rule)
expected_disable_call["enabled"] = False
assert aioclient_mock.mock_calls[call_count][2] == expected_disable_call
call_count = aioclient_mock.call_count
# Enable traffic rule
await hass.services.async_call(
SWITCH_DOMAIN,
"turn_on",
{"entity_id": "switch.unifi_network_test_traffic_rule"},
blocking=True,
)
expected_enable_call = deepcopy(traffic_rule)
expected_enable_call["enabled"] = True
assert aioclient_mock.call_count == call_count + 2
assert aioclient_mock.mock_calls[call_count][2] == expected_enable_call
@pytest.mark.parametrize(
("device_payload", "entity_id", "outlet_index", "expected_switches"),
[