Debian Codegraph @

s_tui/sources/fan_source.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 # Copyright (C) 2017-2025 Alex Manuskin, Maor Veitsman
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 """This module implements a fan source"""
19
20 from __future__ import annotations
21
22 import logging
23
24 import psutil
25
26 from s_tui.sources.source import Source
27
28
29 class FanSource(Source):
30 """Source for fan information"""
31
32 def __init__(self):
33 try:
34 if psutil.sensors_fans():
35 self.is_available = True
36 except AttributeError:
37 self.is_available = False
38 logging.debug("Fans sensors is not available from psutil")
39 return
40 except TypeError:
41 # psutil bug: sensors_fans() raises TypeError on some hardware
42 # when sysfs fan sensor files contain None (e.g. Intel xe GPU fans)
43 # See: https://github.com/amanusk/s-tui/issues/255
44 self.is_available = False
45 logging.debug("Fans sensors raised TypeError (psutil bug)")
46 return
47
48 Source.__init__(self)
49
50 self.name = "Fan"
51 self.measurement_unit = "RPM"
52 self.pallet = ("fan light", "fan dark", "fan light smooth", "fan dark smooth")
53
54 sensors_dict = {}
55 try:
56 sensors_dict = psutil.sensors_fans()
57 except (OSError, TypeError):
58 logging.debug("Unable to create sensors dict")
59 self.is_available = False
60 return
61 if not sensors_dict:
62 self.is_available = False
63 return
64
65 self._sensor_lookup = {}
66 for key, value in sensors_dict.items():
67 sensor_name = key
68 for sensor_idx, sensor in enumerate(value):
69 sensor_label = sensor.label
70
71 full_name = ""
72 if not sensor_label:
73 full_name = sensor_name + "," + str(sensor_idx)
74 else:
75 full_name = sensor_label
76
77 logging.debug("Fan sensor name %s", full_name)
78
79 self.available_sensors.append(full_name)
80 self._sensor_lookup[(key, sensor_idx)] = len(self.available_sensors) - 1
81
82 self.sensor_available = [True] * len(self.available_sensors)
83 self.last_measurement = [0] * len(self.available_sensors)
84
85 def update(self) -> None:
86 try:
87 sample = psutil.sensors_fans()
88 except (TypeError, OSError):
89 # psutil bug: sensors_fans() may raise TypeError on some hardware
90 # when sysfs fan sensor files contain None.
91 # See: https://github.com/amanusk/s-tui/issues/256
92 # Keep last_measurement unchanged (show stale data).
93 logging.debug("sensors_fans() raised an exception, keeping stale data")
94 return
95 if sample is None:
96 logging.debug("sensors_fans() returned None, keeping stale data")
97 return
98
99 updated = set()
100 for key, sensors in sample.items():
101 for sensor_idx, minor_sensor in enumerate(sensors):
102 idx = self._sensor_lookup.get((key, sensor_idx))
103 if idx is None:
104 continue # new sensor not in original list
105 if minor_sensor.current > 10000:
106 self.sensor_available[idx] = False
107 continue
108 self.last_measurement[idx] = int(minor_sensor.current)
109 self.sensor_available[idx] = True
110 updated.add(idx)
111
112 # Mark sensors not seen in this sample as unavailable
113 for idx in range(len(self.available_sensors)):
114 if idx not in updated:
115 self.sensor_available[idx] = False
116
117 def get_edge_triggered(self) -> bool:
118 return False
119
120 def get_top(self) -> int:
121 return 1