Files
home-assistant-core/homeassistant/components/switch/transmission.py
T
MatteGary e95c50c742 New Transmission component (#19230)
* New Transmission component and interaction

First commit for New Transmission component and interaction

* Fix commit

* Fix commit

* Fix + Switch checkin

Fix according to failed build and request, first checkin for Turtle Mode Switch in Transmission, still have to figure it out why it's not working.

* Bugfixing

* Fix commit

Multiple fix

* Fix

* fix for missing config

* Update on requirements_all.txt

* Fix in requirements_all.txt

* Fix

* Fix for build

* fix

* Fix

* Fix (again)

* Fix

* Fix indentation

* Fix indentation

* Fix Throttle

* Update .coveragerc

* Fix import and coveragerc
2019-01-29 10:27:26 +01:00

79 lines
2.3 KiB
Python

"""
Support for setting the Transmission BitTorrent client Turtle Mode.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.transmission/
"""
import logging
from homeassistant.components.transmission import (
DATA_TRANSMISSION, SCAN_INTERVAL)
from homeassistant.const import (
STATE_OFF, STATE_ON)
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.util import Throttle
DEPENDENCIES = ['transmission']
_LOGGING = logging.getLogger(__name__)
DEFAULT_NAME = 'Transmission Turtle Mode'
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Transmission switch."""
if discovery_info is None:
return
component_name = DATA_TRANSMISSION
transmission_api = hass.data[component_name]
name = discovery_info['client_name']
add_entities([TransmissionSwitch(transmission_api, name)], True)
class TransmissionSwitch(ToggleEntity):
"""Representation of a Transmission switch."""
def __init__(self, transmission_client, name):
"""Initialize the Transmission switch."""
self._name = name
self.transmission_client = transmission_client
self._state = STATE_OFF
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def should_poll(self):
"""Poll for status regularly."""
return True
@property
def is_on(self):
"""Return true if device is on."""
return self._state == STATE_ON
def turn_on(self, **kwargs):
"""Turn the device on."""
_LOGGING.debug("Turning Turtle Mode of Transmission on")
self.transmission_client.set_alt_speed_enabled(True)
def turn_off(self, **kwargs):
"""Turn the device off."""
_LOGGING.debug("Turning Turtle Mode of Transmission off")
self.transmission_client.set_alt_speed_enabled(False)
@Throttle(SCAN_INTERVAL)
def update(self):
"""Get the latest data from Transmission and updates the state."""
active = self.transmission_client.get_alt_speed_enabled()
self._state = STATE_ON if active else STATE_OFF