Use voluptuous for NZB sensors (#2847)

This commit is contained in:
Fabian Affolter 2016-08-16 21:42:43 +02:00 committed by GitHub
parent 91e24de3d5
commit 1c140de0dc
2 changed files with 145 additions and 111 deletions

View File

@ -1,48 +1,65 @@
""" """
Support for monitoring NZBGet nzb client. Support for monitoring NZBGet NZB client.
Uses NZBGet's JSON-RPC API to query for monitored variables. For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.nzbget/
""" """
import logging import logging
from datetime import timedelta from datetime import timedelta
import requests
import requests
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, CONF_NAME, CONF_PORT,
CONTENT_TYPE_JSON, CONF_MONITORED_VARIABLES)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
DEFAULT_NAME = 'NZBGet'
DEFAULT_PORT = 6789
REQUIREMENTS = []
SENSOR_TYPES = { SENSOR_TYPES = {
"ArticleCacheMB": ("Article Cache", "MB"), 'article_cache': ['ArticleCacheMB', 'Article Cache', 'MB'],
"AverageDownloadRate": ("Average Speed", "MB/s"), 'average_download_rate': ['AverageDownloadRate', 'Average Speed', 'MB/s'],
"DownloadRate": ("Speed", "MB/s"), 'download_paused': ['DownloadPaused', 'Download Paused', None],
"DownloadPaused": ("Download Paused", None), 'download_rate': ['DownloadRate', 'Speed', 'MB/s'],
"FreeDiskSpaceMB": ("Disk Free", "MB"), 'download_size': ['DownloadedSizeMB', 'Size', 'MB'],
"PostPaused": ("Post Processing Paused", None), 'free_disk_space': ['FreeDiskSpaceMB', 'Disk Free', 'MB'],
"RemainingSizeMB": ("Queue Size", "MB"), 'post_paused': ['PostPaused', 'Post Processing Paused', None],
'remaining_size': ['RemainingSizeMB', 'Queue Size', 'MB'],
'uptime': ['UpTimeSec', 'Uptime', 'min'],
} }
DEFAULT_TYPES = [
"DownloadRate", PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
"DownloadPaused", vol.Required(CONF_HOST): cv.string,
"RemainingSizeMB", vol.Optional(CONF_MONITORED_VARIABLES, default=['download_rate']):
] 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.string,
vol.Optional(CONF_USERNAME): cv.string,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
# pylint: disable=unused-argument
# pylint: disable=unused-argument, too-many-locals
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up nzbget sensors.""" """Setup the NZBGet sensors."""
base_url = config.get("base_url") host = config.get(CONF_HOST)
name = config.get("name", "NZBGet") port = config.get(CONF_PORT)
username = config.get("username") name = config.get(CONF_NAME)
password = config.get("password") username = config.get(CONF_USERNAME)
monitored_types = config.get("monitored_variables", DEFAULT_TYPES) password = config.get(CONF_PASSWORD)
monitored_types = config.get(CONF_MONITORED_VARIABLES)
if not base_url: url = "http://{}:{}/jsonrpc".format(host, port)
_LOGGER.error("Missing base_url config for NzbGet")
return False
url = "{}/jsonrpc".format(base_url)
try: try:
nzbgetapi = NZBGetAPI(api_url=url, nzbgetapi = NZBGetAPI(api_url=url,
@ -51,78 +68,32 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
nzbgetapi.update() nzbgetapi.update()
except (requests.exceptions.ConnectionError, except (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError) as conn_err: requests.exceptions.HTTPError) as conn_err:
_LOGGER.error("Error setting up NZBGet API: %r", conn_err) _LOGGER.error("Error setting up NZBGet API: %s", conn_err)
return False return False
devices = [] devices = []
for ng_type in monitored_types: for ng_type in monitored_types:
if ng_type in SENSOR_TYPES: new_sensor = NZBGetSensor(api=nzbgetapi,
new_sensor = NZBGetSensor(api=nzbgetapi, sensor_type=SENSOR_TYPES.get(ng_type),
sensor_type=ng_type, client_name=name)
client_name=name) devices.append(new_sensor)
devices.append(new_sensor)
else:
_LOGGER.error("Unknown nzbget sensor type: %s", ng_type)
add_devices(devices) add_devices(devices)
class NZBGetAPI(object):
"""Simple json-rpc wrapper for nzbget's api."""
def __init__(self, api_url, username=None, password=None):
"""Initialize NZBGet API and set headers needed later."""
self.api_url = api_url
self.status = None
self.headers = {'content-type': 'application/json'}
if username is not None and password is not None:
self.auth = (username, password)
else:
self.auth = None
# set the intial state
self.update()
def post(self, method, params=None):
"""Send a post request, and return the response as a dict."""
payload = {"method": method}
if params:
payload['params'] = params
try:
response = requests.post(self.api_url,
json=payload,
auth=self.auth,
headers=self.headers,
timeout=30)
response.raise_for_status()
return response.json()
except requests.exceptions.ConnectionError as conn_exc:
_LOGGER.error("Failed to update nzbget status from %s. Error: %s",
self.api_url, conn_exc)
raise
@Throttle(timedelta(seconds=5))
def update(self):
"""Update cached response."""
try:
self.status = self.post('status')['result']
except requests.exceptions.ConnectionError:
# failed to update status - exception already logged in self.post
raise
class NZBGetSensor(Entity): class NZBGetSensor(Entity):
"""Represents an NZBGet sensor.""" """Representation of a NZBGet sensor."""
def __init__(self, api, sensor_type, client_name): def __init__(self, api, sensor_type, client_name):
"""Initialize a new NZBGet sensor.""" """Initialize a new NZBGet sensor."""
self._name = client_name + ' ' + SENSOR_TYPES[sensor_type][0] self._name = '{} {}'.format(client_name, sensor_type[1])
self.type = sensor_type self.type = sensor_type[0]
self.client_name = client_name self.client_name = client_name
self.api = api self.api = api
self._state = None self._state = None
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._unit_of_measurement = sensor_type[2]
# Set initial state
self.update() self.update()
_LOGGER.debug("created nzbget sensor %r", self) _LOGGER.debug("Created NZBGet sensor: %s", self.type)
@property @property
def name(self): def name(self):
@ -144,21 +115,68 @@ class NZBGetSensor(Entity):
try: try:
self.api.update() self.api.update()
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
# Error calling the api, already logged in api.update() # Error calling the API, already logged in api.update()
return return
if self.api.status is None: if self.api.status is None:
_LOGGER.debug("update of %s requested, but no status is available", _LOGGER.debug("Update of %s requested, but no status is available",
self._name) self._name)
return return
value = self.api.status.get(self.type) value = self.api.status.get(self.type)
if value is None: if value is None:
_LOGGER.warning("unable to locate value for %s", self.type) _LOGGER.warning("Unable to locate value for %s", self.type)
return return
if "DownloadRate" in self.type and value > 0: if "DownloadRate" in self.type and value > 0:
# Convert download rate from Bytes/s to MBytes/s # Convert download rate from Bytes/s to MBytes/s
self._state = round(value / 1024 / 1024, 2) self._state = round(value / 2**20, 2)
elif "UpTimeSec" in self.type and value > 0:
# Convert uptime from seconds to minutes
self._state = round(value / 60, 2)
else: else:
self._state = value self._state = value
class NZBGetAPI(object):
"""Simple JSON-RPC wrapper for NZBGet's API."""
def __init__(self, api_url, username=None, password=None):
"""Initialize NZBGet API and set headers needed later."""
self.api_url = api_url
self.status = None
self.headers = {'content-type': CONTENT_TYPE_JSON}
if username is not None and password is not None:
self.auth = (username, password)
else:
self.auth = None
self.update()
def post(self, method, params=None):
"""Send a POST request and return the response as a dict."""
payload = {"method": method}
if params:
payload['params'] = params
try:
response = requests.post(self.api_url,
json=payload,
auth=self.auth,
headers=self.headers,
timeout=5)
response.raise_for_status()
return response.json()
except requests.exceptions.ConnectionError as conn_exc:
_LOGGER.error("Failed to update NZBGet status from %s. Error: %s",
self.api_url, conn_exc)
raise
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Update cached response."""
try:
self.status = self.post('status')['result']
except requests.exceptions.ConnectionError:
# failed to update status - exception already logged in self.post
raise

View File

@ -7,13 +7,22 @@ https://home-assistant.io/components/sensor.sabnzbd/
import logging import logging
from datetime import timedelta from datetime import timedelta
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_API_KEY, CONF_NAME, CONF_PORT, CONF_MONITORED_VARIABLES)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle from homeassistant.util import Throttle
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['https://github.com/jamespcole/home-assistant-nzb-clients/' REQUIREMENTS = ['https://github.com/jamespcole/home-assistant-nzb-clients/'
'archive/616cad59154092599278661af17e2a9f2cf5e2a9.zip' 'archive/616cad59154092599278661af17e2a9f2cf5e2a9.zip'
'#python-sabnzbd==0.1'] '#python-sabnzbd==0.1']
DEFAULT_NAME = 'SABnzbd'
DEFAULT_PORT = 8080
SENSOR_TYPES = { SENSOR_TYPES = {
'current_status': ['Status', None], 'current_status': ['Status', None],
'speed': ['Speed', 'MB/s'], 'speed': ['Speed', 'MB/s'],
@ -23,45 +32,52 @@ SENSOR_TYPES = {
'disk_free': ['Disk Free', 'GB'], 'disk_free': ['Disk Free', 'GB'],
} }
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.string,
})
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_THROTTLED_REFRESH = None _THROTTLED_REFRESH = None
# Return cached results if last scan was less then this time ago.
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=1)
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the SABnzbd sensors.""" """Setup the SABnzbd sensors."""
from pysabnzbd import SabnzbdApi, SabnzbdApiException from pysabnzbd import SabnzbdApi, SabnzbdApiException
api_key = config.get("api_key") host = config.get(CONF_HOST)
base_url = config.get("base_url") port = config.get(CONF_PORT)
name = config.get("name", "SABnzbd") name = config.get(CONF_NAME)
if not base_url: api_key = config.get(CONF_API_KEY)
_LOGGER.error('Missing config variable base_url') monitored_types = config.get(CONF_MONITORED_VARIABLES)
return False base_url = "http://{}:{}/".format(host, port)
if not api_key:
_LOGGER.error('Missing config variable api_key')
return False
sab_api = SabnzbdApi(base_url, api_key) sab_api = SabnzbdApi(base_url, api_key)
try: try:
sab_api.check_available() sab_api.check_available()
except SabnzbdApiException: except SabnzbdApiException:
_LOGGER.exception("Connection to SABnzbd API failed.") _LOGGER.exception("Connection to SABnzbd API failed")
return False return False
# pylint: disable=global-statement # pylint: disable=global-statement
global _THROTTLED_REFRESH global _THROTTLED_REFRESH
_THROTTLED_REFRESH = Throttle(timedelta(seconds=1))(sab_api.refresh_queue) _THROTTLED_REFRESH = Throttle(
MIN_TIME_BETWEEN_UPDATES)(sab_api.refresh_queue)
dev = [] devices = []
for variable in config['monitored_variables']: for variable in monitored_types:
if variable['type'] not in SENSOR_TYPES: devices.append(SabnzbdSensor(variable, sab_api, name))
_LOGGER.error('Sensor type: "%s" does not exist', variable['type'])
else:
dev.append(SabnzbdSensor(variable['type'], sab_api, name))
add_devices(dev) add_devices(devices)
class SabnzbdSensor(Entity): class SabnzbdSensor(Entity):
@ -79,7 +95,7 @@ class SabnzbdSensor(Entity):
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self.client_name + ' ' + self._name return '{} {}'.format(self.client_name, self._name)
@property @property
def state(self): def state(self):
@ -91,6 +107,7 @@ class SabnzbdSensor(Entity):
"""Return the unit of measurement of this entity, if any.""" """Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement return self._unit_of_measurement
# pylint: disable=no-self-use
def refresh_sabnzbd_data(self): def refresh_sabnzbd_data(self):
"""Call the throttled SABnzbd refresh method.""" """Call the throttled SABnzbd refresh method."""
if _THROTTLED_REFRESH is not None: if _THROTTLED_REFRESH is not None:
@ -98,13 +115,12 @@ class SabnzbdSensor(Entity):
try: try:
_THROTTLED_REFRESH() _THROTTLED_REFRESH()
except SabnzbdApiException: except SabnzbdApiException:
_LOGGER.exception( _LOGGER.exception("Connection to SABnzbd API failed")
self.name + " Connection to SABnzbd API failed."
)
def update(self): def update(self):
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
self.refresh_sabnzbd_data() self.refresh_sabnzbd_data()
if self.sabnzb_client.queue: if self.sabnzb_client.queue:
if self.type == 'current_status': if self.type == 'current_status':
self._state = self.sabnzb_client.queue.get('status') self._state = self.sabnzb_client.queue.get('status')