diff --git a/asusrouter/asusrouter.py b/asusrouter/asusrouter.py index 9a869c0..bfad983 100644 --- a/asusrouter/asusrouter.py +++ b/asusrouter/asusrouter.py @@ -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, diff --git a/asusrouter/modules/client.py b/asusrouter/modules/client.py index 6bff77b..be481d6 100644 --- a/asusrouter/modules/client.py +++ b/asusrouter/modules/client.py @@ -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: diff --git a/asusrouter/modules/service.py b/asusrouter/modules/service.py index b1de672..944c9a6 100644 --- a/asusrouter/modules/service.py +++ b/asusrouter/modules/service.py @@ -13,7 +13,7 @@ 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, @@ -21,9 +21,11 @@ async def async_call_service( """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: @@ -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 @@ -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) diff --git a/asusrouter/modules/system.py b/asusrouter/modules/system.py index ea349e9..2782d37 100644 --- a/asusrouter/modules/system.py +++ b/asusrouter/modules/system.py @@ -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( @@ -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, diff --git a/tests/modules/test_service.py b/tests/modules/test_service.py index 5f4fb45..25fdc00 100644 --- a/tests/modules/test_service.py +++ b/tests/modules/test_service.py @@ -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, @@ -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): @@ -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):