Debian Codegraph @

asusrouter/modules/endpoint/port_status.py

raw
1 """Port Status endpoint module."""
2
3 from __future__ import annotations
4
5 import logging
6 from typing import Any
7
8 from asusrouter.modules.data import AsusData
9 from asusrouter.modules.ports import (
10 PORT_CAP2TYPE,
11 PORT_TYPE2LINK,
12 PortCapability,
13 PortSpeed,
14 PortType,
15 )
16 from asusrouter.tools.converters import (
17 get_enum_key_by_value,
18 int_as_capabilities,
19 safe_bool,
20 safe_int,
21 )
22 from asusrouter.tools.readers import read_json_content as read # noqa: F401
23
24 _LOGGER = logging.getLogger(__name__)
25
26
27 def process(data: dict[str, Any]) -> dict[AsusData, Any]:
28 """Process port status data."""
29
30 ports: dict[PortType, Any] = {}
31
32 if not data:
33 return {
34 AsusData.PORTS: ports,
35 AsusData.NODE_INFO: {},
36 }
37
38 state: dict[AsusData, Any] = {}
39
40 # Node info
41 state[AsusData.NODE_INFO] = process_node_info(data)
42
43 # Port info
44 port_info = data.get("port_info", {})
45 if port_info:
46 for mac, info in port_info.items():
47 ports[mac] = {}
48
49 for port, values in info.items():
50 # Process the port info
51 port_description, port_type, port_id = process_port_info(
52 port, values
53 )
54
55 # Create a port type group if it doesn't exist yet
56 if port_type not in ports[mac]:
57 ports[mac][port_type] = {}
58
59 # Save the port info
60 ports[mac][port_type][port_id] = port_description
61
62 state[AsusData.PORTS] = ports
63
64 return state
65
66
67 def process_node_info(data: dict[str, Any]) -> dict[str, Any]:
68 """Process node info data."""
69
70 node_info = data.get("node_info", {})
71
72 if not node_info:
73 return {}
74
75 return dict(node_info.items())
76
77
78 def process_port_info(
79 port: str, values: dict[str, Any]
80 ) -> tuple[dict[str, Any], PortType, int]:
81 """Process port info data."""
82
83 # The port is a string with the format `port_label:port_id`
84 # e.g. `L1` for LAN port 1 or `W0` for WAN port 0 (the main)
85 # port_label = port[0]
86 port_id = safe_int(port[1])
87
88 # Get the capabilities of the port
89 port_capabilities = int_as_capabilities(
90 safe_int(values.get("cap")), PortCapability
91 )
92
93 # Get the port type
94 port_type = PortType.UNKNOWN
95 for pcap, ptype in PORT_CAP2TYPE.items():
96 if port_capabilities.get(pcap) is True:
97 port_type = ptype
98 break
99
100 # Get the correct link enum
101 link_enum: Any = PORT_TYPE2LINK.get(port_type)
102
103 # Get the rates. Set to 0 if not available
104 link_rate = safe_int(values.get("link_rate"), default=0)
105 max_rate = safe_int(values.get("max_rate"), default=0)
106
107 if link_enum is not None:
108 link_rate = get_enum_key_by_value(
109 link_enum,
110 link_rate,
111 link_enum.UNKNOWN,
112 )
113 max_rate = get_enum_key_by_value(
114 link_enum,
115 max_rate,
116 link_enum.UNKNOWN,
117 )
118
119 # Special ports
120 if (
121 max_rate == PortSpeed.LINK_10000
122 and port_capabilities.get(PortCapability.SFPP) is True
123 ):
124 port_type = PortType.SFPP
125
126 # Port state
127 port_state = safe_bool(values.get("is_on"))
128 # For USB ports, the state is 1 only when a modem is connected
129 modem = False
130 port_devices = None
131 if port_type == PortType.USB:
132 # Mark a modem as connected
133 if port_state is True:
134 modem = True
135 # If any other device is connected, mark as connected
136 port_devices = values.get("devices")
137 if port_devices is not None:
138 port_state = True
139
140 # Leave only the capabilities that are available
141 capabilities = [
142 capability
143 for capability, value in port_capabilities.items()
144 if value is True
145 ]
146
147 # Combine the port description
148 port_description = {
149 "state": port_state,
150 "id": port_id,
151 "capabilities": capabilities,
152 "link_rate": link_rate,
153 "max_rate": max_rate,
154 }
155
156 # Add the modem flag if needed and the connected devices
157 if port_type == PortType.USB:
158 port_description["modem"] = modem
159 port_description["devices"] = port_devices
160
161 return port_description, port_type, port_id