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

pull/83707/head
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,
)
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,
),

View File

@ -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",
),

View File

@ -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),
),

View File

@ -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

View File

@ -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,
),

View File

@ -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,
),

View File

@ -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]

View File

@ -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(

View File

@ -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,
),
)

View File

@ -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,
),
)

View File

@ -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",
),
)

View File

@ -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,
),

View File

@ -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,

View File

@ -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(

View File

@ -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),
),

View File

@ -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

View File

@ -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,
),

View File

@ -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."""

View File

@ -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",

View File

@ -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,
),

View File

@ -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,
),
}