core/homeassistant/components/radarr/sensor.py

186 lines
5.7 KiB
Python
Raw Normal View History

"""Support for Radarr."""
from __future__ import annotations
2022-09-23 02:16:24 +00:00
from collections.abc import Callable
from copy import deepcopy
from dataclasses import dataclass
from typing import Generic
2022-09-23 02:16:24 +00:00
from aiopyarr import RootFolder
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
SensorEntity,
SensorEntityDescription,
)
2022-09-23 02:16:24 +00:00
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
CONF_API_KEY,
CONF_HOST,
CONF_MONITORED_CONDITIONS,
CONF_PORT,
2019-07-31 19:25:30 +00:00
CONF_SSL,
DATA_BYTES,
DATA_GIGABYTES,
DATA_KILOBYTES,
DATA_MEGABYTES,
2019-07-31 19:25:30 +00:00
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-09-23 02:16:24 +00:00
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
2022-09-23 02:16:24 +00:00
from . import RadarrEntity
from .const import DEFAULT_NAME, DOMAIN
from .coordinator import RadarrDataUpdateCoordinator, T
2022-09-23 02:16:24 +00:00
def get_space(coordinator: RadarrDataUpdateCoordinator, name: str) -> str:
"""Get space."""
space = [
mount.freeSpace / 1024 ** BYTE_SIZES.index(DATA_GIGABYTES)
for mount in coordinator.data
if name in mount.path
]
return f"{space[0]:.2f}"
def get_modified_description(
description: RadarrSensorEntityDescription, mount: RootFolder
) -> tuple[RadarrSensorEntityDescription, str]:
"""Return modified description and folder name."""
desc = deepcopy(description)
name = mount.path.rsplit("/")[-1].rsplit("\\")[-1]
desc.key = f"{description.key}_{name}"
desc.name = f"{description.name} {name}".capitalize()
return desc, name
@dataclass
class RadarrSensorEntityDescriptionMixIn(Generic[T]):
"""Mixin for required keys."""
2022-09-23 02:16:24 +00:00
value: Callable[[RadarrDataUpdateCoordinator[T], str], str]
2022-09-23 02:16:24 +00:00
@dataclass
class RadarrSensorEntityDescription(
SensorEntityDescription, RadarrSensorEntityDescriptionMixIn[T], Generic[T]
):
"""Class to describe a Radarr sensor."""
description_fn: Callable[
[RadarrSensorEntityDescription, RootFolder],
tuple[RadarrSensorEntityDescription, str] | None,
] = lambda _, __: None
SENSOR_TYPES: dict[str, RadarrSensorEntityDescription] = {
"disk_space": RadarrSensorEntityDescription(
key="disk_space",
name="Disk space",
native_unit_of_measurement=DATA_GIGABYTES,
icon="mdi:harddisk",
2022-09-23 02:16:24 +00:00
value=get_space,
description_fn=get_modified_description,
),
2022-09-23 02:16:24 +00:00
"movie": RadarrSensorEntityDescription(
key="movies",
name="Movies",
native_unit_of_measurement="Movies",
icon="mdi:television",
2022-09-23 02:16:24 +00:00
entity_registry_enabled_default=False,
value=lambda coordinator, _: coordinator.data,
),
}
2022-09-23 02:16:24 +00:00
SENSOR_KEYS: list[str] = [description.key for description in SENSOR_TYPES.values()]
BYTE_SIZES = [
DATA_BYTES,
DATA_KILOBYTES,
DATA_MEGABYTES,
DATA_GIGABYTES,
]
2022-09-23 02:16:24 +00:00
# Deprecated in Home Assistant 2022.10
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
2022-09-23 02:16:24 +00:00
vol.Optional("days", default=1): cv.string,
vol.Optional(CONF_HOST, default="localhost"): cv.string,
vol.Optional("include_paths", default=[]): cv.ensure_list,
2019-07-31 19:25:30 +00:00
vol.Optional(CONF_MONITORED_CONDITIONS, default=["movies"]): vol.All(
cv.ensure_list, [vol.In(SENSOR_KEYS)]
2019-07-31 19:25:30 +00:00
),
2022-09-23 02:16:24 +00:00
vol.Optional(CONF_PORT, default=7878): cv.port,
2019-07-31 19:25:30 +00:00
vol.Optional(CONF_SSL, default=False): cv.boolean,
2022-09-23 02:16:24 +00:00
vol.Optional("unit", default=DATA_GIGABYTES): cv.string,
vol.Optional("urlbase", default=""): cv.string,
2019-07-31 19:25:30 +00:00
}
)
2022-09-23 02:16:24 +00:00
PARALLEL_UPDATES = 1
2022-09-23 02:16:24 +00:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
2022-09-23 02:16:24 +00:00
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Radarr platform."""
2022-09-23 02:16:24 +00:00
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Radarr sensors based on a config entry."""
coordinators: dict[str, RadarrDataUpdateCoordinator] = hass.data[DOMAIN][
entry.entry_id
]
2022-09-23 02:16:24 +00:00
entities = []
for coordinator_type, description in SENSOR_TYPES.items():
coordinator = coordinators[coordinator_type]
if coordinator_type != "disk_space":
entities.append(RadarrSensor(coordinator, description))
else:
entities.extend(
RadarrSensor(coordinator, *get_modified_description(description, mount))
for mount in coordinator.data
if description.description_fn
)
async_add_entities(entities)
2022-09-23 02:16:24 +00:00
class RadarrSensor(RadarrEntity, SensorEntity):
"""Implementation of the Radarr sensor."""
2022-09-23 02:16:24 +00:00
coordinator: RadarrDataUpdateCoordinator
entity_description: RadarrSensorEntityDescription
def __init__(
self,
coordinator: RadarrDataUpdateCoordinator,
description: RadarrSensorEntityDescription,
folder_name: str = "",
) -> None:
"""Create Radarr entity."""
2022-09-23 02:16:24 +00:00
super().__init__(coordinator)
self.entity_description = description
2022-09-23 02:16:24 +00:00
self._attr_name = f"{DEFAULT_NAME} {description.name}"
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}"
self.folder_name = folder_name
@property
2022-09-23 02:16:24 +00:00
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.entity_description.value(self.coordinator, self.folder_name)