2019-09-15 00:44:19 +00:00
|
|
|
"""Monitor the NZBGet API."""
|
2021-03-18 12:21:46 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-05-26 19:17:44 +00:00
|
|
|
from datetime import datetime, timedelta
|
2017-11-04 19:04:05 +00:00
|
|
|
import logging
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2021-12-16 20:42:05 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2020-08-29 21:47:00 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-12-13 09:17:56 +00:00
|
|
|
from homeassistant.const import CONF_NAME, UnitOfDataRate, UnitOfInformation
|
2021-04-22 18:23:19 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-08-22 21:23:13 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
2020-08-30 18:59:15 +00:00
|
|
|
from homeassistant.util.dt import utcnow
|
2019-09-15 00:44:19 +00:00
|
|
|
|
2020-08-29 21:47:00 +00:00
|
|
|
from . import NZBGetEntity
|
|
|
|
from .const import DATA_COORDINATOR, DOMAIN
|
|
|
|
from .coordinator import NZBGetDataUpdateCoordinator
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-08-21 18:30:42 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="ArticleCacheMB",
|
|
|
|
name="Article Cache",
|
2022-12-13 09:17:56 +00:00
|
|
|
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="AverageDownloadRate",
|
|
|
|
name="Average Speed",
|
2022-12-10 10:41:44 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_RATE,
|
2023-08-22 21:23:13 +00:00
|
|
|
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
|
|
|
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="DownloadPaused",
|
|
|
|
name="Download Paused",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="DownloadRate",
|
|
|
|
name="Speed",
|
2022-12-10 10:41:44 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_RATE,
|
2023-08-22 21:23:13 +00:00
|
|
|
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
|
|
|
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="DownloadedSizeMB",
|
|
|
|
name="Size",
|
2022-12-13 09:17:56 +00:00
|
|
|
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="FreeDiskSpaceMB",
|
|
|
|
name="Disk Free",
|
2022-12-13 09:17:56 +00:00
|
|
|
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="PostJobCount",
|
|
|
|
name="Post Processing Jobs",
|
|
|
|
native_unit_of_measurement="Jobs",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="PostPaused",
|
|
|
|
name="Post Processing Paused",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="RemainingSizeMB",
|
|
|
|
name="Queue Size",
|
2022-12-13 09:17:56 +00:00
|
|
|
native_unit_of_measurement=UnitOfInformation.MEGABYTES,
|
|
|
|
device_class=SensorDeviceClass.DATA_SIZE,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="UpTimeSec",
|
|
|
|
name="Uptime",
|
2021-12-16 20:42:05 +00:00
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
2021-08-21 18:30:42 +00:00
|
|
|
),
|
2022-08-22 07:02:05 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key="DownloadLimit",
|
|
|
|
name="Speed Limit",
|
2022-12-10 10:41:44 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_RATE,
|
2023-08-22 21:23:13 +00:00
|
|
|
native_unit_of_measurement=UnitOfDataRate.BYTES_PER_SECOND,
|
|
|
|
suggested_unit_of_measurement=UnitOfDataRate.MEGABYTES_PER_SECOND,
|
2022-08-22 07:02:05 +00:00
|
|
|
),
|
2021-08-21 18:30:42 +00:00
|
|
|
)
|
2016-08-16 19:42:43 +00:00
|
|
|
|
|
|
|
|
2020-08-29 21:47:00 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-22 18:23:19 +00:00
|
|
|
hass: HomeAssistant,
|
2020-08-29 21:47:00 +00:00
|
|
|
entry: ConfigEntry,
|
2021-04-29 10:28:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-08-29 21:47:00 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up NZBGet sensor based on a config entry."""
|
|
|
|
coordinator: NZBGetDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
|
|
|
DATA_COORDINATOR
|
|
|
|
]
|
2021-08-21 18:30:42 +00:00
|
|
|
entities = [
|
|
|
|
NZBGetSensor(coordinator, entry.entry_id, entry.data[CONF_NAME], description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
]
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2021-08-21 18:30:42 +00:00
|
|
|
async_add_entities(entities)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class NZBGetSensor(NZBGetEntity, SensorEntity):
|
2016-08-16 19:42:43 +00:00
|
|
|
"""Representation of a NZBGet sensor."""
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2019-09-15 00:44:19 +00:00
|
|
|
def __init__(
|
2020-08-29 21:47:00 +00:00
|
|
|
self,
|
|
|
|
coordinator: NZBGetDataUpdateCoordinator,
|
|
|
|
entry_id: str,
|
|
|
|
entry_name: str,
|
2021-08-21 18:30:42 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-05-20 12:06:44 +00:00
|
|
|
) -> None:
|
2016-04-03 22:57:50 +00:00
|
|
|
"""Initialize a new NZBGet sensor."""
|
2020-08-29 21:47:00 +00:00
|
|
|
super().__init__(
|
|
|
|
coordinator=coordinator,
|
|
|
|
entry_id=entry_id,
|
2021-08-21 18:30:42 +00:00
|
|
|
name=f"{entry_name} {description.name}",
|
2020-08-29 21:47:00 +00:00
|
|
|
)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2022-05-26 19:17:44 +00:00
|
|
|
self.entity_description = description
|
|
|
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
|
|
|
|
2019-09-15 00:44:19 +00:00
|
|
|
@property
|
2023-08-22 21:23:13 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2020-08-29 21:47:00 +00:00
|
|
|
"""Return the state of the sensor."""
|
2021-08-21 18:30:42 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
value = self.coordinator.data["status"].get(sensor_type)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2023-08-22 21:23:13 +00:00
|
|
|
if value is not None and "UpTimeSec" in sensor_type and value > 0:
|
2022-05-26 19:17:44 +00:00
|
|
|
uptime = utcnow().replace(microsecond=0) - timedelta(seconds=value)
|
|
|
|
if not isinstance(self._attr_native_value, datetime) or abs(
|
|
|
|
uptime - self._attr_native_value
|
|
|
|
) > timedelta(seconds=5):
|
2023-08-22 21:23:13 +00:00
|
|
|
return uptime
|
|
|
|
return value
|