Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 32 additions & 80 deletions homeassistant/components/sensor/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,89 +4,58 @@
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.transmission/
"""
from datetime import timedelta
import logging

import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_MONITORED_VARIABLES, CONF_NAME, CONF_PASSWORD, CONF_PORT,
CONF_USERNAME, STATE_IDLE)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
from homeassistant.exceptions import PlatformNotReady
from homeassistant.const import STATE_IDLE
from homeassistant.helpers.entity import Entity

REQUIREMENTS = ['transmissionrpc==0.11']
DEPENDENCIES = ['transmission']

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Transmission'
DEFAULT_PORT = 9091

SENSOR_TYPES = {
'active_torrents': ['Active Torrents', None],
'current_status': ['Status', None],
'download_speed': ['Down Speed', 'MB/s'],
'paused_torrents': ['Paused Torrents', None],
'total_torrents': ['Total Torrents', None],
'upload_speed': ['Up Speed', 'MB/s'],
}

SCAN_INTERVAL = timedelta(minutes=2)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_MONITORED_VARIABLES, default=['torrents']):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
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,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Transmission sensors."""
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)

try:
transmission = transmissionrpc.Client(
host, port=port, user=username, password=password)
transmission_api = TransmissionData(transmission)
except TransmissionError as error:
if str(error).find("401: Unauthorized"):
_LOGGER.error("Credentials for Transmission client are not valid")
return

_LOGGER.warning(
"Unable to connect to Transmission client: %s:%s", host, port)
if discovery_info is None:
_LOGGER.warning("Unable to connect to Transmission client")
Comment thread
MatteGary marked this conversation as resolved.
Outdated
raise PlatformNotReady

Comment thread
MatteGary marked this conversation as resolved.
component_name = discovery_info['component_name']
transmission_api = hass.data[component_name]
monitored_variables = discovery_info['sensors']
name = discovery_info['client_name']
sensor_types = discovery_info['sensor_types']

dev = []
for variable in config[CONF_MONITORED_VARIABLES]:
dev.append(TransmissionSensor(variable, transmission_api, name))
for variable in monitored_variables:
Comment thread
MatteGary marked this conversation as resolved.
Outdated
dev.append(TransmissionSensor(
variable,
transmission_api,
name,
sensor_types[variable][0],
sensor_types[variable][1]))

add_entities(dev, True)


class TransmissionSensor(Entity):
"""Representation of a Transmission sensor."""

def __init__(self, sensor_type, transmission_api, client_name):
def __init__(
self,
sensor_type,
transmission_api,
client_name,
sensor_name,
unit_of_measurement):
"""Initialize the sensor."""
self._name = SENSOR_TYPES[sensor_type][0]
self._name = sensor_name
self._state = None
self._transmission_api = transmission_api
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self._unit_of_measurement = unit_of_measurement
self._data = None
self.client_name = client_name
self.type = sensor_type
Expand Down Expand Up @@ -116,6 +85,11 @@ def update(self):
self._transmission_api.update()
self._data = self._transmission_api.data

if self.type == 'completed_torrents':
self._state = self._transmission_api.get_completed_torrent_count()
elif self.type == 'started_torrents':
self._state = self._transmission_api.get_started_torrent_count()

if self.type == 'current_status':
if self._data:
upload = self._data.uploadSpeed
Expand Down Expand Up @@ -146,25 +120,3 @@ def update(self):
self._state = self._data.pausedTorrentCount
elif self.type == 'total_torrents':
self._state = self._data.torrentCount


class TransmissionData:
"""Get the latest data and update the states."""

def __init__(self, api):
"""Initialize the Transmission data object."""
self.data = None
self.available = True
self._api = api

@Throttle(SCAN_INTERVAL)
def update(self):
"""Get the latest data from Transmission instance."""
from transmissionrpc.error import TransmissionError

try:
self.data = self._api.session_stats()
self.available = True
except TransmissionError:
self.available = False
_LOGGER.error("Unable to connect to Transmission client")
51 changes: 13 additions & 38 deletions homeassistant/components/switch/transmission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,29 @@
"""
import logging

import voluptuous as vol

from homeassistant.components.switch import PLATFORM_SCHEMA
from homeassistant.exceptions import PlatformNotReady
Comment thread
MatteGary marked this conversation as resolved.
Outdated
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_PORT, CONF_PASSWORD, CONF_USERNAME, STATE_OFF,
STATE_ON)
STATE_OFF, STATE_ON)
from homeassistant.helpers.entity import ToggleEntity
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['transmissionrpc==0.11']
DEPENDENCIES = ['transmission']

_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,
})


def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Transmission switch."""
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)
if discovery_info is None:
_LOGGING.warning("Unable to connect to Transmission client.")
Comment thread
MatteGary marked this conversation as resolved.
Outdated
raise PlatformNotReady
Comment thread
MatteGary marked this conversation as resolved.
Outdated

try:
transmission_api = transmissionrpc.Client(
host, port=port, user=username, password=password)
transmission_api.session_stats()
except TransmissionError as error:
_LOGGING.error(
"Connection to Transmission API failed on %s:%s with message %s",
host, port, error.original
)
return False
component_name = discovery_info['component_name']
transmission_api = hass.data[component_name]
name = discovery_info['client_name']

add_entities([TransmissionSwitch(transmission_api, name)])
add_entities([TransmissionSwitch(transmission_api, name)], True)


class TransmissionSwitch(ToggleEntity):
Expand Down Expand Up @@ -88,14 +63,14 @@ def is_on(self):
def turn_on(self, **kwargs):
"""Turn the device on."""
_LOGGING.debug("Turning Turtle Mode of Transmission on")
self.transmission_client.set_session(alt_speed_enabled=True)
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_session(alt_speed_enabled=False)
self.transmission_client.set_alt_speed_enabled(False)

def update(self):
"""Get the latest data from Transmission and updates the state."""
active = self.transmission_client.get_session().alt_speed_enabled
active = self.transmission_client.get_alt_speed_enabled()
self._state = STATE_ON if active else STATE_OFF
Loading