2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Radarr."""
|
2021-08-23 20:27:42 +00:00
|
|
|
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
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
from aiopyarr import RootFolder
|
2017-06-05 06:44:24 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-23 20:27:42 +00:00
|
|
|
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
|
2017-06-05 06:44:24 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
2019-12-03 00:11:22 +00:00
|
|
|
CONF_PORT,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SSL,
|
2020-02-13 16:52:58 +00:00
|
|
|
DATA_BYTES,
|
|
|
|
DATA_GIGABYTES,
|
|
|
|
DATA_KILOBYTES,
|
|
|
|
DATA_MEGABYTES,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 18:08:34 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-03 00:11:22 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:08:34 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2022-09-23 02:16:24 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
from . import RadarrEntity
|
|
|
|
from .const import DEFAULT_NAME, DOMAIN
|
|
|
|
from .coordinator import RadarrDataUpdateCoordinator, T
|
2017-06-05 06:44:24 +00:00
|
|
|
|
|
|
|
|
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."""
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
value: Callable[[RadarrDataUpdateCoordinator[T], str], str]
|
2017-06-05 06:44:24 +00:00
|
|
|
|
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",
|
2021-08-23 20:27:42 +00:00
|
|
|
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,
|
2021-08-23 20:27:42 +00:00
|
|
|
),
|
2022-09-23 02:16:24 +00:00
|
|
|
"movie": RadarrSensorEntityDescription(
|
2021-08-23 20:27:42 +00:00
|
|
|
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,
|
2021-08-23 20:27:42 +00:00
|
|
|
),
|
2017-06-05 06:44:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
SENSOR_KEYS: list[str] = [description.key for description in SENSOR_TYPES.values()]
|
|
|
|
|
2020-02-13 16:52:58 +00:00
|
|
|
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(
|
2021-08-23 20:27:42 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
)
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
async def async_setup_platform(
|
2022-01-03 18:08:34 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2022-09-23 02:16:24 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-03 18:08:34 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-06-05 06:44:24 +00:00
|
|
|
"""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
|
2021-08-23 20:27:42 +00:00
|
|
|
]
|
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)
|
2017-06-05 06:44:24 +00:00
|
|
|
|
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
class RadarrSensor(RadarrEntity, SensorEntity):
|
2017-08-06 08:05:37 +00:00
|
|
|
"""Implementation of the Radarr sensor."""
|
2017-06-05 06:44:24 +00:00
|
|
|
|
2022-09-23 02:16:24 +00:00
|
|
|
coordinator: RadarrDataUpdateCoordinator
|
|
|
|
entity_description: RadarrSensorEntityDescription
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: RadarrDataUpdateCoordinator,
|
|
|
|
description: RadarrSensorEntityDescription,
|
|
|
|
folder_name: str = "",
|
|
|
|
) -> None:
|
2017-06-05 06:44:24 +00:00
|
|
|
"""Create Radarr entity."""
|
2022-09-23 02:16:24 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-23 20:27:42 +00:00
|
|
|
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
|
2017-06-05 06:44:24 +00:00
|
|
|
|
|
|
|
@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)
|