Skip to content

Commit

Permalink
Add Load Average sensors for HTTP (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ollo69 authored Oct 7, 2023
1 parent 501054e commit e4ab3d6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
13 changes: 13 additions & 0 deletions custom_components/asuswrt_custom/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ async def async_get_available_sensors(self) -> dict[str, dict[str, Any]]:
KEY_SENSORS: sensors_cpu,
KEY_METHOD: self._get_cpu_usage,
},
SENSORS_TYPE_LOAD_AVG: {
KEY_SENSORS: SENSORS_LOAD_AVG,
KEY_METHOD: self._get_load_avg,
},
SENSORS_TYPE_MEMORY: {
KEY_SENSORS: SENSORS_MEMORY,
KEY_METHOD: self._get_memory_usage,
Expand Down Expand Up @@ -552,6 +556,15 @@ async def _get_memory_usage(self) -> dict[str, Any]:

return _get_dict(SENSORS_MEMORY, list(memory.values()))

async def _get_load_avg(self) -> dict[str, Any]:
"""Fetch cpu load avg information from the router."""
try:
load_avg = await self._api.async_get_loadavg()
except AsusWrtError as exc:
raise UpdateFailed(exc) from exc

return _get_dict(SENSORS_LOAD_AVG, list(load_avg.values()))

async def _get_rates(self) -> dict[str, Any]:
"""Fetch rates information from the router."""
try:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/asuswrt_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/ollo69/ha_asuswrt_custom/issues",
"loggers": ["aioasuswrt", "asyncssh", "pyasuswrt"],
"requirements": ["aioasuswrt==1.4.0", "pyasuswrt==0.1.16"],
"requirements": ["aioasuswrt==1.4.0", "pyasuswrt==0.1.17"],
"version": "0.8.0"
}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ flake8==6.0.0
isort==5.12.0
black==23.1.0
aioasuswrt==1.4.0
pyasuswrt==0.1.16
pyasuswrt==0.1.17
2 changes: 1 addition & 1 deletion requirements_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pytest==7.3.1
pytest-homeassistant-custom-component==0.13.42
# From our manifest.json for our custom component
aioasuswrt==1.4.0
pyasuswrt==0.1.16
pyasuswrt==0.1.17
9 changes: 8 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
MOCK_CURRENT_TRANSFER_RATES_HTTP = {
k: v for k, v in enumerate(MOCK_CURRENT_TRANSFER_RATES)
}
MOCK_LOAD_AVG = [1.1, 1.2, 1.3]
MOCK_LOAD_AVG_HTTP = {"load_avg_1": 1.1, "load_avg_5": 1.2, "load_avg_15": 1.3}
MOCK_LOAD_AVG = list(MOCK_LOAD_AVG_HTTP.values())
MOCK_MEMORY_USAGE = {
"mem_usage_perc": 52.48,
"mem_total": 1048576,
Expand Down Expand Up @@ -209,6 +210,9 @@ def mock_controller_connect_http(mock_devices_http):
service_mock.return_value.async_get_cpu_usage = AsyncMock(
return_value=MOCK_CPU_USAGE
)
service_mock.return_value.async_get_loadavg = AsyncMock(
return_value=MOCK_LOAD_AVG_HTTP
)
service_mock.return_value.async_get_memory_usage = AsyncMock(
return_value=MOCK_MEMORY_USAGE
)
Expand Down Expand Up @@ -269,6 +273,9 @@ def mock_controller_connect_http_sens_fail():
service_mock.return_value.async_get_cpu_usage = AsyncMock(
side_effect=AsusWrtError
)
service_mock.return_value.async_get_loadavg = AsyncMock(
side_effect=AsusWrtError
)
service_mock.return_value.async_get_memory_usage = AsyncMock(
side_effect=AsusWrtError
)
Expand Down

0 comments on commit e4ab3d6

Please sign in to comment.