Systemmonitor always load imported disks (#106546)

* Systemmonitor always load legacy disks

* loaded_resources
This commit is contained in:
G Johansson 2023-12-28 20:39:39 +01:00 committed by GitHub
parent 6deb6ddbc4
commit 67629111f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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