| 1 |
"""Onboarding endpoint module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from asusrouter.modules.aimesh import AiMeshDevice |
| 8 |
from asusrouter.modules.data import AsusData |
| 9 |
from asusrouter.tools.converters import safe_bool, safe_int, safe_return |
| 10 |
from asusrouter.tools.readers import read_js_variables as read |
| 11 |
|
| 12 |
__all__ = ["read"] |
| 13 |
|
| 14 |
CONNECTION_TYPE = { |
| 15 |
"2G": 1, |
| 16 |
"5G": 2, |
| 17 |
"5G1": 3, |
| 18 |
"5G2": 3, |
| 19 |
"6G": 4, |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
CONST_AP = { |
| 24 |
"2g": "2ghz", |
| 25 |
"5g": "5ghz", |
| 26 |
"5g1": "5ghz2", |
| 27 |
"6g": "6ghz", |
| 28 |
"dwb": "dwb", |
| 29 |
} |
| 30 |
|
| 31 |
CONST_FREQ = [ |
| 32 |
"2g", |
| 33 |
"5g", |
| 34 |
"6g", |
| 35 |
] |
| 36 |
|
| 37 |
|
| 38 |
def process(data: dict[str, Any]) -> dict[AsusData, Any]: |
| 39 |
"""Process the onboarding data.""" |
| 40 |
|
| 41 |
state: dict[AsusData, Any] = {} |
| 42 |
|
| 43 |
# AiMesh nodes state |
| 44 |
state[AsusData.AIMESH] = { |
| 45 |
node.mac: node |
| 46 |
for device in data.get("get_cfg_clientlist", [[]])[0] |
| 47 |
for node in [process_aimesh_node(device)] |
| 48 |
} |
| 49 |
|
| 50 |
# Client list |
| 51 |
clients = {} |
| 52 |
client_list = data.get("get_allclientlist", [{}])[0] |
| 53 |
for node in client_list: |
| 54 |
for connection in client_list[node]: |
| 55 |
convert = process_connection(connection) |
| 56 |
for mac in client_list[node][connection]: |
| 57 |
description = { |
| 58 |
"connection_type": convert.get("connection_type"), |
| 59 |
"guest": convert.get("guest"), |
| 60 |
"ip": safe_return( |
| 61 |
client_list[node][connection][mac].get("ip", None) |
| 62 |
), |
| 63 |
"mac": mac, |
| 64 |
"node": node, |
| 65 |
"online": True, |
| 66 |
"rssi": client_list[node][connection][mac].get( |
| 67 |
"rssi", None |
| 68 |
), |
| 69 |
} |
| 70 |
clients[mac] = description |
| 71 |
|
| 72 |
state[AsusData.CLIENTS] = clients |
| 73 |
|
| 74 |
return state |
| 75 |
|
| 76 |
|
| 77 |
def process_aimesh_node(data: dict[str, Any]) -> AiMeshDevice: |
| 78 |
"""Process AiMesh node data.""" |
| 79 |
|
| 80 |
# Get the list of WLAN APs |
| 81 |
ap = {} |
| 82 |
for el, value in CONST_AP.items(): |
| 83 |
if f"ap{el}" in data and data[f"ap{el}"] != "": |
| 84 |
ap[value] = data[f"ap{el}"] |
| 85 |
|
| 86 |
# Get the parent data |
| 87 |
parent = {} |
| 88 |
for el in CONST_FREQ: |
| 89 |
if f"pap{el}" in data and data[f"pap{el}"] != "": |
| 90 |
parent["connection"] = CONST_AP[el] |
| 91 |
parent["mac"] = data[f"pap{el}"] |
| 92 |
parent["rssi"] = safe_return(data.get(f"rssi{el}")) |
| 93 |
parent["ssid"] = safe_return(data.get(f"pap{el}_ssid")) |
| 94 |
|
| 95 |
# Stop the loop since we found the parent |
| 96 |
break |
| 97 |
|
| 98 |
level = safe_int(data.get("level", "0")) |
| 99 |
node_type = "router" if level == 0 else "node" |
| 100 |
|
| 101 |
return AiMeshDevice( |
| 102 |
status=safe_bool(data.get("online", 0)) or False, |
| 103 |
alias=data.get("alias"), |
| 104 |
model=data.get("ui_model_name", data.get("model_name")), |
| 105 |
product_id=data.get("product_id"), |
| 106 |
ip=data.get("ip"), |
| 107 |
fw=data.get("fwver"), |
| 108 |
fw_new=safe_return(data.get("newfwver")), |
| 109 |
mac=data.get("mac"), |
| 110 |
ap=ap, |
| 111 |
parent=parent, |
| 112 |
type=node_type, |
| 113 |
level=level, |
| 114 |
config=data.get("config"), |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
def process_connection(data: str) -> dict[str, int]: |
| 119 |
"""Process connection data.""" |
| 120 |
|
| 121 |
# Check that the data is not empty |
| 122 |
if not isinstance(data, str) or data == "": |
| 123 |
return {} |
| 124 |
|
| 125 |
if data == "wired_mac": |
| 126 |
return {"connection_type": 0, "guest": 0} |
| 127 |
|
| 128 |
temp = data.split("_") |
| 129 |
return { |
| 130 |
"connection_type": CONNECTION_TYPE.get(temp[0]) or 0, |
| 131 |
"guest": safe_int(temp[1], 0) if len(temp) > 1 else 0, |
| 132 |
} |