Skip to content

Commit

Permalink
Fix #313
Browse files Browse the repository at this point in the history
  • Loading branch information
dougiteixeira committed Jun 23, 2024
1 parent daf4492 commit 911ec55
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions custom_components/proxmoxve/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 911ec55

Please sign in to comment.