Fix Proxmox VE update version detection (#184823)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
ezaekiel
2026-10-09 08:52:28 +02:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent 6277d8a174
commit ef0f36f6ee
3 changed files with 249 additions and 23 deletions
+11 -16
View File
@@ -3,8 +3,6 @@
from dataclasses import dataclass
from typing import Any
from packaging.version import parse as parse_version
from .const import ProxmoxPermission
@@ -46,13 +44,6 @@ def is_proxmox_package(update: dict[str, Any]) -> bool:
)
def latest_version(versions: list[str]) -> str:
"""Return the latest version from a list of version strings."""
# Fix proxmox -pve1 style suffixes
safe_versions = [v.split("-")[0] for v in versions]
return max(safe_versions, key=parse_version)
def update_version(
current_version: str,
updates: list[dict[str, Any]],
@@ -61,14 +52,18 @@ def update_version(
count = len(updates)
pve_count = sum(is_proxmox_package(u) for u in updates)
other_count = len(updates) - pve_count
versions = [current_version] + [
u["Version"] for u in updates if is_proxmox_package(u)
]
latest = latest_version(versions) if pve_count else current_version
other_count = count - pve_count
pve_manager_update = next(
(update for update in updates if update.get("Package") == "pve-manager"),
None,
)
latest = (
pve_manager_update["Version"].split("-")[0]
if pve_manager_update
else current_version
)
return ProxmoxUpdateInfo(
latest_version=latest if count else current_version,
latest_version=latest,
latest_version_id=f"{latest}-p{pve_count}-d{other_count}"
if count
else current_version,
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_all_entities[update.pve1_software_update-entry]
# name: test_all_entities[all-entities][update.pve1_software_update-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
@@ -36,7 +36,7 @@
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[update.pve1_software_update-state]
# name: test_all_entities[all-entities][update.pve1_software_update-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<UpdateEntityStateAttribute.AUTO_UPDATE: 'auto_update'>: False,
@@ -45,8 +45,194 @@
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'pve1 Software update',
<UpdateEntityStateAttribute.IN_PROGRESS: 'in_progress'>: False,
<UpdateEntityStateAttribute.INSTALLED_VERSION: 'installed_version'>: '9.1.6',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '9.1.9-p4-d1',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: 'A total of 5 package update(s) are pending installation: of these 4 relate to Proxmox and 1 to other updates. Please visit the [Proxmox VE node](https://127.0.0.1:8006/#v1:0:=node/pve1) for details on the pending updates and to upgrade to 9.1.9.',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '9.1.7-p4-d1',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: 'A total of 5 package update(s) are pending installation: of these 4 relate to Proxmox and 1 to other updates. Please visit the [Proxmox VE node](https://127.0.0.1:8006/#v1:0:=node/pve1) for details on the pending updates and to upgrade to 9.1.7.',
<UpdateEntityStateAttribute.RELEASE_URL: 'release_url'>: None,
<UpdateEntityStateAttribute.SKIPPED_VERSION: 'skipped_version'>: None,
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <UpdateEntityFeature: 16>,
<UpdateEntityStateAttribute.TITLE: 'title'>: None,
<UpdateEntityStateAttribute.UPDATE_PERCENTAGE: 'update_percentage'>: None,
}),
'context': <ANY>,
'entity_id': 'update.pve1_software_update',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'on',
})
# ---
# name: test_all_entities[debian-package-version-is-not-parsed][update.pve1_software_update-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.pve1_software_update',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Software update',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Software update',
'platform': 'proxmoxve',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <UpdateEntityFeature: 16>,
'translation_key': 'node_update',
'unique_id': '1234_node/pve1_node_update',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[debian-package-version-is-not-parsed][update.pve1_software_update-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<UpdateEntityStateAttribute.AUTO_UPDATE: 'auto_update'>: False,
<UpdateEntityStateAttribute.DISPLAY_PRECISION: 'display_precision'>: 0,
<EntityStateAttribute.ENTITY_PICTURE: 'entity_picture'>: '/api/brands/integration/proxmoxve/icon.png',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'pve1 Software update',
<UpdateEntityStateAttribute.IN_PROGRESS: 'in_progress'>: False,
<UpdateEntityStateAttribute.INSTALLED_VERSION: 'installed_version'>: '9.1.6',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '9.1.6-p1-d0',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: 'A total of 1 package update(s) are pending installation: of these 1 relate to Proxmox and 0 to other updates. Please visit the [Proxmox VE node](https://127.0.0.1:8006/#v1:0:=node/pve1) for details on the pending updates and to upgrade to 9.1.6.',
<UpdateEntityStateAttribute.RELEASE_URL: 'release_url'>: None,
<UpdateEntityStateAttribute.SKIPPED_VERSION: 'skipped_version'>: None,
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <UpdateEntityFeature: 16>,
<UpdateEntityStateAttribute.TITLE: 'title'>: None,
<UpdateEntityStateAttribute.UPDATE_PERCENTAGE: 'update_percentage'>: None,
}),
'context': <ANY>,
'entity_id': 'update.pve1_software_update',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[higher-ceph-version-is-not-pve-version][update.pve1_software_update-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.pve1_software_update',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Software update',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Software update',
'platform': 'proxmoxve',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <UpdateEntityFeature: 16>,
'translation_key': 'node_update',
'unique_id': '1234_node/pve1_node_update',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[higher-ceph-version-is-not-pve-version][update.pve1_software_update-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<UpdateEntityStateAttribute.AUTO_UPDATE: 'auto_update'>: False,
<UpdateEntityStateAttribute.DISPLAY_PRECISION: 'display_precision'>: 0,
<EntityStateAttribute.ENTITY_PICTURE: 'entity_picture'>: '/api/brands/integration/proxmoxve/icon.png',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'pve1 Software update',
<UpdateEntityStateAttribute.IN_PROGRESS: 'in_progress'>: False,
<UpdateEntityStateAttribute.INSTALLED_VERSION: 'installed_version'>: '9.1.6',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '9.1.6-p1-d0',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: 'A total of 1 package update(s) are pending installation: of these 1 relate to Proxmox and 0 to other updates. Please visit the [Proxmox VE node](https://127.0.0.1:8006/#v1:0:=node/pve1) for details on the pending updates and to upgrade to 9.1.6.',
<UpdateEntityStateAttribute.RELEASE_URL: 'release_url'>: None,
<UpdateEntityStateAttribute.SKIPPED_VERSION: 'skipped_version'>: None,
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <UpdateEntityFeature: 16>,
<UpdateEntityStateAttribute.TITLE: 'title'>: None,
<UpdateEntityStateAttribute.UPDATE_PERCENTAGE: 'update_percentage'>: None,
}),
'context': <ANY>,
'entity_id': 'update.pve1_software_update',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_all_entities[pve-manager-version-is-used][update.pve1_software_update-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'update',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'update.pve1_software_update',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Software update',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Software update',
'platform': 'proxmoxve',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': <UpdateEntityFeature: 16>,
'translation_key': 'node_update',
'unique_id': '1234_node/pve1_node_update',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[pve-manager-version-is-used][update.pve1_software_update-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<UpdateEntityStateAttribute.AUTO_UPDATE: 'auto_update'>: False,
<UpdateEntityStateAttribute.DISPLAY_PRECISION: 'display_precision'>: 0,
<EntityStateAttribute.ENTITY_PICTURE: 'entity_picture'>: '/api/brands/integration/proxmoxve/icon.png',
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'pve1 Software update',
<UpdateEntityStateAttribute.IN_PROGRESS: 'in_progress'>: False,
<UpdateEntityStateAttribute.INSTALLED_VERSION: 'installed_version'>: '9.1.6',
<UpdateEntityStateAttribute.LATEST_VERSION: 'latest_version'>: '9.2.21-p2-d0',
<UpdateEntityStateAttribute.RELEASE_SUMMARY: 'release_summary'>: 'A total of 2 package update(s) are pending installation: of these 2 relate to Proxmox and 0 to other updates. Please visit the [Proxmox VE node](https://127.0.0.1:8006/#v1:0:=node/pve1) for details on the pending updates and to upgrade to 9.2.21.',
<UpdateEntityStateAttribute.RELEASE_URL: 'release_url'>: None,
<UpdateEntityStateAttribute.SKIPPED_VERSION: 'skipped_version'>: None,
<EntityStateAttribute.SUPPORTED_FEATURES: 'supported_features'>: <UpdateEntityFeature: 16>,
+48 -3
View File
@@ -2,6 +2,7 @@
from unittest.mock import MagicMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.const import STATE_OFF, STATE_UNAVAILABLE, Platform
@@ -16,24 +17,68 @@ from tests.typing import WebSocketGenerator
ENTITY_ID = "update.pve1_software_update"
@pytest.mark.parametrize(
("updates", "expected_latest_version"),
[
pytest.param(
[
{"Package": "bind9-host", "Version": "1", "Origin": "Debian"},
{"Package": "pve-manager", "Version": "9.1.7", "Origin": "Proxmox"},
{
"Package": "libpve-common-perl",
"Version": "9.1.9",
"Origin": "Proxmox",
},
{"Package": "corosync", "Version": "3.1.10", "Origin": "Proxmox"},
{"Package": "pve-firmware", "Version": "3.18", "Origin": "Proxmox"},
],
"9.1.7-p4-d1",
id="all-entities",
),
pytest.param(
[{"Package": "ceph-common", "Version": "19.2.6", "Origin": "Proxmox"}],
"9.1.6-p1-d0",
id="higher-ceph-version-is-not-pve-version",
),
pytest.param(
[{"Package": "libpve-storage-perl", "Version": "1+16.1+2+pmx1"}],
"9.1.6-p1-d0",
id="debian-package-version-is-not-parsed",
),
pytest.param(
[
{"Package": "ceph-common", "Version": "19.2.6", "Origin": "Proxmox"},
{"Package": "pve-manager", "Version": "9.2.21-1"},
],
"9.2.21-p2-d0",
id="pve-manager-version-is-used",
),
],
)
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_proxmox_client: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
updates: list[dict[str, str]] | None,
expected_latest_version: str,
) -> None:
"""Test all entities."""
# Ensure Sys.Modify permissions to ensure update status can be determined
mock_proxmox_client.access.permissions.get.return_value = MERGED_PERMISSIONS
mock_proxmox_client.nodes.return_value.apt.update.get.return_value = updates
with patch(
"homeassistant.components.proxmoxve.PLATFORMS",
[Platform.UPDATE],
):
await setup_integration(hass, mock_config_entry)
await snapshot_platform(
hass, entity_registry, snapshot, mock_config_entry.entry_id
)
state = hass.states.get(ENTITY_ID)
assert state.attributes["latest_version"] == expected_latest_version
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
async def test_update_entities_ignored(