From 67629111f9bdbfa8e0c5312f4590c9f3eedae0d7 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Thu, 28 Dec 2023 20:39:39 +0100 Subject: [PATCH] Systemmonitor always load imported disks (#106546) * Systemmonitor always load legacy disks * loaded_resources --- .../components/systemmonitor/sensor.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index 57838c45dc7..2bc1406308c 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -389,6 +389,7 @@ async def async_setup_entry( entities = [] sensor_registry: dict[tuple[str, str], SensorData] = {} legacy_resources: list[str] = entry.options.get("resources", []) + loaded_resources: list[str] = [] disk_arguments = await hass.async_add_executor_job(get_all_disk_mounts) network_arguments = await hass.async_add_executor_job(get_all_network_interfaces) cpu_temperature = await hass.async_add_executor_job(_read_cpu_temperature) @@ -404,6 +405,7 @@ async def async_setup_entry( is_enabled = check_legacy_resource( f"{_type}_{argument}", legacy_resources ) + loaded_resources.append(f"{_type}_{argument}") entities.append( SystemMonitorSensor( sensor_registry, @@ -423,6 +425,7 @@ async def async_setup_entry( is_enabled = check_legacy_resource( f"{_type}_{argument}", legacy_resources ) + loaded_resources.append(f"{_type}_{argument}") entities.append( SystemMonitorSensor( sensor_registry, @@ -446,6 +449,7 @@ async def async_setup_entry( sensor_registry[(_type, argument)] = SensorData( argument, None, None, None, None ) + loaded_resources.append(f"{_type}_{argument}") entities.append( SystemMonitorSensor( sensor_registry, @@ -459,6 +463,7 @@ async def async_setup_entry( sensor_registry[(_type, "")] = SensorData("", None, None, None, None) is_enabled = check_legacy_resource(f"{_type}_", legacy_resources) + loaded_resources.append(f"{_type}_") entities.append( SystemMonitorSensor( sensor_registry, @@ -469,6 +474,31 @@ async def async_setup_entry( ) ) + # Ensure legacy imported disk_* resources are loaded if they are not part + # of mount points automatically discovered + for resource in legacy_resources: + if resource.startswith("disk_"): + _LOGGER.debug( + "Check resource %s already loaded in %s", resource, loaded_resources + ) + if resource not in loaded_resources: + split_index = resource.rfind("_") + _type = resource[:split_index] + argument = resource[split_index + 1 :] + _LOGGER.debug("Loading legacy %s with argument %s", _type, argument) + sensor_registry[(_type, argument)] = SensorData( + argument, None, None, None, None + ) + entities.append( + SystemMonitorSensor( + sensor_registry, + SENSOR_TYPES[_type], + entry.entry_id, + argument, + True, + ) + ) + scan_interval = DEFAULT_SCAN_INTERVAL await async_setup_sensor_registry_updates(hass, sensor_registry, scan_interval) async_add_entities(entities)