2019-04-03 15:40:03 +00:00
|
|
|
"""Support for monitoring the Deluge BitTorrent client API."""
|
2021-08-23 19:20:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-10-25 13:13:11 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-05 05:23:56 +00:00
|
|
|
from deluge_client import DelugeRPCClient, FailedToReconnectException
|
2017-10-25 13:13:11 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-23 19:20:35 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2017-10-25 13:13:11 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
2019-12-05 05:23:56 +00:00
|
|
|
CONF_MONITORED_VARIABLES,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_NAME,
|
2019-12-05 05:23:56 +00:00
|
|
|
CONF_PASSWORD,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_PORT,
|
2019-12-05 05:23:56 +00:00
|
|
|
CONF_USERNAME,
|
2020-02-13 16:52:58 +00:00
|
|
|
DATA_RATE_KILOBYTES_PER_SECOND,
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_IDLE,
|
|
|
|
)
|
2022-01-03 18:16:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-04-28 22:16:22 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-05 05:23:56 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:16:42 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
_THROTTLED_REFRESH = None
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Deluge"
|
2017-10-25 13:13:11 +00:00
|
|
|
DEFAULT_PORT = 58846
|
|
|
|
DHT_UPLOAD = 1000
|
|
|
|
DHT_DOWNLOAD = 1000
|
2021-08-23 19:20:35 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="current_status",
|
|
|
|
name="Status",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="download_speed",
|
|
|
|
name="Down Speed",
|
|
|
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="upload_speed",
|
|
|
|
name="Up Speed",
|
|
|
|
native_unit_of_measurement=DATA_RATE_KILOBYTES_PER_SECOND,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
2017-10-25 13:13:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_MONITORED_VARIABLES, default=[]): vol.All(
|
2021-08-23 19:20:35 +00:00
|
|
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:16:42 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-10-25 13:13:11 +00:00
|
|
|
"""Set up the Deluge sensors."""
|
|
|
|
|
2020-04-15 12:10:07 +00:00
|
|
|
name = config[CONF_NAME]
|
|
|
|
host = config[CONF_HOST]
|
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
|
|
|
port = config[CONF_PORT]
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
deluge_api = DelugeRPCClient(host, port, username, password)
|
|
|
|
try:
|
|
|
|
deluge_api.connect()
|
2020-08-28 11:50:32 +00:00
|
|
|
except ConnectionRefusedError as err:
|
2017-10-25 13:13:11 +00:00
|
|
|
_LOGGER.error("Connection to Deluge Daemon failed")
|
2020-08-28 11:50:32 +00:00
|
|
|
raise PlatformNotReady from err
|
2021-08-23 19:20:35 +00:00
|
|
|
monitored_variables = config[CONF_MONITORED_VARIABLES]
|
|
|
|
entities = [
|
|
|
|
DelugeSensor(deluge_api, name, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_variables
|
|
|
|
]
|
2017-10-25 13:13:11 +00:00
|
|
|
|
2021-08-23 19:20:35 +00:00
|
|
|
add_entities(entities)
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:52:29 +00:00
|
|
|
class DelugeSensor(SensorEntity):
|
2017-10-25 13:13:11 +00:00
|
|
|
"""Representation of a Deluge sensor."""
|
|
|
|
|
2021-08-23 19:20:35 +00:00
|
|
|
def __init__(
|
|
|
|
self, deluge_client, client_name, description: SensorEntityDescription
|
|
|
|
):
|
2017-10-25 13:13:11 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-08-23 19:20:35 +00:00
|
|
|
self.entity_description = description
|
2017-10-25 13:13:11 +00:00
|
|
|
self.client = deluge_client
|
|
|
|
self.data = None
|
2018-04-28 22:16:22 +00:00
|
|
|
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_available = False
|
|
|
|
self._attr_name = f"{client_name} {description.name}"
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Deluge and updates the state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-04-28 22:16:22 +00:00
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data = self.client.call(
|
|
|
|
"core.get_session_status",
|
|
|
|
[
|
|
|
|
"upload_rate",
|
|
|
|
"download_rate",
|
|
|
|
"dht_upload_rate",
|
|
|
|
"dht_download_rate",
|
|
|
|
],
|
|
|
|
)
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_available = True
|
2018-04-28 22:16:22 +00:00
|
|
|
except FailedToReconnectException:
|
|
|
|
_LOGGER.error("Connection to Deluge Daemon Lost")
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_available = False
|
2018-04-28 22:16:22 +00:00
|
|
|
return
|
2017-10-25 13:13:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
upload = self.data[b"upload_rate"] - self.data[b"dht_upload_rate"]
|
|
|
|
download = self.data[b"download_rate"] - self.data[b"dht_download_rate"]
|
2017-10-25 13:13:11 +00:00
|
|
|
|
2021-08-23 19:20:35 +00:00
|
|
|
sensor_type = self.entity_description.key
|
|
|
|
if sensor_type == "current_status":
|
2017-10-25 13:13:11 +00:00
|
|
|
if self.data:
|
|
|
|
if upload > 0 and download > 0:
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = "Up/Down"
|
2017-10-25 13:13:11 +00:00
|
|
|
elif upload > 0 and download == 0:
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = "Seeding"
|
2017-10-25 13:13:11 +00:00
|
|
|
elif upload == 0 and download > 0:
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = "Downloading"
|
2017-10-25 13:13:11 +00:00
|
|
|
else:
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = STATE_IDLE
|
2017-10-25 13:13:11 +00:00
|
|
|
else:
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = None
|
2017-10-25 13:13:11 +00:00
|
|
|
|
|
|
|
if self.data:
|
2021-08-23 19:20:35 +00:00
|
|
|
if sensor_type == "download_speed":
|
2017-10-25 13:13:11 +00:00
|
|
|
kb_spd = float(download)
|
|
|
|
kb_spd = kb_spd / 1024
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = round(kb_spd, 2 if kb_spd < 0.1 else 1)
|
|
|
|
elif sensor_type == "upload_speed":
|
2017-10-25 13:13:11 +00:00
|
|
|
kb_spd = float(upload)
|
|
|
|
kb_spd = kb_spd / 1024
|
2021-08-23 19:20:35 +00:00
|
|
|
self._attr_native_value = round(kb_spd, 2 if kb_spd < 0.1 else 1)
|