Fix division by zero error when calculating volume used percentage (#182428)

This commit is contained in:
Oliver Gründel
2026-09-18 06:58:48 +02:00
committed by GitHub
parent c5eb04bc2e
commit 4b1c9350b6
2 changed files with 49 additions and 1 deletions
+1 -1
View File
@@ -482,7 +482,7 @@ class QNAPVolumeSensor(QNAPSensor):
if self.entity_description.key == "volume_size_used":
return used_gb
if self.entity_description.key == "volume_percentage_used":
if self.entity_description.key == "volume_percentage_used" and total_gb != 0:
return used_gb / total_gb * 100
return None
+48
View File
@@ -0,0 +1,48 @@
"""Test the QNAP sensors."""
from unittest.mock import MagicMock
import pytest
from homeassistant.components.qnap.sensor import _VOLUME_MON_COND, QNAPVolumeSensor
from .conftest import TEST_SERIAL
def _make_coordinator(volumes: dict) -> MagicMock:
"""Create a mock coordinator with the given volumes."""
coordinator = MagicMock()
coordinator.data = {
"system_stats": {
"system": {"name": "Test NAS", "model": "TS-1"},
"firmware": {"version": "1.0"},
},
"volumes": volumes,
}
return coordinator
def _volume_sensor(coordinator: MagicMock) -> QNAPVolumeSensor:
"""Create a volume percentage sensor."""
description = next(
desc for desc in _VOLUME_MON_COND if desc.key == "volume_percentage_used"
)
return QNAPVolumeSensor(coordinator, description, TEST_SERIAL, "Volume 1")
@pytest.mark.parametrize(
("free_size", "total_size", "expected"),
[
(75, 100, 25.0),
(0, 0, None),
],
)
def test_volume_percentage_used(
free_size: int, total_size: int, expected: float | None
) -> None:
"""Test the volume percentage used sensor."""
coordinator = _make_coordinator(
{"Volume 1": {"free_size": free_size, "total_size": total_size}}
)
sensor = _volume_sensor(coordinator)
assert sensor.native_value == expected