2019-02-13 20:21:14 +00:00
|
|
|
"""Support for monitoring the Transmission BitTorrent client API."""
|
2018-05-22 08:06:14 +00:00
|
|
|
import logging
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2019-09-26 09:14:57 +00:00
|
|
|
from homeassistant.const import CONF_NAME, 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
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2019-10-28 09:20:59 +00:00
|
|
|
from .const import DOMAIN, SENSOR_TYPES, STATE_ATTR_TORRENT_INFO
|
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2019-09-26 09:14:57 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Import config from configuration.yaml."""
|
|
|
|
pass
|
2019-02-04 20:08:38 +00:00
|
|
|
|
2015-04-06 12:13:04 +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
|
|
|
|
|
|
|
dev = []
|
2019-09-26 09:14:57 +00:00
|
|
|
for sensor_type in SENSOR_TYPES:
|
2019-07-31 19:25:30 +00:00
|
|
|
dev.append(
|
|
|
|
TransmissionSensor(
|
|
|
|
sensor_type,
|
2019-10-23 20:09:11 +00:00
|
|
|
tm_client,
|
2019-07-31 19:25:30 +00:00
|
|
|
name,
|
|
|
|
SENSOR_TYPES[sensor_type][0],
|
|
|
|
SENSOR_TYPES[sensor_type][1],
|
|
|
|
)
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
class TransmissionSensor(Entity):
|
2016-08-19 07:18:45 +00:00
|
|
|
"""Representation of a Transmission sensor."""
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2019-01-29 09:27:26 +00:00
|
|
|
def __init__(
|
2019-10-23 20:09:11 +00:00
|
|
|
self, sensor_type, tm_client, client_name, sensor_name, unit_of_measurement
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2019-01-29 09:27:26 +00:00
|
|
|
self._name = sensor_name
|
2015-04-06 12:13:04 +00:00
|
|
|
self._state = None
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client = tm_client
|
2019-01-29 09:27:26 +00:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2018-05-22 08:06:14 +00:00
|
|
|
self._data = None
|
|
|
|
self.client_name = client_name
|
|
|
|
self.type = sensor_type
|
2019-11-26 17:22:12 +00:00
|
|
|
self.unsub_update = None
|
2015-04-06 12:13:04 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 19:12:51 +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
|
|
|
|
def state(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
|
|
|
|
|
2015-04-06 12:13:04 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2015-04-06 12:13:04 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
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-10-28 09:20:59 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes, if any."""
|
|
|
|
if self._tm_client.api.started_torrent_dict and self.type == "started_torrents":
|
|
|
|
return {STATE_ATTR_TORRENT_INFO: self._tm_client.api.started_torrent_dict}
|
|
|
|
return None
|
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Handle entity which will be added."""
|
2019-11-26 17:22:12 +00:00
|
|
|
self.unsub_update = async_dispatcher_connect(
|
2019-10-23 20:09:11 +00:00
|
|
|
self.hass,
|
2019-11-26 17:22:12 +00:00
|
|
|
self._tm_client.api.signal_update,
|
2019-10-23 20:09:11 +00:00
|
|
|
self._schedule_immediate_update,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-02-08 17:15:14 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _schedule_immediate_update(self):
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2019-11-26 17:22:12 +00:00
|
|
|
async def will_remove_from_hass(self):
|
|
|
|
"""Unsubscribe from update dispatcher."""
|
|
|
|
if self.unsub_update:
|
|
|
|
self.unsub_update()
|
|
|
|
self.unsub_update = None
|
|
|
|
|
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."""
|
2019-10-23 20:09:11 +00:00
|
|
|
self._data = self._tm_client.api.data
|
2016-08-19 07:18:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "completed_torrents":
|
2019-10-23 20:09:11 +00:00
|
|
|
self._state = self._tm_client.api.get_completed_torrent_count()
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "started_torrents":
|
2019-10-23 20:09:11 +00:00
|
|
|
self._state = self._tm_client.api.get_started_torrent_count()
|
2019-01-29 09:27:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "current_status":
|
2018-05-22 08:06:14 +00:00
|
|
|
if self._data:
|
|
|
|
upload = self._data.uploadSpeed
|
|
|
|
download = self._data.downloadSpeed
|
2015-04-06 12:13:04 +00:00
|
|
|
if upload > 0 and download > 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = "Up/Down"
|
2015-04-06 12:13:04 +00:00
|
|
|
elif upload > 0 and download == 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = "Seeding"
|
2015-04-06 12:13:04 +00:00
|
|
|
elif upload == 0 and download > 0:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = "Downloading"
|
2015-04-06 12:13:04 +00:00
|
|
|
else:
|
2016-08-19 07:18:45 +00:00
|
|
|
self._state = STATE_IDLE
|
2015-04-06 12:13:04 +00:00
|
|
|
else:
|
2017-10-17 20:45:37 +00:00
|
|
|
self._state = None
|
2015-04-06 12:13:04 +00:00
|
|
|
|
2018-05-22 08:06:14 +00:00
|
|
|
if self._data:
|
2019-07-31 19:25:30 +00:00
|
|
|
if self.type == "download_speed":
|
2018-05-22 08:06:14 +00:00
|
|
|
mb_spd = float(self._data.downloadSpeed)
|
2015-04-07 13:21:41 +00:00
|
|
|
mb_spd = mb_spd / 1024 / 1024
|
2015-04-06 12:13:04 +00:00
|
|
|
self._state = round(mb_spd, 2 if mb_spd < 0.1 else 1)
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "upload_speed":
|
2018-05-22 08:06:14 +00:00
|
|
|
mb_spd = float(self._data.uploadSpeed)
|
2015-04-07 13:21:41 +00:00
|
|
|
mb_spd = mb_spd / 1024 / 1024
|
2015-04-06 12:13:04 +00:00
|
|
|
self._state = round(mb_spd, 2 if mb_spd < 0.1 else 1)
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "active_torrents":
|
2018-05-22 08:06:14 +00:00
|
|
|
self._state = self._data.activeTorrentCount
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "paused_torrents":
|
2018-05-22 08:06:14 +00:00
|
|
|
self._state = self._data.pausedTorrentCount
|
2019-07-31 19:25:30 +00:00
|
|
|
elif self.type == "total_torrents":
|
2018-05-22 08:06:14 +00:00
|
|
|
self._state = self._data.torrentCount
|