mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 13:57:10 +00:00
Use SensorEntityDescription for AsusWRT sensors (#54111)
This commit is contained in:
parent
acc0288f4c
commit
099a1de92b
@ -1,11 +1,16 @@
|
|||||||
"""Asuswrt status sensors."""
|
"""Asuswrt status sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
from numbers import Number
|
from numbers import Number
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
STATE_CLASS_MEASUREMENT,
|
||||||
|
SensorEntity,
|
||||||
|
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
|
||||||
@ -14,6 +19,7 @@ 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,
|
||||||
@ -25,62 +31,83 @@ from .const import (
|
|||||||
)
|
)
|
||||||
from .router import KEY_COORDINATOR, KEY_SENSORS, AsusWrtRouter
|
from .router import KEY_COORDINATOR, KEY_SENSORS, AsusWrtRouter
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class AsusWrtSensorEntityDescription(SensorEntityDescription):
|
||||||
|
"""A class that describes AsusWrt sensor entities."""
|
||||||
|
|
||||||
|
factor: int | None = None
|
||||||
|
precision: int = 2
|
||||||
|
|
||||||
|
|
||||||
DEFAULT_PREFIX = "Asuswrt"
|
DEFAULT_PREFIX = "Asuswrt"
|
||||||
|
|
||||||
SENSOR_DEVICE_CLASS = "device_class"
|
|
||||||
SENSOR_ICON = "icon"
|
|
||||||
SENSOR_NAME = "name"
|
|
||||||
SENSOR_UNIT = "unit"
|
|
||||||
SENSOR_FACTOR = "factor"
|
|
||||||
SENSOR_DEFAULT_ENABLED = "default_enabled"
|
|
||||||
|
|
||||||
UNIT_DEVICES = "Devices"
|
UNIT_DEVICES = "Devices"
|
||||||
|
|
||||||
CONNECTION_SENSORS = {
|
CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
|
||||||
SENSORS_CONNECTED_DEVICE[0]: {
|
AsusWrtSensorEntityDescription(
|
||||||
SENSOR_NAME: "Devices Connected",
|
key=SENSORS_CONNECTED_DEVICE[0],
|
||||||
SENSOR_UNIT: UNIT_DEVICES,
|
name="Devices Connected",
|
||||||
SENSOR_FACTOR: 0,
|
icon="mdi:router-network",
|
||||||
SENSOR_ICON: "mdi:router-network",
|
unit_of_measurement=UNIT_DEVICES,
|
||||||
SENSOR_DEFAULT_ENABLED: True,
|
entity_registry_enabled_default=True,
|
||||||
},
|
),
|
||||||
SENSORS_RATES[0]: {
|
AsusWrtSensorEntityDescription(
|
||||||
SENSOR_NAME: "Download Speed",
|
key=SENSORS_RATES[0],
|
||||||
SENSOR_UNIT: DATA_RATE_MEGABITS_PER_SECOND,
|
name="Download Speed",
|
||||||
SENSOR_FACTOR: 125000,
|
icon="mdi:download-network",
|
||||||
SENSOR_ICON: "mdi:download-network",
|
unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
|
||||||
},
|
entity_registry_enabled_default=False,
|
||||||
SENSORS_RATES[1]: {
|
factor=125000,
|
||||||
SENSOR_NAME: "Upload Speed",
|
),
|
||||||
SENSOR_UNIT: DATA_RATE_MEGABITS_PER_SECOND,
|
AsusWrtSensorEntityDescription(
|
||||||
SENSOR_FACTOR: 125000,
|
key=SENSORS_RATES[1],
|
||||||
SENSOR_ICON: "mdi:upload-network",
|
name="Upload Speed",
|
||||||
},
|
icon="mdi:upload-network",
|
||||||
SENSORS_BYTES[0]: {
|
unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
|
||||||
SENSOR_NAME: "Download",
|
entity_registry_enabled_default=False,
|
||||||
SENSOR_UNIT: DATA_GIGABYTES,
|
factor=125000,
|
||||||
SENSOR_FACTOR: 1000000000,
|
),
|
||||||
SENSOR_ICON: "mdi:download",
|
AsusWrtSensorEntityDescription(
|
||||||
},
|
key=SENSORS_BYTES[0],
|
||||||
SENSORS_BYTES[1]: {
|
name="Download",
|
||||||
SENSOR_NAME: "Upload",
|
icon="mdi:download",
|
||||||
SENSOR_UNIT: DATA_GIGABYTES,
|
unit_of_measurement=DATA_GIGABYTES,
|
||||||
SENSOR_FACTOR: 1000000000,
|
entity_registry_enabled_default=False,
|
||||||
SENSOR_ICON: "mdi:upload",
|
factor=1000000000,
|
||||||
},
|
),
|
||||||
SENSORS_LOAD_AVG[0]: {
|
AsusWrtSensorEntityDescription(
|
||||||
SENSOR_NAME: "Load Avg (1m)",
|
key=SENSORS_BYTES[1],
|
||||||
SENSOR_ICON: "mdi:cpu-32-bit",
|
name="Upload",
|
||||||
},
|
icon="mdi:upload",
|
||||||
SENSORS_LOAD_AVG[1]: {
|
unit_of_measurement=DATA_GIGABYTES,
|
||||||
SENSOR_NAME: "Load Avg (5m)",
|
entity_registry_enabled_default=False,
|
||||||
SENSOR_ICON: "mdi:cpu-32-bit",
|
factor=1000000000,
|
||||||
},
|
),
|
||||||
SENSORS_LOAD_AVG[2]: {
|
AsusWrtSensorEntityDescription(
|
||||||
SENSOR_NAME: "Load Avg (15m)",
|
key=SENSORS_LOAD_AVG[0],
|
||||||
SENSOR_ICON: "mdi:cpu-32-bit",
|
name="Load Avg (1m)",
|
||||||
},
|
icon="mdi:cpu-32-bit",
|
||||||
}
|
entity_registry_enabled_default=False,
|
||||||
|
factor=1,
|
||||||
|
precision=1,
|
||||||
|
),
|
||||||
|
AsusWrtSensorEntityDescription(
|
||||||
|
key=SENSORS_LOAD_AVG[1],
|
||||||
|
name="Load Avg (5m)",
|
||||||
|
icon="mdi:cpu-32-bit",
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
factor=1,
|
||||||
|
precision=1,
|
||||||
|
),
|
||||||
|
AsusWrtSensorEntityDescription(
|
||||||
|
key=SENSORS_LOAD_AVG[2],
|
||||||
|
name="Load Avg (15m)",
|
||||||
|
icon="mdi:cpu-32-bit",
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
factor=1,
|
||||||
|
precision=1,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -95,12 +122,12 @@ async def async_setup_entry(
|
|||||||
for sensor_data in router.sensors_coordinator.values():
|
for sensor_data in router.sensors_coordinator.values():
|
||||||
coordinator = sensor_data[KEY_COORDINATOR]
|
coordinator = sensor_data[KEY_COORDINATOR]
|
||||||
sensors = sensor_data[KEY_SENSORS]
|
sensors = sensor_data[KEY_SENSORS]
|
||||||
for sensor_key in sensors:
|
entities.extend(
|
||||||
if sensor_key in CONNECTION_SENSORS:
|
[
|
||||||
entities.append(
|
AsusWrtSensor(coordinator, router, sensor_descr)
|
||||||
AsusWrtSensor(
|
for sensor_descr in CONNECTION_SENSORS
|
||||||
coordinator, router, sensor_key, CONNECTION_SENSORS[sensor_key]
|
if sensor_descr.key in sensors
|
||||||
)
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
@ -113,31 +140,29 @@ class AsusWrtSensor(CoordinatorEntity, SensorEntity):
|
|||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
router: AsusWrtRouter,
|
router: AsusWrtRouter,
|
||||||
sensor_type: str,
|
description: AsusWrtSensorEntityDescription,
|
||||||
sensor_def: dict[str, Any],
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a AsusWrt sensor."""
|
"""Initialize a AsusWrt sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._router = router
|
self._router = router
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._attr_name = f"{DEFAULT_PREFIX} {sensor_def[SENSOR_NAME]}"
|
|
||||||
self._factor = sensor_def.get(SENSOR_FACTOR)
|
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_entity_registry_enabled_default = sensor_def.get(
|
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
SENSOR_DEFAULT_ENABLED, False
|
|
||||||
)
|
if description.unit_of_measurement == DATA_GIGABYTES:
|
||||||
self._attr_unit_of_measurement = sensor_def.get(SENSOR_UNIT)
|
self._attr_last_reset = dt_util.utc_from_timestamp(0)
|
||||||
self._attr_icon = sensor_def.get(SENSOR_ICON)
|
|
||||||
self._attr_device_class = sensor_def.get(SENSOR_DEVICE_CLASS)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> str:
|
||||||
"""Return current state."""
|
"""Return current state."""
|
||||||
state = self.coordinator.data.get(self._sensor_type)
|
descr = self.entity_description
|
||||||
|
state = self.coordinator.data.get(descr.key)
|
||||||
if state is None:
|
if state is None:
|
||||||
return None
|
return None
|
||||||
if self._factor and isinstance(state, Number):
|
if descr.factor and isinstance(state, Number):
|
||||||
return round(state / self._factor, 2)
|
return round(state / descr.factor, descr.precision)
|
||||||
return state
|
return state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
x
Reference in New Issue
Block a user