core/homeassistant/components/switch/transmission.py

100 lines
3.0 KiB
Python
Raw Normal View History

"""
2016-03-08 12:35:39 +00:00
Support for setting the Transmission BitTorrent client Turtle Mode.
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-11-29 21:49:05 +00:00
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PORT, CONF_PASSWORD, CONF_USERNAME, STATE_OFF,
STATE_ON)
from homeassistant.helpers.entity import ToggleEntity
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['transmissionrpc==0.11']
_LOGGING = logging.getLogger(__name__)
DEFAULT_NAME = 'Transmission Turtle Mode'
DEFAULT_PORT = 9091
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_USERNAME): cv.string,
})
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Transmission switch."""
2015-11-17 08:18:42 +00:00
import transmissionrpc
from transmissionrpc.error import TransmissionError
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
port = config.get(CONF_PORT)
transmission_api = transmissionrpc.Client(
host, port=port, user=username, password=password)
try:
transmission_api.session_stats()
except TransmissionError:
_LOGGING.error("Connection to Transmission API failed")
return False
add_devices([TransmissionSwitch(transmission_api, name)])
class TransmissionSwitch(ToggleEntity):
"""Representation of a Transmission switch."""
def __init__(self, transmission_client, name):
2016-03-08 12:35:39 +00:00
"""Initialize the Transmission switch."""
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."""
return self._name
@property
def state(self):
2016-03-08 12:35:39 +00:00
"""Return the state of the device."""
return self._state
@property
def should_poll(self):
2016-03-08 12:35:39 +00:00
"""Poll for status regularly."""
return True
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
return self._state == STATE_ON
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
_LOGGING.debug("Turning Turtle Mode of Transmission on")
self.transmission_client.set_session(alt_speed_enabled=True)
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
_LOGGING.debug("Turning Turtle Mode of Transmission off")
self.transmission_client.set_session(alt_speed_enabled=False)
def update(self):
2016-03-08 12:35:39 +00:00
"""Get the latest data from Transmission and updates the state."""
active = self.transmission_client.get_session().alt_speed_enabled
self._state = STATE_ON if active else STATE_OFF