3 Python Bindings for libsensors3
5 use the documentation of libsensors for the low level API.
6 see example.py for high level API usage.
8 @author: Pavel Rojtberg (http://www.rojtberg.net)
9 @see: https://github.com/paroj/sensors.py
10 @copyright: LGPLv2 (same as libsensors) <http://opensource.org/licenses/LGPL-2.1>
16 _libc = cdll.LoadLibrary(ctypes.util.find_library("c"))
17 # see https://github.com/paroj/sensors.py/issues/1
18 _libc.free.argtypes = [c_void_p]
19 _hdl = cdll.LoadLibrary(ctypes.util.find_library("sensors"))
21 version = c_char_p.in_dll(_hdl, "libsensors_version").value.decode("ascii")
24 class bus_id(Structure):
25 _fields_ = [("type", c_short),
29 class chip_name(Structure):
30 _fields_ = [("prefix", c_char_p),
36 class feature(Structure):
37 _fields_ = [("name", c_char_p),
41 # sensors_feature_type
56 class subfeature(Structure):
57 _fields_ = [("name", c_char_p),
64 _hdl.sensors_get_detected_chips.restype = POINTER(chip_name)
65 _hdl.sensors_get_features.restype = POINTER(feature)
66 _hdl.sensors_get_all_subfeatures.restype = POINTER(subfeature)
67 _hdl.sensors_get_label.restype = c_void_p # return pointer instead of str so we can free it
68 _hdl.sensors_get_adapter_name.restype = c_char_p # docs do not say whether to free this or not
69 _hdl.sensors_strerror.restype = c_char_p
77 def init(cfg_file=None):
78 file = _libc.fopen(cfg_file.encode("utf-8"), "r") if cfg_file is not None else None
80 if _hdl.sensors_init(file) != 0:
81 raise Exception("sensors_init failed")
88 _hdl.sensors_cleanup()
91 def parse_chip_name(orig_name):
93 err = _hdl.sensors_parse_chip_name(orig_name.encode("utf-8"), byref(ret))
96 raise Exception(strerror(err))
101 def strerror(errnum):
102 return _hdl.sensors_strerror(errnum).decode("utf-8")
105 def free_chip_name(chip):
106 _hdl.sensors_free_chip_name(byref(chip))
109 def get_detected_chips(match, nr):
111 @return: (chip, next nr to query)
115 if match is not None:
118 chip = _hdl.sensors_get_detected_chips(match, byref(_nr))
119 chip = chip.contents if bool(chip) else None
120 return chip, _nr.value
123 def chip_snprintf_name(chip, buffer_size=200):
125 @param buffer_size defaults to the size used in the sensors utility
127 ret = create_string_buffer(buffer_size)
128 err = _hdl.sensors_snprintf_chip_name(ret, buffer_size, byref(chip))
131 raise Exception(strerror(err))
133 return ret.value.decode("utf-8")
136 def do_chip_sets(chip):
138 @attention this function was not tested
140 err = _hdl.sensors_do_chip_sets(byref(chip))
142 raise Exception(strerror(err))
145 def get_adapter_name(bus):
146 return _hdl.sensors_get_adapter_name(byref(bus)).decode("utf-8")
149 def get_features(chip, nr):
151 @return: (feature, next nr to query)
154 feature = _hdl.sensors_get_features(byref(chip), byref(_nr))
155 feature = feature.contents if bool(feature) else None
156 return feature, _nr.value
159 def get_label(chip, feature):
160 ptr = _hdl.sensors_get_label(byref(chip), byref(feature))
161 val = cast(ptr, c_char_p).value.decode("utf-8")
166 def get_all_subfeatures(chip, feature, nr):
168 @return: (subfeature, next nr to query)
171 subfeature = _hdl.sensors_get_all_subfeatures(byref(chip), byref(feature), byref(_nr))
172 subfeature = subfeature.contents if bool(subfeature) else None
173 return subfeature, _nr.value
176 def get_value(chip, subfeature_nr):
178 err = _hdl.sensors_get_value(byref(chip), subfeature_nr, byref(val))
180 raise Exception(strerror(err))
184 def set_value(chip, subfeature_nr, value):
186 @attention this function was not tested
188 val = c_double(value)
189 err = _hdl.sensors_set_value(byref(chip), subfeature_nr, byref(val))
191 raise Exception(strerror(err))
194 ### Convenience API ###
196 def __init__(self, match=None):
197 self.match = parse_chip_name(match) if match is not None else None
204 chip, self.nr = get_detected_chips(self.match, self.nr)
212 if self.match is not None:
213 free_chip_name(self.match)
215 def next(self): # python2 compability
216 return self.__next__()
219 class FeatureIterator:
220 def __init__(self, chip):
228 feature, self.nr = get_features(self.chip, self.nr)
235 def next(self): # python2 compability
236 return self.__next__()
239 class SubFeatureIterator:
240 def __init__(self, chip, feature):
242 self.feature = feature
249 subfeature, self.nr = get_all_subfeatures(self.chip, self.feature, self.nr)
251 if subfeature is None:
256 def next(self): # python2 compability
257 return self.__next__()