Skip to content

Commit

Permalink
Remove unused initialize from controller class (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 committed Apr 22, 2024
1 parent afc9fe0 commit ad3ac73
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 53 deletions.
19 changes: 18 additions & 1 deletion aiounifi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,24 @@ async def main(
await websession.close()
return

await controller.initialize()
await asyncio.gather(
*(
controller.clients.update(),
controller.clients_all.update(),
controller.devices.update(),
controller.dpi_apps.update(),
controller.dpi_groups.update(),
controller.port_forwarding.update(),
controller.sites.update(),
controller.system_information.update(),
controller.traffic_rules.update(),
controller.traffic_routes.update(),
controller.vouchers.update(),
controller.wlans.update(),
),
return_exceptions=True,
)

ws_task = asyncio.create_task(controller.start_websocket())

try:
Expand Down
29 changes: 1 addition & 28 deletions aiounifi/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

from __future__ import annotations

import asyncio
from collections.abc import Callable, Coroutine
import logging
from typing import TYPE_CHECKING, Any
from typing import TYPE_CHECKING

from .interfaces.clients import Clients
from .interfaces.clients_all import ClientsAll
Expand Down Expand Up @@ -57,21 +55,6 @@ def __init__(self, config: Configuration) -> None:
self.vouchers = Vouchers(self)
self.wlans = Wlans(self)

self.update_handlers: tuple[Callable[[], Coroutine[Any, Any, None]], ...] = (
self.clients.update,
self.clients_all.update,
self.devices.update,
self.dpi_apps.update,
self.dpi_groups.update,
self.port_forwarding.update,
self.sites.update,
self.system_information.update,
self.traffic_rules.update,
self.traffic_routes.update,
self.vouchers.update,
self.wlans.update,
)

async def login(self) -> None:
"""Log in to controller."""
await self.connectivity.check_unifi_os()
Expand All @@ -81,16 +64,6 @@ async def request(self, api_request: ApiRequest) -> TypedApiResponse:
"""Make a request to the API, retry login on failure."""
return await self.connectivity.request(api_request)

async def initialize(self) -> None:
"""Load UniFi parameters."""
results = await asyncio.gather(
*[update() for update in self.update_handlers],
return_exceptions=True,
)
for result in results:
if result is not None:
LOGGER.warning("Exception on update %s", result)

async def start_websocket(self) -> None:
"""Start websocket session."""
await self.connectivity.websocket(self.messages.new_data)
2 changes: 1 addition & 1 deletion tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ async def test_message_client_removed(
unifi_controller: Controller, new_ws_data_fn: Callable[[dict[str, Any]], None]
) -> None:
"""Test controller communicating client has been removed."""
await unifi_controller.initialize()
await unifi_controller.clients.update()
assert len(unifi_controller.clients.items()) == 1

new_ws_data_fn(MESSAGE_WIRELESS_CLIENT_REMOVED)
Expand Down
64 changes: 42 additions & 22 deletions tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,34 +258,54 @@ async def test_relogin_fails(mock_aioresponse, unifi_controller):
@pytest.mark.usefixtures("_mock_endpoints")
async def test_controller(unifi_controller, unifi_called_with, new_ws_data_fn):
"""Test controller communicating with a non UniFiOS UniFi controller."""
await unifi_controller.initialize()

await unifi_controller.clients.update()
assert unifi_called_with("get", "/api/s/default/stat/sta")
assert unifi_called_with("get", "/api/s/default/rest/user")
assert unifi_called_with("get", "/api/s/default/stat/device")
assert unifi_called_with("get", "/api/s/default/rest/dpiapp")
assert unifi_called_with("get", "/api/s/default/rest/dpigroup")
assert unifi_called_with("get", "/api/s/default/rest/portforward")
assert unifi_called_with("get", "/api/self/sites")
assert unifi_called_with("get", "/api/s/default/stat/sysinfo")
assert unifi_called_with("get", "/v2/api/site/default/trafficroutes")
assert unifi_called_with("get", "/v2/api/site/default/trafficrules")
assert unifi_called_with("get", "/api/s/default/stat/voucher")
assert unifi_called_with("get", "/api/s/default/rest/wlanconf")

assert len(unifi_controller.clients.items()) == 0

await unifi_controller.clients_all.update()
assert unifi_called_with("get", "/api/s/default/rest/user")
assert len(unifi_controller.clients_all.items()) == 0

await unifi_controller.devices.update()
assert unifi_called_with("get", "/api/s/default/stat/device")
assert len(unifi_controller.devices.items()) == 0
assert len(unifi_controller.outlets.items()) == 0
assert len(unifi_controller.ports.items()) == 0

await unifi_controller.dpi_apps.update()
assert unifi_called_with("get", "/api/s/default/rest/dpiapp")
assert len(unifi_controller.dpi_apps.items()) == 0

await unifi_controller.dpi_groups.update()
assert unifi_called_with("get", "/api/s/default/rest/dpigroup")
assert len(unifi_controller.dpi_groups.items()) == 0

await unifi_controller.port_forwarding.update()
assert unifi_called_with("get", "/api/s/default/rest/portforward")
assert len(unifi_controller.port_forwarding.items()) == 0

await unifi_controller.sites.update()
assert unifi_called_with("get", "/api/self/sites")
assert len(unifi_controller.sites.items()) == 1

await unifi_controller.system_information.update()
assert unifi_called_with("get", "/api/s/default/stat/sysinfo")
assert len(unifi_controller.system_information.items()) == 0

await unifi_controller.traffic_routes.update()
assert unifi_called_with("get", "/v2/api/site/default/trafficroutes")
assert len(unifi_controller.traffic_routes.items()) == 0

await unifi_controller.traffic_rules.update()
assert unifi_called_with("get", "/v2/api/site/default/trafficrules")
assert len(unifi_controller.traffic_rules.items()) == 0

await unifi_controller.vouchers.update()
assert unifi_called_with("get", "/api/s/default/stat/voucher")
assert len(unifi_controller.vouchers.items()) == 0

await unifi_controller.wlans.update()
assert unifi_called_with("get", "/api/s/default/rest/wlanconf")
assert len(unifi_controller.wlans.items()) == 0


Expand All @@ -305,43 +325,50 @@ async def test_unifios_controller(
content_type="application/json",
)
await unifi_controller.connectivity.login()
await unifi_controller.initialize()

await unifi_controller.clients.update()
assert unifi_called_with(
"get",
"/proxy/network/api/s/default/stat/sta",
headers={"x-csrf-token": "123"},
)
await unifi_controller.devices.update()
assert unifi_called_with(
"get",
"/proxy/network/api/s/default/stat/device",
headers={"x-csrf-token": "123"},
)
await unifi_controller.clients_all.update()
assert unifi_called_with(
"get",
"/proxy/network/api/s/default/rest/user",
headers={"x-csrf-token": "123"},
)
await unifi_controller.sites.update()
assert unifi_called_with(
"get",
"/proxy/network/api/self/sites",
headers={"x-csrf-token": "123"},
)
await unifi_controller.traffic_routes.update()
assert unifi_called_with(
"get",
"/proxy/network/v2/api/site/default/trafficroutes",
headers={"x-csrf-token": "123"},
)
await unifi_controller.traffic_rules.update()
assert unifi_called_with(
"get",
"/proxy/network/v2/api/site/default/trafficrules",
headers={"x-csrf-token": "123"},
)
await unifi_controller.vouchers.update()
assert unifi_called_with(
"get",
"/proxy/network/api/s/default/stat/voucher",
headers={"x-csrf-token": "123"},
)
await unifi_controller.wlans.update()
assert unifi_called_with(
"get",
"/proxy/network/api/s/default/rest/wlanconf",
Expand Down Expand Up @@ -433,13 +460,6 @@ async def test_controller_raise_expected_exception(
await unifi_controller.connectivity.login()


@pytest.mark.parametrize("traffic_rule_status", [404])
@pytest.mark.usefixtures("_mock_endpoints")
async def test_initialize_handles_404(unifi_controller):
"""Validate initialize does not abort on exception."""
await unifi_controller.initialize()


api_request_data = [
(
ApiRequest,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ async def test_sub_device_requests(
@pytest.mark.usefixtures("_mock_endpoints")
async def test_set_poe_request_raise_error(unifi_controller: Controller) -> None:
"""Test device class."""
await unifi_controller.initialize()
await unifi_controller.devices.update()
device = next(iter(unifi_controller.devices.values()))
with pytest.raises(AttributeError):
DeviceSetPoePortModeRequest.create(device)
Expand Down

0 comments on commit ad3ac73

Please sign in to comment.