2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring an SABnzbd NZB client."""
|
2022-01-03 18:06:08 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-04-27 06:09:10 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
2018-05-07 07:35:55 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2022-04-27 06:09:10 +00:00
|
|
|
|
2022-04-28 20:25:17 +00:00
|
|
|
from . import DOMAIN, SIGNAL_SABNZBD_UPDATED
|
2022-04-27 06:09:10 +00:00
|
|
|
from ...config_entries import ConfigEntry
|
|
|
|
from ...const import DATA_GIGABYTES, DATA_MEGABYTES, DATA_RATE_MEGABYTES_PER_SECOND
|
|
|
|
from ...core import HomeAssistant
|
2022-05-05 21:34:30 +00:00
|
|
|
from ...helpers.device_registry import DeviceEntryType
|
|
|
|
from ...helpers.entity import DeviceInfo
|
2022-04-27 06:09:10 +00:00
|
|
|
from ...helpers.entity_platform import AddEntitiesCallback
|
2022-05-05 21:34:30 +00:00
|
|
|
from .const import DEFAULT_NAME, KEY_API_DATA, KEY_NAME
|
2022-04-27 06:09:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SabnzbdRequiredKeysMixin:
|
|
|
|
"""Mixin for required keys."""
|
|
|
|
|
|
|
|
key: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SabnzbdSensorEntityDescription(SensorEntityDescription, SabnzbdRequiredKeysMixin):
|
|
|
|
"""Describes Sabnzbd sensor entity."""
|
|
|
|
|
|
|
|
|
2022-05-07 20:16:51 +00:00
|
|
|
SPEED_KEY = "kbpersec"
|
|
|
|
|
2022-04-27 06:09:10 +00:00
|
|
|
SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="status",
|
|
|
|
name="Status",
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
2022-05-07 20:16:51 +00:00
|
|
|
key=SPEED_KEY,
|
2022-04-27 06:09:10 +00:00
|
|
|
name="Speed",
|
|
|
|
native_unit_of_measurement=DATA_RATE_MEGABYTES_PER_SECOND,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="mb",
|
|
|
|
name="Queue",
|
|
|
|
native_unit_of_measurement=DATA_MEGABYTES,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="mbleft",
|
|
|
|
name="Left",
|
|
|
|
native_unit_of_measurement=DATA_MEGABYTES,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="diskspacetotal1",
|
|
|
|
name="Disk",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="diskspace1",
|
|
|
|
name="Disk Free",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="noofslots_total",
|
|
|
|
name="Queue Count",
|
|
|
|
state_class=SensorStateClass.TOTAL,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="day_size",
|
|
|
|
name="Daily Total",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="week_size",
|
|
|
|
name="Weekly Total",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="month_size",
|
|
|
|
name="Monthly Total",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
|
|
|
SabnzbdSensorEntityDescription(
|
|
|
|
key="total_size",
|
|
|
|
name="Total",
|
|
|
|
native_unit_of_measurement=DATA_GIGABYTES,
|
|
|
|
state_class=SensorStateClass.TOTAL_INCREASING,
|
|
|
|
),
|
2021-09-06 07:54:07 +00:00
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-05-09 10:34:16 +00:00
|
|
|
OLD_SENSOR_KEYS = [
|
|
|
|
"current_status",
|
|
|
|
"speed",
|
|
|
|
"queue_size",
|
|
|
|
"queue_remaining",
|
|
|
|
"disk_size",
|
|
|
|
"disk_free",
|
|
|
|
"queue_count",
|
|
|
|
"day_size",
|
|
|
|
"week_size",
|
|
|
|
"month_size",
|
|
|
|
"total_size",
|
|
|
|
]
|
2022-04-27 06:09:10 +00:00
|
|
|
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2022-04-27 06:09:10 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 18:06:08 +00:00
|
|
|
hass: HomeAssistant,
|
2022-04-27 06:09:10 +00:00
|
|
|
config_entry: ConfigEntry,
|
2022-01-03 18:06:08 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-04-27 06:09:10 +00:00
|
|
|
"""Set up a Sabnzbd sensor entry."""
|
|
|
|
|
2022-05-09 07:27:23 +00:00
|
|
|
entry_id = config_entry.entry_id
|
|
|
|
|
|
|
|
sab_api_data = hass.data[DOMAIN][entry_id][KEY_API_DATA]
|
|
|
|
client_name = hass.data[DOMAIN][entry_id][KEY_NAME]
|
2017-08-04 21:24:55 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async_add_entities(
|
2022-05-09 07:27:23 +00:00
|
|
|
[
|
|
|
|
SabnzbdSensor(sab_api_data, client_name, sensor, entry_id)
|
|
|
|
for sensor in SENSOR_TYPES
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2017-08-04 21:24:55 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SabnzbdSensor(SensorEntity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of an SABnzbd sensor."""
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2021-09-06 07:54:07 +00:00
|
|
|
entity_description: SabnzbdSensorEntityDescription
|
|
|
|
_attr_should_poll = False
|
|
|
|
|
|
|
|
def __init__(
|
2022-05-09 07:27:23 +00:00
|
|
|
self,
|
|
|
|
sabnzbd_api_data,
|
|
|
|
client_name,
|
|
|
|
description: SabnzbdSensorEntityDescription,
|
|
|
|
entry_id,
|
2021-09-06 07:54:07 +00:00
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2022-05-09 07:27:23 +00:00
|
|
|
|
|
|
|
self._attr_unique_id = f"{entry_id}_{description.key}"
|
2021-09-06 07:54:07 +00:00
|
|
|
self.entity_description = description
|
2018-05-07 07:35:55 +00:00
|
|
|
self._sabnzbd_api = sabnzbd_api_data
|
2021-09-06 07:54:07 +00:00
|
|
|
self._attr_name = f"{client_name} {description.name}"
|
2022-05-05 21:34:30 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
2022-05-09 07:27:23 +00:00
|
|
|
identifiers={(DOMAIN, entry_id)},
|
2022-05-05 21:34:30 +00:00
|
|
|
name=DEFAULT_NAME,
|
|
|
|
)
|
2015-03-08 18:28:12 +00:00
|
|
|
|
2018-05-07 07:35:55 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Call when entity about to be added to hass."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, SIGNAL_SABNZBD_UPDATED, self.update_state
|
|
|
|
)
|
|
|
|
)
|
2018-05-07 07:35:55 +00:00
|
|
|
|
|
|
|
def update_state(self, args):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data and updates the states."""
|
2021-09-06 07:54:07 +00:00
|
|
|
self._attr_native_value = self._sabnzbd_api.get_queue_field(
|
2022-04-27 06:09:10 +00:00
|
|
|
self.entity_description.key
|
2021-09-06 07:54:07 +00:00
|
|
|
)
|
2018-05-07 07:35:55 +00:00
|
|
|
|
2022-05-09 07:27:23 +00:00
|
|
|
if self._attr_native_value is not None:
|
|
|
|
if self.entity_description.key == SPEED_KEY:
|
|
|
|
self._attr_native_value = round(
|
|
|
|
float(self._attr_native_value) / 1024, 1
|
|
|
|
)
|
|
|
|
elif "size" in self.entity_description.key:
|
|
|
|
self._attr_native_value = round(float(self._attr_native_value), 2)
|
2018-05-07 07:35:55 +00:00
|
|
|
self.schedule_update_ha_state()
|