mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Support multiple disks in systemmonitor (#50362)
* Fix #50158 - add support for multiple disks * Rework as a tuple
This commit is contained in:
parent
70b09ed9a1
commit
a404c138fa
@ -204,7 +204,7 @@ async def async_setup_platform(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the system monitor sensors."""
|
"""Set up the system monitor sensors."""
|
||||||
entities = []
|
entities = []
|
||||||
sensor_registry: dict[str, SensorData] = {}
|
sensor_registry: dict[tuple[str, str], SensorData] = {}
|
||||||
|
|
||||||
for resource in config[CONF_RESOURCES]:
|
for resource in config[CONF_RESOURCES]:
|
||||||
type_ = resource[CONF_TYPE]
|
type_ = resource[CONF_TYPE]
|
||||||
@ -226,7 +226,9 @@ async def async_setup_platform(
|
|||||||
_LOGGER.warning("Cannot read CPU / processor temperature information")
|
_LOGGER.warning("Cannot read CPU / processor temperature information")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sensor_registry[type_] = SensorData(argument, None, None, None, None)
|
sensor_registry[(type_, argument)] = SensorData(
|
||||||
|
argument, None, None, None, None
|
||||||
|
)
|
||||||
entities.append(SystemMonitorSensor(sensor_registry, type_, argument))
|
entities.append(SystemMonitorSensor(sensor_registry, type_, argument))
|
||||||
|
|
||||||
scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
||||||
@ -237,7 +239,7 @@ async def async_setup_platform(
|
|||||||
|
|
||||||
async def async_setup_sensor_registry_updates(
|
async def async_setup_sensor_registry_updates(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
sensor_registry: dict[str, SensorData],
|
sensor_registry: dict[tuple[str, str], SensorData],
|
||||||
scan_interval: datetime.timedelta,
|
scan_interval: datetime.timedelta,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Update the registry and create polling."""
|
"""Update the registry and create polling."""
|
||||||
@ -246,11 +248,11 @@ async def async_setup_sensor_registry_updates(
|
|||||||
|
|
||||||
def _update_sensors() -> None:
|
def _update_sensors() -> None:
|
||||||
"""Update sensors and store the result in the registry."""
|
"""Update sensors and store the result in the registry."""
|
||||||
for type_, data in sensor_registry.items():
|
for (type_, argument), data in sensor_registry.items():
|
||||||
try:
|
try:
|
||||||
state, value, update_time = _update(type_, data)
|
state, value, update_time = _update(type_, data)
|
||||||
except Exception as ex: # pylint: disable=broad-except
|
except Exception as ex: # pylint: disable=broad-except
|
||||||
_LOGGER.exception("Error updating sensor: %s", type_)
|
_LOGGER.exception("Error updating sensor: %s (%s)", type_, argument)
|
||||||
data.last_exception = ex
|
data.last_exception = ex
|
||||||
else:
|
else:
|
||||||
data.state = state
|
data.state = state
|
||||||
@ -296,7 +298,7 @@ class SystemMonitorSensor(SensorEntity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
sensor_registry: dict[str, SensorData],
|
sensor_registry: dict[tuple[str, str], SensorData],
|
||||||
sensor_type: str,
|
sensor_type: str,
|
||||||
argument: str = "",
|
argument: str = "",
|
||||||
) -> None:
|
) -> None:
|
||||||
@ -305,6 +307,7 @@ class SystemMonitorSensor(SensorEntity):
|
|||||||
self._name: str = f"{self.sensor_type[SENSOR_TYPE_NAME]} {argument}".rstrip()
|
self._name: str = f"{self.sensor_type[SENSOR_TYPE_NAME]} {argument}".rstrip()
|
||||||
self._unique_id: str = slugify(f"{sensor_type}_{argument}")
|
self._unique_id: str = slugify(f"{sensor_type}_{argument}")
|
||||||
self._sensor_registry = sensor_registry
|
self._sensor_registry = sensor_registry
|
||||||
|
self._argument: str = argument
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
@ -354,7 +357,7 @@ class SystemMonitorSensor(SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def data(self) -> SensorData:
|
def data(self) -> SensorData:
|
||||||
"""Return registry entry for the data."""
|
"""Return registry entry for the data."""
|
||||||
return self._sensor_registry[self._type]
|
return self._sensor_registry[(self._type, self._argument)]
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user