diff --git a/homeassistant/components/sensor/deluge.py b/homeassistant/components/sensor/deluge.py index f4793867d4c..8acbda74d7d 100644 --- a/homeassistant/components/sensor/deluge.py +++ b/homeassistant/components/sensor/deluge.py @@ -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[ diff --git a/homeassistant/components/switch/deluge.py b/homeassistant/components/switch/deluge.py index 30287a2669e..da0b3bf3228 100644 --- a/homeassistant/components/switch/deluge.py +++ b/homeassistant/components/switch/deluge.py @@ -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]: diff --git a/requirements_all.txt b/requirements_all.txt index e9bd0d71de1..f0be99705c1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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