diff --git a/custom_components/proxmoxve/sensor.py b/custom_components/proxmoxve/sensor.py index 0a4469f..d66a51b 100644 --- a/custom_components/proxmoxve/sensor.py +++ b/custom_components/proxmoxve/sensor.py @@ -61,7 +61,9 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr name="Disk free", icon="mdi:harddisk", native_unit_of_measurement=UnitOfInformation.BYTES, - value_fn=lambda x: (x.disk_total - x.disk_used), + value_fn=lambda x: (x.disk_total - x.disk_used) + if (UNDEFINED not in (x.disk_total, x.disk_used)) + else 0, device_class=SensorDeviceClass.DATA_SIZE, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=2, @@ -75,7 +77,9 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr icon="mdi:harddisk", native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, - value_fn=lambda x: 1 - (x.disk_used / x.disk_total) if x.disk_total > 0 else 0, + value_fn=lambda x: 1 - (x.disk_used / x.disk_total) + if (UNDEFINED not in (x.disk_used, x.disk_total) and x.disk_total > 0) + else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=1, entity_registry_enabled_default=False, @@ -111,7 +115,9 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr icon="mdi:harddisk", native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, - value_fn=lambda x: (x.disk_used / x.disk_total) if x.disk_total > 0 else 0, + value_fn=lambda x: (x.disk_used / x.disk_total) + if (UNDEFINED not in (x.disk_used, x.disk_total) and x.disk_total > 0) + else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=1, translation_key="disk_used_perc", @@ -136,7 +142,7 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, value_fn=lambda x: (x.memory_free / x.memory_total) - if x.memory_total > 0 + if (UNDEFINED not in (x.memory_free, x.memory_total) and x.memory_total > 0) else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=2, @@ -173,7 +179,7 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, value_fn=lambda x: (x.memory_used / x.memory_total) - if x.memory_total > 0 + if (UNDEFINED not in (x.memory_used, x.memory_total) and x.memory_total > 0) else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=2, @@ -199,7 +205,9 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr icon="mdi:memory", native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, - value_fn=lambda x: (x.swap_free / x.swap_total) if x.swap_total > 0 else 0, + value_fn=lambda x: (x.swap_free / x.swap_total) + if (UNDEFINED not in (x.swap_free, x.swap_total) and x.swap_total > 0) + else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=2, entity_registry_enabled_default=False, @@ -235,7 +243,9 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr icon="mdi:memory", native_unit_of_measurement=PERCENTAGE, conversion_fn=lambda x: (x * 100) if x > 0 else 0, - value_fn=lambda x: (x.swap_used / x.swap_total) if x.swap_total > 0 else 0, + value_fn=lambda x: (x.swap_used / x.swap_total) + if (UNDEFINED not in (x.swap_used, x.swap_total) and x.swap_total > 0) + else 0, state_class=SensorStateClass.MEASUREMENT, suggested_display_precision=2, entity_registry_enabled_default=False, @@ -342,7 +352,7 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr icon="mdi:server", translation_key="status_raw", value_fn=lambda x: x.health - if x.health not in ["running", "stopped"] + if (x.health not in ["running", "stopped", UNDEFINED]) else x.status, ), *PROXMOX_SENSOR_CPU,