2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring the Transmission BitTorrent client API."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
2021-01-08 15:53:47 +00:00
|
|
|
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
|
|
|
|
2021-01-08 15:53:47 +00:00
|
|
|
from transmissionrpc.torrent import Torrent
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-04-20 13:07:26 +00:00
|
|
|
from homeassistant.const import CONF_NAME, DATA_RATE_MEGABYTES_PER_SECOND, STATE_IDLE
|
2019-02-08 17:15:14 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2021-01-08 15:53:47 +00:00
|
|
|
from . import TransmissionClient
|
2020-06-28 11:56:54 +00:00
|
|
|
from .const import (
|
|
|
|
CONF_LIMIT,
|
|
|
|
CONF_ORDER,
|
|
|
|
DOMAIN,
|
|
|
|
STATE_ATTR_TORRENT_INFO,
|
|
|
|
SUPPORTED_ORDER_MODES,
|
|
|
|
)
|
2019-10-28 09:20:59 +00:00
|
|
|
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2019-09-26 09:14:57 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Transmission sensors."""
|
2019-01-29 09:27:26 +00:00
|
|
|
|
2019-10-23 20:09:11 +00:00
|
|
|
tm_client = hass.data[DOMAIN][config_entry.entry_id]
|
2019-09-26 09:14:57 +00:00
|
|
|
name = config_entry.data[CONF_NAME]
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2020-04-20 13:07:26 +00:00
|
|
|
dev = [
|
|
|
|
TransmissionSpeedSensor(tm_client, name, "Down Speed", "download"),
|
|
|
|
TransmissionSpeedSensor(tm_client, name, "Up Speed", "upload"),
|
|
|
|
TransmissionStatusSensor(tm_client, name, "Status"),
|
|
|
|
TransmissionTorrentsSensor(tm_client, name, "Active Torrents", "active"),
|
|
|
|
TransmissionTorrentsSensor(tm_client, name, "Paused Torrents", "paused"),
|
|
|
|
TransmissionTorrentsSensor(tm_client, name, "Total Torrents", "total"),
|
|
|
|
TransmissionTorrentsSensor(tm_client, name, "Completed Torrents", "completed"),
|
|
|
|
TransmissionTorrentsSensor(tm_client, name, "Started Torrents", "started"),
|
|
|
|
]
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
async_add_entities(dev, True)
|
2015-04-06 12:13:04 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class TransmissionSensor(SensorEntity):
|
2020-04-20 13:07:26 +00:00
|
|
|
"""A base class for all Transmission sensors."""
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2020-04-20 13:07:26 +00:00
|
|
|
def __init__(self, tm_client, client_name, sensor_name, sub_type=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-03-18 21:58:19 +00:00
|
|
|
self._tm_client: TransmissionClient = tm_client
|
2020-04-20 13:07:26 +00:00
|
|
|
self._client_name = client_name
|
2019-01-29 09:27:26 +00:00
|
|
|
self._name = sensor_name
|
2020-04-20 13:07:26 +00:00
|
|
|
self._sub_type = sub_type
|
2015-04-06 12:13:04 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2020-04-20 13:07:26 +00:00
|
|
|
return f"{self._client_name} {self._name}"
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2019-10-23 20:09:11 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return the unique id of the entity."""
|
|
|
|
return f"{self._tm_client.api.host}-{self.name}"
|
|
|
|
|
2015-04-06 12:13:04 +00:00
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-04-06 12:13:04 +00:00
|
|
|
return self._state
|
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling requirement for this sensor."""
|
|
|
|
return False
|
|
|
|
|
2018-05-22 08:06:14 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
2019-10-23 20:09:11 +00:00
|
|
|
return self._tm_client.api.available
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
2020-04-20 13:07:26 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def update():
|
|
|
|
"""Update the state."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
self.async_on_remove(
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, self._tm_client.api.signal_update, update
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-08 17:15:14 +00:00
|
|
|
|
|
|
|
|
2020-04-20 13:07:26 +00:00
|
|
|
class TransmissionSpeedSensor(TransmissionSensor):
|
|
|
|
"""Representation of a Transmission speed sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2020-04-20 13:07:26 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return DATA_RATE_MEGABYTES_PER_SECOND
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Transmission and updates the state."""
|
|
|
|
data = self._tm_client.api.data
|
|
|
|
if data:
|
|
|
|
mb_spd = (
|
|
|
|
float(data.downloadSpeed)
|
|
|
|
if self._sub_type == "download"
|
|
|
|
else float(data.uploadSpeed)
|
|
|
|
)
|
|
|
|
mb_spd = mb_spd / 1024 / 1024
|
|
|
|
self._state = round(mb_spd, 2 if mb_spd < 0.1 else 1)
|
|
|
|
|
|
|
|
|
|
|
|
class TransmissionStatusSensor(TransmissionSensor):
|
|
|
|
"""Representation of a Transmission status sensor."""
|
2019-11-26 17:22:12 +00:00
|
|
|
|
2015-04-06 12:13:04 +00:00
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data from Transmission and updates the state."""
|
2020-04-20 13:07:26 +00:00
|
|
|
data = self._tm_client.api.data
|
|
|
|
if data:
|
|
|
|
upload = data.uploadSpeed
|
|
|
|
download = data.downloadSpeed
|
|
|
|
if upload > 0 and download > 0:
|
|
|
|
self._state = "Up/Down"
|
|
|
|
elif upload > 0 and download == 0:
|
|
|
|
self._state = "Seeding"
|
|
|
|
elif upload == 0 and download > 0:
|
|
|
|
self._state = "Downloading"
|
2015-04-06 12:13:04 +00:00
|
|
|
else:
|
2020-04-20 13:07:26 +00:00
|
|
|
self._state = STATE_IDLE
|
|
|
|
else:
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
|
|
|
|
class TransmissionTorrentsSensor(TransmissionSensor):
|
|
|
|
"""Representation of a Transmission torrents sensor."""
|
|
|
|
|
|
|
|
SUBTYPE_MODES = {
|
|
|
|
"started": ("downloading"),
|
|
|
|
"completed": ("seeding"),
|
|
|
|
"paused": ("stopped"),
|
|
|
|
"active": ("seeding", "downloading"),
|
|
|
|
"total": None,
|
|
|
|
}
|
|
|
|
|
|
|
|
@property
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2020-04-20 13:07:26 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return "Torrents"
|
|
|
|
|
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-04-20 13:07:26 +00:00
|
|
|
"""Return the state attributes, if any."""
|
|
|
|
info = _torrents_info(
|
2020-12-10 09:09:08 +00:00
|
|
|
torrents=self._tm_client.api.torrents,
|
|
|
|
order=self._tm_client.config_entry.options[CONF_ORDER],
|
|
|
|
limit=self._tm_client.config_entry.options[CONF_LIMIT],
|
2020-08-27 11:56:20 +00:00
|
|
|
statuses=self.SUBTYPE_MODES[self._sub_type],
|
2020-04-20 13:07:26 +00:00
|
|
|
)
|
2020-06-28 11:56:54 +00:00
|
|
|
return {
|
|
|
|
STATE_ATTR_TORRENT_INFO: info,
|
|
|
|
}
|
2020-04-20 13:07:26 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data from Transmission and updates the state."""
|
2020-06-28 11:56:54 +00:00
|
|
|
torrents = _filter_torrents(
|
|
|
|
self._tm_client.api.torrents, statuses=self.SUBTYPE_MODES[self._sub_type]
|
|
|
|
)
|
|
|
|
self._state = len(torrents)
|
|
|
|
|
|
|
|
|
2021-03-18 13:43:52 +00:00
|
|
|
def _filter_torrents(torrents: list[Torrent], statuses=None) -> list[Torrent]:
|
2020-06-28 11:56:54 +00:00
|
|
|
return [
|
|
|
|
torrent
|
|
|
|
for torrent in torrents
|
|
|
|
if statuses is None or torrent.status in statuses
|
|
|
|
]
|
2020-04-20 13:07:26 +00:00
|
|
|
|
|
|
|
|
2020-12-10 09:09:08 +00:00
|
|
|
def _torrents_info(torrents, order, limit, statuses=None):
|
2020-04-20 13:07:26 +00:00
|
|
|
infos = {}
|
2020-06-28 11:56:54 +00:00
|
|
|
torrents = _filter_torrents(torrents, statuses)
|
|
|
|
torrents = SUPPORTED_ORDER_MODES[order](torrents)
|
2020-12-10 09:09:08 +00:00
|
|
|
for torrent in torrents[:limit]:
|
2020-06-28 11:56:54 +00:00
|
|
|
info = infos[torrent.name] = {
|
|
|
|
"added_date": torrent.addedDate,
|
|
|
|
"percent_done": f"{torrent.percentDone * 100:.2f}",
|
|
|
|
"status": torrent.status,
|
|
|
|
"id": torrent.id,
|
|
|
|
}
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(ValueError):
|
2020-06-28 11:56:54 +00:00
|
|
|
info["eta"] = str(torrent.eta)
|
2020-04-20 13:07:26 +00:00
|
|
|
return infos
|