From 535aba10ee8ab30ff30ae45ca58db3338076ea3c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 10 Dec 2022 11:41:44 +0100 Subject: [PATCH] Use DataRate unit and device class in integrations (#83610) --- homeassistant/components/asuswrt/sensor.py | 12 ++++----- homeassistant/components/bbox/sensor.py | 18 ++++++------- homeassistant/components/deluge/sensor.py | 9 ++++--- homeassistant/components/fastdotcom/sensor.py | 11 ++++---- homeassistant/components/fritz/sensor.py | 25 ++++++++++--------- homeassistant/components/huawei_lte/sensor.py | 8 +++--- homeassistant/components/iperf3/__init__.py | 9 ++++--- homeassistant/components/netgear/sensor.py | 8 +++--- homeassistant/components/nzbget/sensor.py | 15 ++++++----- .../components/qbittorrent/sensor.py | 9 ++++--- homeassistant/components/qnap/sensor.py | 8 +++--- homeassistant/components/qnap_qsw/sensor.py | 13 ++++------ homeassistant/components/rtorrent/sensor.py | 9 ++++--- homeassistant/components/sabnzbd/sensor.py | 10 +++----- .../components/speedtestdotnet/const.py | 16 ++++++------ .../components/synology_dsm/sensor.py | 10 +++++--- .../components/systemmonitor/sensor.py | 10 ++++---- .../components/transmission/sensor.py | 10 +++----- .../components/unifiprotect/sensor.py | 9 ++++--- homeassistant/components/upnp/sensor.py | 13 ++++++---- tests/components/nzbget/test_sensor.py | 11 +++++--- 21 files changed, 132 insertions(+), 111 deletions(-) diff --git a/homeassistant/components/asuswrt/sensor.py b/homeassistant/components/asuswrt/sensor.py index e66874e4137..57a8c62ce23 100644 --- a/homeassistant/components/asuswrt/sensor.py +++ b/homeassistant/components/asuswrt/sensor.py @@ -10,11 +10,7 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - DATA_GIGABYTES, - DATA_RATE_MEGABITS_PER_SECOND, - TEMP_CELSIUS, -) +from homeassistant.const import DATA_GIGABYTES, TEMP_CELSIUS, UnitOfDataRate from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -57,8 +53,9 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( key=SENSORS_RATES[0], name="Download Speed", icon="mdi:download-network", + device_class=SensorDeviceClass.DATA_RATE, 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, factor=125000, ), @@ -66,8 +63,9 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = ( key=SENSORS_RATES[1], name="Upload Speed", icon="mdi:upload-network", + device_class=SensorDeviceClass.DATA_RATE, 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, factor=125000, ), diff --git a/homeassistant/components/bbox/sensor.py b/homeassistant/components/bbox/sensor.py index 43ba6956507..4cc77a4b780 100644 --- a/homeassistant/components/bbox/sensor.py +++ b/homeassistant/components/bbox/sensor.py @@ -15,11 +15,7 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.const import ( - CONF_MONITORED_VARIABLES, - CONF_NAME, - DATA_RATE_MEGABITS_PER_SECOND, -) +from homeassistant.const import CONF_MONITORED_VARIABLES, CONF_NAME, UnitOfDataRate from homeassistant.core import HomeAssistant import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -39,26 +35,30 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="down_max_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", ), SensorEntityDescription( key="up_max_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", ), SensorEntityDescription( key="current_down_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, icon="mdi:download", ), SensorEntityDescription( key="current_up_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, icon="mdi:upload", ), diff --git a/homeassistant/components/deluge/sensor.py b/homeassistant/components/deluge/sensor.py index bcdca8b3d92..12b7ce0dd8d 100644 --- a/homeassistant/components/deluge/sensor.py +++ b/homeassistant/components/deluge/sensor.py @@ -6,12 +6,13 @@ from dataclasses import dataclass from typing import Any from homeassistant.components.sensor import ( + SensorDeviceClass, SensorEntity, SensorEntityDescription, SensorStateClass, ) 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.helpers import entity_platform from homeassistant.helpers.typing import StateType @@ -53,14 +54,16 @@ SENSOR_TYPES: tuple[DelugeSensorEntityDescription, ...] = ( DelugeSensorEntityDescription( key=DOWNLOAD_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, value=lambda data: get_state(data, DOWNLOAD_SPEED), ), DelugeSensorEntityDescription( key=UPLOAD_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, value=lambda data: get_state(data, UPLOAD_SPEED), ), diff --git a/homeassistant/components/fastdotcom/sensor.py b/homeassistant/components/fastdotcom/sensor.py index 8363981b526..1df431d0bf3 100644 --- a/homeassistant/components/fastdotcom/sensor.py +++ b/homeassistant/components/fastdotcom/sensor.py @@ -3,8 +3,8 @@ from __future__ import annotations from typing import Any -from homeassistant.components.sensor import SensorEntity -from homeassistant.const import DATA_RATE_MEGABITS_PER_SECOND +from homeassistant.components.sensor import SensorDeviceClass, SensorEntity +from homeassistant.const import UnitOfDataRate from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect 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 -ICON = "mdi:speedometer" - async def async_setup_platform( hass: HomeAssistant, @@ -30,8 +28,9 @@ class SpeedtestSensor(RestoreEntity, SensorEntity): """Implementation of a FAst.com sensor.""" _attr_name = "Fast.com Download" - _attr_native_unit_of_measurement = DATA_RATE_MEGABITS_PER_SECOND - _attr_icon = ICON + _attr_device_class = SensorDeviceClass.DATA_RATE + _attr_native_unit_of_measurement = UnitOfDataRate.MEGABITS_PER_SECOND + _attr_icon = "mdi:speedometer" _attr_should_poll = False _attr_native_value = None diff --git a/homeassistant/components/fritz/sensor.py b/homeassistant/components/fritz/sensor.py index 821a0000a5e..9fba4f4df71 100644 --- a/homeassistant/components/fritz/sensor.py +++ b/homeassistant/components/fritz/sensor.py @@ -17,12 +17,7 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - DATA_GIGABYTES, - DATA_RATE_KILOBITS_PER_SECOND, - DATA_RATE_KILOBYTES_PER_SECOND, - SIGNAL_STRENGTH_DECIBELS, -) +from homeassistant.const import DATA_GIGABYTES, SIGNAL_STRENGTH_DECIBELS, UnitOfDataRate from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -186,7 +181,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( key="kb_s_sent", name="Upload Throughput", 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", value_fn=_retrieve_kb_s_sent_state, ), @@ -194,14 +190,16 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( key="kb_s_received", name="Download Throughput", 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", value_fn=_retrieve_kb_s_received_state, ), FritzSensorEntityDescription( key="max_kb_s_sent", 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", entity_category=EntityCategory.DIAGNOSTIC, value_fn=_retrieve_max_kb_s_sent_state, @@ -209,7 +207,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( FritzSensorEntityDescription( key="max_kb_s_received", 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", entity_category=EntityCategory.DIAGNOSTIC, value_fn=_retrieve_max_kb_s_received_state, @@ -233,14 +232,16 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( FritzSensorEntityDescription( key="link_kb_s_sent", 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", value_fn=_retrieve_link_kb_s_sent_state, ), FritzSensorEntityDescription( key="link_kb_s_received", 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", value_fn=_retrieve_link_kb_s_received_state, ), diff --git a/homeassistant/components/huawei_lte/sensor.py b/homeassistant/components/huawei_lte/sensor.py index c4cce70cbb7..e974bfa48da 100644 --- a/homeassistant/components/huawei_lte/sensor.py +++ b/homeassistant/components/huawei_lte/sensor.py @@ -17,11 +17,11 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( DATA_BYTES, - DATA_RATE_BYTES_PER_SECOND, FREQUENCY_MEGAHERTZ, PERCENTAGE, STATE_UNKNOWN, TIME_SECONDS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant 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( 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", state_class=SensorStateClass.MEASUREMENT, ), @@ -383,7 +384,8 @@ SENSOR_META: dict[str | tuple[str, str], SensorMeta] = { ), (KEY_MONITORING_TRAFFIC_STATISTICS, "CurrentUploadRate"): SensorMeta( 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", state_class=SensorStateClass.MEASUREMENT, ), diff --git a/homeassistant/components/iperf3/__init__.py b/homeassistant/components/iperf3/__init__.py index ad7c6775107..951397e7e61 100644 --- a/homeassistant/components/iperf3/__init__.py +++ b/homeassistant/components/iperf3/__init__.py @@ -9,6 +9,7 @@ import voluptuous as vol from homeassistant.components.sensor import ( DOMAIN as SENSOR_DOMAIN, + SensorDeviceClass, SensorEntityDescription, ) from homeassistant.const import ( @@ -18,7 +19,7 @@ from homeassistant.const import ( CONF_PORT, CONF_PROTOCOL, CONF_SCAN_INTERVAL, - DATA_RATE_MEGABITS_PER_SECOND, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant, ServiceCall import homeassistant.helpers.config_validation as cv @@ -51,12 +52,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key=ATTR_DOWNLOAD, 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( key=ATTR_UPLOAD, 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] diff --git a/homeassistant/components/netgear/sensor.py b/homeassistant/components/netgear/sensor.py index dc857e1377a..a21f0da49b1 100644 --- a/homeassistant/components/netgear/sensor.py +++ b/homeassistant/components/netgear/sensor.py @@ -17,9 +17,9 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( DATA_MEGABYTES, - DATA_RATE_MEGABITS_PER_SECOND, PERCENTAGE, TIME_MILLISECONDS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import EntityCategory @@ -228,14 +228,16 @@ SENSOR_SPEED_TYPES = [ key="NewOOKLAUplinkBandwidth", name="Uplink Bandwidth", 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", ), NetgearSensorEntityDescription( key="NewOOKLADownlinkBandwidth", name="Downlink Bandwidth", 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", ), NetgearSensorEntityDescription( diff --git a/homeassistant/components/nzbget/sensor.py b/homeassistant/components/nzbget/sensor.py index 941c528f544..b31c9d33a66 100644 --- a/homeassistant/components/nzbget/sensor.py +++ b/homeassistant/components/nzbget/sensor.py @@ -10,11 +10,7 @@ from homeassistant.components.sensor import ( SensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - CONF_NAME, - DATA_MEGABYTES, - DATA_RATE_MEGABYTES_PER_SECOND, -) +from homeassistant.const import CONF_NAME, DATA_MEGABYTES, UnitOfDataRate from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.dt import utcnow @@ -34,7 +30,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="AverageDownloadRate", 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( key="DownloadPaused", @@ -43,7 +40,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="DownloadRate", 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( key="DownloadedSizeMB", @@ -77,7 +75,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="DownloadLimit", 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, ), ) diff --git a/homeassistant/components/qbittorrent/sensor.py b/homeassistant/components/qbittorrent/sensor.py index 151055a1688..ff37d4f59dd 100644 --- a/homeassistant/components/qbittorrent/sensor.py +++ b/homeassistant/components/qbittorrent/sensor.py @@ -9,6 +9,7 @@ import voluptuous as vol from homeassistant.components.sensor import ( PLATFORM_SCHEMA, + SensorDeviceClass, SensorEntity, SensorEntityDescription, ) @@ -17,8 +18,8 @@ from homeassistant.const import ( CONF_PASSWORD, CONF_URL, CONF_USERNAME, - DATA_RATE_KIBIBYTES_PER_SECOND, STATE_IDLE, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady @@ -42,12 +43,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key=SENSOR_TYPE_DOWNLOAD_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( key=SENSOR_TYPE_UPLOAD_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, ), ) diff --git a/homeassistant/components/qnap/sensor.py b/homeassistant/components/qnap/sensor.py index 8c093eb9232..1bae440cc03 100644 --- a/homeassistant/components/qnap/sensor.py +++ b/homeassistant/components/qnap/sensor.py @@ -24,9 +24,9 @@ from homeassistant.const import ( CONF_USERNAME, CONF_VERIFY_SSL, DATA_GIBIBYTES, - DATA_RATE_MEBIBYTES_PER_SECOND, PERCENTAGE, TEMP_CELSIUS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady @@ -121,13 +121,15 @@ _NETWORK_MON_COND: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="network_tx", 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", ), SensorEntityDescription( key="network_rx", 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", ), ) diff --git a/homeassistant/components/qnap_qsw/sensor.py b/homeassistant/components/qnap_qsw/sensor.py index 5fecf28c9f2..4975b9f9402 100644 --- a/homeassistant/components/qnap_qsw/sensor.py +++ b/homeassistant/components/qnap_qsw/sensor.py @@ -31,12 +31,7 @@ from homeassistant.components.sensor import ( SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - DATA_BYTES, - DATA_RATE_BYTES_PER_SECOND, - TEMP_CELSIUS, - TIME_SECONDS, -) +from homeassistant.const import DATA_BYTES, TEMP_CELSIUS, TIME_SECONDS, UnitOfDataRate from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -101,10 +96,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = ( ), QswSensorEntityDescription( entity_registry_enabled_default=False, + device_class=SensorDeviceClass.DATA_RATE, icon="mdi:download-network", key=QSD_PORTS_STATISTICS, name="RX Speed", - native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, + native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND, state_class=SensorStateClass.MEASUREMENT, subkey=QSD_RX_SPEED, ), @@ -130,10 +126,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = ( ), QswSensorEntityDescription( entity_registry_enabled_default=False, + device_class=SensorDeviceClass.DATA_RATE, icon="mdi:upload-network", key=QSD_PORTS_STATISTICS, name="TX Speed", - native_unit_of_measurement=DATA_RATE_BYTES_PER_SECOND, + native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND, state_class=SensorStateClass.MEASUREMENT, subkey=QSD_TX_SPEED, ), diff --git a/homeassistant/components/rtorrent/sensor.py b/homeassistant/components/rtorrent/sensor.py index 278741f20db..e9c7aa89ffe 100644 --- a/homeassistant/components/rtorrent/sensor.py +++ b/homeassistant/components/rtorrent/sensor.py @@ -8,6 +8,7 @@ import voluptuous as vol from homeassistant.components.sensor import ( PLATFORM_SCHEMA, + SensorDeviceClass, SensorEntity, SensorEntityDescription, ) @@ -15,8 +16,8 @@ from homeassistant.const import ( CONF_MONITORED_VARIABLES, CONF_NAME, CONF_URL, - DATA_RATE_KILOBYTES_PER_SECOND, STATE_IDLE, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import PlatformNotReady @@ -45,12 +46,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key=SENSOR_TYPE_DOWNLOAD_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( key=SENSOR_TYPE_UPLOAD_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( key=SENSOR_TYPE_ALL_TORRENTS, diff --git a/homeassistant/components/sabnzbd/sensor.py b/homeassistant/components/sabnzbd/sensor.py index b4231a469ec..0c32d450c38 100644 --- a/homeassistant/components/sabnzbd/sensor.py +++ b/homeassistant/components/sabnzbd/sensor.py @@ -4,16 +4,13 @@ from __future__ import annotations from dataclasses import dataclass from homeassistant.components.sensor import ( + SensorDeviceClass, SensorEntity, SensorEntityDescription, SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( - DATA_GIGABYTES, - DATA_MEGABYTES, - DATA_RATE_MEGABYTES_PER_SECOND, -) +from homeassistant.const import DATA_GIGABYTES, DATA_MEGABYTES, UnitOfDataRate from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -46,7 +43,8 @@ SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = ( SabnzbdSensorEntityDescription( key=SPEED_KEY, 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, ), SabnzbdSensorEntityDescription( diff --git a/homeassistant/components/speedtestdotnet/const.py b/homeassistant/components/speedtestdotnet/const.py index e6ced462b45..4843cf3f60d 100644 --- a/homeassistant/components/speedtestdotnet/const.py +++ b/homeassistant/components/speedtestdotnet/const.py @@ -5,12 +5,12 @@ from collections.abc import Callable from dataclasses import dataclass from typing import Final -from homeassistant.components.sensor import SensorEntityDescription, SensorStateClass -from homeassistant.const import ( - DATA_RATE_MEGABITS_PER_SECOND, - TIME_MILLISECONDS, - Platform, +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntityDescription, + SensorStateClass, ) +from homeassistant.const import TIME_MILLISECONDS, Platform, UnitOfDataRate DOMAIN: Final = "speedtestdotnet" @@ -32,14 +32,16 @@ SENSOR_TYPES: Final[tuple[SpeedtestSensorEntityDescription, ...]] = ( SpeedtestSensorEntityDescription( key="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, value=lambda value: round(value / 10**6, 2), ), SpeedtestSensorEntityDescription( key="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, value=lambda value: round(value / 10**6, 2), ), diff --git a/homeassistant/components/synology_dsm/sensor.py b/homeassistant/components/synology_dsm/sensor.py index 6a2a92b9fd5..6800375311d 100644 --- a/homeassistant/components/synology_dsm/sensor.py +++ b/homeassistant/components/synology_dsm/sensor.py @@ -19,10 +19,10 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_DISKS, DATA_MEGABYTES, - DATA_RATE_KILOBYTES_PER_SECOND, DATA_TERABYTES, PERCENTAGE, TEMP_CELSIUS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import EntityCategory @@ -166,7 +166,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = ( api_key=SynoCoreUtilization.API_KEY, key="network_up", 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", state_class=SensorStateClass.MEASUREMENT, ), @@ -174,7 +175,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = ( api_key=SynoCoreUtilization.API_KEY, key="network_down", 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", state_class=SensorStateClass.MEASUREMENT, ), @@ -351,7 +353,7 @@ class SynoDSMUtilSensor(SynoDSMSensor): return round(attr / 1024.0**2, 1) # 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) # CPU load average diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index d16e2ac3190..c5a40c757f6 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -27,12 +27,12 @@ from homeassistant.const import ( CONF_TYPE, DATA_GIBIBYTES, DATA_MEBIBYTES, - DATA_RATE_MEGABYTES_PER_SECOND, EVENT_HOMEASSISTANT_STOP, PERCENTAGE, STATE_OFF, STATE_ON, TEMP_CELSIUS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv @@ -183,16 +183,16 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = { "throughput_network_in": SysMonitorSensorEntityDescription( key="throughput_network_in", name="Network throughput in", - native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, - icon="mdi:server-network", + native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, + device_class=SensorDeviceClass.DATA_RATE, state_class=SensorStateClass.MEASUREMENT, mandatory_arg=True, ), "throughput_network_out": SysMonitorSensorEntityDescription( key="throughput_network_out", name="Network throughput out", - native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND, - icon="mdi:server-network", + native_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND, + device_class=SensorDeviceClass.DATA_RATE, state_class=SensorStateClass.MEASUREMENT, mandatory_arg=True, ), diff --git a/homeassistant/components/transmission/sensor.py b/homeassistant/components/transmission/sensor.py index 0472ce221fb..839060f6a12 100644 --- a/homeassistant/components/transmission/sensor.py +++ b/homeassistant/components/transmission/sensor.py @@ -5,9 +5,9 @@ from contextlib import suppress 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.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.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -97,10 +97,8 @@ class TransmissionSensor(SensorEntity): class TransmissionSpeedSensor(TransmissionSensor): """Representation of a Transmission speed sensor.""" - @property - def native_unit_of_measurement(self): - """Return the unit of measurement of this entity, if any.""" - return DATA_RATE_MEGABYTES_PER_SECOND + _attr_device_class = SensorDeviceClass.DATA_RATE + _attr_native_unit_of_measurement = UnitOfDataRate.MEGABYTES_PER_SECOND def update(self) -> None: """Get the latest data from Transmission and updates the state.""" diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index 9b22c00dbbd..aebec0d082f 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -27,14 +27,13 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( DATA_BYTES, - DATA_RATE_BYTES_PER_SECOND, - DATA_RATE_MEGABITS_PER_SECOND, ELECTRIC_POTENTIAL_VOLT, LIGHT_LUX, PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT, TEMP_CELSIUS, TIME_SECONDS, + UnitOfDataRate, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -140,7 +139,8 @@ ALL_DEVICES_SENSORS: tuple[ProtectSensorEntityDescription, ...] = ( ProtectSensorEntityDescription( key="phy_rate", 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_registry_enabled_default=False, state_class=SensorStateClass.MEASUREMENT, @@ -180,7 +180,8 @@ CAMERA_SENSORS: tuple[ProtectSensorEntityDescription, ...] = ( ProtectSensorEntityDescription( key="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, state_class=SensorStateClass.MEASUREMENT, ufp_value="stats.storage.rate_per_second", diff --git a/homeassistant/components/upnp/sensor.py b/homeassistant/components/upnp/sensor.py index 3d0c71fafdb..4fb89f64a51 100644 --- a/homeassistant/components/upnp/sensor.py +++ b/homeassistant/components/upnp/sensor.py @@ -4,12 +4,13 @@ from __future__ import annotations from dataclasses import dataclass from homeassistant.components.sensor import ( + SensorDeviceClass, SensorEntity, SensorEntityDescription, SensorStateClass, ) 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.helpers.entity import EntityCategory from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -103,9 +104,10 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( key=BYTES_RECEIVED, value_key=KIBIBYTES_PER_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", - native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, + device_class=SensorDeviceClass.DATA_RATE, + native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND, format=".1f", state_class=SensorStateClass.MEASUREMENT, ), @@ -113,9 +115,10 @@ SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( key=BYTES_SENT, value_key=KIBIBYTES_PER_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", - native_unit_of_measurement=DATA_RATE_KIBIBYTES_PER_SECOND, + device_class=SensorDeviceClass.DATA_RATE, + native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND, format=".1f", state_class=SensorStateClass.MEASUREMENT, ), diff --git a/tests/components/nzbget/test_sensor.py b/tests/components/nzbget/test_sensor.py index 9cedf29c82e..78918fadfad 100644 --- a/tests/components/nzbget/test_sensor.py +++ b/tests/components/nzbget/test_sensor.py @@ -30,10 +30,15 @@ async def test_sensors(hass, nzbget_api) -> None: "AverageDownloadRate", "1.19", DATA_RATE_MEGABYTES_PER_SECOND, - None, + SensorDeviceClass.DATA_RATE, ), "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), "disk_free": ("FreeDiskSpaceMB", "1024", DATA_MEGABYTES, None), "post_processing_jobs": ("PostJobCount", "2", "Jobs", None), @@ -44,7 +49,7 @@ async def test_sensors(hass, nzbget_api) -> None: "DownloadLimit", "0.95", DATA_RATE_MEGABYTES_PER_SECOND, - None, + SensorDeviceClass.DATA_RATE, ), }