Skip to content

Commit

Permalink
Revert "refresh offline nodes"
Browse files Browse the repository at this point in the history
  • Loading branch information
dougiteixeira authored Nov 26, 2024
1 parent 5df10af commit f38356c
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 131 deletions.
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

221 changes: 104 additions & 117 deletions custom_components/proxmoxve/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,22 @@ def __init__(
async def _async_update_data(self) -> ProxmoxNodeData:
"""Update data for Proxmox Node."""

api_path = f"nodes/{self.resource_id}/status"
api_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.Node,
self.resource_id,
)
if api_status is None:
raise UpdateFailed(
f"Node {self.resource_id} unable to be found in host {self.config_entry.data[CONF_HOST]}"
)

api_path = "nodes"
node_status = ""
node_api = {}
api_status = {}
if nodes_api := await self.hass.async_add_executor_job(
poll_api,
self.hass,
Expand All @@ -91,123 +103,98 @@ async def _async_update_data(self) -> ProxmoxNodeData:
):
for node_api in nodes_api:
if node_api[CONF_NODE] == self.resource_id:
node_status = node_api["status"]
api_status["status"] = node_api["status"]
api_status["cpu"] = node_api["cpu"]
api_status["disk_max"] = node_api["maxdisk"]
api_status["disk_used"] = node_api["disk"]
break
if node_status == "":
LOGGER.warning("Node %s Status is %s", self.resource_id, node_status)
node_status = "offline"

if node_status == "online":
api_path = f"nodes/{self.resource_id}/status"
api_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.Node,
self.resource_id,
)
if api_status is None:
raise UpdateFailed(
f"Node {self.resource_id} unable to be found in host {self.config_entry.data[CONF_HOST]}"
)

api_status["status"] = node_api["status"]
api_status["cpu"] = node_api["cpu"]
api_status["disk_max"] = node_api["maxdisk"]
api_status["disk_used"] = node_api["disk"]

api_path = f"nodes/{self.resource_id}/version"
api_status["version"] = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.Node,
self.resource_id,
)
api_path = f"nodes/{self.resource_id}/version"
api_status["version"] = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.Node,
self.resource_id,
)

api_path = f"nodes/{self.resource_id}/qemu"
qemu_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.QEMU,
self.resource_id,
)
node_qemu: dict[str, Any] = {}
node_qemu_on: int = 0
node_qemu_on_list: list[str] = []
for qemu in qemu_status if qemu_status is not None else []:
if "status" in qemu and qemu["status"] == "running":
node_qemu_on += 1
node_qemu_on_list.append(f"{qemu['name']} ({qemu['vmid']})")
node_qemu["total"] = node_qemu_on
node_qemu["list"] = node_qemu_on_list
api_status["qemu"] = node_qemu

api_path = f"nodes/{self.resource_id}/lxc"
lxc_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.LXC,
self.resource_id,
)
node_lxc: dict[str, Any] = {}
node_lxc_on: int = 0
node_lxc_on_list: list[str] = []
for lxc in lxc_status if lxc_status is not None else []:
if lxc["status"] == "running":
node_lxc_on += 1
node_lxc_on_list.append(f"{lxc['name']} ({lxc['vmid']})")
node_lxc["total"] = node_lxc_on
node_lxc["list"] = node_lxc_on_list
api_status["lxc"] = node_lxc

if node_status != "":
return ProxmoxNodeData(
type=ProxmoxType.Node,
model=api_status["cpuinfo"]["model"]
if (("cpuinfo" in api_status) and "model" in api_status["cpuinfo"])
else UNDEFINED,
status=api_status.get("status", UNDEFINED),
version=api_status["version"].get("version", UNDEFINED),
uptime=api_status.get("uptime", UNDEFINED),
cpu=api_status.get("cpu", UNDEFINED),
disk_total=api_status.get("disk_max", UNDEFINED),
disk_used=api_status.get("disk_used", UNDEFINED),
memory_total=api_status["memory"]["total"]
if (("memory" in api_status) and "total" in api_status["memory"])
else UNDEFINED,
memory_used=api_status["memory"]["used"]
if (("memory" in api_status) and "used" in api_status["memory"])
else UNDEFINED,
memory_free=api_status["memory"]["free"]
if (("memory" in api_status) and "free" in api_status["memory"])
else UNDEFINED,
swap_total=api_status["swap"]["total"]
if (("swap" in api_status) and "total" in api_status["swap"])
else UNDEFINED,
swap_free=api_status["swap"]["free"]
if (("swap" in api_status) and "free" in api_status["swap"])
else UNDEFINED,
swap_used=api_status["swap"]["used"]
if (("swap" in api_status) and "used" in api_status["swap"])
else UNDEFINED,
qemu_on=api_status["qemu"]["total"],
qemu_on_list=api_status["qemu"]["list"],
lxc_on=api_status["lxc"]["total"],
lxc_on_list=api_status["lxc"]["list"],
)
raise UpdateFailed(
f"Node {self.resource_id} unable to be found in host {self.config_entry.data[CONF_HOST]}"
api_path = f"nodes/{self.resource_id}/qemu"
qemu_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.QEMU,
self.resource_id,
)
node_qemu: dict[str, Any] = {}
node_qemu_on: int = 0
node_qemu_on_list: list[str] = []
for qemu in qemu_status if qemu_status is not None else []:
if "status" in qemu and qemu["status"] == "running":
node_qemu_on += 1
node_qemu_on_list.append(f"{qemu['name']} ({qemu['vmid']})")
node_qemu["total"] = node_qemu_on
node_qemu["list"] = node_qemu_on_list
api_status["qemu"] = node_qemu

api_path = f"nodes/{self.resource_id}/lxc"
lxc_status = await self.hass.async_add_executor_job(
poll_api,
self.hass,
self.config_entry,
self.proxmox,
api_path,
ProxmoxType.LXC,
self.resource_id,
)
node_lxc: dict[str, Any] = {}
node_lxc_on: int = 0
node_lxc_on_list: list[str] = []
for lxc in lxc_status if lxc_status is not None else []:
if lxc["status"] == "running":
node_lxc_on += 1
node_lxc_on_list.append(f"{lxc['name']} ({lxc['vmid']})")
node_lxc["total"] = node_lxc_on
node_lxc["list"] = node_lxc_on_list
api_status["lxc"] = node_lxc

return ProxmoxNodeData(
type=ProxmoxType.Node,
model=api_status["cpuinfo"]["model"]
if (("cpuinfo" in api_status) and "model" in api_status["cpuinfo"])
else UNDEFINED,
status=api_status.get("status", UNDEFINED),
version=api_status["version"].get("version", UNDEFINED),
uptime=api_status.get("uptime", UNDEFINED),
cpu=api_status.get("cpu", UNDEFINED),
disk_total=api_status.get("disk_max", UNDEFINED),
disk_used=api_status.get("disk_used", UNDEFINED),
memory_total=api_status["memory"]["total"]
if (("memory" in api_status) and "total" in api_status["memory"])
else UNDEFINED,
memory_used=api_status["memory"]["used"]
if (("memory" in api_status) and "used" in api_status["memory"])
else UNDEFINED,
memory_free=api_status["memory"]["free"]
if (("memory" in api_status) and "free" in api_status["memory"])
else UNDEFINED,
swap_total=api_status["swap"]["total"]
if (("swap" in api_status) and "total" in api_status["swap"])
else UNDEFINED,
swap_free=api_status["swap"]["free"]
if (("swap" in api_status) and "free" in api_status["swap"])
else UNDEFINED,
swap_used=api_status["swap"]["used"]
if (("swap" in api_status) and "used" in api_status["swap"])
else UNDEFINED,
qemu_on=api_status["qemu"]["total"],
qemu_on_list=api_status["qemu"]["list"],
lxc_on=api_status["lxc"]["total"],
lxc_on_list=api_status["lxc"]["list"],
)


Expand Down
37 changes: 24 additions & 13 deletions custom_components/proxmoxve/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Disk free percentage",
icon="mdi:harddisk",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: 1 - (x.disk_used / x.disk_total) if x.disk_total != UNDEFINED and x.disk_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 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 @@ -112,9 +114,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Disk used percentage",
icon="mdi:harddisk",
native_unit_of_measurement=PERCENTAGE,
update_offline_node
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: (x.disk_used / x.disk_total) if x.disk_total != UNDEFINED and x.disk_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 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 @@ -137,8 +140,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Memory free percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: (x.memory_free / x.memory_total) if x.memory_total != UNDEFINED and x.memory_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: (x.memory_free / x.memory_total)
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,
entity_registry_enabled_default=False,
Expand Down Expand Up @@ -172,8 +177,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Memory used percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: (x.memory_used / x.memory_total) if x.memory_total != UNDEFINED and x.memory_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 0 else 0,
value_fn=lambda x: (x.memory_used / x.memory_total)
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,
translation_key="memory_used_perc",
Expand All @@ -197,8 +204,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Swap free percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: (x.swap_free / x.swap_total) if x.swap_total != UNDEFINED and x.swap_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 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 @@ -233,8 +242,10 @@ class ProxmoxSensorEntityDescription(ProxmoxEntityDescription, SensorEntityDescr
name="Swap used percentage",
icon="mdi:memory",
native_unit_of_measurement=PERCENTAGE,
conversion_fn=lambda x: (x * 100) if x != UNDEFINED and x > 0 else 0,
value_fn=lambda x: (x.swap_used / x.swap_total) if x.swap_total != UNDEFINED and x.swap_total > 0 else 0,
conversion_fn=lambda x: (x * 100) if x > 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

0 comments on commit f38356c

Please sign in to comment.