11import clr # from pythonnet package, not clr package !
2+ import ctypes
23import math
34import os
5+ import sys
46from typing import Tuple
57from statistics import mean
68from win32api import *
2628 str (HIWORD (ls_file_version )),
2729 str (LOWORD (ls_file_version ))]))
2830
31+ if ctypes .windll .shell32 .IsUserAnAdmin () == 0 :
32+ logger .error (
33+ "Program is not running as administrator. Please run with admin rights or choose another HW_SENSORS option in "
34+ "config.yaml" )
35+ try :
36+ sys .exit (0 )
37+ except :
38+ os ._exit (0 )
39+
2940handle = Hardware .Computer ()
3041handle .IsCpuEnabled = True
3142handle .IsGpuEnabled = True
@@ -66,7 +77,7 @@ def get_net_interface_and_update(if_name: str) -> Hardware.Hardware:
6677 hardware .Update ()
6778 return hardware
6879
69- logger .error ("Network interface '%s' not found. Check names in config.yaml." % if_name )
80+ logger .warning ("Network interface '%s' not found. Check names in config.yaml." % if_name )
7081 return None
7182
7283
@@ -86,7 +97,7 @@ def frequency() -> float:
8697 frequencies = []
8798 cpu = get_hw_and_update (Hardware .HardwareType .Cpu )
8899 for sensor in cpu .Sensors :
89- if sensor .SensorType == Hardware .SensorType .Clock and str ( sensor . Name ). startswith ( "CPU Core #" ):
100+ if sensor .SensorType == Hardware .SensorType .Clock and " Core #" in str ( sensor . Name ):
90101 frequencies .append (float (sensor .Value ))
91102 return mean (frequencies )
92103
@@ -100,11 +111,9 @@ def is_temperature_available() -> bool:
100111 cpu = get_hw_and_update (Hardware .HardwareType .Cpu )
101112 for sensor in cpu .Sensors :
102113 if sensor .SensorType == Hardware .SensorType .Temperature :
103- if str (sensor .Name ).startswith ("Core Average" ) or str (sensor .Name ).startswith ("Core Max" ) or str (
104- sensor .Name ).startswith ("CPU Package" ):
114+ if str (sensor .Name ).startswith ("Core" ) or str (sensor .Name ).startswith ("CPU Package" ):
105115 return True
106116
107- logger .error ("CPU temperature cannot be read" )
108117 return False
109118
110119 @staticmethod
@@ -118,10 +127,14 @@ def temperature() -> float:
118127 for sensor in cpu .Sensors :
119128 if sensor .SensorType == Hardware .SensorType .Temperature and str (sensor .Name ).startswith ("Core Max" ):
120129 return float (sensor .Value )
121- # Otherwise the CPU Package temperature (usually same as max core temperature) will be used
130+ # If not available, the CPU Package temperature (usually same as max core temperature) will be used
122131 for sensor in cpu .Sensors :
123132 if sensor .SensorType == Hardware .SensorType .Temperature and str (sensor .Name ).startswith ("CPU Package" ):
124133 return float (sensor .Value )
134+ # Otherwise any sensor named "Core..." will be used
135+ for sensor in cpu .Sensors :
136+ if sensor .SensorType == Hardware .SensorType .Temperature and str (sensor .Name ).startswith ("Core" ):
137+ return float (sensor .Value )
125138
126139 return math .nan
127140
@@ -233,7 +246,8 @@ def disk_usage_percent() -> float:
233246 if sensor .SensorType == Hardware .SensorType .Load and str (sensor .Name ).startswith ("Used Space" ):
234247 return float (sensor .Value )
235248
236- return math .nan
249+ # Get this data from psutil if it is not available from LibreHardwareMonitor
250+ return psutil .disk_usage ("/" ).percent
237251
238252 @staticmethod
239253 def disk_used () -> int : # In bytes
@@ -251,25 +265,25 @@ class Net(sensors.Net):
251265 def stats (if_name , interval ) -> Tuple [
252266 int , int , int , int ]: # up rate (B/s), uploaded (B), dl rate (B/s), downloaded (B)
253267
254- dl_speed = 0
255- dl = 0
256- up_speed = 0
257- up = 0
268+ upload_rate = 0
269+ uploaded = 0
270+ download_rate = 0
271+ downloaded = 0
258272
259273 if if_name != "" :
260274 net_if = get_net_interface_and_update (if_name )
261275 if net_if is not None :
262276 for sensor in net_if .Sensors :
263277 if sensor .SensorType == Hardware .SensorType .Data and str (sensor .Name ).startswith ("Data Uploaded" ):
264- up = int (sensor .Value * 1000000000.0 )
278+ uploaded = int (sensor .Value * 1000000000.0 )
265279 elif sensor .SensorType == Hardware .SensorType .Data and str (sensor .Name ).startswith (
266280 "Data Downloaded" ):
267- dl = int (sensor .Value * 1000000000.0 )
281+ downloaded = int (sensor .Value * 1000000000.0 )
268282 elif sensor .SensorType == Hardware .SensorType .Throughput and str (sensor .Name ).startswith (
269283 "Upload Speed" ):
270- up_speed = int (sensor .Value )
284+ upload_rate = int (sensor .Value )
271285 elif sensor .SensorType == Hardware .SensorType .Throughput and str (sensor .Name ).startswith (
272286 "Download Speed" ):
273- dl_speed = int (sensor .Value )
287+ download_rate = int (sensor .Value )
274288
275- return up_speed , up , dl_speed , dl
289+ return upload_rate , uploaded , download_rate , downloaded
0 commit comments