Use DataRate unit and device class in pyload (#83611)

This commit is contained in:
epenet 2022-12-09 13:32:31 +01:00 committed by GitHub
parent 4192e2377c
commit 549d1151b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,7 +7,12 @@ import logging
import requests import requests
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
CONF_MONITORED_VARIABLES, CONF_MONITORED_VARIABLES,
@ -17,7 +22,7 @@ from homeassistant.const import (
CONF_SSL, CONF_SSL,
CONF_USERNAME, CONF_USERNAME,
CONTENT_TYPE_JSON, CONTENT_TYPE_JSON,
DATA_RATE_MEGABYTES_PER_SECOND, UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -33,7 +38,14 @@ DEFAULT_PORT = 8000
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=15) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=15)
SENSOR_TYPES = {"speed": ["speed", "Speed", DATA_RATE_MEGABYTES_PER_SECOND]} SENSOR_TYPES = {
"speed": SensorEntityDescription(
key="speed",
name="Speed",
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
)
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
@ -78,7 +90,7 @@ def setup_platform(
devices = [] devices = []
for ng_type in monitored_types: for ng_type in monitored_types:
new_sensor = PyLoadSensor( new_sensor = PyLoadSensor(
api=pyloadapi, sensor_type=SENSOR_TYPES.get(ng_type), client_name=name api=pyloadapi, sensor_type=SENSOR_TYPES[ng_type], client_name=name
) )
devices.append(new_sensor) devices.append(new_sensor)
@ -88,28 +100,14 @@ def setup_platform(
class PyLoadSensor(SensorEntity): class PyLoadSensor(SensorEntity):
"""Representation of a pyLoad sensor.""" """Representation of a pyLoad sensor."""
def __init__(self, api, sensor_type, client_name): def __init__(
self, api: PyLoadAPI, sensor_type: SensorEntityDescription, client_name
):
"""Initialize a new pyLoad sensor.""" """Initialize a new pyLoad sensor."""
self._name = f"{client_name} {sensor_type[1]}" self._attr_name = f"{client_name} {sensor_type.name}"
self.type = sensor_type[0] self.type = sensor_type.key
self.api = api self.api = api
self._state = None self.entity_description = sensor_type
self._unit_of_measurement = sensor_type[2]
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
def update(self) -> None: def update(self) -> None:
"""Update state of sensor.""" """Update state of sensor."""
@ -121,7 +119,7 @@ class PyLoadSensor(SensorEntity):
if self.api.status is None: if self.api.status is None:
_LOGGER.debug( _LOGGER.debug(
"Update of %s requested, but no status is available", self._name "Update of %s requested, but no status is available", self.name
) )
return return
@ -131,9 +129,9 @@ class PyLoadSensor(SensorEntity):
if "speed" in self.type and value > 0: if "speed" in self.type and value > 0:
# Convert download rate from Bytes/s to MBytes/s # Convert download rate from Bytes/s to MBytes/s
self._state = round(value / 2**20, 2) self._attr_native_value = round(value / 2**20, 2)
else: else:
self._state = value self._attr_native_value = value
class PyLoadAPI: class PyLoadAPI: