Don't create AsusWRT loadavg sensors when unavailable (#106790)

pull/109689/head
ollo69 2024-02-05 09:03:43 +01:00 committed by GitHub
parent 458e1f3a5e
commit b56dd3f808
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 2 deletions

View File

@ -11,6 +11,7 @@ from typing import Any, TypeVar, cast
from aioasuswrt.asuswrt import AsusWrt as AsusWrtLegacy
from aiohttp import ClientSession
from pyasuswrt import AsusWrtError, AsusWrtHttp
from pyasuswrt.exceptions import AsusWrtNotAvailableInfoError
from homeassistant.const import (
CONF_HOST,
@ -354,13 +355,14 @@ class AsusWrtHttpBridge(AsusWrtBridge):
async def async_get_available_sensors(self) -> dict[str, dict[str, Any]]:
"""Return a dictionary of available sensors for this bridge."""
sensors_temperatures = await self._get_available_temperature_sensors()
sensors_loadavg = await self._get_loadavg_sensors_availability()
sensors_types = {
SENSORS_TYPE_BYTES: {
KEY_SENSORS: SENSORS_BYTES,
KEY_METHOD: self._get_bytes,
},
SENSORS_TYPE_LOAD_AVG: {
KEY_SENSORS: SENSORS_LOAD_AVG,
KEY_SENSORS: sensors_loadavg,
KEY_METHOD: self._get_load_avg,
},
SENSORS_TYPE_RATES: {
@ -393,6 +395,16 @@ class AsusWrtHttpBridge(AsusWrtBridge):
return []
return available_sensors
async def _get_loadavg_sensors_availability(self) -> list[str]:
"""Check if load avg is available on the router."""
try:
await self._api.async_get_loadavg()
except AsusWrtNotAvailableInfoError:
return []
except AsusWrtError:
pass
return SENSORS_LOAD_AVG
@handle_errors_and_zip(AsusWrtError, SENSORS_BYTES)
async def _get_bytes(self) -> Any:
"""Fetch byte information from the router."""

View File

@ -1,7 +1,7 @@
"""Tests for the AsusWrt sensor."""
from datetime import timedelta
from pyasuswrt.asuswrt import AsusWrtError
from pyasuswrt.exceptions import AsusWrtError, AsusWrtNotAvailableInfoError
import pytest
from homeassistant.components import device_tracker, sensor
@ -226,6 +226,29 @@ async def test_loadavg_sensors_http(hass: HomeAssistant, connect_http) -> None:
await _test_loadavg_sensors(hass, CONFIG_DATA_HTTP)
async def test_loadavg_sensors_unaivalable_http(
hass: HomeAssistant, connect_http
) -> None:
"""Test load average sensors no available using http."""
config_entry, sensor_prefix = _setup_entry(hass, CONFIG_DATA_HTTP, SENSORS_LOAD_AVG)
config_entry.add_to_hass(hass)
connect_http.return_value.async_get_loadavg.side_effect = (
AsusWrtNotAvailableInfoError
)
# initial devices setup
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
async_fire_time_changed(hass, utcnow() + timedelta(seconds=30))
await hass.async_block_till_done()
# assert load average sensors not available
assert not hass.states.get(f"{sensor_prefix}_sensor_load_avg1")
assert not hass.states.get(f"{sensor_prefix}_sensor_load_avg5")
assert not hass.states.get(f"{sensor_prefix}_sensor_load_avg15")
async def test_temperature_sensors_http_fail(
hass: HomeAssistant, connect_http_sens_fail
) -> None: