Use DataRate unit and device class in integrations (#83610)

This commit is contained in:
epenet 2022-12-10 11:41:44 +01:00 committed by GitHub
parent 3970da0ad3
commit 535aba10ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 132 additions and 111 deletions

View file

@ -10,11 +10,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import DATA_GIGABYTES, TEMP_CELSIUS, UnitOfDataRate
DATA_GIGABYTES,
DATA_RATE_MEGABITS_PER_SECOND,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -57,8 +53,9 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_RATES[0], key=SENSORS_RATES[0],
name="Download Speed", name="Download Speed",
icon="mdi:download-network", icon="mdi:download-network",
device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=125000, factor=125000,
), ),
@ -66,8 +63,9 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
key=SENSORS_RATES[1], key=SENSORS_RATES[1],
name="Upload Speed", name="Upload Speed",
icon="mdi:upload-network", icon="mdi:upload-network",
device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=125000, factor=125000,
), ),

View file

@ -15,11 +15,7 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.const import ( from homeassistant.const import CONF_MONITORED_VARIABLES, CONF_NAME, UnitOfDataRate
CONF_MONITORED_VARIABLES,
CONF_NAME,
DATA_RATE_MEGABITS_PER_SECOND,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -39,26 +35,30 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="down_max_bandwidth", key="down_max_bandwidth",
name="Maximum Download Bandwidth", name="Maximum Download Bandwidth",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="up_max_bandwidth", key="up_max_bandwidth",
name="Maximum Upload Bandwidth", name="Maximum Upload Bandwidth",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="current_down_bandwidth", key="current_down_bandwidth",
name="Currently Used Download Bandwidth", name="Currently Used Download Bandwidth",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="current_up_bandwidth", key="current_up_bandwidth",
name="Currently Used Upload Bandwidth", name="Currently Used Upload Bandwidth",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
icon="mdi:upload", icon="mdi:upload",
), ),

View file

@ -6,12 +6,13 @@ from dataclasses import dataclass
from typing import Any from typing import Any
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_RATE_KILOBYTES_PER_SECOND, STATE_IDLE, Platform from homeassistant.const import STATE_IDLE, Platform, UnitOfDataRate
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_platform from homeassistant.helpers import entity_platform
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
@ -53,14 +54,16 @@ SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = (
DelugeSensorEntityDescription( DelugeSensorEntityDescription(
key=DOWNLOAD_SPEED, key=DOWNLOAD_SPEED,
name="Down speed", name="Down speed",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value=lambda data: get_state(data, DOWNLOAD_SPEED), value=lambda data: get_state(data, DOWNLOAD_SPEED),
), ),
DelugeSensorEntityDescription( DelugeSensorEntityDescription(
key=UPLOAD_SPEED, key=UPLOAD_SPEED,
name="Up speed", name="Up speed",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value=lambda data: get_state(data, UPLOAD_SPEED), value=lambda data: get_state(data, UPLOAD_SPEED),
), ),

View file

@ -3,8 +3,8 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.const import DATA_RATE_MEGABITS_PER_SECOND from homeassistant.const import UnitOfDataRate
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -13,8 +13,6 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import DATA_UPDATED, DOMAIN as FASTDOTCOM_DOMAIN from . import DATA_UPDATED, DOMAIN as FASTDOTCOM_DOMAIN
ICON = "mdi:speedometer"
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -30,8 +28,9 @@ class SpeedtestSensor(RestoreEntity, SensorEntity):
"""Implementation of a FAst.com sensor.""" """Implementation of a FAst.com sensor."""
_attr_name = "Fast.com Download" _attr_name = "Fast.com Download"
_attr_native_unit_of_measurement = DATA_RATE_MEGABITS_PER_SECOND _attr_device_class = SensorDeviceClass.DATA_RATE
_attr_icon = ICON _attr_native_unit_of_measurement = UnitOfDataRate.MEGABITS_PER_SECOND
_attr_icon = "mdi:speedometer"
_attr_should_poll = False _attr_should_poll = False
_attr_native_value = None _attr_native_value = None

View file

@ -17,12 +17,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import DATA_GIGABYTES, SIGNAL_STRENGTH_DECIBELS, UnitOfDataRate
DATA_GIGABYTES,
DATA_RATE_KILOBITS_PER_SECOND,
DATA_RATE_KILOBYTES_PER_SECOND,
SIGNAL_STRENGTH_DECIBELS,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -186,7 +181,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
key="kb_s_sent", key="kb_s_sent",
name="Upload Throughput", name="Upload Throughput",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
value_fn=_retrieve_kb_s_sent_state, value_fn=_retrieve_kb_s_sent_state,
), ),
@ -194,14 +190,16 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
key="kb_s_received", key="kb_s_received",
name="Download Throughput", name="Download Throughput",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
value_fn=_retrieve_kb_s_received_state, value_fn=_retrieve_kb_s_received_state,
), ),
FritzSensorEntityDescription( FritzSensorEntityDescription(
key="max_kb_s_sent", key="max_kb_s_sent",
name="Max Connection Upload Throughput", name="Max Connection Upload Throughput",
native_unit_of_measurement=DATA_RATE_KILOBITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=_retrieve_max_kb_s_sent_state, value_fn=_retrieve_max_kb_s_sent_state,
@ -209,7 +207,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
FritzSensorEntityDescription( FritzSensorEntityDescription(
key="max_kb_s_received", key="max_kb_s_received",
name="Max Connection Download Throughput", name="Max Connection Download Throughput",
native_unit_of_measurement=DATA_RATE_KILOBITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=_retrieve_max_kb_s_received_state, value_fn=_retrieve_max_kb_s_received_state,
@ -233,14 +232,16 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
FritzSensorEntityDescription( FritzSensorEntityDescription(
key="link_kb_s_sent", key="link_kb_s_sent",
name="Link Upload Throughput", name="Link Upload Throughput",
native_unit_of_measurement=DATA_RATE_KILOBITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
value_fn=_retrieve_link_kb_s_sent_state, value_fn=_retrieve_link_kb_s_sent_state,
), ),
FritzSensorEntityDescription( FritzSensorEntityDescription(
key="link_kb_s_received", key="link_kb_s_received",
name="Link Download Throughput", name="Link Download Throughput",
native_unit_of_measurement=DATA_RATE_KILOBITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
value_fn=_retrieve_link_kb_s_received_state, value_fn=_retrieve_link_kb_s_received_state,
), ),

View file

@ -17,11 +17,11 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DATA_BYTES, DATA_BYTES,
DATA_RATE_BYTES_PER_SECOND,
FREQUENCY_MEGAHERTZ, FREQUENCY_MEGAHERTZ,
PERCENTAGE, PERCENTAGE,
STATE_UNKNOWN, STATE_UNKNOWN,
TIME_SECONDS, TIME_SECONDS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity, EntityCategory from homeassistant.helpers.entity import Entity, EntityCategory
@ -371,7 +371,8 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
), ),
(KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentDownloadRate"): SensorMeta( (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentDownloadRate"): SensorMeta(
name="Current download rate", name="Current download rate",
native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -383,7 +384,8 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = {
), ),
(KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUploadRate"): SensorMeta( (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUploadRate"): SensorMeta(
name="Current upload rate", name="Current upload rate",
native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),

View file

@ -9,6 +9,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
DOMAIN as SENSOR_DOMAIN, DOMAIN as SENSOR_DOMAIN,
SensorDeviceClass,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.const import ( from homeassistant.const import (
@ -18,7 +19,7 @@ from homeassistant.const import (
CONF_PORT, CONF_PORT,
CONF_PROTOCOL, CONF_PROTOCOL,
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
DATA_RATE_MEGABITS_PER_SECOND, UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -51,12 +52,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key=ATTR_DOWNLOAD, key=ATTR_DOWNLOAD,
name=ATTR_DOWNLOAD.capitalize(), name=ATTR_DOWNLOAD.capitalize(),
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key=ATTR_UPLOAD, key=ATTR_UPLOAD,
name=ATTR_UPLOAD.capitalize(), name=ATTR_UPLOAD.capitalize(),
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
), ),
) )
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES] SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]

View file

@ -17,9 +17,9 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DATA_MEGABYTES, DATA_MEGABYTES,
DATA_RATE_MEGABITS_PER_SECOND,
PERCENTAGE, PERCENTAGE,
TIME_MILLISECONDS, TIME_MILLISECONDS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -228,14 +228,16 @@ SENSOR_SPEED_TYPES = [
key="NewOOKLAUplinkBandwidth", key="NewOOKLAUplinkBandwidth",
name="Uplink Bandwidth", name="Uplink Bandwidth",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(
key="NewOOKLADownlinkBandwidth", key="NewOOKLADownlinkBandwidth",
name="Downlink Bandwidth", name="Downlink Bandwidth",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(

View file

@ -10,11 +10,7 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import CONF_NAME, DATA_MEGABYTES, UnitOfDataRate
CONF_NAME,
DATA_MEGABYTES,
DATA_RATE_MEGABYTES_PER_SECOND,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
@ -34,7 +30,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="AverageDownloadRate", key="AverageDownloadRate",
name="Average Speed", name="Average Speed",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key="DownloadPaused", key="DownloadPaused",
@ -43,7 +40,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="DownloadRate", key="DownloadRate",
name="Speed", name="Speed",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key="DownloadedSizeMB", key="DownloadedSizeMB",
@ -77,7 +75,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="DownloadLimit", key="DownloadLimit",
name="Speed Limit", name="Speed Limit",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
), ),
) )

View file

@ -9,6 +9,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -17,8 +18,8 @@ from homeassistant.const import (
CONF_PASSWORD, CONF_PASSWORD,
CONF_URL, CONF_URL,
CONF_USERNAME, CONF_USERNAME,
DATA_RATE_KIBIBYTES_PER_SECOND,
STATE_IDLE, STATE_IDLE,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -42,12 +43,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key=SENSOR_TYPE_DOWNLOAD_SPEED, key=SENSOR_TYPE_DOWNLOAD_SPEED,
name="Down Speed", name="Down Speed",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key=SENSOR_TYPE_UPLOAD_SPEED, key=SENSOR_TYPE_UPLOAD_SPEED,
name="Up Speed", name="Up Speed",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
), ),
) )

View file

@ -24,9 +24,9 @@ from homeassistant.const import (
CONF_USERNAME, CONF_USERNAME,
CONF_VERIFY_SSL, CONF_VERIFY_SSL,
DATA_GIBIBYTES, DATA_GIBIBYTES,
DATA_RATE_MEBIBYTES_PER_SECOND,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -121,13 +121,15 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="network_tx", key="network_tx",
name="Network Up", name="Network Up",
native_unit_of_measurement=DATA_RATE_MEBIBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEBIBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="network_rx", key="network_rx",
name="Network Down", name="Network Down",
native_unit_of_measurement=DATA_RATE_MEBIBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEBIBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
), ),
) )

View file

@ -31,12 +31,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import DATA_BYTES, TEMP_CELSIUS, TIME_SECONDS, UnitOfDataRate
DATA_BYTES,
DATA_RATE_BYTES_PER_SECOND,
TEMP_CELSIUS,
TIME_SECONDS,
)
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -101,10 +96,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
), ),
QswSensorEntityDescription( QswSensorEntityDescription(
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download-network", icon="mdi:download-network",
key=QSD_PORTS_STATISTICS, key=QSD_PORTS_STATISTICS,
name="RX Speed", name="RX Speed",
native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_RX_SPEED, subkey=QSD_RX_SPEED,
), ),
@ -130,10 +126,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
), ),
QswSensorEntityDescription( QswSensorEntityDescription(
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload-network", icon="mdi:upload-network",
key=QSD_PORTS_STATISTICS, key=QSD_PORTS_STATISTICS,
name="TX Speed", name="TX Speed",
native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
subkey=QSD_TX_SPEED, subkey=QSD_TX_SPEED,
), ),

View file

@ -8,6 +8,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -15,8 +16,8 @@ from homeassistant.const import (
CONF_MONITORED_VARIABLES, CONF_MONITORED_VARIABLES,
CONF_NAME, CONF_NAME,
CONF_URL, CONF_URL,
DATA_RATE_KILOBYTES_PER_SECOND,
STATE_IDLE, STATE_IDLE,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -45,12 +46,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key=SENSOR_TYPE_DOWNLOAD_SPEED, key=SENSOR_TYPE_DOWNLOAD_SPEED,
name="Down Speed", name="Down Speed",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key=SENSOR_TYPE_UPLOAD_SPEED, key=SENSOR_TYPE_UPLOAD_SPEED,
name="Up Speed", name="Up Speed",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
), ),
SensorEntityDescription( SensorEntityDescription(
key=SENSOR_TYPE_ALL_TORRENTS, key=SENSOR_TYPE_ALL_TORRENTS,

View file

@ -4,16 +4,13 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import DATA_GIGABYTES, DATA_MEGABYTES, UnitOfDataRate
DATA_GIGABYTES,
DATA_MEGABYTES,
DATA_RATE_MEGABYTES_PER_SECOND,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -46,7 +43,8 @@ SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key=SPEED_KEY, key=SPEED_KEY,
name="Speed", name="Speed",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(

View file

@ -5,12 +5,12 @@ from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from typing import Final from typing import Final
from homeassistant.components.sensor import SensorEntityDescription, SensorStateClass from homeassistant.components.sensor import (
from homeassistant.const import ( SensorDeviceClass,
DATA_RATE_MEGABITS_PER_SECOND, SensorEntityDescription,
TIME_MILLISECONDS, SensorStateClass,
Platform,
) )
from homeassistant.const import TIME_MILLISECONDS, Platform, UnitOfDataRate
DOMAIN: Final = "speedtestdotnet" DOMAIN: Final = "speedtestdotnet"
@ -32,14 +32,16 @@ SENSOR_TYPES: Final[tuple[SpeedtestSensorEntityDescription, ...]] = (
SpeedtestSensorEntityDescription( SpeedtestSensorEntityDescription(
key="download", key="download",
name="Download", name="Download",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value=lambda value: round(value / 10**6, 2), value=lambda value: round(value / 10**6, 2),
), ),
SpeedtestSensorEntityDescription( SpeedtestSensorEntityDescription(
key="upload", key="upload",
name="Upload", name="Upload",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value=lambda value: round(value / 10**6, 2), value=lambda value: round(value / 10**6, 2),
), ),

View file

@ -19,10 +19,10 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONF_DISKS, CONF_DISKS,
DATA_MEGABYTES, DATA_MEGABYTES,
DATA_RATE_KILOBYTES_PER_SECOND,
DATA_TERABYTES, DATA_TERABYTES,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -166,7 +166,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="network_up", key="network_up",
name="Upload Throughput", name="Upload Throughput",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:upload", icon="mdi:upload",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -174,7 +175,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="network_down", key="network_down",
name="Download Throughput", name="Download Throughput",
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
device_class=SensorDeviceClass.DATA_RATE,
icon="mdi:download", icon="mdi:download",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -351,7 +353,7 @@ class SynoDSMUtilSensor(SynoDSMSensor):
return round(attr / 1024.0**2, 1) return round(attr / 1024.0**2, 1)
# Network # Network
if self.native_unit_of_measurement == DATA_RATE_KILOBYTES_PER_SECOND: if self.native_unit_of_measurement == UnitOfDataRate.KILOBYTES_PER_SECOND:
return round(attr / 1024.0, 1) return round(attr / 1024.0, 1)
# CPU load average # CPU load average

View file

@ -27,12 +27,12 @@ from homeassistant.const import (
CONF_TYPE, CONF_TYPE,
DATA_GIBIBYTES, DATA_GIBIBYTES,
DATA_MEBIBYTES, DATA_MEBIBYTES,
DATA_RATE_MEGABYTES_PER_SECOND,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
PERCENTAGE, PERCENTAGE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -183,16 +183,16 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"throughput_network_in": SysMonitorSensorEntityDescription( "throughput_network_in": SysMonitorSensorEntityDescription(
key="throughput_network_in", key="throughput_network_in",
name="Network throughput in", name="Network throughput in",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
icon="mdi:server-network", device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
mandatory_arg=True, mandatory_arg=True,
), ),
"throughput_network_out": SysMonitorSensorEntityDescription( "throughput_network_out": SysMonitorSensorEntityDescription(
key="throughput_network_out", key="throughput_network_out",
name="Network throughput out", name="Network throughput out",
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
icon="mdi:server-network", device_class=SensorDeviceClass.DATA_RATE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
mandatory_arg=True, mandatory_arg=True,
), ),

View file

@ -5,9 +5,9 @@ from contextlib import suppress
from transmissionrpc.torrent import Torrent from transmissionrpc.torrent import Torrent
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, DATA_RATE_MEGABYTES_PER_SECOND, STATE_IDLE from homeassistant.const import CONF_NAME, STATE_IDLE, UnitOfDataRate
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -97,10 +97,8 @@ class TransmissionSensor(SensorEntity):
class TransmissionSpeedSensor(TransmissionSensor): class TransmissionSpeedSensor(TransmissionSensor):
"""Representation of a Transmission speed sensor.""" """Representation of a Transmission speed sensor."""
@property _attr_device_class = SensorDeviceClass.DATA_RATE
def native_unit_of_measurement(self): _attr_native_unit_of_measurement = UnitOfDataRate.MEGABYTES_PER_SECOND
"""Return the unit of measurement of this entity, if any."""
return DATA_RATE_MEGABYTES_PER_SECOND
def update(self) -> None: def update(self) -> None:
"""Get the latest data from Transmission and updates the state.""" """Get the latest data from Transmission and updates the state."""

View file

@ -27,14 +27,13 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DATA_BYTES, DATA_BYTES,
DATA_RATE_BYTES_PER_SECOND,
DATA_RATE_MEGABITS_PER_SECOND,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT, SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
TEMP_CELSIUS, TEMP_CELSIUS,
TIME_SECONDS, TIME_SECONDS,
UnitOfDataRate,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
@ -140,7 +139,8 @@ ALL_DEVICES_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
ProtectSensorEntityDescription( ProtectSensorEntityDescription(
key="phy_rate", key="phy_rate",
name="Link Speed", name="Link Speed",
native_unit_of_measurement=DATA_RATE_MEGABITS_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -180,7 +180,8 @@ CAMERA_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
ProtectSensorEntityDescription( ProtectSensorEntityDescription(
key="write_rate", key="write_rate",
name="Disk Write Rate", name="Disk Write Rate",
native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
ufp_value="stats.storage.rate_per_second", ufp_value="stats.storage.rate_per_second",

View file

@ -4,12 +4,13 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_BYTES, DATA_RATE_KIBIBYTES_PER_SECOND, TIME_SECONDS from homeassistant.const import DATA_BYTES, TIME_SECONDS, UnitOfDataRate
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -103,9 +104,10 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
key=BYTES_RECEIVED, key=BYTES_RECEIVED,
value_key=KIBIBYTES_PER_SEC_RECEIVED, value_key=KIBIBYTES_PER_SEC_RECEIVED,
unique_id="KiB/sec_received", unique_id="KiB/sec_received",
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} received", name=f"{UnitOfDataRate.KIBIBYTES_PER_SECOND} received",
icon="mdi:server-network", icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
format=".1f", format=".1f",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -113,9 +115,10 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
key=BYTES_SENT, key=BYTES_SENT,
value_key=KIBIBYTES_PER_SEC_SENT, value_key=KIBIBYTES_PER_SEC_SENT,
unique_id="KiB/sec_sent", unique_id="KiB/sec_sent",
name=f"{DATA_RATE_KIBIBYTES_PER_SECOND} sent", name=f"{UnitOfDataRate.KIBIBYTES_PER_SECOND} sent",
icon="mdi:server-network", icon="mdi:server-network",
native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
format=".1f", format=".1f",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),

View file

@ -30,10 +30,15 @@ async def test_sensors(hass, nzbget_api) -> None:
"AverageDownloadRate", "AverageDownloadRate",
"1.19", "1.19",
DATA_RATE_MEGABYTES_PER_SECOND, DATA_RATE_MEGABYTES_PER_SECOND,
None, SensorDeviceClass.DATA_RATE,
), ),
"download_paused": ("DownloadPaused", "False", None, None), "download_paused": ("DownloadPaused", "False", None, None),
"speed": ("DownloadRate", "2.38", DATA_RATE_MEGABYTES_PER_SECOND, None), "speed": (
"DownloadRate",
"2.38",
DATA_RATE_MEGABYTES_PER_SECOND,
SensorDeviceClass.DATA_RATE,
),
"size": ("DownloadedSizeMB", "256", DATA_MEGABYTES, None), "size": ("DownloadedSizeMB", "256", DATA_MEGABYTES, None),
"disk_free": ("FreeDiskSpaceMB", "1024", DATA_MEGABYTES, None), "disk_free": ("FreeDiskSpaceMB", "1024", DATA_MEGABYTES, None),
"post_processing_jobs": ("PostJobCount", "2", "Jobs", None), "post_processing_jobs": ("PostJobCount", "2", "Jobs", None),
@ -44,7 +49,7 @@ async def test_sensors(hass, nzbget_api) -> None:
"DownloadLimit", "DownloadLimit",
"0.95", "0.95",
DATA_RATE_MEGABYTES_PER_SECOND, DATA_RATE_MEGABYTES_PER_SECOND,
None, SensorDeviceClass.DATA_RATE,
), ),
} }