Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down