Only read cpu once during systemmonitor setup (#112863)

* Only read cpu once during systemmonitor setup

* type
This commit is contained in:
J. Nick Koston 2024-03-09 18:00:53 -10:00 committed by GitHub
parent caaa03536b
commit 1ffc459aa7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 23 deletions

View file

@ -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)