From ef0f36f6ee5104f77c9cdcdc20205553d5389114 Mon Sep 17 00:00:00 2001 From: ezaekiel <130962901+ezaekiel@users.noreply.github.com> Date: Fri, 9 Oct 2026 08:52:28 +0200 Subject: [PATCH] Fix Proxmox VE update version detection (#184823) Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/proxmoxve/helpers.py | 27 +-- .../proxmoxve/snapshots/test_update.ambr | 194 +++++++++++++++++- tests/components/proxmoxve/test_update.py | 51 ++++- 3 files changed, 249 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/proxmoxve/helpers.py b/homeassistant/components/proxmoxve/helpers.py index c968cc5583a9..f84309457e68 100644 --- a/homeassistant/components/proxmoxve/helpers.py +++ b/homeassistant/components/proxmoxve/helpers.py @@ -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, diff --git a/tests/components/proxmoxve/snapshots/test_update.ambr b/tests/components/proxmoxve/snapshots/test_update.ambr index 169283b73a1b..96133b90953f 100644 --- a/tests/components/proxmoxve/snapshots/test_update.ambr +++ b/tests/components/proxmoxve/snapshots/test_update.ambr @@ -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({ : False, @@ -45,8 +45,194 @@ : 'pve1 Software update', : False, : '9.1.6', - : '9.1.9-p4-d1', - : '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.', + : '9.1.7-p4-d1', + : '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.', + : None, + : None, + : , + : None, + : None, + }), + 'context': , + 'entity_id': 'update.pve1_software_update', + 'last_changed': , + 'last_reported': , + 'last_updated': , + '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': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.pve1_software_update', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + '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({ + : False, + : 0, + : '/api/brands/integration/proxmoxve/icon.png', + : 'pve1 Software update', + : False, + : '9.1.6', + : '9.1.6-p1-d0', + : '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.', + : None, + : None, + : , + : None, + : None, + }), + 'context': , + 'entity_id': 'update.pve1_software_update', + 'last_changed': , + 'last_reported': , + 'last_updated': , + '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': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.pve1_software_update', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + '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({ + : False, + : 0, + : '/api/brands/integration/proxmoxve/icon.png', + : 'pve1 Software update', + : False, + : '9.1.6', + : '9.1.6-p1-d0', + : '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.', + : None, + : None, + : , + : None, + : None, + }), + 'context': , + 'entity_id': 'update.pve1_software_update', + 'last_changed': , + 'last_reported': , + 'last_updated': , + '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': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'update', + 'entity_category': , + 'entity_id': 'update.pve1_software_update', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + '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': , + '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({ + : False, + : 0, + : '/api/brands/integration/proxmoxve/icon.png', + : 'pve1 Software update', + : False, + : '9.1.6', + : '9.2.21-p2-d0', + : '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.', : None, : None, : , diff --git a/tests/components/proxmoxve/test_update.py b/tests/components/proxmoxve/test_update.py index d15f2327ca3d..31fe33b45e6a 100644 --- a/tests/components/proxmoxve/test_update.py +++ b/tests/components/proxmoxve/test_update.py @@ -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(