Use DATA_SIZE device class in integrations (#83895)

This commit is contained in:
epenet 2022-12-13 10:17:56 +01:00 committed by GitHub
parent 677f0dc335
commit 69bc95a715
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
28 changed files with 319 additions and 169 deletions

View File

@ -10,7 +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 DATA_GIGABYTES, TEMP_CELSIUS, UnitOfDataRate from homeassistant.const import TEMP_CELSIUS, UnitOfDataRate, UnitOfInformation
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
@ -74,7 +74,8 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
name="Download", name="Download",
icon="mdi:download", icon="mdi:download",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1000000000, factor=1000000000,
), ),
@ -83,7 +84,8 @@ CONNECTION_SENSORS: tuple[AsusWrtSensorEntityDescription, ...] = (
name="Upload", name="Upload",
icon="mdi:upload", icon="mdi:upload",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
factor=1000000000, factor=1000000000,
), ),

View File

@ -7,12 +7,13 @@ import re
from typing import Any, cast from typing import Any, cast
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_KILOBYTES, DATA_MEGABYTES, TIME_DAYS from homeassistant.const import TIME_DAYS, UnitOfInformation
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.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
@ -36,21 +37,24 @@ SENSOR_DESCRIPTIONS: tuple[SensorValueEntityDescription, ...] = (
key="usedMb", key="usedMb",
name="Data used", name="Data used",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:network", icon="mdi:network",
), ),
SensorValueEntityDescription( SensorValueEntityDescription(
key="downloadedMb", key="downloadedMb",
name="Downloaded", name="Downloaded",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download-network", icon="mdi:download-network",
), ),
SensorValueEntityDescription( SensorValueEntityDescription(
key="uploadedMb", key="uploadedMb",
name="Uploaded", name="Uploaded",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload-network", icon="mdi:upload-network",
), ),
# Mobile Phone Services sensors # Mobile Phone Services sensors
@ -86,7 +90,8 @@ SENSOR_DESCRIPTIONS: tuple[SensorValueEntityDescription, ...] = (
key="internet", key="internet",
name="Data used", name="Data used",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_KILOBYTES, native_unit_of_measurement=UnitOfInformation.KILOBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:network", icon="mdi:network",
value=lambda x: x.get("kbytes"), value=lambda x: x.get("kbytes"),
), ),

View File

@ -9,10 +9,11 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.const import CONF_SENSORS, DATA_GIGABYTES, PERCENTAGE from homeassistant.const import CONF_SENSORS, PERCENTAGE, UnitOfInformation
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
@ -65,14 +66,16 @@ SENSOR_TYPES: tuple[DovadoSensorEntityDescription, ...] = (
identifier=SENSOR_UPLOAD, identifier=SENSOR_UPLOAD,
key="traffic modem tx", key="traffic modem tx",
name="Sent", name="Sent",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:cloud-upload", icon="mdi:cloud-upload",
), ),
DovadoSensorEntityDescription( DovadoSensorEntityDescription(
identifier=SENSOR_DOWNLOAD, identifier=SENSOR_DOWNLOAD,
key="traffic modem rx", key="traffic modem rx",
name="Received", name="Received",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:cloud-download", icon="mdi:cloud-download",
), ),
) )

View File

@ -14,6 +14,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -22,9 +23,9 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_USERNAME, CONF_USERNAME,
DATA_GIGABITS,
PERCENTAGE, PERCENTAGE,
TIME_DAYS, TIME_DAYS,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -61,7 +62,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="limit", key="limit",
name="Data limit", name="Data limit",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
@ -73,55 +75,64 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="before_offpeak_download", key="before_offpeak_download",
name="Download before offpeak", name="Download before offpeak",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="before_offpeak_upload", key="before_offpeak_upload",
name="Upload before offpeak", name="Upload before offpeak",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="before_offpeak_total", key="before_offpeak_total",
name="Total before offpeak", name="Total before offpeak",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="offpeak_download", key="offpeak_download",
name="Offpeak download", name="Offpeak download",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="offpeak_upload", key="offpeak_upload",
name="Offpeak Upload", name="Offpeak Upload",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="offpeak_total", key="offpeak_total",
name="Offpeak Total", name="Offpeak Total",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="download", key="download",
name="Download", name="Download",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="upload", key="upload",
name="Upload", name="Upload",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="total", key="total",
name="Total", name="Total",
native_unit_of_measurement=DATA_GIGABITS, native_unit_of_measurement=UnitOfInformation.GIGABITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
) )

View File

@ -15,6 +15,7 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -23,8 +24,8 @@ from homeassistant.const import (
CONF_NAME, CONF_NAME,
CONF_PASSWORD, CONF_PASSWORD,
CONF_USERNAME, CONF_USERNAME,
DATA_KILOBITS,
TIME_MINUTES, TIME_MINUTES,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -59,19 +60,22 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="data_used", key="data_used",
name="Data used", name="Data used",
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="data_limit", key="data_limit",
name="Data limit", name="Data limit",
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="data_remaining", key="data_remaining",
name="Data remaining", name="Data remaining",
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(

View File

@ -13,7 +13,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_FILE_PATH, DATA_BYTES, DATA_MEGABYTES from homeassistant.const import CONF_FILE_PATH, UnitOfInformation
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.entity import DeviceInfo, EntityCategory from homeassistant.helpers.entity import DeviceInfo, EntityCategory
@ -36,7 +36,8 @@ SENSOR_TYPES = (
key="file", key="file",
icon=ICON, icon=ICON,
name="Size", name="Size",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
@ -44,7 +45,8 @@ SENSOR_TYPES = (
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
icon=ICON, icon=ICON,
name="Size bytes", name="Size bytes",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
), ),

View File

@ -17,7 +17,11 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_GIGABYTES, SIGNAL_STRENGTH_DECIBELS, UnitOfDataRate from homeassistant.const import (
SIGNAL_STRENGTH_DECIBELS,
UnitOfDataRate,
UnitOfInformation,
)
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
@ -217,7 +221,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
key="gb_sent", key="gb_sent",
name="GB sent", name="GB sent",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
value_fn=_retrieve_gb_sent_state, value_fn=_retrieve_gb_sent_state,
), ),
@ -225,7 +230,8 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = (
key="gb_received", key="gb_received",
name="GB received", name="GB received",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
value_fn=_retrieve_gb_received_state, value_fn=_retrieve_gb_received_state,
), ),

View File

@ -12,7 +12,7 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_MEGABYTES, PERCENTAGE from homeassistant.const import PERCENTAGE, UnitOfInformation
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
@ -62,7 +62,8 @@ SENSORS: tuple[FullySensorEntityDescription, ...] = (
key="internalStorageFreeSpace", key="internalStorageFreeSpace",
name="Internal storage free space", name="Internal storage free space",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
state_fn=round_storage, state_fn=round_storage,
), ),
@ -70,7 +71,8 @@ SENSORS: tuple[FullySensorEntityDescription, ...] = (
key="internalStorageTotalSpace", key="internalStorageTotalSpace",
name="Internal storage total space", name="Internal storage total space",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
state_fn=round_storage, state_fn=round_storage,
), ),
@ -78,7 +80,8 @@ SENSORS: tuple[FullySensorEntityDescription, ...] = (
key="ramFreeMemory", key="ramFreeMemory",
name="Free memory", name="Free memory",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
state_fn=round_storage, state_fn=round_storage,
), ),
@ -86,7 +89,8 @@ SENSORS: tuple[FullySensorEntityDescription, ...] = (
key="ramTotalMemory", key="ramTotalMemory",
name="Total memory", name="Total memory",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
state_fn=round_storage, state_fn=round_storage,
), ),

View File

@ -13,13 +13,12 @@ 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 (
CONF_NAME, CONF_NAME,
DATA_GIBIBYTES,
DATA_MEBIBYTES,
PERCENTAGE, PERCENTAGE,
REVOLUTIONS_PER_MINUTE, REVOLUTIONS_PER_MINUTE,
STATE_UNAVAILABLE, STATE_UNAVAILABLE,
TEMP_CELSIUS, TEMP_CELSIUS,
Platform, Platform,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry from homeassistant.helpers import entity_registry
@ -59,7 +58,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="disk_use", key="disk_use",
type="fs", type="fs",
name_suffix="used", name_suffix="used",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -67,7 +67,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="disk_free", key="disk_free",
type="fs", type="fs",
name_suffix="free", name_suffix="free",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -83,7 +84,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="memory_use", key="memory_use",
type="mem", type="mem",
name_suffix="RAM used", name_suffix="RAM used",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -91,7 +93,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="memory_free", key="memory_free",
type="mem", type="mem",
name_suffix="RAM free", name_suffix="RAM free",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -107,7 +110,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="swap_use", key="swap_use",
type="memswap", type="memswap",
name_suffix="Swap used", name_suffix="Swap used",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -115,7 +119,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="swap_free", key="swap_free",
type="memswap", type="memswap",
name_suffix="Swap free", name_suffix="Swap free",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -213,7 +218,8 @@ SENSOR_TYPES: tuple[GlancesSensorEntityDescription, ...] = (
key="docker_memory_use", key="docker_memory_use",
type="docker", type="docker",
name_suffix="Containers RAM used", name_suffix="Containers RAM used",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:docker", icon="mdi:docker",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),

View File

@ -16,10 +16,10 @@ 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,
PERCENTAGE, PERCENTAGE,
TIME_MILLISECONDS, TIME_MILLISECONDS,
UnitOfDataRate, UnitOfDataRate,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -89,35 +89,40 @@ SENSOR_TRAFFIC_TYPES = [
key="NewTodayUpload", key="NewTodayUpload",
name="Upload today", name="Upload today",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(
key="NewTodayDownload", key="NewTodayDownload",
name="Download today", name="Download today",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(
key="NewYesterdayUpload", key="NewYesterdayUpload",
name="Upload yesterday", name="Upload yesterday",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(
key="NewYesterdayDownload", key="NewYesterdayDownload",
name="Download yesterday", name="Download yesterday",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
NetgearSensorEntityDescription( NetgearSensorEntityDescription(
key="NewWeekUpload", key="NewWeekUpload",
name="Upload week", name="Upload week",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -126,7 +131,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewWeekUpload", key="NewWeekUpload",
name="Upload week average", name="Upload week average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],
@ -135,7 +141,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewWeekDownload", key="NewWeekDownload",
name="Download week", name="Download week",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -144,7 +151,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewWeekDownload", key="NewWeekDownload",
name="Download week average", name="Download week average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],
@ -153,7 +161,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewMonthUpload", key="NewMonthUpload",
name="Upload month", name="Upload month",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -162,7 +171,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewMonthUpload", key="NewMonthUpload",
name="Upload month average", name="Upload month average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],
@ -171,7 +181,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewMonthDownload", key="NewMonthDownload",
name="Download month", name="Download month",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -180,7 +191,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewMonthDownload", key="NewMonthDownload",
name="Download month average", name="Download month average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],
@ -189,7 +201,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewLastMonthUpload", key="NewLastMonthUpload",
name="Upload last month", name="Upload last month",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -198,7 +211,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewLastMonthUpload", key="NewLastMonthUpload",
name="Upload last month average", name="Upload last month average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],
@ -207,7 +221,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewLastMonthDownload", key="NewLastMonthDownload",
name="Download last month", name="Download last month",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=0, index=0,
value=lambda data: data[0], value=lambda data: data[0],
@ -216,7 +231,8 @@ SENSOR_TRAFFIC_TYPES = [
key="NewLastMonthDownload", key="NewLastMonthDownload",
name="Download last month average", name="Download last month average",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
index=1, index=1,
value=lambda data: data[1], value=lambda data: data[1],

View File

@ -10,7 +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 CONF_NAME, DATA_MEGABYTES, UnitOfDataRate from homeassistant.const import CONF_NAME, UnitOfDataRate, UnitOfInformation
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
@ -25,7 +25,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="ArticleCacheMB", key="ArticleCacheMB",
name="Article Cache", name="Article Cache",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
), ),
SensorEntityDescription( SensorEntityDescription(
key="AverageDownloadRate", key="AverageDownloadRate",
@ -46,12 +47,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="DownloadedSizeMB", key="DownloadedSizeMB",
name="Size", name="Size",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
), ),
SensorEntityDescription( SensorEntityDescription(
key="FreeDiskSpaceMB", key="FreeDiskSpaceMB",
name="Disk Free", name="Disk Free",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
), ),
SensorEntityDescription( SensorEntityDescription(
key="PostJobCount", key="PostJobCount",
@ -65,7 +68,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="RemainingSizeMB", key="RemainingSizeMB",
name="Queue Size", name="Queue Size",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
), ),
SensorEntityDescription( SensorEntityDescription(
key="UpTimeSec", key="UpTimeSec",

View File

@ -23,10 +23,10 @@ from homeassistant.const import (
CONF_TIMEOUT, CONF_TIMEOUT,
CONF_USERNAME, CONF_USERNAME,
CONF_VERIFY_SSL, CONF_VERIFY_SSL,
DATA_GIBIBYTES,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate, UnitOfDataRate,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import PlatformNotReady from homeassistant.exceptions import PlatformNotReady
@ -96,13 +96,15 @@ _MEMORY_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="memory_free", key="memory_free",
name="Memory Available", name="Memory Available",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
), ),
SensorEntityDescription( SensorEntityDescription(
key="memory_used", key="memory_used",
name="Memory Used", name="Memory Used",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
), ),
SensorEntityDescription( SensorEntityDescription(
@ -150,13 +152,15 @@ _VOLUME_MON_COND: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="volume_size_used", key="volume_size_used",
name="Used Space", name="Used Space",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
), ),
SensorEntityDescription( SensorEntityDescription(
key="volume_size_free", key="volume_size_free",
name="Free Space", name="Free Space",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
), ),
SensorEntityDescription( SensorEntityDescription(
@ -374,7 +378,7 @@ class QNAPMemorySensor(QNAPSensor):
if self._api.data: if self._api.data:
data = self._api.data["system_stats"]["memory"] data = self._api.data["system_stats"]["memory"]
size = round_nicely(float(data["total"]) / 1024) size = round_nicely(float(data["total"]) / 1024)
return {ATTR_MEMORY_SIZE: f"{size} {DATA_GIBIBYTES}"} return {ATTR_MEMORY_SIZE: f"{size} {UnitOfInformation.GIBIBYTES}"}
class QNAPNetworkSensor(QNAPSensor): class QNAPNetworkSensor(QNAPSensor):
@ -501,4 +505,6 @@ class QNAPVolumeSensor(QNAPSensor):
data = self._api.data["volumes"][self.monitor_device] data = self._api.data["volumes"][self.monitor_device]
total_gb = int(data["total_size"]) / 1024 / 1024 / 1024 total_gb = int(data["total_size"]) / 1024 / 1024 / 1024
return {ATTR_VOLUME_SIZE: f"{round_nicely(total_gb)} {DATA_GIBIBYTES}"} return {
ATTR_VOLUME_SIZE: f"{round_nicely(total_gb)} {UnitOfInformation.GIBIBYTES}"
}

View File

@ -31,7 +31,12 @@ from homeassistant.components.sensor import (
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_BYTES, TEMP_CELSIUS, TIME_SECONDS, UnitOfDataRate from homeassistant.const import (
TEMP_CELSIUS,
TIME_SECONDS,
UnitOfDataRate,
UnitOfInformation,
)
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
@ -78,10 +83,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
), ),
QswSensorEntityDescription( QswSensorEntityDescription(
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download-network", icon="mdi:download-network",
key=QSD_PORTS_STATISTICS, key=QSD_PORTS_STATISTICS,
name="RX", name="RX",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_RX_OCTETS, subkey=QSD_RX_OCTETS,
), ),
@ -117,10 +123,11 @@ SENSOR_TYPES: Final[tuple[QswSensorEntityDescription, ...]] = (
), ),
QswSensorEntityDescription( QswSensorEntityDescription(
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload-network", icon="mdi:upload-network",
key=QSD_PORTS_STATISTICS, key=QSD_PORTS_STATISTICS,
name="TX", name="TX",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
subkey=QSD_TX_OCTETS, subkey=QSD_TX_OCTETS,
), ),

View File

@ -15,12 +15,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 UnitOfInformation
DATA_BYTES,
DATA_GIGABYTES,
DATA_KILOBYTES,
DATA_MEGABYTES,
)
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
@ -35,7 +30,7 @@ from .coordinator import RadarrDataUpdateCoordinator, T
def get_space(data: list[Diskspace], name: str) -> str: def get_space(data: list[Diskspace], name: str) -> str:
"""Get space.""" """Get space."""
space = [ space = [
mount.freeSpace / 1024 ** BYTE_SIZES.index(DATA_GIGABYTES) mount.freeSpace / 1024 ** BYTE_SIZES.index(UnitOfInformation.GIGABYTES)
for mount in data for mount in data
if name in mount.path if name in mount.path
] ]
@ -76,7 +71,8 @@ SENSOR_TYPES: dict[str, RadarrSensorEntityDescription[Any]] = {
"disk_space": RadarrSensorEntityDescription( "disk_space": RadarrSensorEntityDescription(
key="disk_space", key="disk_space",
name="Disk space", name="Disk space",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
value_fn=get_space, value_fn=get_space,
description_fn=get_modified_description, description_fn=get_modified_description,
@ -100,10 +96,10 @@ SENSOR_TYPES: dict[str, RadarrSensorEntityDescription[Any]] = {
} }
BYTE_SIZES = [ BYTE_SIZES = [
DATA_BYTES, UnitOfInformation.BYTES,
DATA_KILOBYTES, UnitOfInformation.KILOBYTES,
DATA_MEGABYTES, UnitOfInformation.MEGABYTES,
DATA_GIGABYTES, UnitOfInformation.GIGABYTES,
] ]
PARALLEL_UPDATES = 1 PARALLEL_UPDATES = 1

View File

@ -10,7 +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 DATA_GIGABYTES, DATA_MEGABYTES, UnitOfDataRate from homeassistant.const import UnitOfDataRate, UnitOfInformation
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
@ -50,25 +50,29 @@ SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="mb", key="mb",
name="Queue", name="Queue",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="mbleft", key="mbleft",
name="Left", name="Left",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="diskspacetotal1", key="diskspacetotal1",
name="Disk", name="Disk",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="diskspace1", key="diskspace1",
name="Disk Free", name="Disk Free",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
@ -79,28 +83,32 @@ SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="day_size", key="day_size",
name="Daily Total", name="Daily Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="week_size", key="week_size",
name="Weekly Total", name="Weekly Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="month_size", key="month_size",
name="Monthly Total", name="Monthly Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SabnzbdSensorEntityDescription( SabnzbdSensorEntityDescription(
key="total_size", key="total_size",
name="Total", name="Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
) )

View File

@ -14,9 +14,13 @@ from aiopyarr import (
SonarrWantedMissing, SonarrWantedMissing,
) )
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_GIGABYTES from homeassistant.const import UnitOfInformation
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.helpers.typing import StateType from homeassistant.helpers.typing import StateType
@ -49,7 +53,9 @@ def get_disk_space_attr(disks: list[Diskspace]) -> dict[str, str]:
free = disk.freeSpace / 1024**3 free = disk.freeSpace / 1024**3
total = disk.totalSpace / 1024**3 total = disk.totalSpace / 1024**3
usage = free / total * 100 usage = free / total * 100
attrs[disk.path] = f"{free:.2f}/{total:.2f}{DATA_GIGABYTES} ({usage:.2f}%)" attrs[
disk.path
] = f"{free:.2f}/{total:.2f}{UnitOfInformation.GIGABYTES} ({usage:.2f}%)"
return attrs return attrs
@ -93,7 +99,8 @@ SENSOR_TYPES: dict[str, SonarrSensorEntityDescription[Any]] = {
key="diskspace", key="diskspace",
name="Disk space", name="Disk space",
icon="mdi:harddisk", icon="mdi:harddisk",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda data: f"{sum(disk.freeSpace for disk in data) / 1024**3:.2f}", value_fn=lambda data: f"{sum(disk.freeSpace for disk in data) / 1024**3:.2f}",
attributes_fn=get_disk_space_attr, attributes_fn=get_disk_space_attr,

View File

@ -12,6 +12,7 @@ import xmltodict
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
@ -19,8 +20,8 @@ from homeassistant.const import (
CONF_API_KEY, CONF_API_KEY,
CONF_MONITORED_VARIABLES, CONF_MONITORED_VARIABLES,
CONF_NAME, CONF_NAME,
DATA_GIGABYTES,
PERCENTAGE, PERCENTAGE,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -47,67 +48,78 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="usage_gb", key="usage_gb",
name="Usage", name="Usage",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="limit", key="limit",
name="Data limit", name="Data limit",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="used_download", key="used_download",
name="Used Download", name="Used Download",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="used_upload", key="used_upload",
name="Used Upload", name="Used Upload",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="used_total", key="used_total",
name="Used Total", name="Used Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="grace_download", key="grace_download",
name="Grace Download", name="Grace Download",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="grace_upload", key="grace_upload",
name="Grace Upload", name="Grace Upload",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:upload", icon="mdi:upload",
), ),
SensorEntityDescription( SensorEntityDescription(
key="grace_total", key="grace_total",
name="Grace Total", name="Grace Total",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_download", key="total_download",
name="Total Download", name="Total Download",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_upload", key="total_upload",
name="Total Upload", name="Total Upload",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
SensorEntityDescription( SensorEntityDescription(
key="used_remaining", key="used_remaining",
name="Remaining", name="Remaining",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:download", icon="mdi:download",
), ),
) )

View File

@ -18,11 +18,10 @@ 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 (
CONF_DISKS, CONF_DISKS,
DATA_MEGABYTES,
DATA_TERABYTES,
PERCENTAGE, PERCENTAGE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate, UnitOfDataRate,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -116,7 +115,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_size", key="memory_size",
name="Memory Size", name="Memory Size",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -125,7 +125,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_cached", key="memory_cached",
name="Memory Cached", name="Memory Cached",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -134,7 +135,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_available_swap", key="memory_available_swap",
name="Memory Available (Swap)", name="Memory Available (Swap)",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -142,7 +144,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_available_real", key="memory_available_real",
name="Memory Available (Real)", name="Memory Available (Real)",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -150,7 +153,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_total_swap", key="memory_total_swap",
name="Memory Total (Swap)", name="Memory Total (Swap)",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -158,7 +162,8 @@ UTILISATION_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoCoreUtilization.API_KEY, api_key=SynoCoreUtilization.API_KEY,
key="memory_total_real", key="memory_total_real",
name="Memory Total (Real)", name="Memory Total (Real)",
native_unit_of_measurement=DATA_MEGABYTES, native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -192,7 +197,8 @@ STORAGE_VOL_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoStorage.API_KEY, api_key=SynoStorage.API_KEY,
key="volume_size_total", key="volume_size_total",
name="Total Size", name="Total Size",
native_unit_of_measurement=DATA_TERABYTES, native_unit_of_measurement=UnitOfInformation.TERABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
@ -201,7 +207,8 @@ STORAGE_VOL_SENSORS: tuple[SynologyDSMSensorEntityDescription, ...] = (
api_key=SynoStorage.API_KEY, api_key=SynoStorage.API_KEY,
key="volume_size_used", key="volume_size_used",
name="Used Space", name="Used Space",
native_unit_of_measurement=DATA_TERABYTES, native_unit_of_measurement=UnitOfInformation.TERABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-pie", icon="mdi:chart-pie",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -349,7 +356,7 @@ class SynoDSMUtilSensor(SynoDSMSensor):
return None return None
# Data (RAM) # Data (RAM)
if self.native_unit_of_measurement == DATA_MEGABYTES: if self.native_unit_of_measurement == UnitOfInformation.MEGABYTES:
return round(attr / 1024.0**2, 1) return round(attr / 1024.0**2, 1)
# Network # Network
@ -391,7 +398,7 @@ class SynoDSMStorageSensor(SynologyDSMDeviceEntity, SynoDSMSensor):
return None return None
# Data (disk space) # Data (disk space)
if self.native_unit_of_measurement == DATA_TERABYTES: if self.native_unit_of_measurement == UnitOfInformation.TERABYTES:
return round(attr / 1024.0**4, 2) return round(attr / 1024.0**4, 2)
return attr return attr

View File

@ -15,7 +15,6 @@ 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 (
CONF_PORT, CONF_PORT,
DATA_GIGABYTES,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
FREQUENCY_GIGAHERTZ, FREQUENCY_GIGAHERTZ,
FREQUENCY_HERTZ, FREQUENCY_HERTZ,
@ -23,6 +22,7 @@ from homeassistant.const import (
PERCENTAGE, PERCENTAGE,
REVOLUTIONS_PER_MINUTE, REVOLUTIONS_PER_MINUTE,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfInformation,
UnitOfPower, UnitOfPower,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -171,7 +171,8 @@ BASE_SENSOR_TYPES: tuple[SystemBridgeSensorEntityDescription, ...] = (
key="memory_free", key="memory_free",
name="Memory Free", name="Memory Free",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
value=memory_free, value=memory_free,
), ),
@ -188,7 +189,8 @@ BASE_SENSOR_TYPES: tuple[SystemBridgeSensorEntityDescription, ...] = (
name="Memory Used", name="Memory Used",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
value=memory_used, value=memory_used,
), ),
@ -399,7 +401,8 @@ async def async_setup_entry(
key=f"gpu_{index}_memory_free", key=f"gpu_{index}_memory_free",
name=f"{gpu['name']} Memory Free", name=f"{gpu['name']} Memory Free",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
value=lambda data, k=gpu["key"]: gpu_memory_free(data, k), value=lambda data, k=gpu["key"]: gpu_memory_free(data, k),
), ),
@ -426,7 +429,8 @@ async def async_setup_entry(
name=f"{gpu['name']} Memory Used", name=f"{gpu['name']} Memory Used",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
value=lambda data, k=gpu["key"]: gpu_memory_used(data, k), value=lambda data, k=gpu["key"]: gpu_memory_used(data, k),
), ),

View File

@ -25,14 +25,13 @@ from homeassistant.const import (
CONF_RESOURCES, CONF_RESOURCES,
CONF_SCAN_INTERVAL, CONF_SCAN_INTERVAL,
CONF_TYPE, CONF_TYPE,
DATA_GIBIBYTES,
DATA_MEBIBYTES,
EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_STOP,
PERCENTAGE, PERCENTAGE,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
TEMP_CELSIUS, TEMP_CELSIUS,
UnitOfDataRate, UnitOfDataRate,
UnitOfInformation,
) )
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
@ -76,14 +75,16 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"disk_free": SysMonitorSensorEntityDescription( "disk_free": SysMonitorSensorEntityDescription(
key="disk_free", key="disk_free",
name="Disk free", name="Disk free",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"disk_use": SysMonitorSensorEntityDescription( "disk_use": SysMonitorSensorEntityDescription(
key="disk_use", key="disk_use",
name="Disk use", name="Disk use",
native_unit_of_measurement=DATA_GIBIBYTES, native_unit_of_measurement=UnitOfInformation.GIBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -132,14 +133,16 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"memory_free": SysMonitorSensorEntityDescription( "memory_free": SysMonitorSensorEntityDescription(
key="memory_free", key="memory_free",
name="Memory free", name="Memory free",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"memory_use": SysMonitorSensorEntityDescription( "memory_use": SysMonitorSensorEntityDescription(
key="memory_use", key="memory_use",
name="Memory use", name="Memory use",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:memory", icon="mdi:memory",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
@ -153,7 +156,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"network_in": SysMonitorSensorEntityDescription( "network_in": SysMonitorSensorEntityDescription(
key="network_in", key="network_in",
name="Network in", name="Network in",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:server-network", icon="mdi:server-network",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
mandatory_arg=True, mandatory_arg=True,
@ -161,7 +165,8 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"network_out": SysMonitorSensorEntityDescription( "network_out": SysMonitorSensorEntityDescription(
key="network_out", key="network_out",
name="Network out", name="Network out",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:server-network", icon="mdi:server-network",
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
mandatory_arg=True, mandatory_arg=True,
@ -220,14 +225,16 @@ SENSOR_TYPES: dict[str, SysMonitorSensorEntityDescription] = {
"swap_free": SysMonitorSensorEntityDescription( "swap_free": SysMonitorSensorEntityDescription(
key="swap_free", key="swap_free",
name="Swap free", name="Swap free",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
"swap_use": SysMonitorSensorEntityDescription( "swap_use": SysMonitorSensorEntityDescription(
key="swap_use", key="swap_use",
name="Swap use", name="Swap use",
native_unit_of_measurement=DATA_MEBIBYTES, native_unit_of_measurement=UnitOfInformation.MEBIBYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:harddisk", icon="mdi:harddisk",
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),

View File

@ -13,12 +13,13 @@ from pytautulli import (
) )
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import DATA_KILOBITS, PERCENTAGE from homeassistant.const import PERCENTAGE, UnitOfInformation
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory, EntityDescription from homeassistant.helpers.entity import EntityCategory, EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -101,7 +102,8 @@ SENSOR_TYPES: tuple[TautulliSensorEntityDescription, ...] = (
key="total_bandwidth", key="total_bandwidth",
name="Total bandwidth", name="Total bandwidth",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda home_stats, activity, _: cast(int, activity.total_bandwidth), value_fn=lambda home_stats, activity, _: cast(int, activity.total_bandwidth),
), ),
@ -109,7 +111,8 @@ SENSOR_TYPES: tuple[TautulliSensorEntityDescription, ...] = (
key="lan_bandwidth", key="lan_bandwidth",
name="LAN bandwidth", name="LAN bandwidth",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda home_stats, activity, _: cast(int, activity.lan_bandwidth), value_fn=lambda home_stats, activity, _: cast(int, activity.lan_bandwidth),
@ -118,7 +121,8 @@ SENSOR_TYPES: tuple[TautulliSensorEntityDescription, ...] = (
key="wan_bandwidth", key="wan_bandwidth",
name="WAN bandwidth", name="WAN bandwidth",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=DATA_KILOBITS, native_unit_of_measurement=UnitOfInformation.KILOBITS,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda home_stats, activity, _: cast(int, activity.wan_bandwidth), value_fn=lambda home_stats, activity, _: cast(int, activity.wan_bandwidth),

View File

@ -7,7 +7,7 @@ from datetime import datetime, timedelta
from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DATA_MEGABYTES from homeassistant.const import UnitOfInformation
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 import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -90,8 +90,9 @@ class UniFiBandwidthSensor(UniFiClient, SensorEntity):
DOMAIN = DOMAIN DOMAIN = DOMAIN
_attr_device_class = SensorDeviceClass.DATA_SIZE
_attr_entity_category = EntityCategory.DIAGNOSTIC _attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_native_unit_of_measurement = DATA_MEGABYTES _attr_native_unit_of_measurement = UnitOfInformation.MEGABYTES
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -26,7 +26,6 @@ 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,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
LIGHT_LUX, LIGHT_LUX,
PERCENTAGE, PERCENTAGE,
@ -34,6 +33,7 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
TIME_SECONDS, TIME_SECONDS,
UnitOfDataRate, UnitOfDataRate,
UnitOfInformation,
) )
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
@ -172,7 +172,8 @@ CAMERA_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
ProtectSensorEntityDescription( ProtectSensorEntityDescription(
key="storage_used", key="storage_used",
name="Storage Used", name="Storage Used",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
ufp_value="stats.storage.used", ufp_value="stats.storage.used",
@ -269,7 +270,8 @@ CAMERA_DISABLED_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
ProtectSensorEntityDescription( ProtectSensorEntityDescription(
key="stats_rx", key="stats_rx",
name="Received Data", name="Received Data",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
@ -278,7 +280,8 @@ CAMERA_DISABLED_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
ProtectSensorEntityDescription( ProtectSensorEntityDescription(
key="stats_tx", key="stats_tx",
name="Transferred Data", name="Transferred Data",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,

View File

@ -10,7 +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 DATA_BYTES, TIME_SECONDS, UnitOfDataRate from homeassistant.const import TIME_SECONDS, UnitOfDataRate, UnitOfInformation
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
@ -44,18 +44,20 @@ class UpnpSensorEntityDescription(UpnpEntityDescription, SensorEntityDescription
SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = ( SENSOR_DESCRIPTIONS: tuple[UpnpSensorEntityDescription, ...] = (
UpnpSensorEntityDescription( UpnpSensorEntityDescription(
key=BYTES_RECEIVED, key=BYTES_RECEIVED,
name=f"{DATA_BYTES} received", name=f"{UnitOfInformation.BYTES} received",
icon="mdi:server-network", icon="mdi:server-network",
native_unit_of_measurement=DATA_BYTES, device_class=SensorDeviceClass.DATA_SIZE,
native_unit_of_measurement=UnitOfInformation.BYTES,
format="d", format="d",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
UpnpSensorEntityDescription( UpnpSensorEntityDescription(
key=BYTES_SENT, key=BYTES_SENT,
name=f"{DATA_BYTES} sent", name=f"{UnitOfInformation.BYTES} sent",
icon="mdi:server-network", icon="mdi:server-network",
native_unit_of_measurement=DATA_BYTES, device_class=SensorDeviceClass.DATA_SIZE,
native_unit_of_measurement=UnitOfInformation.BYTES,
format="d", format="d",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,

View File

@ -7,10 +7,11 @@ import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, DATA_GIGABYTES from homeassistant.const import CONF_MONITORED_CONDITIONS, CONF_NAME, UnitOfInformation
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
@ -30,7 +31,8 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key=ATTR_CURRENT_BANDWIDTH_USED, key=ATTR_CURRENT_BANDWIDTH_USED,
name="Current Bandwidth Used", name="Current Bandwidth Used",
native_unit_of_measurement=DATA_GIGABYTES, native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
icon="mdi:chart-histogram", icon="mdi:chart-histogram",
), ),
SensorEntityDescription( SensorEntityDescription(

View File

@ -15,10 +15,10 @@ 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,
PERCENTAGE, PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT, SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
UnitOfElectricCurrent, UnitOfElectricCurrent,
UnitOfInformation,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
@ -85,8 +85,9 @@ SENSORS: tuple[WLEDSensorEntityDescription, ...] = (
key="free_heap", key="free_heap",
name="Free memory", name="Free memory",
icon="mdi:memory", icon="mdi:memory",
native_unit_of_measurement=DATA_BYTES, native_unit_of_measurement=UnitOfInformation.BYTES,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DATA_SIZE,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
value_fn=lambda device: device.info.free_heap, value_fn=lambda device: device.info.free_heap,

View File

@ -74,7 +74,7 @@ async def test_sensors_sensors(
state = hass.states.get("sensor.amazon_fire_internal_storage_free_space") state = hass.states.get("sensor.amazon_fire_internal_storage_free_space")
assert state assert state
assert state.state == "11675.5" assert state.state == "11675.5"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DATA_SIZE
assert ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) state.attributes.get(ATTR_FRIENDLY_NAME)
== "Amazon Fire Internal storage free space" == "Amazon Fire Internal storage free space"
@ -89,7 +89,7 @@ async def test_sensors_sensors(
state = hass.states.get("sensor.amazon_fire_internal_storage_total_space") state = hass.states.get("sensor.amazon_fire_internal_storage_total_space")
assert state assert state
assert state.state == "12938.5" assert state.state == "12938.5"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DATA_SIZE
assert ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) state.attributes.get(ATTR_FRIENDLY_NAME)
== "Amazon Fire Internal storage total space" == "Amazon Fire Internal storage total space"
@ -104,7 +104,7 @@ async def test_sensors_sensors(
state = hass.states.get("sensor.amazon_fire_free_memory") state = hass.states.get("sensor.amazon_fire_free_memory")
assert state assert state
assert state.state == "362.4" assert state.state == "362.4"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DATA_SIZE
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Free memory" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Free memory"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
@ -116,7 +116,7 @@ async def test_sensors_sensors(
state = hass.states.get("sensor.amazon_fire_total_memory") state = hass.states.get("sensor.amazon_fire_total_memory")
assert state assert state
assert state.state == "1440.1" assert state.state == "1440.1"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.DATA_SIZE
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Total memory" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Total memory"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT

View File

@ -25,7 +25,12 @@ async def test_sensors(hass, nzbget_api) -> None:
uptime = now - timedelta(seconds=600) uptime = now - timedelta(seconds=600)
sensors = { sensors = {
"article_cache": ("ArticleCacheMB", "64", DATA_MEGABYTES, None), "article_cache": (
"ArticleCacheMB",
"64",
DATA_MEGABYTES,
SensorDeviceClass.DATA_SIZE,
),
"average_speed": ( "average_speed": (
"AverageDownloadRate", "AverageDownloadRate",
"1.19", "1.19",
@ -39,11 +44,26 @@ async def test_sensors(hass, nzbget_api) -> None:
DATA_RATE_MEGABYTES_PER_SECOND, DATA_RATE_MEGABYTES_PER_SECOND,
SensorDeviceClass.DATA_RATE, SensorDeviceClass.DATA_RATE,
), ),
"size": ("DownloadedSizeMB", "256", DATA_MEGABYTES, None), "size": (
"disk_free": ("FreeDiskSpaceMB", "1024", DATA_MEGABYTES, None), "DownloadedSizeMB",
"256",
DATA_MEGABYTES,
SensorDeviceClass.DATA_SIZE,
),
"disk_free": (
"FreeDiskSpaceMB",
"1024",
DATA_MEGABYTES,
SensorDeviceClass.DATA_SIZE,
),
"post_processing_jobs": ("PostJobCount", "2", "Jobs", None), "post_processing_jobs": ("PostJobCount", "2", "Jobs", None),
"post_processing_paused": ("PostPaused", "False", None, None), "post_processing_paused": ("PostPaused", "False", None, None),
"queue_size": ("RemainingSizeMB", "512", DATA_MEGABYTES, None), "queue_size": (
"RemainingSizeMB",
"512",
DATA_MEGABYTES,
SensorDeviceClass.DATA_SIZE,
),
"uptime": ("UpTimeSec", uptime.isoformat(), None, SensorDeviceClass.TIMESTAMP), "uptime": ("UpTimeSec", uptime.isoformat(), None, SensorDeviceClass.TIMESTAMP),
"speed_limit": ( "speed_limit": (
"DownloadLimit", "DownloadLimit",