mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 09:17:10 +00:00
Only read cpu once during systemmonitor setup (#112863)
* Only read cpu once during systemmonitor setup * type
This commit is contained in:
parent
caaa03536b
commit
1ffc459aa7
@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
import contextlib
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from functools import lru_cache
|
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"
|
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:
|
def get_process(entity: SystemMonitorSensor) -> str:
|
||||||
"""Return process."""
|
"""Return process."""
|
||||||
state = STATE_OFF
|
state = STATE_OFF
|
||||||
@ -374,7 +368,9 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
|
|||||||
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
value_fn=get_processor_temperature,
|
value_fn=lambda entity: read_cpu_temperature(
|
||||||
|
entity.coordinator.data.temperatures
|
||||||
|
),
|
||||||
none_is_unavailable=True,
|
none_is_unavailable=True,
|
||||||
add_to_update=lambda entity: ("temperatures", ""),
|
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", []))
|
legacy_resources: set[str] = set(entry.options.get("resources", []))
|
||||||
loaded_resources: set[str] = set()
|
loaded_resources: set[str] = set()
|
||||||
coordinator: SystemMonitorCoordinator = hass.data[DOMAIN_COORDINATOR]
|
coordinator: SystemMonitorCoordinator = hass.data[DOMAIN_COORDINATOR]
|
||||||
|
sensor_data = coordinator.data
|
||||||
|
|
||||||
def get_arguments() -> dict[str, Any]:
|
def get_arguments() -> dict[str, Any]:
|
||||||
"""Return startup information."""
|
"""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 {
|
return {
|
||||||
"disk_arguments": disk_arguments,
|
"disk_arguments": get_all_disk_mounts(hass),
|
||||||
"network_arguments": network_arguments,
|
"network_arguments": get_all_network_interfaces(hass),
|
||||||
"cpu_temperature": cpu_temperature,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = await hass.async_add_executor_job(get_arguments)
|
||||||
|
startup_arguments["cpu_temperature"] = cpu_temperature
|
||||||
|
|
||||||
_LOGGER.debug("Setup from options %s", entry.options)
|
_LOGGER.debug("Setup from options %s", entry.options)
|
||||||
|
|
||||||
|
@ -77,13 +77,8 @@ def get_all_running_processes(hass: HomeAssistant) -> set[str]:
|
|||||||
return processes
|
return processes
|
||||||
|
|
||||||
|
|
||||||
def read_cpu_temperature(
|
def read_cpu_temperature(temps: dict[str, list[shwtemp]]) -> float | None:
|
||||||
hass: HomeAssistant, temps: dict[str, list[shwtemp]] | None = None
|
|
||||||
) -> float | None:
|
|
||||||
"""Attempt to read CPU / processor temperature."""
|
"""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
|
entry: shwtemp
|
||||||
|
|
||||||
_LOGGER.debug("CPU Temperatures: %s", temps)
|
_LOGGER.debug("CPU Temperatures: %s", temps)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user