Debian Codegraph @

asusrouter/modules/connection.py

raw
1 """Connection module."""
2
3 from enum import Enum, IntEnum
4
5 from asusrouter.modules.wlan import Wlan
6 from asusrouter.tools.converters import get_enum_key_by_value, safe_int
7
8
9 class ConnectionState(IntEnum):
10 """Connection state enum."""
11
12 UNKNOWN = -999
13
14 DISCONNECTED = 0
15 CONNECTED = 1
16
17
18 class ConnectionStatus(IntEnum):
19 """Connection status enum."""
20
21 UNKNOWN = -999
22
23 ERROR = -1
24 DISCONNECTED = 0
25 CONNECTING = 1
26 CONNECTED = 2
27
28
29 class ConnectionType(str, Enum):
30 """Connection type class."""
31
32 WIRED = "wired"
33 WLAN_2G = Wlan.FREQ_2G.value
34 WLAN_5G = Wlan.FREQ_5G.value
35 WLAN_5G2 = Wlan.FREQ_5G2.value
36 WLAN_6G = Wlan.FREQ_6G.value
37 DISCONNECTED = "disconnected"
38
39
40 CONNECTION_TYPE = {
41 0: ConnectionType.WIRED,
42 1: ConnectionType.WLAN_2G,
43 2: ConnectionType.WLAN_5G,
44 3: ConnectionType.WLAN_5G2,
45 4: ConnectionType.WLAN_6G,
46 }
47
48
49 class InternetMode(str, Enum):
50 """Internet mode class."""
51
52 ALLOW = "allow"
53 BLOCK = "block"
54 TIME = "time"
55 UNKNOWN = "unknown"
56
57
58 def get_internet_mode(value: str) -> InternetMode:
59 """Get internet mode."""
60
61 return get_enum_key_by_value(InternetMode, value, InternetMode.UNKNOWN)
62
63
64 def get_connection_type(value: int | None) -> ConnectionType:
65 """Get connection type."""
66
67 # Check that it's actually an int
68 # This would actually work with float as well doing direct conversion
69 value = safe_int(value)
70
71 # ------------------------------------------------------ #
72 # I am officially considering this a feature, not a bug. #
73 # ------------------------------------------------------ #
74
75 if value is None:
76 value = 0
77
78 return CONNECTION_TYPE.get(value, ConnectionType.WIRED)