diff --git a/glances_api/__init__.py b/glances_api/__init__.py index 9c0f56d..b377c66 100644 --- a/glances_api/__init__.py +++ b/glances_api/__init__.py @@ -261,11 +261,13 @@ async def get_ha_sensor_data(self) -> dict[str, Any]: if data := self.data.get("amps"): sensor_data["amps"] = {} for amps in data: + rescount: str | int if amps["result"] is None: rescount = 0 restext = "None" else: - rescount = re.findall(r"^\d+",amps["result"])[0] + prefix = re.match(r"^\d+", amps["result"]) + rescount = prefix.group() if prefix is not None else 0 restext = amps["result"] sensor_data["amps"][amps["name"]] = { "result":restext, diff --git a/tests/test_responses.py b/tests/test_responses.py index d65f72f..8f843b2 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -487,6 +487,38 @@ async def test_ha_sensor_data_diskio_first_sample(httpx_mock: HTTPXMock) -> None assert result == expected +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("amp_result", "expected_text", "expected_count"), + [ + pytest.param(None, "None", 0, id="no-result"), + pytest.param("", "", 0, id="empty-result"), + pytest.param("No running process", "No running process", 0, id="no-process"), + pytest.param("healthy", "healthy", 0, id="text-result"), + pytest.param("12 packages", "12 packages", "12", id="numeric-prefix"), + pytest.param("0 packages", "0 packages", "0", id="zero-prefix"), + ], +) +async def test_ha_sensor_data_preserves_amp_result( + httpx_mock: HTTPXMock, + amp_result: str | None, + expected_text: str, + expected_count: str | int, +) -> None: + """AMP text without a numeric prefix must not interrupt sensor updates.""" + httpx_mock.add_response( + json={**RESPONSE_V4, "amps": [{"name": "example", "result": amp_result}]} + ) + + client = Glances(version=4) + result = await client.get_ha_sensor_data() + + assert result == { + **HA_SENSOR_DATA_V4, + "amps": {"example": {"result": expected_text, "resultcount": expected_count}}, + } + + @pytest.mark.asyncio async def test_ha_sensor_data_with_incomplete_container_information( httpx_mock: HTTPXMock,