Use EntityDescription - vilfo (#55746)

This commit is contained in:
Marc Mueller 2021-09-06 09:33:58 +02:00 committed by GitHub
parent 0dd128af77
commit 99ef2ae54d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 67 deletions

View File

@ -1,19 +1,16 @@
"""Constants for the Vilfo Router integration."""
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_ICON,
DEVICE_CLASS_TIMESTAMP,
PERCENTAGE,
)
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.const import DEVICE_CLASS_TIMESTAMP, PERCENTAGE
DOMAIN = "vilfo"
ATTR_API_DATA_FIELD = "api_data_field"
ATTR_API_DATA_FIELD_LOAD = "load"
ATTR_API_DATA_FIELD_BOOT_TIME = "boot_time"
ATTR_LABEL = "label"
ATTR_LOAD = "load"
ATTR_UNIT = "unit"
ATTR_BOOT_TIME = "boot_time"
ROUTER_DEFAULT_HOST = "admin.vilfo.com"
@ -21,17 +18,32 @@ ROUTER_DEFAULT_MODEL = "Vilfo Router"
ROUTER_DEFAULT_NAME = "Vilfo Router"
ROUTER_MANUFACTURER = "Vilfo AB"
SENSOR_TYPES = {
ATTR_LOAD: {
ATTR_LABEL: "Load",
ATTR_UNIT: PERCENTAGE,
ATTR_ICON: "mdi:memory",
ATTR_API_DATA_FIELD: ATTR_API_DATA_FIELD_LOAD,
},
ATTR_BOOT_TIME: {
ATTR_LABEL: "Boot time",
ATTR_ICON: "mdi:timer-outline",
ATTR_API_DATA_FIELD: ATTR_API_DATA_FIELD_BOOT_TIME,
ATTR_DEVICE_CLASS: DEVICE_CLASS_TIMESTAMP,
},
}
@dataclass
class VilfoRequiredKeysMixin:
"""Mixin for required keys."""
api_key: str
@dataclass
class VilfoSensorEntityDescription(SensorEntityDescription, VilfoRequiredKeysMixin):
"""Describes Vilfo sensor entity."""
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,
),
)

View File

@ -1,17 +1,13 @@
"""Support for Vilfo Router sensors."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import ATTR_ICON
from .const import (
ATTR_API_DATA_FIELD,
ATTR_DEVICE_CLASS,
ATTR_LABEL,
ATTR_UNIT,
DOMAIN,
ROUTER_DEFAULT_MODEL,
ROUTER_DEFAULT_NAME,
ROUTER_MANUFACTURER,
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."""
vilfo = hass.data[DOMAIN][config_entry.entry_id]
sensors = []
entities = [VilfoRouterSensor(vilfo, description) for description in SENSOR_TYPES]
for sensor_type in SENSOR_TYPES:
sensors.append(VilfoRouterSensor(sensor_type, vilfo))
async_add_entities(sensors, True)
async_add_entities(entities, True)
class VilfoRouterSensor(SensorEntity):
"""Define a Vilfo Router Sensor."""
def __init__(self, sensor_type, api):
entity_description: VilfoSensorEntityDescription
def __init__(self, api, description: VilfoSensorEntityDescription):
"""Initialize."""
self.entity_description = description
self.api = api
self.sensor_type = sensor_type
self._device_info = {
"identifiers": {(DOMAIN, api.host, api.mac_address)},
"name": ROUTER_DEFAULT_NAME,
@ -41,8 +36,7 @@ class VilfoRouterSensor(SensorEntity):
"model": ROUTER_DEFAULT_MODEL,
"sw_version": api.firmware_version,
}
self._unique_id = f"{self.api.unique_id}_{self.sensor_type}"
self._state = None
self._attr_unique_id = f"{api.unique_id}_{description.key}"
@property
def available(self):
@ -54,41 +48,13 @@ class VilfoRouterSensor(SensorEntity):
"""Return the 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
def name(self):
"""Return the name of the sensor."""
parent_device_name = self._device_info["name"]
sensor_name = SENSOR_TYPES[self.sensor_type][ATTR_LABEL]
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)
return f"{parent_device_name} {self.entity_description.name}"
async def async_update(self):
"""Update the router data."""
await self.api.async_update()
self._state = self.api.data.get(
SENSOR_TYPES[self.sensor_type][ATTR_API_DATA_FIELD]
)
self._attr_native_value = self.api.data.get(self.entity_description.api_key)