| 1 |
"""Ethernet ports 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 PORT_SPEED, PortSpeed, PortType |
| 10 |
from asusrouter.tools.converters import safe_int |
| 11 |
from asusrouter.tools.readers import read_json_content |
| 12 |
|
| 13 |
_LOGGER = logging.getLogger(__name__) |
| 14 |
|
| 15 |
|
| 16 |
def read(content: str, **kwargs: Any) -> dict[str, Any]: |
| 17 |
"""Read ethernet ports data.""" |
| 18 |
|
| 19 |
# Read the json content |
| 20 |
ethernet_ports: dict[str, Any] = read_json_content( |
| 21 |
content.replace("get_wan_lan_status = ", "").replace(";", "") |
| 22 |
) |
| 23 |
|
| 24 |
return ethernet_ports |
| 25 |
|
| 26 |
|
| 27 |
def process(data: dict[str, Any]) -> dict[AsusData, Any]: |
| 28 |
"""Process ethernet ports data.""" |
| 29 |
|
| 30 |
# Ports info |
| 31 |
ports: dict[PortType, dict[Any, Any]] = { |
| 32 |
PortType.LAN: {}, |
| 33 |
PortType.WAN: {}, |
| 34 |
} |
| 35 |
|
| 36 |
port_speed = data.get("portSpeed") |
| 37 |
|
| 38 |
if port_speed is None: |
| 39 |
return { |
| 40 |
AsusData.PORTS: ports, |
| 41 |
} |
| 42 |
|
| 43 |
for port, value in port_speed.items(): |
| 44 |
# Get the port code |
| 45 |
port_code = port[0:3].lower() |
| 46 |
# Check whether the port code is in PortType enum |
| 47 |
try: |
| 48 |
port_type = PortType(port_code) |
| 49 |
except ValueError: |
| 50 |
# This should be some other kind of port and not LAN or WAN |
| 51 |
# Based on https://github.com/Vaskivskyi/ha-asusrouter/issues/774 |
| 52 |
# it is probably the SFPP port, since 10G WAN/LAN should be |
| 53 |
# detected properly. |
| 54 |
if port_code == "10g": |
| 55 |
port_type = PortType.SFPP |
| 56 |
else: |
| 57 |
continue |
| 58 |
|
| 59 |
# If the port type is not in the ports dict, add it |
| 60 |
if port_type not in ports: |
| 61 |
ports[port_type] = {} |
| 62 |
|
| 63 |
# Get the port id and link rate |
| 64 |
port_id = safe_int(port[3:]) |
| 65 |
link_rate = PORT_SPEED.get(value) |
| 66 |
# Save the port info |
| 67 |
ports[port_type][port_id] = { |
| 68 |
"state": link_rate not in (PortSpeed.LINK_DOWN, PortSpeed.UNKNOWN), |
| 69 |
"link_rate": link_rate, |
| 70 |
} |
| 71 |
|
| 72 |
return { |
| 73 |
AsusData.PORTS: ports, |
| 74 |
} |