Skip to content

Commit

Permalink
Add update_clients service (#383)
Browse files Browse the repository at this point in the history
* Add `update_clients` service

* Add test for the `update_client_list` action
  • Loading branch information
Vaskivskyi authored Nov 20, 2023
1 parent b36b52e commit 43a2f5f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 110 deletions.
2 changes: 1 addition & 1 deletion asusrouter/asusrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ async def async_get_data(self, datatype: AsusData, force: bool = False) -> Any:

async def async_run_service(
self,
service: str,
service: Optional[str],
arguments: Optional[dict[str, Any]] = None,
apply: bool = False,
expect_modify: bool = True,
Expand Down
99 changes: 0 additions & 99 deletions asusrouter/modules/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,105 +23,6 @@
safe_unpack_key,
)

# # Map of the parameters for connected device
# # value_to_find: [where_to_search, method to convert]
# # List is sorted by importance with the most important first
# MAP_CONNECTED_DEVICE: dict[str, list[SearchKey]] = {
# "connected_since": [
# SearchKey("wlConnectTime", time_from_delta),
# ],
# CONNECTION_TYPE: [
# SearchKey(CONNECTION_TYPE),
# SearchKey("isWL", int_from_str),
# ],
# GUEST: [
# SearchKey(GUEST),
# SearchKey("isGN", int_from_str),
# ],
# "internet_mode": [
# SearchKey("internetMode"),
# ],
# "internet_state": [
# SearchKey("internetState", bool_from_any),
# ],
# IP: [
# SearchKey(IP),
# ],
# "ip_method": [
# SearchKey("ipMethod"),
# ],
# MAC: [
# SearchKey(MAC),
# ],
# NAME: [
# SearchKey("nickName"),
# SearchKey(NAME),
# SearchKey(MAC),
# ],
# NODE: [
# SearchKey(NODE),
# ],
# ONLINE: [
# SearchKey(ONLINE),
# SearchKey("isOnline", bool_from_any),
# ],
# RSSI: [
# SearchKey(RSSI, int_from_str),
# ],
# "rx_speed": [
# SearchKey("curRx", float_from_str),
# ],
# "tx_speed": [
# SearchKey("curTx", float_from_str),
# ],
# }


# List of available attributes for a device:
#
# "amesh_bind_band": "0",
# "amesh_bind_mac": "",
# "amesh_isRe": "0",
# "amesh_isReClient": "1",
# "amesh_papMac": "7C:10:C9:03:6D:90",
# "callback": "",
# "curRx": " ",
# "curTx": "1",
# "defaultType": "0",
# "dpiDevice": "",
# "dpiType": "",
# "group": "",
# "internetMode": "block",
# "internetState": 0,
# "ip": "192.168.55.112",
# "ipMethod": "Manual",
# "isGN": "",
# "isGateway": "0",
# "isITunes": "0",
# "isLogin": "0",
# "isOnline": "1",
# "isPrinter": "0",
# "isWL": "1",
# "isWebServer": "0",
# "keeparp": "",
# "mac": "E0:98:06:C4:25:92",
# "macRepeat": "0",
# "name": "Espressif Inc ",
# "nickName": "Fr/Socket/Bookshelf",
# "opMode": "0",
# "os_type": 0,
# "qosLevel": "",
# "ROG": "0",
# "rssi": "-55",
# "ssid": "",
# "totalRx": "",
# "totalTx": "",
# "type": "0",
# "vendor": "Espressif Inc.",
# "vendorclass": "Espressif Inc.",
# "wtfast": "0",
# "wlConnectTime": "00:00:03"


@dataclass
class AsusClient:
Expand Down
22 changes: 15 additions & 7 deletions asusrouter/modules/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@

async def async_call_service(
callback: Callable[..., Awaitable[dict[str, Any]]],
service: str,
service: Optional[str],
arguments: Optional[dict[str, Any]] = None,
apply: bool = False,
expect_modify: bool = True,
) -> Tuple[bool, Optional[int], Optional[int]]:
"""Call a service."""

# Generate commands
commands = {
"rc_service": service,
}
commands = {}

# Service call provided
if service is not None:
commands["rc_service"] = service

# Check arguments
if not arguments:
Expand All @@ -42,9 +44,11 @@ async def async_call_service(
except Exception as ex: # pylint: disable=broad-except
raise ex

run_service = result.get("run_service", None)
if run_service != service:
raise AsusRouterServiceError(f"Service not run. Raw result: {result}")
if service is not None:
# Check if the service is run
run_service = result.get("run_service", None)
if run_service != service:
raise AsusRouterServiceError(f"Service not run. Raw result: {result}")

_LOGGER.debug(
"Service `%s` run with arguments `%s`. Result: `%s`", service, arguments, result
Expand All @@ -59,6 +63,10 @@ async def async_call_service(
if needed_time is None and last_id is not None:
needed_time = 5

# Special services that won't return any result
if arguments.get("action_mode") == "update_client_list":
return (True, needed_time, last_id)

if expect_modify:
return (safe_bool(result.get("modify")) or False, needed_time, last_id)

Expand Down
10 changes: 10 additions & 0 deletions asusrouter/modules/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class AsusSystem(str, Enum):
RESTART_FIREWALL = "restart_firewall"
RESTART_HTTPD = "restart_httpd"
RESTART_WIRELESS = "restart_wireless"
UPDATE_CLIENTS = "update_clients"


async def set_state(
Expand All @@ -28,6 +29,15 @@ async def set_state(
if not arguments:
arguments = {}

# Special cases
if state == AsusSystem.UPDATE_CLIENTS:
return await callback(
service=None,
arguments={"action_mode": "update_client_list"},
apply=False,
expect_modify=False,
)

# Run the service
return await callback(
service=state.value,
Expand Down
15 changes: 12 additions & 3 deletions tests/modules/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ async def callback(arguments: dict[str, str]) -> dict[str, str]:
None,
), # Don't apply, don't expect
("restart_httpd", {"id": "1"}, True, True, True, 5, 1), # Provided ID
(
None,
{"action_mode": "update_client_list"},
False,
False,
True,
None,
None,
), # Special service
],
)
async def test_async_call_service(
async def test_async_call_service( # pylint: disable=too-many-arguments
service,
arguments,
apply,
Expand Down Expand Up @@ -80,7 +89,7 @@ async def test_async_call_service(
async def test_async_call_service_failing_callback(exception):
"""Test the async_call_service method with a failing callback."""

async def failing_callback(arguments):
async def failing_callback(_):
raise exception("Test exception")

with pytest.raises(exception):
Expand All @@ -91,7 +100,7 @@ async def failing_callback(arguments):
async def test_async_call_service_with_invalid_service():
"""Test the async_call_service method with an invalid service."""

async def invalid_callback(arguments):
async def invalid_callback(_):
return {"run_service": "invalid_service"}

with pytest.raises(AsusRouterServiceError):
Expand Down

0 comments on commit 43a2f5f

Please sign in to comment.