| 1 |
"""Data transform module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import Any |
| 6 |
|
| 7 |
from asusrouter.modules.client import process_client |
| 8 |
from asusrouter.modules.data import AsusDataState |
| 9 |
from asusrouter.modules.ports import PortType |
| 10 |
from asusrouter.tools.readers import readable_mac |
| 11 |
|
| 12 |
# List of models with 6Ghz support |
| 13 |
# and no 5Ghz2 support |
| 14 |
MODEL_WITH_6GHZ = [ |
| 15 |
"RT-AXE95Q", |
| 16 |
] |
| 17 |
|
| 18 |
|
| 19 |
def transform_network( |
| 20 |
data: dict[str, Any], |
| 21 |
services: list[str] | None, |
| 22 |
history: AsusDataState | None, |
| 23 |
**kwargs: Any, |
| 24 |
) -> dict[str, Any]: |
| 25 |
"""Transform network data.""" |
| 26 |
|
| 27 |
# Check if the device has dualwan support |
| 28 |
if not services: |
| 29 |
return data |
| 30 |
if "dualwan" not in services: |
| 31 |
return data |
| 32 |
|
| 33 |
network = data.copy() |
| 34 |
# Add speed if not available - fix first empty value round |
| 35 |
for interface in network: |
| 36 |
for speed in ("rx_speed", "tx_speed"): |
| 37 |
if speed not in network[interface]: |
| 38 |
network[interface][speed] = 0.0 |
| 39 |
|
| 40 |
# Add usb network if not available |
| 41 |
if "usb" not in network: |
| 42 |
# Check history |
| 43 |
usb_history = ( |
| 44 |
history.data.get("usb") if history and history.data else None |
| 45 |
) |
| 46 |
# Revert to history if available |
| 47 |
if usb_history: |
| 48 |
network["usb"] = usb_history |
| 49 |
network["usb"]["rx_speed"] = 0.0 |
| 50 |
network["usb"]["tx_speed"] = 0.0 |
| 51 |
else: |
| 52 |
network["usb"] = { |
| 53 |
"rx": 0, |
| 54 |
"tx": 0, |
| 55 |
"rx_speed": 0.0, |
| 56 |
"tx_speed": 0.0, |
| 57 |
} |
| 58 |
|
| 59 |
# Get the model if available |
| 60 |
model = kwargs.get("model") |
| 61 |
|
| 62 |
# Check if we have 5GHz2 available in the network data |
| 63 |
if "5ghz2" in network: |
| 64 |
# Check interfaces for 5Ghz2/6Ghz |
| 65 |
support_5ghz2 = "5G-2" in services |
| 66 |
support_6ghz = "wifi6e" in services |
| 67 |
|
| 68 |
if ( |
| 69 |
support_5ghz2 is False and support_6ghz is True |
| 70 |
) or model in MODEL_WITH_6GHZ: |
| 71 |
# Rename 5Ghz2 to 6Ghz |
| 72 |
network["6ghz"] = network.pop("5ghz2") |
| 73 |
|
| 74 |
return network |
| 75 |
|
| 76 |
|
| 77 |
def transform_clients( |
| 78 |
data: dict[str, Any], history: AsusDataState | None, **kwargs: Any |
| 79 |
) -> dict[str, Any]: |
| 80 |
"""Transform clients data.""" |
| 81 |
|
| 82 |
clients = {} |
| 83 |
for mac, client in data.items(): |
| 84 |
if readable_mac(mac): |
| 85 |
# Check client history |
| 86 |
client_history = ( |
| 87 |
history.data.get(mac) if history and history.data else None |
| 88 |
) |
| 89 |
# Process the client |
| 90 |
clients[mac] = process_client(client, client_history, **kwargs) |
| 91 |
|
| 92 |
return clients |
| 93 |
|
| 94 |
|
| 95 |
def transform_cpu( |
| 96 |
data: dict[str, dict[str, Any]], |
| 97 |
) -> dict[str, Any]: |
| 98 |
"""Transform cpu data.""" |
| 99 |
|
| 100 |
for info in data.values(): |
| 101 |
info.setdefault("usage", None) |
| 102 |
|
| 103 |
return data |
| 104 |
|
| 105 |
|
| 106 |
def transform_ethernet_ports( |
| 107 |
data: dict[str, Any], |
| 108 |
mac: str | None, |
| 109 |
) -> dict[str, dict[str, Any]]: |
| 110 |
"""Transform the legacy ethernet ports data to the new format.""" |
| 111 |
|
| 112 |
# Check if the first level of the dict is PortType enum |
| 113 |
# If any other key is found, return the data as is |
| 114 |
for data_key in data: |
| 115 |
if not isinstance(data_key, PortType): |
| 116 |
return data |
| 117 |
|
| 118 |
# If mac is not available, return the data as is |
| 119 |
if not mac: |
| 120 |
return data |
| 121 |
|
| 122 |
# Transform the data |
| 123 |
return {mac: data} |
| 124 |
|
| 125 |
|
| 126 |
def transform_wan( |
| 127 |
data: dict[str, Any], |
| 128 |
services: list[str] | None, |
| 129 |
) -> dict[str, Any]: |
| 130 |
"""Transform WAN data.""" |
| 131 |
|
| 132 |
wan = data.copy() |
| 133 |
|
| 134 |
if not services: |
| 135 |
return wan |
| 136 |
|
| 137 |
service_keys = {"dualwan": "dualwan", "wanbonding": "aggregation"} |
| 138 |
|
| 139 |
for service, key in service_keys.items(): |
| 140 |
if service not in services: |
| 141 |
wan.pop(key, None) |
| 142 |
|
| 143 |
return wan |