From 620db191b1e765ff983f60065b79ac53e441abdb Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Thu, 4 Nov 2021 16:17:20 +0100 Subject: [PATCH] Use entity category and state class in devolo Home Network (#59071) * Use entity category and state class * Add tests --- .../components/devolo_home_network/sensor.py | 10 +++++++++- tests/components/devolo_home_network/test_sensor.py | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/devolo_home_network/sensor.py b/homeassistant/components/devolo_home_network/sensor.py index 3b0175d8c31..66f686a603e 100644 --- a/homeassistant/components/devolo_home_network/sensor.py +++ b/homeassistant/components/devolo_home_network/sensor.py @@ -7,8 +7,13 @@ from typing import Any from devolo_plc_api.device import Device -from homeassistant.components.sensor import SensorEntity, SensorEntityDescription +from homeassistant.components.sensor import ( + STATE_CLASS_MEASUREMENT, + SensorEntity, + SensorEntityDescription, +) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import HomeAssistantType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator @@ -39,6 +44,7 @@ class DevoloSensorEntityDescription( SENSOR_TYPES: dict[str, DevoloSensorEntityDescription] = { CONNECTED_PLC_DEVICES: DevoloSensorEntityDescription( key=CONNECTED_PLC_DEVICES, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_registry_enabled_default=False, icon="mdi:lan", name="Connected PLC devices", @@ -51,10 +57,12 @@ SENSOR_TYPES: dict[str, DevoloSensorEntityDescription] = { entity_registry_enabled_default=True, icon="mdi:wifi", name="Connected Wifi clients", + state_class=STATE_CLASS_MEASUREMENT, value_func=lambda data: len(data["connected_stations"]), ), NEIGHBORING_WIFI_NETWORKS: DevoloSensorEntityDescription( key=NEIGHBORING_WIFI_NETWORKS, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, entity_registry_enabled_default=False, icon="mdi:wifi-marker", name="Neighboring Wifi networks", diff --git a/tests/components/devolo_home_network/test_sensor.py b/tests/components/devolo_home_network/test_sensor.py index 100db9005aa..6ce4c36cdb5 100644 --- a/tests/components/devolo_home_network/test_sensor.py +++ b/tests/components/devolo_home_network/test_sensor.py @@ -8,9 +8,10 @@ from homeassistant.components.devolo_home_network.const import ( LONG_UPDATE_INTERVAL, SHORT_UPDATE_INTERVAL, ) -from homeassistant.components.sensor import DOMAIN -from homeassistant.const import STATE_UNAVAILABLE +from homeassistant.components.sensor import DOMAIN, STATE_CLASS_MEASUREMENT +from homeassistant.const import ENTITY_CATEGORY_DIAGNOSTIC, STATE_UNAVAILABLE from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry from homeassistant.util import dt from . import configure_integration @@ -46,6 +47,7 @@ async def test_update_connected_wifi_clients(hass: HomeAssistant): state = hass.states.get(state_key) assert state is not None assert state.state == "1" + assert state.attributes["state_class"] == STATE_CLASS_MEASUREMENT # Emulate device failure with patch( @@ -86,6 +88,9 @@ async def test_update_neighboring_wifi_networks(hass: HomeAssistant): assert state is not None assert state.state == "1" + er = entity_registry.async_get(hass) + assert er.async_get(state_key).entity_category == ENTITY_CATEGORY_DIAGNOSTIC + # Emulate device failure with patch( "devolo_plc_api.device_api.deviceapi.DeviceApi.async_get_wifi_neighbor_access_points", @@ -125,6 +130,9 @@ async def test_update_connected_plc_devices(hass: HomeAssistant): assert state is not None assert state.state == "1" + er = entity_registry.async_get(hass) + assert er.async_get(state_key).entity_category == ENTITY_CATEGORY_DIAGNOSTIC + # Emulate device failure with patch( "devolo_plc_api.plcnet_api.plcnetapi.PlcNetApi.async_get_network_overview",