deluge-components-update (#14016)

This commit is contained in:
engrbm87 2018-04-29 02:16:22 +04:00 committed by Fabian Affolter
parent 07f94eaa92
commit 44ddc6ba62
3 changed files with 45 additions and 16 deletions

View file

@ -14,8 +14,9 @@ from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_NAME, CONF_PORT,
CONF_MONITORED_VARIABLES, STATE_IDLE)
from homeassistant.helpers.entity import Entity
from homeassistant.exceptions import PlatformNotReady
REQUIREMENTS = ['deluge-client==1.0.5']
REQUIREMENTS = ['deluge-client==1.4.0']
_LOGGER = logging.getLogger(__name__)
_THROTTLED_REFRESH = None
@ -24,7 +25,6 @@ DEFAULT_NAME = 'Deluge'
DEFAULT_PORT = 58846
DHT_UPLOAD = 1000
DHT_DOWNLOAD = 1000
SENSOR_TYPES = {
'current_status': ['Status', None],
'download_speed': ['Down Speed', 'kB/s'],
@ -58,8 +58,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
deluge_api.connect()
except ConnectionRefusedError:
_LOGGER.error("Connection to Deluge Daemon failed")
return
raise PlatformNotReady
dev = []
for variable in config[CONF_MONITORED_VARIABLES]:
dev.append(DelugeSensor(variable, deluge_api, name))
@ -79,6 +78,7 @@ class DelugeSensor(Entity):
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
self.data = None
self._available = False
@property
def name(self):
@ -90,6 +90,11 @@ class DelugeSensor(Entity):
"""Return the state of the sensor."""
return self._state
@property
def available(self):
"""Return true if device is available."""
return self._available
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
@ -97,9 +102,17 @@ class DelugeSensor(Entity):
def update(self):
"""Get the latest data from Deluge and updates the state."""
self.data = self.client.call('core.get_session_status',
['upload_rate', 'download_rate',
'dht_upload_rate', 'dht_download_rate'])
from deluge_client import FailedToReconnectException
try:
self.data = self.client.call('core.get_session_status',
['upload_rate', 'download_rate',
'dht_upload_rate',
'dht_download_rate'])
self._available = True
except FailedToReconnectException:
_LOGGER.error("Connection to Deluge Daemon Lost")
self._available = False
return
upload = self.data[b'upload_rate'] - self.data[b'dht_upload_rate']
download = self.data[b'download_rate'] - self.data[

View file

@ -9,15 +9,16 @@ import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA
from homeassistant.exceptions import PlatformNotReady
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 = ['deluge-client==1.0.5']
REQUIREMENTS = ['deluge-client==1.4.0']
_LOGGING = logging.getLogger(__name__)
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'Deluge Switch'
DEFAULT_PORT = 58846
@ -46,8 +47,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try:
deluge_api.connect()
except ConnectionRefusedError:
_LOGGING.error("Connection to Deluge Daemon failed")
return
_LOGGER.error("Connection to Deluge Daemon failed")
raise PlatformNotReady
add_devices([DelugeSwitch(deluge_api, name)])
@ -60,6 +61,7 @@ class DelugeSwitch(ToggleEntity):
self._name = name
self.deluge_client = deluge_client
self._state = STATE_OFF
self._available = False
@property
def name(self):
@ -76,18 +78,32 @@ class DelugeSwitch(ToggleEntity):
"""Return true if device is on."""
return self._state == STATE_ON
@property
def available(self):
"""Return true if device is available."""
return self._available
def turn_on(self, **kwargs):
"""Turn the device on."""
self.deluge_client.call('core.resume_all_torrents')
torrent_ids = self.deluge_client.call('core.get_session_state')
self.deluge_client.call('core.resume_torrent', torrent_ids)
def turn_off(self, **kwargs):
"""Turn the device off."""
self.deluge_client.call('core.pause_all_torrents')
torrent_ids = self.deluge_client.call('core.get_session_state')
self.deluge_client.call('core.pause_torrent', torrent_ids)
def update(self):
"""Get the latest data from deluge and updates the state."""
torrent_list = self.deluge_client.call('core.get_torrents_status', {},
['paused'])
from deluge_client import FailedToReconnectException
try:
torrent_list = self.deluge_client.call('core.get_torrents_status',
{}, ['paused'])
self._available = True
except FailedToReconnectException:
_LOGGER.error("Connection to Deluge Daemon Lost")
self._available = False
return
for torrent in torrent_list.values():
item = torrent.popitem()
if not item[1]:

View file

@ -243,7 +243,7 @@ defusedxml==0.5.0
# homeassistant.components.sensor.deluge
# homeassistant.components.switch.deluge
deluge-client==1.0.5
deluge-client==1.4.0
# homeassistant.components.media_player.denonavr
denonavr==0.6.1