Skip to content

Commit

Permalink
Merge pull request #182 from dougiteixeira/fix-disk-attributes
Browse files Browse the repository at this point in the history
Fix disk attributes when the API returns in text format
  • Loading branch information
dougiteixeira authored Nov 17, 2023
2 parents 2272ff4 + ef580f9 commit 721d866
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
41 changes: 29 additions & 12 deletions custom_components/proxmoxve/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,23 +676,40 @@ def poll_api_attributes() -> dict[str, Any] | None:
if disk["devpath"] == self.resource_id:
disk_attributes = {}
disk_attributes_api = await self.hass.async_add_executor_job(poll_api_attributes)
for disk_attribute in disk_attributes_api["attributes"]:
if disk_attribute["name"] == "Power_Cycle_Count":
disk_attributes[disk_attribute["name"]]=disk_attribute["raw"]
elif disk_attribute["name"] == "Temperature_Celsius":
disk_attributes[disk_attribute["name"]]=disk_attribute["raw"].split(" ", 1)[0]

attributes_json = []
if "attributes" in disk_attributes_api:
attributes_json = disk_attributes_api["attributes"]
else:
if "type" in disk_attributes_api and disk_attributes_api["type"] == "text":
attributes_text = disk_attributes_api["text"].split("\n")
for value_text in attributes_text:
value_json = value_text.split(":")
if len(value_json) >= 2:
attributes_json.append(
{
"name": value_json[0].strip(),
"raw": value_json[1].strip(),
}
)

for disk_attribute in attributes_json:
if disk_attribute["name"] in ("Power_Cycle_Count", "Power Cycles"):
disk_attributes["Power_Cycle_Count"]=disk_attribute["raw"]
elif disk_attribute["name"] in ("Temperature_Celsius", "Temperature"):
disk_attributes["Temperature_Celsius"]=disk_attribute["raw"].split(" ", 1)[0]

return ProxmoxDiskData(
type=ProxmoxType.Disk,
node=self.node_name,
path=self.resource_id,
size=disk["size"],
health=disk["health"],
vendor=disk["vendor"],
serial=disk["serial"],
model=disk["model"],
disk_rpm=disk["rpm"],
disk_type=disk["type"],
size=disk["size"] if "size" in disk else None,
health=disk["health"] if "health" in disk else None,
vendor=disk["vendor"] if "vendor" in disk else None,
serial=disk["serial"] if "serial" in disk else None,
model=disk["model"] if "model" in disk else None,
disk_rpm=disk["rpm"] if "rpm" in disk else None,
disk_type=disk["type"] if "type" in disk else None,
temperature=disk_attributes["Temperature_Celsius"] if "Temperature_Celsius" in disk_attributes else None,
power_cycles=disk_attributes["Power_Cycle_Count"] if "Power_Cycle_Count" in disk_attributes else None,
)
Expand Down
17 changes: 11 additions & 6 deletions custom_components/proxmoxve/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def poll_api_attributes(proxmox, node: str, disk: str) -> dict[str, Any] | None:
if error.status_code == 403:
resources = "403 Forbidden: Permission check failed"
else:
resources = "Error"
resources = error


nodes = {}
Expand All @@ -59,7 +59,7 @@ def poll_api_attributes(proxmox, node: str, disk: str) -> dict[str, Any] | None:
if error.status_code == 403:
nodes_api = "403 Forbidden: Permission check failed"
else:
nodes_api = "Error"
nodes_api = error

for node in nodes_api:
nodes[node["node"]] = node
Expand All @@ -70,31 +70,36 @@ def poll_api_attributes(proxmox, node: str, disk: str) -> dict[str, Any] | None:
if error.status_code == 403:
nodes[node["node"]]["qemu"] = "403 Forbidden: Permission check failed"
else:
nodes[node["node"]]["qemu"] = "Error"
nodes[node["node"]]["qemu"] = error

try:
nodes[node["node"]]["lxc"] = await hass.async_add_executor_job(proxmox.nodes(node["node"]).lxc.get)
except ResourceException as error:
if error.status_code == 403:
nodes[node["node"]]["lxc"] = "403 Forbidden: Permission check failed"
else:
nodes[node["node"]]["lxc"] = "Error"
nodes[node["node"]]["lxc"] = error

try:
nodes[node["node"]]["storage"] = await hass.async_add_executor_job(proxmox.nodes(node["node"]).storage.get)
except ResourceException as error:
if error.status_code == 403:
nodes[node["node"]]["storage"] = "403 Forbidden: Permission check failed"
else:
nodes[node["node"]]["storage"] = "Error"
nodes[node["node"]]["storage"] = error

try:
nodes[node["node"]]["updates"] = await hass.async_add_executor_job(proxmox.nodes(node["node"]).apt.update.get)
except ResourceException as error:
if error.status_code == 403:
nodes[node["node"]]["updates"] = "403 Forbidden: Permission check failed"
else:
nodes[node["node"]]["updates"] = "Error"
nodes[node["node"]]["updates"] = error

try:
nodes[node["node"]]["versions"] = await hass.async_add_executor_job(proxmox.nodes(node["node"]).apt.versions.get)
except ResourceException as error:
nodes[node["node"]]["updates"] = error

try:
disks = await hass.async_add_executor_job(proxmox.nodes(node["node"]).disks.list.get)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/proxmoxve/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"issue_tracker": "https://github.com/dougiteixeira/proxmoxve/issues",
"loggers": ["proxmoxer"],
"requirements": ["proxmoxer==2.0.1"],
"version": "3.2.0"
"version": "3.1.0"
}

0 comments on commit 721d866

Please sign in to comment.