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
11 changes: 8 additions & 3 deletions glances_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,14 @@ async def get_ha_sensor_data(self) -> dict[str, Any]:
if data := self.data.get("diskio"):
sensor_data["diskio"] = {}
for disk in data:
time_since_update = disk["time_since_update"]
read = write = None
# Glances v4 omits "time_since_update" on the first sample after
# the server starts, as there is no previous sample to compare to
if time_since_update := disk.get("time_since_update"):
read = round(disk["read_bytes"] / time_since_update)
write = round(disk["write_bytes"] / time_since_update)
sensor_data["diskio"][disk["disk_name"]] = {
"read": round(disk["read_bytes"] / time_since_update),
"write": round(disk["write_bytes"] / time_since_update),
"read": read,
"write": write,
}
return sensor_data
32 changes: 32 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,38 @@ async def test_ha_sensor_data(
assert result == expected


@pytest.mark.asyncio
async def test_ha_sensor_data_diskio_first_sample(httpx_mock: HTTPXMock) -> None:
"""Test the diskio sensors on the first sample after Glances v4 starts."""
# No previous sample yet: no "time_since_update", no rates, raw counters
response = {
**RESPONSE_V4,
"diskio": [
{
"read_count": 442933,
"write_count": 937984,
"read_bytes": 15653558272,
"write_bytes": 16872076288,
"read_time": 64763666,
"write_time": 171581008,
"key": "disk_name",
"disk_name": "nvme0n1",
},
],
}
expected = {
**HA_SENSOR_DATA_V4,
"diskio": {"nvme0n1": {"read": None, "write": None}},
}

httpx_mock.add_response(json=response)

client = Glances(version=4)
result = await client.get_ha_sensor_data()

assert result == expected


@pytest.mark.asyncio
async def test_ha_sensor_data_with_incomplete_container_information(
httpx_mock: HTTPXMock,
Expand Down