Fix AsusWRT scanner entity DeviceInfo (#54648)

This commit is contained in:
ollo69 2021-08-16 19:37:49 +02:00 committed by GitHub
parent a41ee9e870
commit c68253b580
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 28 deletions

View file

@ -60,6 +60,12 @@ class AsusWrtDevice(ScannerEntity):
self._device = device self._device = device
self._attr_unique_id = device.mac self._attr_unique_id = device.mac
self._attr_name = device.name or DEFAULT_DEVICE_NAME self._attr_name = device.name or DEFAULT_DEVICE_NAME
self._attr_device_info = {
"connections": {(CONNECTION_NETWORK_MAC, device.mac)},
"default_model": "ASUSWRT Tracked device",
}
if device.name:
self._attr_device_info["default_name"] = device.name
@property @property
def is_connected(self): def is_connected(self):
@ -90,11 +96,6 @@ class AsusWrtDevice(ScannerEntity):
def async_on_demand_update(self): def async_on_demand_update(self):
"""Update state.""" """Update state."""
self._device = self._router.devices[self._device.mac] self._device = self._router.devices[self._device.mac]
self._attr_device_info = {
"connections": {(CONNECTION_NETWORK_MAC, self._device.mac)},
}
if self._device.name:
self._attr_device_info["default_name"] = self._device.name
self._attr_extra_state_attributes = {} self._attr_extra_state_attributes = {}
if self._device.last_activity: if self._device.last_activity:
self._attr_extra_state_attributes[ self._attr_extra_state_attributes[

View file

@ -4,22 +4,20 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from numbers import Number from numbers import Number
from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_GIGABYTES, DATA_RATE_MEGABITS_PER_SECOND from homeassistant.const import DATA_GIGABYTES, DATA_RATE_MEGABITS_PER_SECOND
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import ( from homeassistant.helpers.update_coordinator import (
CoordinatorEntity, CoordinatorEntity,
DataUpdateCoordinator, DataUpdateCoordinator,
) )
from homeassistant.util import dt as dt_util
from .const import ( from .const import (
DATA_ASUSWRT, DATA_ASUSWRT,
@ -48,12 +46,14 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_CONNECTED_DEVICE[0], key=SENSORS_CONNECTED_DEVICE[0],
name="Devices Connected", name="Devices Connected",
icon="mdi:router-network", icon="mdi:router-network",
state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=UNIT_DEVICES, native_unit_of_measurement=UNIT_DEVICES,
), ),
AsusWrtSensorEntityDescription( AsusWrtSensorEntityDescription(
key=SENSORS_RATES[0], key=SENSORS_RATES[0],
name="Download Speed", name="Download Speed",
icon="mdi:download-network", icon="mdi:download-network",
state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=125000, factor=125000,
@ -62,6 +62,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_RATES[1], key=SENSORS_RATES[1],
name="Upload Speed", name="Upload Speed",
icon="mdi:upload-network", icon="mdi:upload-network",
state_class=STATE_CLASS_MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=125000, factor=125000,
@ -70,6 +71,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_BYTES[0], key=SENSORS_BYTES[0],
name="Download", name="Download",
icon="mdi:download", icon="mdi:download",
state_class=STATE_CLASS_TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=DATA_GIGABYTES,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1000000000, factor=1000000000,
@ -78,6 +80,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_BYTES[1], key=SENSORS_BYTES[1],
name="Upload", name="Upload",
icon="mdi:upload", icon="mdi:upload",
state_class=STATE_CLASS_TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=DATA_GIGABYTES,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1000000000, factor=1000000000,
@ -86,6 +89,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_LOAD_AVG[0], key=SENSORS_LOAD_AVG[0],
name="Load Avg (1m)", name="Load Avg (1m)",
icon="mdi:cpu-32-bit", icon="mdi:cpu-32-bit",
state_class=STATE_CLASS_MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1, factor=1,
precision=1, precision=1,
@ -94,6 +98,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_LOAD_AVG[1], key=SENSORS_LOAD_AVG[1],
name="Load Avg (5m)", name="Load Avg (5m)",
icon="mdi:cpu-32-bit", icon="mdi:cpu-32-bit",
state_class=STATE_CLASS_MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1, factor=1,
precision=1, precision=1,
@ -102,6 +107,7 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_LOAD_AVG[2], key=SENSORS_LOAD_AVG[2],
name="Load Avg (15m)", name="Load Avg (15m)",
icon="mdi:cpu-32-bit", icon="mdi:cpu-32-bit",
state_class=STATE_CLASS_MEASUREMENT,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1, factor=1,
precision=1, precision=1,
@ -143,33 +149,19 @@ class AsusWrtSensor(CoordinatorEntity, SensorEntity):
) -> None: ) -> None:
"""Initialize a AsusWrt sensor.""" """Initialize a AsusWrt sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._router = router
self.entity_description = description self.entity_description = description
self._attr_name = f"{DEFAULT_PREFIX} {description.name}" self._attr_name = f"{DEFAULT_PREFIX} {description.name}"
self._attr_unique_id = f"{DOMAIN} {self.name}" self._attr_unique_id = f"{DOMAIN} {self.name}"
self._attr_state_class = STATE_CLASS_MEASUREMENT self._attr_device_info = router.device_info
self._attr_extra_state_attributes = {"hostname": router.host}
if description.native_unit_of_measurement == DATA_GIGABYTES:
self._attr_last_reset = dt_util.utc_from_timestamp(0)
@property @property
def native_value(self) -> str: def native_value(self) -> str | None:
"""Return current state.""" """Return current state."""
descr = self.entity_description descr = self.entity_description
state = self.coordinator.data.get(descr.key) state = self.coordinator.data.get(descr.key)
if state is None: if state is not None:
return None
if descr.factor and isinstance(state, Number): if descr.factor and isinstance(state, Number):
return round(state / descr.factor, descr.precision) return round(state / descr.factor, descr.precision)
return state return state
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the attributes."""
return {"hostname": self._router.host}
@property
def device_info(self) -> DeviceInfo:
"""Return the device information."""
return self._router.device_info