2019-02-13 20:21:14 +00:00
|
|
|
"""Support for setting the Transmission BitTorrent client Turtle Mode."""
|
2015-11-29 21:49:05 +00:00
|
|
|
import logging
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-09-26 09:14:57 +00:00
|
|
|
from homeassistant.const import CONF_NAME, STATE_OFF, STATE_ON
|
2019-02-08 17:15:14 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2015-05-30 13:34:23 +00:00
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
|
2019-10-23 20:09:11 +00:00
|
|
|
from .const import DOMAIN, SWITCH_TYPES
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGING = logging.getLogger(__name__)
|
|
|
|
|
2015-05-30 13:34:23 +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 switch."""
|
2015-05-30 13:34:23 +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-05-30 13:34:23 +00:00
|
|
|
|
2019-09-26 09:14:57 +00:00
|
|
|
dev = []
|
|
|
|
for switch_type, switch_name in SWITCH_TYPES.items():
|
2019-10-23 20:09:11 +00:00
|
|
|
dev.append(TransmissionSwitch(switch_type, switch_name, tm_client, name))
|
2019-09-26 09:14:57 +00:00
|
|
|
|
|
|
|
async_add_entities(dev, True)
|
2015-05-30 13:34:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TransmissionSwitch(ToggleEntity):
|
2016-08-19 07:18:45 +00:00
|
|
|
"""Representation of a Transmission switch."""
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-10-23 20:09:11 +00:00
|
|
|
def __init__(self, switch_type, switch_name, tm_client, name):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the Transmission switch."""
|
2019-09-26 09:14:57 +00:00
|
|
|
self._name = switch_name
|
|
|
|
self.client_name = name
|
|
|
|
self.type = switch_type
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client = tm_client
|
2015-05-30 13:34:23 +00:00
|
|
|
self._state = STATE_OFF
|
2019-09-26 09:14:57 +00:00
|
|
|
self._data = None
|
2019-11-26 17:22:12 +00:00
|
|
|
self.unsub_update = None
|
2015-05-30 13:34:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch."""
|
2019-09-26 09:14:57 +00:00
|
|
|
return f"{self.client_name} {self._name}"
|
2015-05-30 13:34:23 +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-05-30 13:34:23 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state of the device."""
|
2015-05-30 13:34:23 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Poll for status regularly."""
|
2019-02-08 17:15:14 +00:00
|
|
|
return False
|
2015-05-30 13:34:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if device is on."""
|
2015-05-30 13:34:23 +00:00
|
|
|
return self._state == STATE_ON
|
|
|
|
|
2019-09-26 09:14:57 +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
|
2019-09-26 09:14:57 +00:00
|
|
|
|
2015-05-30 13:34:23 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device on."""
|
2019-09-26 09:14:57 +00:00
|
|
|
if self.type == "on_off":
|
|
|
|
_LOGGING.debug("Starting all torrents")
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client.api.start_torrents()
|
2019-09-26 09:14:57 +00:00
|
|
|
elif self.type == "turtle_mode":
|
|
|
|
_LOGGING.debug("Turning Turtle Mode of Transmission on")
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client.api.set_alt_speed_enabled(True)
|
|
|
|
self._tm_client.api.update()
|
2015-05-30 13:34:23 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device off."""
|
2019-09-26 09:14:57 +00:00
|
|
|
if self.type == "on_off":
|
2020-01-31 16:33:00 +00:00
|
|
|
_LOGGING.debug("Stopping all torrents")
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client.api.stop_torrents()
|
2019-09-26 09:14:57 +00:00
|
|
|
if self.type == "turtle_mode":
|
|
|
|
_LOGGING.debug("Turning Turtle Mode of Transmission off")
|
2019-10-23 20:09:11 +00:00
|
|
|
self._tm_client.api.set_alt_speed_enabled(False)
|
|
|
|
self._tm_client.api.update()
|
2015-05-30 13:34:23 +00:00
|
|
|
|
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-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-05-30 13:34:23 +00:00
|
|
|
def update(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Get the latest data from Transmission and updates the state."""
|
2019-09-26 09:14:57 +00:00
|
|
|
active = None
|
|
|
|
if self.type == "on_off":
|
2019-10-23 20:09:11 +00:00
|
|
|
self._data = self._tm_client.api.data
|
2019-09-26 09:14:57 +00:00
|
|
|
if self._data:
|
|
|
|
active = self._data.activeTorrentCount > 0
|
|
|
|
|
|
|
|
elif self.type == "turtle_mode":
|
2019-10-23 20:09:11 +00:00
|
|
|
active = self._tm_client.api.get_alt_speed_enabled()
|
2019-02-08 17:15:14 +00:00
|
|
|
|
|
|
|
if active is None:
|
|
|
|
return
|
|
|
|
|
2015-05-31 12:00:30 +00:00
|
|
|
self._state = STATE_ON if active else STATE_OFF
|