Netgear add CPU and Memory utilization sensors (#72667)

This commit is contained in:
starkillerOG 2022-06-29 11:28:46 +02:00 committed by GitHub
parent d323508f79
commit f2809262d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 0 deletions

View File

@ -18,6 +18,7 @@ from .const import (
KEY_COORDINATOR_LINK,
KEY_COORDINATOR_SPEED,
KEY_COORDINATOR_TRAFFIC,
KEY_COORDINATOR_UTIL,
KEY_ROUTER,
PLATFORMS,
)
@ -84,6 +85,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Fetch data from the router."""
return await router.async_get_speed_test()
async def async_update_utilization() -> dict[str, Any] | None:
"""Fetch data from the router."""
return await router.async_get_utilization()
async def async_check_link_status() -> dict[str, Any] | None:
"""Fetch data from the router."""
return await router.async_get_link_status()
@ -110,6 +115,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
update_method=async_update_speed_test,
update_interval=SPEED_TEST_INTERVAL,
)
coordinator_utilization = DataUpdateCoordinator(
hass,
_LOGGER,
name=f"{router.device_name} Utilization",
update_method=async_update_utilization,
update_interval=SCAN_INTERVAL,
)
coordinator_link = DataUpdateCoordinator(
hass,
_LOGGER,
@ -121,6 +133,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if router.track_devices:
await coordinator.async_config_entry_first_refresh()
await coordinator_traffic_meter.async_config_entry_first_refresh()
await coordinator_utilization.async_config_entry_first_refresh()
await coordinator_link.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = {
@ -128,6 +141,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
KEY_COORDINATOR: coordinator,
KEY_COORDINATOR_TRAFFIC: coordinator_traffic_meter,
KEY_COORDINATOR_SPEED: coordinator_speed_test,
KEY_COORDINATOR_UTIL: coordinator_utilization,
KEY_COORDINATOR_LINK: coordinator_link,
}

View File

@ -13,6 +13,7 @@ KEY_ROUTER = "router"
KEY_COORDINATOR = "coordinator"
KEY_COORDINATOR_TRAFFIC = "coordinator_traffic"
KEY_COORDINATOR_SPEED = "coordinator_speed"
KEY_COORDINATOR_UTIL = "coordinator_utilization"
KEY_COORDINATOR_LINK = "coordinator_link"
DEFAULT_CONSIDER_HOME = timedelta(seconds=180)

View File

@ -240,6 +240,11 @@ class NetgearRouter:
self._api.allow_block_device, mac, allow_block
)
async def async_get_utilization(self) -> dict[str, Any] | None:
"""Get the system information about utilization of the router."""
async with self._api_lock:
return await self.hass.async_add_executor_job(self._api.get_system_info)
async def async_reboot(self) -> None:
"""Reboot the router."""
async with self._api_lock:

View File

@ -12,6 +12,7 @@ from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
@ -32,6 +33,7 @@ from .const import (
KEY_COORDINATOR_LINK,
KEY_COORDINATOR_SPEED,
KEY_COORDINATOR_TRAFFIC,
KEY_COORDINATOR_UTIL,
KEY_ROUTER,
)
from .router import NetgearDeviceEntity, NetgearRouter, NetgearRouterEntity
@ -245,6 +247,25 @@ SENSOR_SPEED_TYPES = [
),
]
SENSOR_UTILIZATION = [
NetgearSensorEntityDescription(
key="NewCPUUtilization",
name="CPU Utilization",
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=PERCENTAGE,
icon="mdi:cpu-64-bit",
state_class=SensorStateClass.MEASUREMENT,
),
NetgearSensorEntityDescription(
key="NewMemoryUtilization",
name="Memory Utilization",
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=PERCENTAGE,
icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT,
),
]
SENSOR_LINK_TYPES = [
NetgearSensorEntityDescription(
key="NewEthernetLinkStatus",
@ -263,6 +284,7 @@ async def async_setup_entry(
coordinator = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR]
coordinator_traffic = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_TRAFFIC]
coordinator_speed = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_SPEED]
coordinator_utilization = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_UTIL]
coordinator_link = hass.data[DOMAIN][entry.entry_id][KEY_COORDINATOR_LINK]
# Router entities
@ -278,6 +300,11 @@ async def async_setup_entry(
NetgearRouterSensorEntity(coordinator_speed, router, description)
)
for description in SENSOR_UTILIZATION:
router_entities.append(
NetgearRouterSensorEntity(coordinator_utilization, router, description)
)
for description in SENSOR_LINK_TYPES:
router_entities.append(
NetgearRouterSensorEntity(coordinator_link, router, description)