2015-05-30 13:34:23 +00:00
|
|
|
"""
|
2016-03-08 12:35:39 +00:00
|
|
|
Support for setting the Transmission BitTorrent client Turtle Mode.
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2015-10-21 08:56:32 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/switch.transmission/
|
2015-05-30 13:34:23 +00:00
|
|
|
"""
|
2015-11-29 21:49:05 +00:00
|
|
|
import logging
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-01-29 09:27:26 +00:00
|
|
|
from homeassistant.components.transmission import (
|
2019-02-08 17:15:14 +00:00
|
|
|
DATA_TRANSMISSION, DATA_UPDATED)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.const import (
|
2019-01-29 09:27:26 +00:00
|
|
|
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-01-29 09:27:26 +00:00
|
|
|
DEPENDENCIES = ['transmission']
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGING = logging.getLogger(__name__)
|
|
|
|
|
2016-08-19 07:18:45 +00:00
|
|
|
DEFAULT_NAME = 'Transmission Turtle Mode'
|
|
|
|
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass,
|
|
|
|
config,
|
|
|
|
async_add_entities,
|
|
|
|
discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Transmission switch."""
|
2019-01-29 09:27:26 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-01-29 09:27:26 +00:00
|
|
|
component_name = DATA_TRANSMISSION
|
|
|
|
transmission_api = hass.data[component_name]
|
|
|
|
name = discovery_info['client_name']
|
2015-05-30 13:34:23 +00:00
|
|
|
|
2019-02-08 17:15:14 +00:00
|
|
|
async_add_entities([TransmissionSwitch(transmission_api, name)], 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
|
|
|
|
|
|
|
def __init__(self, transmission_client, name):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Initialize the Transmission switch."""
|
2015-05-30 13:34:23 +00:00
|
|
|
self._name = name
|
|
|
|
self.transmission_client = transmission_client
|
|
|
|
self._state = STATE_OFF
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the name of the switch."""
|
2015-05-30 13:34:23 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the device on."""
|
2016-08-19 07:18:45 +00:00
|
|
|
_LOGGING.debug("Turning Turtle Mode of Transmission on")
|
2019-01-29 09:27:26 +00:00
|
|
|
self.transmission_client.set_alt_speed_enabled(True)
|
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."""
|
2016-08-19 07:18:45 +00:00
|
|
|
_LOGGING.debug("Turning Turtle Mode of Transmission off")
|
2019-01-29 09:27:26 +00:00
|
|
|
self.transmission_client.set_alt_speed_enabled(False)
|
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."""
|
|
|
|
async_dispatcher_connect(
|
|
|
|
self.hass, DATA_UPDATED, self._schedule_immediate_update
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _schedule_immediate_update(self):
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
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-01-29 09:27:26 +00:00
|
|
|
active = self.transmission_client.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
|