Use EntityDescription - dovado (#54945)

This commit is contained in:
Marc Mueller 2021-08-23 22:41:02 +02:00 committed by GitHub
parent a23f27a7a8
commit 1152330865
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,17 @@
"""Support for sensors from the Dovado router.""" """Support for sensors from the Dovado router."""
from __future__ import annotations
from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
import re import re
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import CONF_SENSORS, DATA_GIGABYTES, PERCENTAGE from homeassistant.const import CONF_SENSORS, DATA_GIGABYTES, PERCENTAGE
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -18,26 +25,59 @@ SENSOR_SIGNAL = "signal"
SENSOR_NETWORK = "network" SENSOR_NETWORK = "network"
SENSOR_SMS_UNREAD = "sms" SENSOR_SMS_UNREAD = "sms"
SENSORS = {
SENSOR_NETWORK: ("signal strength", "Network", None, "mdi:access-point-network"), @dataclass
SENSOR_SIGNAL: ( class DovadoRequiredKeysMixin:
"signal strength", """Mixin for required keys."""
"Signal Strength",
PERCENTAGE, identifier: str
"mdi:signal",
@dataclass
class DovadoSensorEntityDescription(SensorEntityDescription, DovadoRequiredKeysMixin):
"""Describes Dovado sensor entity."""
SENSOR_TYPES: tuple[DovadoSensorEntityDescription, ...] = (
DovadoSensorEntityDescription(
identifier=SENSOR_NETWORK,
key="signal strength",
name="Network",
icon="mdi:access-point-network",
), ),
SENSOR_SMS_UNREAD: ("sms unread", "SMS unread", "", "mdi:message-text-outline"), DovadoSensorEntityDescription(
SENSOR_UPLOAD: ("traffic modem tx", "Sent", DATA_GIGABYTES, "mdi:cloud-upload"), identifier=SENSOR_SIGNAL,
SENSOR_DOWNLOAD: ( key="signal strength",
"traffic modem rx", name="Signal Strength",
"Received", native_unit_of_measurement=PERCENTAGE,
DATA_GIGABYTES, icon="mdi:signal",
"mdi:cloud-download",
), ),
} DovadoSensorEntityDescription(
identifier=SENSOR_SMS_UNREAD,
key="sms unread",
name="SMS unread",
icon="mdi:message-text-outline",
),
DovadoSensorEntityDescription(
identifier=SENSOR_UPLOAD,
key="traffic modem tx",
name="Sent",
native_unit_of_measurement=DATA_GIGABYTES,
icon="mdi:cloud-upload",
),
DovadoSensorEntityDescription(
identifier=SENSOR_DOWNLOAD,
key="traffic modem rx",
name="Received",
native_unit_of_measurement=DATA_GIGABYTES,
icon="mdi:cloud-download",
),
)
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_SENSORS): vol.All(cv.ensure_list, [vol.In(SENSORS)])} {vol.Required(CONF_SENSORS): vol.All(cv.ensure_list, [vol.In(SENSOR_KEYS)])}
) )
@ -45,63 +85,50 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Dovado sensor platform.""" """Set up the Dovado sensor platform."""
dovado = hass.data[DOVADO_DOMAIN] dovado = hass.data[DOVADO_DOMAIN]
entities = [] sensors = config[CONF_SENSORS]
for sensor in config[CONF_SENSORS]: entities = [
entities.append(DovadoSensor(dovado, sensor)) DovadoSensor(dovado, description)
for description in SENSOR_TYPES
if description.key in sensors
]
add_entities(entities) add_entities(entities)
class DovadoSensor(SensorEntity): class DovadoSensor(SensorEntity):
"""Representation of a Dovado sensor.""" """Representation of a Dovado sensor."""
def __init__(self, data, sensor): entity_description: DovadoSensorEntityDescription
def __init__(self, data, description: DovadoSensorEntityDescription):
"""Initialize the sensor.""" """Initialize the sensor."""
self.entity_description = description
self._data = data self._data = data
self._sensor = sensor
self._state = self._compute_state() self._attr_name = f"{data.name} {description.name}"
self._attr_native_value = self._compute_state()
def _compute_state(self): def _compute_state(self):
"""Compute the state of the sensor.""" """Compute the state of the sensor."""
state = self._data.state.get(SENSORS[self._sensor][0]) state = self._data.state.get(self.entity_description.key)
if self._sensor == SENSOR_NETWORK: sensor_identifier = self.entity_description.identifier
if sensor_identifier == SENSOR_NETWORK:
match = re.search(r"\((.+)\)", state) match = re.search(r"\((.+)\)", state)
return match.group(1) if match else None return match.group(1) if match else None
if self._sensor == SENSOR_SIGNAL: if sensor_identifier == SENSOR_SIGNAL:
try: try:
return int(state.split()[0]) return int(state.split()[0])
except ValueError: except ValueError:
return None return None
if self._sensor == SENSOR_SMS_UNREAD: if sensor_identifier == SENSOR_SMS_UNREAD:
return int(state) return int(state)
if self._sensor in [SENSOR_UPLOAD, SENSOR_DOWNLOAD]: if sensor_identifier in [SENSOR_UPLOAD, SENSOR_DOWNLOAD]:
return round(float(state) / 1e6, 1) return round(float(state) / 1e6, 1)
return state return state
def update(self): def update(self):
"""Update sensor values.""" """Update sensor values."""
self._data.update() self._data.update()
self._state = self._compute_state() self._attr_native_value = self._compute_state()
@property
def name(self):
"""Return the name of the sensor."""
return f"{self._data.name} {SENSORS[self._sensor][1]}"
@property
def native_value(self):
"""Return the sensor state."""
return self._state
@property
def icon(self):
"""Return the icon for the sensor."""
return SENSORS[self._sensor][3]
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return SENSORS[self._sensor][2]
@property @property
def extra_state_attributes(self): def extra_state_attributes(self):