File size: 6,651 Bytes
7a71a69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | """Asuswrt status sensors."""
from __future__ import annotations
from dataclasses import dataclass
from numbers import Real
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
DATA_GIGABYTES,
DATA_RATE_MEGABITS_PER_SECOND,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
from .const import (
DATA_ASUSWRT,
DOMAIN,
SENSORS_BYTES,
SENSORS_CONNECTED_DEVICE,
SENSORS_LOAD_AVG,
SENSORS_RATES,
SENSORS_TEMPERATURES,
)
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"
UNIT_DEVICES = "Devices"
CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
AsusWrtSensorEntityDescription(
key=SENSORS_CONNECTED_DEVICE[0],
name="Devices Connected",
icon="mdi:router-network",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UNIT_DEVICES,
),
AsusWrtSensorEntityDescription(
key=SENSORS_RATES[0],
name="Download Speed",
icon="mdi:download-network",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
entity_registry_enabled_default=False,
factor=125000,
),
AsusWrtSensorEntityDescription(
key=SENSORS_RATES[1],
name="Upload Speed",
icon="mdi:upload-network",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND,
entity_registry_enabled_default=False,
factor=125000,
),
AsusWrtSensorEntityDescription(
key=SENSORS_BYTES[0],
name="Download",
icon="mdi:download",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES,
entity_registry_enabled_default=False,
factor=1000000000,
),
AsusWrtSensorEntityDescription(
key=SENSORS_BYTES[1],
name="Upload",
icon="mdi:upload",
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES,
entity_registry_enabled_default=False,
factor=1000000000,
),
AsusWrtSensorEntityDescription(
key=SENSORS_LOAD_AVG[0],
name="Load Avg (1m)",
icon="mdi:cpu-32-bit",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
AsusWrtSensorEntityDescription(
key=SENSORS_LOAD_AVG[1],
name="Load Avg (5m)",
icon="mdi:cpu-32-bit",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
AsusWrtSensorEntityDescription(
key=SENSORS_LOAD_AVG[2],
name="Load Avg (15m)",
icon="mdi:cpu-32-bit",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
AsusWrtSensorEntityDescription(
key=SENSORS_TEMPERATURES[0],
name="2.4GHz Temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
AsusWrtSensorEntityDescription(
key=SENSORS_TEMPERATURES[1],
name="5GHz Temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
AsusWrtSensorEntityDescription(
key=SENSORS_TEMPERATURES[2],
name="CPU Temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
factor=1,
precision=1,
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the sensors."""
router: AsusWrtRouter = hass.data[DOMAIN][entry.entry_id][DATA_ASUSWRT]
entities = []
for sensor_data in router.sensors_coordinator.values():
coordinator = sensor_data[KEY_COORDINATOR]
sensors = sensor_data[KEY_SENSORS]
entities.extend(
[
AsusWrtSensor(coordinator, router, sensor_descr)
for sensor_descr in CONNECTION_SENSORS
if sensor_descr.key in sensors
]
)
async_add_entities(entities, True)
class AsusWrtSensor(CoordinatorEntity, SensorEntity):
"""Representation of a AsusWrt sensor."""
def __init__(
self,
coordinator: DataUpdateCoordinator,
router: AsusWrtRouter,
description: AsusWrtSensorEntityDescription,
) -> None:
"""Initialize a AsusWrt sensor."""
super().__init__(coordinator)
self.entity_description: AsusWrtSensorEntityDescription = description
self._attr_name = f"{DEFAULT_PREFIX} {description.name}"
self._attr_unique_id = f"{DOMAIN} {self.name}"
self._attr_device_info = router.device_info
self._attr_extra_state_attributes = {"hostname": router.host}
@property
def native_value(self) -> float | str | None:
"""Return current state."""
descr = self.entity_description
state = self.coordinator.data.get(descr.key)
if state is not None and descr.factor and isinstance(state, Real):
return round(state / descr.factor, descr.precision)
return state
|