core/homeassistant/components/sabnzbd/sensor.py

135 lines
4.5 KiB
Python

"""Support for monitoring an SABnzbd NZB client."""
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfDataRate, UnitOfInformation
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import SabnzbdConfigEntry
from .entity import SabnzbdEntity
@dataclass(frozen=True, kw_only=True)
class SabnzbdSensorEntityDescription(SensorEntityDescription):
"""Describes Sabnzbd sensor entity."""
key: str
SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
SabnzbdSensorEntityDescription(
key="status",
translation_key="status",
),
SabnzbdSensorEntityDescription(
key="kbpersec",
translation_key="speed",
device_class=SensorDeviceClass.DATA_RATE,
native_unit_of_measurement=UnitOfDataRate.KILOBYTES_PER_SECOND,
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
suggested_display_precision=1,
state_class=SensorStateClass.MEASUREMENT,
),
SabnzbdSensorEntityDescription(
key="mb",
translation_key="queue",
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
),
SabnzbdSensorEntityDescription(
key="mbleft",
translation_key="left",
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
),
SabnzbdSensorEntityDescription(
key="diskspacetotal1",
translation_key="total_disk_space",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
),
SabnzbdSensorEntityDescription(
key="diskspace1",
translation_key="free_disk_space",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.MEASUREMENT,
),
SabnzbdSensorEntityDescription(
key="noofslots_total",
translation_key="queue_count",
state_class=SensorStateClass.TOTAL,
suggested_display_precision=2,
),
SabnzbdSensorEntityDescription(
key="day_size",
translation_key="daily_total",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING,
suggested_display_precision=2,
),
SabnzbdSensorEntityDescription(
key="week_size",
translation_key="weekly_total",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING,
suggested_display_precision=2,
),
SabnzbdSensorEntityDescription(
key="month_size",
translation_key="monthly_total",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
entity_registry_enabled_default=False,
state_class=SensorStateClass.TOTAL_INCREASING,
suggested_display_precision=2,
),
SabnzbdSensorEntityDescription(
key="total_size",
translation_key="overall_total",
native_unit_of_measurement=UnitOfInformation.GIGABYTES,
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING,
suggested_display_precision=2,
),
)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: SabnzbdConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Sabnzbd sensor entry."""
coordinator = config_entry.runtime_data
async_add_entities([SabnzbdSensor(coordinator, sensor) for sensor in SENSOR_TYPES])
class SabnzbdSensor(SabnzbdEntity, SensorEntity):
"""Representation of an SABnzbd sensor."""
entity_description: SabnzbdSensorEntityDescription
@property
def native_value(self) -> StateType:
"""Return latest sensor data."""
return self.coordinator.data.get(self.entity_description.key)