mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use EntityDescription - vilfo (#55746)
This commit is contained in:
parent
0dd128af77
commit
99ef2ae54d
@ -1,19 +1,16 @@
|
|||||||
"""Constants for the Vilfo Router integration."""
|
"""Constants for the Vilfo Router integration."""
|
||||||
from homeassistant.const import (
|
from __future__ import annotations
|
||||||
ATTR_DEVICE_CLASS,
|
|
||||||
ATTR_ICON,
|
from dataclasses import dataclass
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
|
||||||
PERCENTAGE,
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
)
|
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, PERCENTAGE
|
||||||
|
|
||||||
DOMAIN = "vilfo"
|
DOMAIN = "vilfo"
|
||||||
|
|
||||||
ATTR_API_DATA_FIELD = "api_data_field"
|
|
||||||
ATTR_API_DATA_FIELD_LOAD = "load"
|
ATTR_API_DATA_FIELD_LOAD = "load"
|
||||||
ATTR_API_DATA_FIELD_BOOT_TIME = "boot_time"
|
ATTR_API_DATA_FIELD_BOOT_TIME = "boot_time"
|
||||||
ATTR_LABEL = "label"
|
|
||||||
ATTR_LOAD = "load"
|
ATTR_LOAD = "load"
|
||||||
ATTR_UNIT = "unit"
|
|
||||||
ATTR_BOOT_TIME = "boot_time"
|
ATTR_BOOT_TIME = "boot_time"
|
||||||
|
|
||||||
ROUTER_DEFAULT_HOST = "admin.vilfo.com"
|
ROUTER_DEFAULT_HOST = "admin.vilfo.com"
|
||||||
@ -21,17 +18,32 @@ ROUTER_DEFAULT_MODEL = "Vilfo Router"
|
|||||||
ROUTER_DEFAULT_NAME = "Vilfo Router"
|
ROUTER_DEFAULT_NAME = "Vilfo Router"
|
||||||
ROUTER_MANUFACTURER = "Vilfo AB"
|
ROUTER_MANUFACTURER = "Vilfo AB"
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
|
||||||
ATTR_LOAD: {
|
@dataclass
|
||||||
ATTR_LABEL: "Load",
|
class VilfoRequiredKeysMixin:
|
||||||
ATTR_UNIT: PERCENTAGE,
|
"""Mixin for required keys."""
|
||||||
ATTR_ICON: "mdi:memory",
|
|
||||||
ATTR_API_DATA_FIELD: ATTR_API_DATA_FIELD_LOAD,
|
api_key: str
|
||||||
},
|
|
||||||
ATTR_BOOT_TIME: {
|
|
||||||
ATTR_LABEL: "Boot time",
|
@dataclass
|
||||||
ATTR_ICON: "mdi:timer-outline",
|
class VilfoSensorEntityDescription(SensorEntityDescription, VilfoRequiredKeysMixin):
|
||||||
ATTR_API_DATA_FIELD: ATTR_API_DATA_FIELD_BOOT_TIME,
|
"""Describes Vilfo sensor entity."""
|
||||||
ATTR_DEVICE_CLASS: DEVICE_CLASS_TIMESTAMP,
|
|
||||||
},
|
|
||||||
}
|
SENSOR_TYPES: tuple[VilfoSensorEntityDescription, ...] = (
|
||||||
|
VilfoSensorEntityDescription(
|
||||||
|
key=ATTR_LOAD,
|
||||||
|
name="Load",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
icon="mdi:memory",
|
||||||
|
api_key=ATTR_API_DATA_FIELD_LOAD,
|
||||||
|
),
|
||||||
|
VilfoSensorEntityDescription(
|
||||||
|
key=ATTR_BOOT_TIME,
|
||||||
|
name="Boot time",
|
||||||
|
icon="mdi:timer-outline",
|
||||||
|
api_key=ATTR_API_DATA_FIELD_BOOT_TIME,
|
||||||
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
"""Support for Vilfo Router sensors."""
|
"""Support for Vilfo Router sensors."""
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.const import ATTR_ICON
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_API_DATA_FIELD,
|
|
||||||
ATTR_DEVICE_CLASS,
|
|
||||||
ATTR_LABEL,
|
|
||||||
ATTR_UNIT,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
ROUTER_DEFAULT_MODEL,
|
ROUTER_DEFAULT_MODEL,
|
||||||
ROUTER_DEFAULT_NAME,
|
ROUTER_DEFAULT_NAME,
|
||||||
ROUTER_MANUFACTURER,
|
ROUTER_MANUFACTURER,
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
|
VilfoSensorEntityDescription,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -19,21 +15,20 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"""Add Vilfo Router entities from a config_entry."""
|
"""Add Vilfo Router entities from a config_entry."""
|
||||||
vilfo = hass.data[DOMAIN][config_entry.entry_id]
|
vilfo = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
|
||||||
sensors = []
|
entities = [VilfoRouterSensor(vilfo, description) for description in SENSOR_TYPES]
|
||||||
|
|
||||||
for sensor_type in SENSOR_TYPES:
|
async_add_entities(entities, True)
|
||||||
sensors.append(VilfoRouterSensor(sensor_type, vilfo))
|
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
|
||||||
|
|
||||||
|
|
||||||
class VilfoRouterSensor(SensorEntity):
|
class VilfoRouterSensor(SensorEntity):
|
||||||
"""Define a Vilfo Router Sensor."""
|
"""Define a Vilfo Router Sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, api):
|
entity_description: VilfoSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(self, api, description: VilfoSensorEntityDescription):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
|
self.entity_description = description
|
||||||
self.api = api
|
self.api = api
|
||||||
self.sensor_type = sensor_type
|
|
||||||
self._device_info = {
|
self._device_info = {
|
||||||
"identifiers": {(DOMAIN, api.host, api.mac_address)},
|
"identifiers": {(DOMAIN, api.host, api.mac_address)},
|
||||||
"name": ROUTER_DEFAULT_NAME,
|
"name": ROUTER_DEFAULT_NAME,
|
||||||
@ -41,8 +36,7 @@ class VilfoRouterSensor(SensorEntity):
|
|||||||
"model": ROUTER_DEFAULT_MODEL,
|
"model": ROUTER_DEFAULT_MODEL,
|
||||||
"sw_version": api.firmware_version,
|
"sw_version": api.firmware_version,
|
||||||
}
|
}
|
||||||
self._unique_id = f"{self.api.unique_id}_{self.sensor_type}"
|
self._attr_unique_id = f"{api.unique_id}_{description.key}"
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
@ -54,41 +48,13 @@ class VilfoRouterSensor(SensorEntity):
|
|||||||
"""Return the device info."""
|
"""Return the device info."""
|
||||||
return self._device_info
|
return self._device_info
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class."""
|
|
||||||
return SENSOR_TYPES[self.sensor_type].get(ATTR_DEVICE_CLASS)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon for the sensor."""
|
|
||||||
return SENSOR_TYPES[self.sensor_type][ATTR_ICON]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
parent_device_name = self._device_info["name"]
|
parent_device_name = self._device_info["name"]
|
||||||
sensor_name = SENSOR_TYPES[self.sensor_type][ATTR_LABEL]
|
return f"{parent_device_name} {self.entity_description.name}"
|
||||||
return f"{parent_device_name} {sensor_name}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique_id for this entity."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity."""
|
|
||||||
return SENSOR_TYPES[self.sensor_type].get(ATTR_UNIT)
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update the router data."""
|
"""Update the router data."""
|
||||||
await self.api.async_update()
|
await self.api.async_update()
|
||||||
self._state = self.api.data.get(
|
self._attr_native_value = self.api.data.get(self.entity_description.api_key)
|
||||||
SENSOR_TYPES[self.sensor_type][ATTR_API_DATA_FIELD]
|
|
||||||
)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user