core/homeassistant/components/sensor/sabnzbd.py

148 lines
4.8 KiB
Python
Raw Normal View History

2015-03-08 18:28:12 +00:00
"""
2016-02-23 05:21:49 +00:00
Support for monitoring an SABnzbd NZB client.
2015-03-08 18:28:12 +00:00
2015-10-14 06:35:47 +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/sensor.sabnzbd/
2015-03-08 18:28:12 +00:00
"""
2015-11-29 21:49:05 +00:00
import logging
2016-02-19 05:27:50 +00:00
from datetime import timedelta
2016-08-16 19:42:43 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
2016-09-03 23:45:31 +00:00
CONF_HOST, CONF_API_KEY, CONF_NAME, CONF_PORT, CONF_MONITORED_VARIABLES,
CONF_SSL)
from homeassistant.helpers.entity import Entity
2015-11-29 21:49:05 +00:00
from homeassistant.util import Throttle
2016-08-16 19:42:43 +00:00
import homeassistant.helpers.config_validation as cv
2015-03-08 18:28:12 +00:00
2015-09-09 03:11:25 +00:00
REQUIREMENTS = ['https://github.com/jamespcole/home-assistant-nzb-clients/'
'archive/616cad59154092599278661af17e2a9f2cf5e2a9.zip'
'#python-sabnzbd==0.1']
_LOGGER = logging.getLogger(__name__)
_THROTTLED_REFRESH = None
2016-08-16 19:42:43 +00:00
DEFAULT_NAME = 'SABnzbd'
DEFAULT_PORT = 8080
2016-09-03 23:45:31 +00:00
DEFAULT_SSL = False
2016-08-16 19:42:43 +00:00
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)
2015-03-08 18:28:12 +00:00
SENSOR_TYPES = {
2016-01-18 01:50:20 +00:00
'current_status': ['Status', None],
2015-03-08 18:28:12 +00:00
'speed': ['Speed', 'MB/s'],
'queue_size': ['Queue', 'MB'],
'queue_remaining': ['Left', 'MB'],
'disk_size': ['Disk', 'GB'],
'disk_free': ['Disk Free', 'GB'],
}
2016-08-16 19:42:43 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_MONITORED_VARIABLES, default=['current_status']):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
2016-09-03 23:45:31 +00:00
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
2016-08-16 19:42:43 +00:00
})
2015-03-08 18:28:12 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the SABnzbd sensors."""
2015-09-09 03:22:13 +00:00
from pysabnzbd import SabnzbdApi, SabnzbdApiException
2015-09-09 03:11:25 +00:00
2016-08-16 19:42:43 +00:00
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
name = config.get(CONF_NAME)
api_key = config.get(CONF_API_KEY)
monitored_types = config.get(CONF_MONITORED_VARIABLES)
2016-09-03 23:45:31 +00:00
use_ssl = config.get(CONF_SSL)
if use_ssl:
uri_scheme = 'https://'
else:
uri_scheme = 'http://'
base_url = "{}{}:{}/".format(uri_scheme, host, port)
2015-03-08 18:28:12 +00:00
sab_api = SabnzbdApi(base_url, api_key)
try:
sab_api.check_available()
except SabnzbdApiException:
2016-09-03 23:45:31 +00:00
_LOGGER.error("Connection to SABnzbd API failed")
return False
# pylint: disable=global-statement
global _THROTTLED_REFRESH
2016-08-16 19:42:43 +00:00
_THROTTLED_REFRESH = Throttle(
MIN_TIME_BETWEEN_UPDATES)(sab_api.refresh_queue)
2016-08-16 19:42:43 +00:00
devices = []
for variable in monitored_types:
devices.append(SabnzbdSensor(variable, sab_api, name))
2015-03-08 18:28:12 +00:00
2016-08-16 19:42:43 +00:00
add_devices(devices)
2015-03-08 18:28:12 +00:00
class SabnzbdSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of an SABnzbd sensor."""
2015-03-08 18:28:12 +00:00
def __init__(self, sensor_type, sabnzb_client, client_name):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2015-03-08 18:28:12 +00:00
self._name = SENSOR_TYPES[sensor_type][0]
self.sabnzb_client = sabnzb_client
self.type = sensor_type
self.client_name = client_name
self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1]
2015-03-08 18:28:12 +00:00
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
2016-08-16 19:42:43 +00:00
return '{} {}'.format(self.client_name, self._name)
2015-03-08 18:28:12 +00:00
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
2015-03-08 18:28:12 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
2015-03-08 18:28:12 +00:00
2016-08-16 19:42:43 +00:00
# pylint: disable=no-self-use
def refresh_sabnzbd_data(self):
2016-03-08 15:46:34 +00:00
"""Call the throttled SABnzbd refresh method."""
if _THROTTLED_REFRESH is not None:
2015-09-09 03:22:13 +00:00
from pysabnzbd import SabnzbdApiException
try:
_THROTTLED_REFRESH()
except SabnzbdApiException:
2016-08-16 19:42:43 +00:00
_LOGGER.exception("Connection to SABnzbd API failed")
2015-03-08 18:28:12 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data and updates the states."""
self.refresh_sabnzbd_data()
2016-08-16 19:42:43 +00:00
2015-03-08 18:28:12 +00:00
if self.sabnzb_client.queue:
if self.type == 'current_status':
self._state = self.sabnzb_client.queue.get('status')
elif self.type == 'speed':
mb_spd = float(self.sabnzb_client.queue.get('kbpersec')) / 1024
self._state = round(mb_spd, 1)
elif self.type == 'queue_size':
self._state = self.sabnzb_client.queue.get('mb')
elif self.type == 'queue_remaining':
self._state = self.sabnzb_client.queue.get('mbleft')
elif self.type == 'disk_size':
self._state = self.sabnzb_client.queue.get('diskspacetotal1')
elif self.type == 'disk_free':
self._state = self.sabnzb_client.queue.get('diskspace1')
else:
self._state = 'Unknown'