2019-04-03 15:40:03 +00:00
|
|
|
"""Support for monitoring NZBGet NZB client."""
|
2016-04-03 22:57:50 +00:00
|
|
|
from datetime import timedelta
|
2017-11-04 19:04:05 +00:00
|
|
|
import logging
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import CONTENT_TYPE
|
2016-04-03 22:57:50 +00:00
|
|
|
import requests
|
2016-08-16 19:42:43 +00:00
|
|
|
import voluptuous as vol
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2016-08-16 19:42:43 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SSL,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONTENT_TYPE_JSON,
|
|
|
|
CONF_MONITORED_VARIABLES,
|
|
|
|
)
|
2017-11-04 19:04:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-04-03 22:57:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import Throttle
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "NZBGet"
|
2016-08-16 19:42:43 +00:00
|
|
|
DEFAULT_PORT = 6789
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
|
|
|
|
|
2016-04-03 22:57:50 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"article_cache": ["ArticleCacheMB", "Article Cache", "MB"],
|
|
|
|
"average_download_rate": ["AverageDownloadRate", "Average Speed", "MB/s"],
|
|
|
|
"download_paused": ["DownloadPaused", "Download Paused", None],
|
|
|
|
"download_rate": ["DownloadRate", "Speed", "MB/s"],
|
|
|
|
"download_size": ["DownloadedSizeMB", "Size", "MB"],
|
|
|
|
"free_disk_space": ["FreeDiskSpaceMB", "Disk Free", "MB"],
|
|
|
|
"post_paused": ["PostPaused", "Post Processing Paused", None],
|
|
|
|
"remaining_size": ["RemainingSizeMB", "Queue Size", "MB"],
|
|
|
|
"uptime": ["UpTimeSec", "Uptime", "min"],
|
2016-04-03 22:57:50 +00:00
|
|
|
}
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
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.port,
|
|
|
|
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_USERNAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2016-08-16 19:42:43 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the NZBGet sensors."""
|
2016-08-16 19:42:43 +00:00
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
port = config.get(CONF_PORT)
|
2019-07-31 19:25:30 +00:00
|
|
|
ssl = "s" if config.get(CONF_SSL) else ""
|
2016-08-16 19:42:43 +00:00
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
username = config.get(CONF_USERNAME)
|
|
|
|
password = config.get(CONF_PASSWORD)
|
|
|
|
monitored_types = config.get(CONF_MONITORED_VARIABLES)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2019-09-03 18:35:00 +00:00
|
|
|
url = f"http{ssl}://{host}:{port}/jsonrpc"
|
2016-04-03 22:57:50 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
nzbgetapi = NZBGetAPI(api_url=url, username=username, password=password)
|
2016-04-03 22:57:50 +00:00
|
|
|
nzbgetapi.update()
|
2019-07-31 19:25:30 +00:00
|
|
|
except (
|
|
|
|
requests.exceptions.ConnectionError,
|
|
|
|
requests.exceptions.HTTPError,
|
|
|
|
) as conn_err:
|
2016-08-16 19:42:43 +00:00
|
|
|
_LOGGER.error("Error setting up NZBGet API: %s", conn_err)
|
2016-04-03 22:57:50 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
devices = []
|
|
|
|
for ng_type in monitored_types:
|
2016-10-30 08:58:34 +00:00
|
|
|
new_sensor = NZBGetSensor(
|
2019-07-31 19:25:30 +00:00
|
|
|
api=nzbgetapi, sensor_type=SENSOR_TYPES.get(ng_type), client_name=name
|
|
|
|
)
|
2016-08-16 19:42:43 +00:00
|
|
|
devices.append(new_sensor)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(devices)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NZBGetSensor(Entity):
|
2016-08-16 19:42:43 +00:00
|
|
|
"""Representation of a NZBGet sensor."""
|
2016-04-03 22:57:50 +00:00
|
|
|
|
|
|
|
def __init__(self, api, sensor_type, client_name):
|
|
|
|
"""Initialize a new NZBGet sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "{} {}".format(client_name, sensor_type[1])
|
2016-08-16 19:42:43 +00:00
|
|
|
self.type = sensor_type[0]
|
2016-04-03 22:57:50 +00:00
|
|
|
self.client_name = client_name
|
|
|
|
self.api = api
|
|
|
|
self._state = None
|
2016-08-16 19:42:43 +00:00
|
|
|
self._unit_of_measurement = sensor_type[2]
|
2016-04-03 22:57:50 +00:00
|
|
|
self.update()
|
2016-08-16 19:42:43 +00:00
|
|
|
_LOGGER.debug("Created NZBGet sensor: %s", self.type)
|
2016-04-03 22:57:50 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-04-03 22:57:50 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update state of sensor."""
|
|
|
|
try:
|
|
|
|
self.api.update()
|
|
|
|
except requests.exceptions.ConnectionError:
|
2016-08-16 19:42:43 +00:00
|
|
|
# Error calling the API, already logged in api.update()
|
2016-04-03 22:57:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if self.api.status is None:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Update of %s requested, but no status is available", self._name
|
|
|
|
)
|
2016-04-03 22:57:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
value = self.api.status.get(self.type)
|
|
|
|
if value is None:
|
2016-08-16 19:42:43 +00:00
|
|
|
_LOGGER.warning("Unable to locate value for %s", self.type)
|
2016-04-03 22:57:50 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if "DownloadRate" in self.type and value > 0:
|
2016-06-09 15:06:01 +00:00
|
|
|
# Convert download rate from Bytes/s to MBytes/s
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = round(value / 2 ** 20, 2)
|
2016-08-16 19:42:43 +00:00
|
|
|
elif "UpTimeSec" in self.type and value > 0:
|
|
|
|
# Convert uptime from seconds to minutes
|
|
|
|
self._state = round(value / 60, 2)
|
2016-04-03 22:57:50 +00:00
|
|
|
else:
|
|
|
|
self._state = value
|
2016-08-16 19:42:43 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class NZBGetAPI:
|
2016-08-16 19:42:43 +00:00
|
|
|
"""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
|
2017-11-04 19:04:05 +00:00
|
|
|
self.headers = {CONTENT_TYPE: CONTENT_TYPE_JSON}
|
2016-08-16 19:42:43 +00:00
|
|
|
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
payload = {"method": method}
|
2016-08-16 19:42:43 +00:00
|
|
|
|
|
|
|
if params:
|
2019-07-31 19:25:30 +00:00
|
|
|
payload["params"] = params
|
2016-08-16 19:42:43 +00:00
|
|
|
try:
|
2016-10-30 08:58:34 +00:00
|
|
|
response = requests.post(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.api_url,
|
|
|
|
json=payload,
|
|
|
|
auth=self.auth,
|
|
|
|
headers=self.headers,
|
|
|
|
timeout=5,
|
|
|
|
)
|
2016-08-16 19:42:43 +00:00
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
except requests.exceptions.ConnectionError as conn_exc:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Failed to update NZBGet status from %s. Error: %s",
|
|
|
|
self.api_url,
|
|
|
|
conn_exc,
|
|
|
|
)
|
2016-08-16 19:42:43 +00:00
|
|
|
raise
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self):
|
|
|
|
"""Update cached response."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.status = self.post("status")["result"]
|