1 """Utils for System Monitor."""
6 from psutil._common
import shwtemp
7 import psutil_home_assistant
as ha_psutil
11 from .const
import CPU_SENSOR_PREFIXES
13 _LOGGER = logging.getLogger(__name__)
15 SKIP_DISK_TYPES = {
"proc",
"tmpfs",
"devtmpfs"}
19 hass: HomeAssistant, psutil_wrapper: ha_psutil.PsutilWrapper
21 """Return all disk mount points on system."""
22 disks: set[str] = set()
23 for part
in psutil_wrapper.psutil.disk_partitions(all=
True):
25 if "cdrom" in part.opts
or part.fstype ==
"":
30 if part.fstype
in SKIP_DISK_TYPES:
34 if not os.path.isdir(part.mountpoint):
36 "Mountpoint %s was excluded because it is not a directory",
40 usage = psutil_wrapper.psutil.disk_usage(part.mountpoint)
41 except PermissionError:
43 "No permission for running user to access %s", part.mountpoint
46 except OSError
as err:
48 "Mountpoint %s was excluded because of: %s", part.mountpoint, err
51 if usage.total > 0
and part.device !=
"":
52 disks.add(part.mountpoint)
53 _LOGGER.debug(
"Adding disks: %s",
", ".join(disks))
58 hass: HomeAssistant, psutil_wrapper: ha_psutil.PsutilWrapper
60 """Return all network interfaces on system."""
61 interfaces: set[str] = set()
62 for interface
in psutil_wrapper.psutil.net_if_addrs():
63 if interface.startswith(
"veth"):
66 interfaces.add(interface)
67 _LOGGER.debug(
"Adding interfaces: %s",
", ".join(interfaces))
72 """Return all running processes on system."""
73 psutil_wrapper = ha_psutil.PsutilWrapper()
74 processes: set[str] = set()
75 for proc
in psutil_wrapper.psutil.process_iter([
"name"]):
76 if proc.name()
not in processes:
77 processes.add(proc.name())
78 _LOGGER.debug(
"Running processes: %s",
", ".join(processes))
83 """Attempt to read CPU / processor temperature."""
86 _LOGGER.debug(
"CPU Temperatures: %s", temps)
87 for name, entries
in temps.items():
88 for i, entry
in enumerate(entries, start=1):
91 _label = f
"{name} {i}" if not entry.label
else entry.label
94 if _label
in CPU_SENSOR_PREFIXES
or name
in CPU_SENSOR_PREFIXES:
95 return round(entry.current, 1)
set[str] get_all_running_processes(HomeAssistant hass)
float|None read_cpu_temperature(dict[str, list[shwtemp]] temps)
set[str] get_all_disk_mounts(HomeAssistant hass, ha_psutil.PsutilWrapper psutil_wrapper)
set[str] get_all_network_interfaces(HomeAssistant hass, ha_psutil.PsutilWrapper psutil_wrapper)