Add diagnostic sensor to Radarr (#79044)

* Add diagnostic sensor to Radarr

* coverage
This commit is contained in:
Robert Hillis 2022-09-25 10:11:53 -04:00 committed by GitHub
parent ef30ebd9e1
commit 42bd664305
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 32 deletions

View file

@ -4,13 +4,15 @@ from __future__ import annotations
from collections.abc import Callable
from copy import deepcopy
from dataclasses import dataclass
from datetime import timezone
from typing import Generic
from aiopyarr import RootFolder
from aiopyarr import Diskspace, RootFolder
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
)
@ -28,6 +30,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
@ -36,11 +39,11 @@ from .const import DEFAULT_NAME, DOMAIN
from .coordinator import RadarrDataUpdateCoordinator, T
def get_space(coordinator: RadarrDataUpdateCoordinator, name: str) -> str:
def get_space(data: list[Diskspace], name: str) -> str:
"""Get space."""
space = [
mount.freeSpace / 1024 ** BYTE_SIZES.index(DATA_GIGABYTES)
for mount in coordinator.data
for mount in data
if name in mount.path
]
return f"{space[0]:.2f}"
@ -61,7 +64,7 @@ def get_modified_description(
class RadarrSensorEntityDescriptionMixIn(Generic[T]):
"""Mixin for required keys."""
value: Callable[[RadarrDataUpdateCoordinator[T], str], str]
value_fn: Callable[[T, str], str]
@dataclass
@ -82,7 +85,7 @@ SENSOR_TYPES: dict[str, RadarrSensorEntityDescription] = {
name="Disk space",
native_unit_of_measurement=DATA_GIGABYTES,
icon="mdi:harddisk",
value=get_space,
value_fn=get_space,
description_fn=get_modified_description,
),
"movie": RadarrSensorEntityDescription(
@ -91,7 +94,15 @@ SENSOR_TYPES: dict[str, RadarrSensorEntityDescription] = {
native_unit_of_measurement="Movies",
icon="mdi:television",
entity_registry_enabled_default=False,
value=lambda coordinator, _: coordinator.data,
value_fn=lambda data, _: data,
),
"status": RadarrSensorEntityDescription(
key="start_time",
name="Start time",
device_class=SensorDeviceClass.TIMESTAMP,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda data, _: data.startTime.replace(tzinfo=timezone.utc),
),
}
@ -182,4 +193,4 @@ class RadarrSensor(RadarrEntity, SensorEntity):
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.entity_description.value(self.coordinator, self.folder_name)
return self.entity_description.value_fn(self.coordinator.data, self.folder_name)