Debian Codegraph @

asusrouter/modules/endpoint/temperature.py

raw
1 """Temperature endpoint module."""
2
3 from __future__ import annotations
4
5 import logging
6 import threading
7 from typing import Any
8
9 from asusrouter.config import ARConfig, ARConfigKey as ARConfKey
10 from asusrouter.modules.data import AsusData
11 from asusrouter.modules.wlan import Wlan
12 from asusrouter.tools.cleaners import clean_content
13 from asusrouter.tools.converters import safe_float
14 from asusrouter.tools.readers import read_js_variables
15 from asusrouter.tools.writers import ensure_notification_flag
16
17 _LOGGER = logging.getLogger(__name__)
18
19 EXPECTED_DECIMAL_PLACES = 2
20 EXPECTED_TEMPERATURE_MIN = 10.0
21 EXPECTED_TEMPERATURE_MAX = 150.0
22 MAX_SCALE_COUNT = 5
23
24 _temperature_warned_lock = threading.Lock()
25
26
27 # The temperature data can be presented it the following JS variables:
28 # 1) curr_coreTmp_2_raw, curr_coreTmp_5_raw, curr_coreTmp_52_raw
29 # 2) curr_coreTmp_0_raw, curr_coreTmp_1_raw, curr_coreTmp_2_raw,
30 # curr_coreTmp_3_raw
31 # 3) curr_coreTmp_wl0_raw, curr_coreTmp_wl1_raw, curr_coreTmp_wl2_raw,
32 # curr_coreTmp_wl3_raw
33 # for 2ghz, 5ghz, 5ghz2, 6ghz respectively
34 # CPU temperature is set either in curr_cpuTemp or curr_coreTmp_cpu
35 def read(content: str, **kwargs: Any) -> dict[str, Any]:
36 """Read temperature data."""
37
38 temperature: dict[str, Any] = {}
39
40 # Get the correct configuration to proceed
41 config = kwargs.get("config", ARConfig)
42
43 # Prepare the content. This page is a set of JS variables
44 variables = read_js_variables(clean_content(content))
45
46 # Read WLAN temperatures
47 # If there is curr_coreTmp_5_raw, we have type 1
48 if "curr_coreTmp_5_raw" in variables:
49 temperature[Wlan.FREQ_2G] = variables.get("curr_coreTmp_2_raw")
50 temperature[Wlan.FREQ_5G] = variables.get("curr_coreTmp_5_raw")
51 temperature[Wlan.FREQ_5G2] = variables.get("curr_coreTmp_52_raw")
52 # If there is curr_coreTmp_0_raw, we have type 2
53 elif "curr_coreTmp_0_raw" in variables:
54 temperature[Wlan.FREQ_2G] = variables.get("curr_coreTmp_0_raw")
55 temperature[Wlan.FREQ_5G] = variables.get("curr_coreTmp_1_raw")
56 temperature[Wlan.FREQ_5G2] = variables.get("curr_coreTmp_2_raw")
57 temperature[Wlan.FREQ_6G] = variables.get("curr_coreTmp_3_raw")
58 # If there is curr_coreTmp_wl0_raw, we have type 3
59 elif "curr_coreTmp_wl0_raw" in variables:
60 temperature[Wlan.FREQ_2G] = variables.get("curr_coreTmp_wl0_raw")
61 temperature[Wlan.FREQ_5G] = variables.get("curr_coreTmp_wl1_raw")
62 temperature[Wlan.FREQ_5G2] = variables.get("curr_coreTmp_wl2_raw")
63 temperature[Wlan.FREQ_6G] = variables.get("curr_coreTmp_wl3_raw")
64
65 # Clean the temperature values from `°C`
66 for key, value in temperature.items():
67 if value:
68 temperature[key] = value.replace("°C", "")
69
70 # Read CPU temperature
71 # If there is curr_coreTmp_cpu
72 if "curr_coreTmp_cpu" in variables:
73 temperature["cpu"] = variables.get("curr_coreTmp_cpu")
74 # If there is curr_cpuTemp
75 elif "curr_cpuTemp" in variables:
76 temperature["cpu"] = variables.get("curr_cpuTemp")
77
78 # Convert the temperature values to float or remove them
79 # if they have "disabled"
80 temperature = {
81 key: safe_float(value)
82 for key, value in temperature.items()
83 if value and "disabled" not in value
84 }
85
86 # While this functional is performing a kind of post-processing,
87 # it should stay a part of the read method to have access to the raw data
88 if config.get(ARConfKey.OPTIMISTIC_TEMPERATURE):
89 temperature, scaled = _scale_temperature(temperature)
90 # Check if notification flag is registered
91 ensure_notification_flag(
92 config, ARConfKey.NOTIFIED_OPTIMISTIC_TEMPERATURE
93 )
94 with _temperature_warned_lock:
95 if (
96 scaled
97 and config.get(ARConfKey.NOTIFIED_OPTIMISTIC_TEMPERATURE)
98 is False
99 ):
100 _LOGGER.warning(
101 "Temperature values were rescaled due to the issue with "
102 "the raw data. The original data is: "
103 "`%s` and the expected range is between %s and %s.",
104 variables,
105 EXPECTED_TEMPERATURE_MIN,
106 EXPECTED_TEMPERATURE_MAX,
107 )
108 config.set(ARConfKey.NOTIFIED_OPTIMISTIC_TEMPERATURE, True)
109
110 return temperature
111
112
113 def process(data: dict[str, Any]) -> dict[AsusData, Any]:
114 """Process temperature data."""
115
116 temperature: dict[AsusData, Any] = {
117 AsusData.TEMPERATURE: data,
118 }
119
120 return temperature
121
122
123 def _scale_temperature(
124 temperature: dict[str, float | None],
125 range_min: float = EXPECTED_TEMPERATURE_MIN,
126 range_max: float = EXPECTED_TEMPERATURE_MAX,
127 max_scale_count: int = MAX_SCALE_COUNT,
128 ) -> tuple[dict[str, float], bool]:
129 """Scale temperature values to a range.
130
131 This method is a temporary solution for those routers
132 with orders of magnitude lower temperature values.
133 """
134
135 scaled_temperature = {}
136 scaled = False
137
138 for key, temp in temperature.items():
139 if temp is None:
140 continue
141
142 scaled_temp = temp
143
144 count = 0
145 # Scale up if too small
146 while scaled_temp < range_min and count < max_scale_count:
147 scaled_temp *= 10
148 count += 1
149
150 # Scale down if too large
151 while scaled_temp > range_max and count < max_scale_count:
152 scaled_temp /= 10
153 count += 1
154
155 if scaled_temp != temp:
156 scaled = True
157 scaled_temperature[key] = round(scaled_temp, EXPECTED_DECIMAL_PLACES)
158
159 return scaled_temperature, scaled