| 1 |
"""System module for AsusRouter.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections.abc import Awaitable, Callable |
| 6 |
from enum import Enum |
| 7 |
import logging |
| 8 |
from typing import Any |
| 9 |
|
| 10 |
_LOGGER = logging.getLogger(__name__) |
| 11 |
|
| 12 |
ACTION_MODE = "action_mode" |
| 13 |
APPLY = "apply" |
| 14 |
ARGUMENTS = "arguments" |
| 15 |
EXPECT_MODIFY = "expect_modify" |
| 16 |
SERVICE = "service" |
| 17 |
|
| 18 |
|
| 19 |
class AsusSystem(str, Enum): |
| 20 |
"""Asus system enum. |
| 21 |
|
| 22 |
This enum contains all known system services. The availability of these |
| 23 |
services depends on the router model and firmware version. |
| 24 |
|
| 25 |
Some services might not be tested and might not work as expected. Use with |
| 26 |
caution and at your own risk. |
| 27 |
""" |
| 28 |
|
| 29 |
# --------------------- |
| 30 |
# DISCLAIMER: Migration to the new format |
| 31 |
# |
| 32 |
# AsusSystem enum is being migrated to a new format. Instead of |
| 33 |
# `{ACTION}_{FEATURE}`, the format should be `{FEATURE}_{ACTION}`. This |
| 34 |
# will allow better grouping of related actions and easier access. |
| 35 |
# |
| 36 |
# E.g. `REBUILD_AIMESH` -> `AIMESH_REBUILD` |
| 37 |
# |
| 38 |
# The old format will be deprecated and removed in a future version. |
| 39 |
# Please, only add new actions in the new format. |
| 40 |
# |
| 41 |
# All the old actions will be migrated to the new format with time |
| 42 |
# (and updates in the corresponding modules). Any deprecated actions |
| 43 |
# with a new format should be added to the `AsusSystemDeprecated` dict. |
| 44 |
# |
| 45 |
# Check the AiMesh for an example of the new format. |
| 46 |
# --------------------- |
| 47 |
|
| 48 |
# --------------------- |
| 49 |
# AiMesh |
| 50 |
AIMESH_ADD_NODE = "ob_selection" # Add a new node to AiMesh |
| 51 |
AIMESH_ONBOARDING = ( |
| 52 |
"onboarding" # Start AiMesh onboarding (search for nodes) |
| 53 |
) |
| 54 |
AIMESH_REBOOT = "device_reboot" # Restart router + all nodes |
| 55 |
AIMESH_REBUILD = "re_reconnect" # Rebuild AiMesh |
| 56 |
NODE_CONFIG_CHANGE = "config_changed" # Change node configuration |
| 57 |
NODE_REBOOT = "node_reboot" # Reboot a node by MAC (when specified) |
| 58 |
REBUILD_AIMESH = "_depr_re_reconnect" # Rebuild AiMesh / legacy name |
| 59 |
# --------------------- |
| 60 |
# Aura |
| 61 |
AURA_RESTART = "restart_ledg" # Restart Aura RGB |
| 62 |
# --------------------- |
| 63 |
# DDNS |
| 64 |
DDNS_RESTART = "restart_ddns_le" # Restart DDNS |
| 65 |
RESTART_DDNS_LE = "_depr_restart_ddns_le" # Restart DDNS / legacy name |
| 66 |
# --------------------- |
| 67 |
# Firmware |
| 68 |
FIRMWARE_CHECK = "firmware_check" # Check for firmware update |
| 69 |
# Firmware upgrade will upgrade the firmware to the latest version |
| 70 |
# if available. The firmware file is downloaded from the Asus server. |
| 71 |
# After the download, the router will reboot and install the firmware. |
| 72 |
FIRMWARE_UPGRADE = "firmware_upgrade" # Firmware upgrade |
| 73 |
# --------------------- |
| 74 |
# Upgrades and updates |
| 75 |
UPGRADE_RESTART = "restart_upgrade" # Upgrade firmware and restart device |
| 76 |
UPGRADE_START = "start_upgrade" # Start firmware upgrade / installation |
| 77 |
UPGRADE_STOP = "stop_upgrade" # Stop firmware upgrade / installation |
| 78 |
RESTART_UPGRADE = "_depr_restart_upgrade" |
| 79 |
START_UPGRADE = "_depr_start_upgrade" |
| 80 |
STOP_UPGRADE = "_depr_stop_upgrade" |
| 81 |
# --------------------- |
| 82 |
PREPARE_CERT = "prepare_cert" # Prepare certificate |
| 83 |
REBOOT = "reboot" # Reboot the router |
| 84 |
RESTART_CHPASS = "restart_chpass" |
| 85 |
RESTART_CLOUDSYNC = "restart_cloudsync" # AiCloud 2.0 sync |
| 86 |
RESTART_CP = "restart_CP" # Captive Portal |
| 87 |
RESTART_DEFAULT_WAN = "restart_default_wan" # Default WAN |
| 88 |
RESTART_DISKMON = "restart_diskmon" # Disk monitor |
| 89 |
RESTART_DNSFILTER = "restart_dnsfilter" # DNS filter |
| 90 |
RESTART_DNSMASQ = "restart_dnsmasq" # DNS |
| 91 |
RESTART_DNSQD = "restart_dnsqd" |
| 92 |
RESTART_FIREWALL = "restart_firewall" # Firewall |
| 93 |
RESTART_FTPD = "restart_ftpd" # FTP server |
| 94 |
RESTART_FTPSAMBA = "restart_ftpsamba" |
| 95 |
RESTART_HTTPD = "restart_httpd" # Web server |
| 96 |
RESTART_KEY_GUARD = "restart_key_guard" |
| 97 |
RESTART_LEDS = "restart_leds" |
| 98 |
RESTART_LPD = "restart_lpd" # Line Printer Daemon (LPD) |
| 99 |
RESTART_NASAPPS = "restart_nasapps" |
| 100 |
RESTART_NET = "restart_net" # Restart all wired interfaces |
| 101 |
RESTART_NET_AND_PHY = ( |
| 102 |
"restart_net_and_phy" # Restart all network interfaces |
| 103 |
) |
| 104 |
RESTART_NFCM = "restart_nfcm" |
| 105 |
RESTART_OAM = "restart_oam" |
| 106 |
RESTART_OPENVPND = "restart_openvpnd" # OpenVPN daemon |
| 107 |
RESTART_QOS = "restart_qos" # Quality of Service (QoS) |
| 108 |
RESTART_ROUTERBOOST = "restart_routerboost" |
| 109 |
RESTART_SAMBA = "restart_samba" # Samba file sharing |
| 110 |
RESTART_SETTINGS_WEBDAV = "restart_settings_webdav" # WebDAV |
| 111 |
RESTART_SNMPD = ( |
| 112 |
"restart_snmpd" # Simple Network Management Protocol (SNMP) |
| 113 |
) |
| 114 |
RESTART_SUBNET = "restart_subnet" |
| 115 |
RESTART_TIME = "restart_time" |
| 116 |
RESTART_TIMEMACHINE = "restart_timemachine" |
| 117 |
RESTART_TOR = "restart_tor" # The Onion Router (TOR) |
| 118 |
RESTART_TR = "restart_tr" |
| 119 |
RESTART_U2EC = "restart_u2ec" # USB to Ethernet Connector |
| 120 |
RESTART_UPNP = "restart_upnp" # Universal Plug and Play (UPnP) |
| 121 |
RESTART_USB_IDLE = "restart_usb_idle" |
| 122 |
RESTART_VPNC = "restart_vpnc" |
| 123 |
RESTART_VPND = "restart_vpnd" # VPN daemon for legacy VPNs |
| 124 |
RESTART_WAN = ( |
| 125 |
"restart_wan" # Restart WAN connection (optional if number, e.g. 0) |
| 126 |
) |
| 127 |
RESTART_WAN_DNS = "restart_wan_dns" # Restart WAN DNS (optional if number) |
| 128 |
RESTART_WAN_IF = ( |
| 129 |
"restart_wan_if" # Restart WAN interfaces (optional if number) |
| 130 |
) |
| 131 |
RESTART_WEBDAV = "restart_webdav" # WebDAV |
| 132 |
RESTART_WIRELESS = "restart_wireless" # Restart all wireless interfaces |
| 133 |
RESTART_WLCSCAN = "restart_wlcscan" |
| 134 |
RESTART_WGS = "restart_wgs" # Restart all wireguard server interfaces |
| 135 |
RESTART_WPSIE = "restart_wpsie" |
| 136 |
RESTART_WRS = "restart_wrs" |
| 137 |
RESTART_WTFAST_RULE = "restart_wtfast_rule" |
| 138 |
RESET_LED = "reset_led" |
| 139 |
START_AURARGB = "start_aurargb" # Aura RGB |
| 140 |
START_DISKFORMAT = "start_diskformat" # Format disk |
| 141 |
START_DISKSCAN = "start_diskscan" # Scan disk |
| 142 |
START_WEBS_UPGRADE = ( |
| 143 |
"start_webs_upgrade" # Start firmware upgrade from the web |
| 144 |
) |
| 145 |
START_WEBS_UPDATE = "start_webs_update" # Check for firmware update |
| 146 |
START_WRS = "start_wrs" |
| 147 |
STOP_AURARGB = "stop_aurargb" # Aura RGB |
| 148 |
STOP_LOGGER = "stop_logger" |
| 149 |
STOP_OPENVPND = "stop_openvpnd" # Stop OpenVPN server |
| 150 |
STOP_VPNC = "stop_vpnc" |
| 151 |
STOP_VPND = "stop_vpnd" # Stop VPN server for legacy VPNs |
| 152 |
STOP_WRS_FORCE = "stop_wrs_force" |
| 153 |
UPDATE_CLIENTS = "update_clients" |
| 154 |
|
| 155 |
|
| 156 |
# Deprecated states for AsusSystem |
| 157 |
# Format: {DeprecatedState: (NewState, Version)} |
| 158 |
# Version is the version when the deprecated state will be removed (optional) |
| 159 |
AsusSystemDeprecated = { |
| 160 |
# AiMesh |
| 161 |
AsusSystem.REBUILD_AIMESH: (AsusSystem.AIMESH_REBUILD, None), |
| 162 |
# DDNS |
| 163 |
AsusSystem.RESTART_DDNS_LE: (AsusSystem.DDNS_RESTART, None), |
| 164 |
# Upgrades and updates |
| 165 |
AsusSystem.RESTART_UPGRADE: (AsusSystem.UPGRADE_RESTART, None), |
| 166 |
AsusSystem.START_UPGRADE: (AsusSystem.UPGRADE_START, None), |
| 167 |
AsusSystem.STOP_UPGRADE: (AsusSystem.UPGRADE_STOP, None), |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
# Map AsusSystem special cases to service calls |
| 172 |
STATE_MAP: dict[AsusSystem, dict[str, Any]] = { |
| 173 |
AsusSystem.AIMESH_REBOOT: { |
| 174 |
SERVICE: None, |
| 175 |
ARGUMENTS: {ACTION_MODE: AsusSystem.AIMESH_REBOOT.value}, |
| 176 |
APPLY: False, |
| 177 |
EXPECT_MODIFY: False, |
| 178 |
}, |
| 179 |
AsusSystem.NODE_CONFIG_CHANGE: { |
| 180 |
SERVICE: None, |
| 181 |
ARGUMENTS: {ACTION_MODE: AsusSystem.NODE_CONFIG_CHANGE.value}, |
| 182 |
APPLY: False, |
| 183 |
EXPECT_MODIFY: False, |
| 184 |
}, |
| 185 |
AsusSystem.NODE_REBOOT: { |
| 186 |
SERVICE: None, |
| 187 |
# NODE_REBOOT is an alias for AIMESH_REBOOT, but it requires |
| 188 |
# passing a node MAC address. For the safety, this AsusSystem |
| 189 |
# state is separated to avoid accidental reboot of the whole |
| 190 |
# AiMesh network. |
| 191 |
ARGUMENTS: {ACTION_MODE: AsusSystem.AIMESH_REBOOT.value}, |
| 192 |
APPLY: False, |
| 193 |
EXPECT_MODIFY: False, |
| 194 |
}, |
| 195 |
AsusSystem.FIRMWARE_CHECK: { |
| 196 |
SERVICE: None, |
| 197 |
ARGUMENTS: {ACTION_MODE: AsusSystem.FIRMWARE_CHECK.value}, |
| 198 |
APPLY: False, |
| 199 |
EXPECT_MODIFY: False, |
| 200 |
}, |
| 201 |
AsusSystem.FIRMWARE_UPGRADE: { |
| 202 |
SERVICE: None, |
| 203 |
ARGUMENTS: {ACTION_MODE: AsusSystem.FIRMWARE_UPGRADE.value}, |
| 204 |
APPLY: False, |
| 205 |
EXPECT_MODIFY: False, |
| 206 |
}, |
| 207 |
AsusSystem.UPDATE_CLIENTS: { |
| 208 |
SERVICE: None, |
| 209 |
ARGUMENTS: {ACTION_MODE: AsusSystem.UPDATE_CLIENTS.value}, |
| 210 |
APPLY: False, |
| 211 |
EXPECT_MODIFY: False, |
| 212 |
}, |
| 213 |
} |
| 214 |
|
| 215 |
ARGUMENTS_APPEND: dict[AsusSystem, list[str]] = { |
| 216 |
AsusSystem.NODE_CONFIG_CHANGE: ["re_mac", "config"], |
| 217 |
AsusSystem.NODE_REBOOT: ["device_list"], |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
async def set_state( |
| 222 |
callback: Callable[..., Awaitable[bool]], |
| 223 |
state: AsusSystem, |
| 224 |
**kwargs: Any, |
| 225 |
) -> bool: |
| 226 |
"""Set the system state.""" |
| 227 |
|
| 228 |
# Check and notify if the state is deprecated |
| 229 |
if state in AsusSystemDeprecated: |
| 230 |
repl_state, repl_ver = AsusSystemDeprecated[state] |
| 231 |
message = ( |
| 232 |
f"Deprecated state `{state.name}` from `AsusSystem` " |
| 233 |
f"enum used. Use `{repl_state.name}` instead" |
| 234 |
) |
| 235 |
if repl_ver: |
| 236 |
message += f". This state will be removed in version {repl_ver}" |
| 237 |
_LOGGER.warning( |
| 238 |
message, |
| 239 |
) |
| 240 |
state = repl_state |
| 241 |
|
| 242 |
arguments: dict[str, Any] = kwargs.get(ARGUMENTS, {}) |
| 243 |
|
| 244 |
# Get the arguments for the callback function based on the state |
| 245 |
callback_args: dict[str, Any] = STATE_MAP.get( |
| 246 |
state, |
| 247 |
{ |
| 248 |
SERVICE: state.value, |
| 249 |
ARGUMENTS: arguments, |
| 250 |
APPLY: True, |
| 251 |
EXPECT_MODIFY: kwargs.get(EXPECT_MODIFY, False), |
| 252 |
}, |
| 253 |
) |
| 254 |
|
| 255 |
# Append additional arguments if the state is in the append list |
| 256 |
if state in ARGUMENTS_APPEND: |
| 257 |
search_args = ARGUMENTS_APPEND[state] |
| 258 |
if not all(arg in kwargs for arg in search_args): |
| 259 |
_LOGGER.error( |
| 260 |
"Setting state `%s` requires ALL of the following " |
| 261 |
"arguments: %s", |
| 262 |
state, |
| 263 |
search_args, |
| 264 |
) |
| 265 |
return False |
| 266 |
|
| 267 |
# Append the arguments to the callback arguments |
| 268 |
callback_args[ARGUMENTS].update( |
| 269 |
{arg: kwargs[arg] for arg in search_args} |
| 270 |
) |
| 271 |
|
| 272 |
# Run the service |
| 273 |
return await callback(**callback_args) |