Fix AsusWRT scanner entity DeviceInfo (#54648)

pull/54719/head
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._attr_unique_id = device.mac
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
def is_connected(self):
@ -90,11 +96,6 @@ class AsusWrtDevice(ScannerEntity):
def async_on_demand_update(self):
"""Update state."""
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 = {}
if self._device.last_activity:
self._attr_extra_state_attributes[

View File

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