2019-04-03 15:40:03 +00:00
|
|
|
"""Support for monitoring the qBittorrent API."""
|
2021-08-16 20:52:47 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-11-29 19:40:26 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-02 23:57:47 +00:00
|
|
|
from qbittorrent.client import Client, LoginRequired
|
2018-11-29 19:40:26 +00:00
|
|
|
from requests.exceptions import RequestException
|
2019-12-02 23:57:47 +00:00
|
|
|
import voluptuous as vol
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2022-12-10 10:41:44 +00:00
|
|
|
SensorDeviceClass,
|
2021-08-16 20:52:47 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2023-02-27 10:52:07 +00:00
|
|
|
SensorStateClass,
|
2021-08-16 20:52:47 +00:00
|
|
|
)
|
2018-11-29 19:40:26 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_URL,
|
|
|
|
CONF_USERNAME,
|
|
|
|
STATE_IDLE,
|
2022-12-10 10:41:44 +00:00
|
|
|
UnitOfDataRate,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-11-29 19:40:26 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-02 23:57:47 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:10:57 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2023-03-24 13:20:37 +00:00
|
|
|
from .const import DEFAULT_NAME
|
|
|
|
|
2018-11-29 19:40:26 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPE_CURRENT_STATUS = "current_status"
|
|
|
|
SENSOR_TYPE_DOWNLOAD_SPEED = "download_speed"
|
|
|
|
SENSOR_TYPE_UPLOAD_SPEED = "upload_speed"
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_CURRENT_STATUS,
|
|
|
|
name="Status",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_DOWNLOAD_SPEED,
|
|
|
|
name="Down Speed",
|
2023-03-24 06:23:05 +00:00
|
|
|
icon="mdi:cloud-download",
|
2022-12-10 10:41:44 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_RATE,
|
|
|
|
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
2023-02-27 10:52:07 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-16 20:52:47 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_UPLOAD_SPEED,
|
|
|
|
name="Up Speed",
|
2023-03-24 06:23:05 +00:00
|
|
|
icon="mdi:cloud-upload",
|
2022-12-10 10:41:44 +00:00
|
|
|
device_class=SensorDeviceClass.DATA_RATE,
|
|
|
|
native_unit_of_measurement=UnitOfDataRate.KIBIBYTES_PER_SECOND,
|
2023-02-27 10:52:07 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-16 20:52:47 +00:00
|
|
|
),
|
|
|
|
)
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_URL): cv.url,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2018-11-29 19:40:26 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:10:57 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-11-29 19:40:26 +00:00
|
|
|
"""Set up the qBittorrent sensors."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
client = Client(config[CONF_URL])
|
|
|
|
client.login(config[CONF_USERNAME], config[CONF_PASSWORD])
|
|
|
|
except LoginRequired:
|
|
|
|
_LOGGER.error("Invalid authentication")
|
|
|
|
return
|
2020-08-28 11:50:32 +00:00
|
|
|
except RequestException as err:
|
2018-11-29 19:40:26 +00:00
|
|
|
_LOGGER.error("Connection failed")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise PlatformNotReady from err
|
2018-11-29 19:40:26 +00:00
|
|
|
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
entities = [
|
|
|
|
QBittorrentSensor(description, client, name, LoginRequired)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
]
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
add_entities(entities, True)
|
2018-11-29 19:40:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_speed(speed):
|
|
|
|
"""Return a bytes/s measurement as a human readable string."""
|
|
|
|
kb_spd = float(speed) / 1024
|
|
|
|
return round(kb_spd, 2 if kb_spd < 0.1 else 1)
|
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class QBittorrentSensor(SensorEntity):
|
2018-11-29 19:40:26 +00:00
|
|
|
"""Representation of an qBittorrent sensor."""
|
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
qbittorrent_client,
|
|
|
|
client_name,
|
|
|
|
exception,
|
2023-02-06 10:37:25 +00:00
|
|
|
) -> None:
|
2018-11-29 19:40:26 +00:00
|
|
|
"""Initialize the qBittorrent sensor."""
|
2021-08-16 20:52:47 +00:00
|
|
|
self.entity_description = description
|
2018-11-29 19:40:26 +00:00
|
|
|
self.client = qbittorrent_client
|
|
|
|
self._exception = exception
|
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_name = f"{client_name} {description.name}"
|
|
|
|
self._attr_available = False
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2022-09-06 07:43:49 +00:00
|
|
|
def update(self) -> None:
|
2018-11-29 19:40:26 +00:00
|
|
|
"""Get the latest data from qBittorrent and updates the state."""
|
|
|
|
try:
|
2019-12-28 09:09:42 +00:00
|
|
|
data = self.client.sync_main_data()
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_available = True
|
2018-11-29 19:40:26 +00:00
|
|
|
except RequestException:
|
|
|
|
_LOGGER.error("Connection lost")
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_available = False
|
2020-05-08 12:56:22 +00:00
|
|
|
return
|
2018-11-29 19:40:26 +00:00
|
|
|
except self._exception:
|
|
|
|
_LOGGER.error("Invalid authentication")
|
|
|
|
return
|
|
|
|
|
|
|
|
if data is None:
|
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
download = data["server_state"]["dl_info_speed"]
|
|
|
|
upload = data["server_state"]["up_info_speed"]
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == SENSOR_TYPE_CURRENT_STATUS:
|
2018-11-29 19:40:26 +00:00
|
|
|
if upload > 0 and download > 0:
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_native_value = "up_down"
|
2018-11-29 19:40:26 +00:00
|
|
|
elif upload > 0 and download == 0:
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_native_value = "seeding"
|
2018-11-29 19:40:26 +00:00
|
|
|
elif upload == 0 and download > 0:
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_native_value = "downloading"
|
2018-11-29 19:40:26 +00:00
|
|
|
else:
|
2021-08-16 20:52:47 +00:00
|
|
|
self._attr_native_value = STATE_IDLE
|
2018-11-29 19:40:26 +00:00
|
|
|
|
2021-08-16 20:52:47 +00:00
|
|
|
elif sensor_type == SENSOR_TYPE_DOWNLOAD_SPEED:
|
|
|
|
self._attr_native_value = format_speed(download)
|
|
|
|
elif sensor_type == SENSOR_TYPE_UPLOAD_SPEED:
|
|
|
|
self._attr_native_value = format_speed(upload)
|