From 1ffc459aa7bda990062e7c055dcbb05daf99a1c5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 9 Mar 2024 18:00:53 -1000 Subject: [PATCH] Only read cpu once during systemmonitor setup (#112863) * Only read cpu once during systemmonitor setup * type --- .../components/systemmonitor/sensor.py | 29 ++++++++----------- .../components/systemmonitor/util.py | 7 +---- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index 94fc1719c4a..fb04ac815b9 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import Callable +import contextlib from dataclasses import dataclass from datetime import datetime from functools import lru_cache @@ -71,13 +72,6 @@ def get_cpu_icon() -> Literal["mdi:cpu-64-bit", "mdi:cpu-32-bit"]: return "mdi:cpu-32-bit" -def get_processor_temperature( - entity: SystemMonitorSensor, -) -> float | None: - """Return processor temperature.""" - return read_cpu_temperature(entity.hass, entity.coordinator.data.temperatures) - - def get_process(entity: SystemMonitorSensor) -> str: """Return process.""" state = STATE_OFF @@ -374,7 +368,9 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, state_class=SensorStateClass.MEASUREMENT, - value_fn=get_processor_temperature, + value_fn=lambda entity: read_cpu_temperature( + entity.coordinator.data.temperatures + ), none_is_unavailable=True, add_to_update=lambda entity: ("temperatures", ""), ), @@ -506,22 +502,21 @@ async def async_setup_entry( # noqa: C901 legacy_resources: set[str] = set(entry.options.get("resources", [])) loaded_resources: set[str] = set() coordinator: SystemMonitorCoordinator = hass.data[DOMAIN_COORDINATOR] + sensor_data = coordinator.data def get_arguments() -> dict[str, Any]: """Return startup information.""" - disk_arguments = get_all_disk_mounts(hass) - network_arguments = get_all_network_interfaces(hass) - try: - cpu_temperature = read_cpu_temperature(hass) - except AttributeError: - cpu_temperature = 0.0 return { - "disk_arguments": disk_arguments, - "network_arguments": network_arguments, - "cpu_temperature": cpu_temperature, + "disk_arguments": get_all_disk_mounts(hass), + "network_arguments": get_all_network_interfaces(hass), } + cpu_temperature: float | None = None + with contextlib.suppress(AttributeError): + cpu_temperature = read_cpu_temperature(sensor_data.temperatures) + startup_arguments = await hass.async_add_executor_job(get_arguments) + startup_arguments["cpu_temperature"] = cpu_temperature _LOGGER.debug("Setup from options %s", entry.options) diff --git a/homeassistant/components/systemmonitor/util.py b/homeassistant/components/systemmonitor/util.py index 75f8d2b2df1..1889e443b2d 100644 --- a/homeassistant/components/systemmonitor/util.py +++ b/homeassistant/components/systemmonitor/util.py @@ -77,13 +77,8 @@ def get_all_running_processes(hass: HomeAssistant) -> set[str]: return processes -def read_cpu_temperature( - hass: HomeAssistant, temps: dict[str, list[shwtemp]] | None = None -) -> float | None: +def read_cpu_temperature(temps: dict[str, list[shwtemp]]) -> float | None: """Attempt to read CPU / processor temperature.""" - if temps is None: - psutil_wrapper: ha_psutil = hass.data[DOMAIN] - temps = psutil_wrapper.psutil.sensors_temperatures() entry: shwtemp _LOGGER.debug("CPU Temperatures: %s", temps)