From 0ce3bf08594d69d05aa1371aa6e3730bbc5b8b1c Mon Sep 17 00:00:00 2001 From: Colin Robbins Date: Mon, 10 May 2021 18:48:00 +0100 Subject: [PATCH] Support multiple disks in systemmonitor (#50362) * Fix #50158 - add support for multiple disks * Rework as a tuple --- .../components/systemmonitor/sensor.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index 70fd8275bd7..bfaf7231945 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -203,7 +203,7 @@ async def async_setup_platform( ) -> None: """Set up the system monitor sensors.""" entities = [] - sensor_registry: dict[str, SensorData] = {} + sensor_registry: dict[tuple[str, str], SensorData] = {} for resource in config[CONF_RESOURCES]: type_ = resource[CONF_TYPE] @@ -225,7 +225,9 @@ async def async_setup_platform( _LOGGER.warning("Cannot read CPU / processor temperature information") 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)) scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) @@ -236,7 +238,7 @@ async def async_setup_platform( async def async_setup_sensor_registry_updates( hass: HomeAssistant, - sensor_registry: dict[str, SensorData], + sensor_registry: dict[tuple[str, str], SensorData], scan_interval: datetime.timedelta, ) -> None: """Update the registry and create polling.""" @@ -245,11 +247,11 @@ async def async_setup_sensor_registry_updates( def _update_sensors() -> None: """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: state, value, update_time = _update(type_, data) 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 else: data.state = state @@ -295,7 +297,7 @@ class SystemMonitorSensor(SensorEntity): def __init__( self, - sensor_registry: dict[str, SensorData], + sensor_registry: dict[tuple[str, str], SensorData], sensor_type: str, argument: str = "", ) -> None: @@ -304,6 +306,7 @@ class SystemMonitorSensor(SensorEntity): self._name: str = f"{self.sensor_type[SENSOR_TYPE_NAME]} {argument}".rstrip() self._unique_id: str = slugify(f"{sensor_type}_{argument}") self._sensor_registry = sensor_registry + self._argument: str = argument @property def name(self) -> str: @@ -353,7 +356,7 @@ class SystemMonitorSensor(SensorEntity): @property def data(self) -> SensorData: """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: """When entity is added to hass."""