1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -8,48 +8,58 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_SSL, CONF_HOST, CONF_NAME, CONF_PORT, CONF_PASSWORD, CONF_USERNAME,
CONTENT_TYPE_JSON, CONF_MONITORED_VARIABLES)
CONF_SSL,
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_PASSWORD,
CONF_USERNAME,
CONTENT_TYPE_JSON,
CONF_MONITORED_VARIABLES,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'NZBGet'
DEFAULT_NAME = "NZBGet"
DEFAULT_PORT = 6789
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
SENSOR_TYPES = {
'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'],
"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"],
}
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,
})
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,
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the NZBGet sensors."""
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
ssl = 's' if config.get(CONF_SSL) else ''
ssl = "s" if config.get(CONF_SSL) else ""
name = config.get(CONF_NAME)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)
@@ -58,19 +68,20 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
url = "http{}://{}:{}/jsonrpc".format(ssl, host, port)
try:
nzbgetapi = NZBGetAPI(
api_url=url, username=username, password=password)
nzbgetapi = NZBGetAPI(api_url=url, username=username, password=password)
nzbgetapi.update()
except (requests.exceptions.ConnectionError,
requests.exceptions.HTTPError) as conn_err:
except (
requests.exceptions.ConnectionError,
requests.exceptions.HTTPError,
) as conn_err:
_LOGGER.error("Error setting up NZBGet API: %s", conn_err)
return False
devices = []
for ng_type in monitored_types:
new_sensor = NZBGetSensor(
api=nzbgetapi, sensor_type=SENSOR_TYPES.get(ng_type),
client_name=name)
api=nzbgetapi, sensor_type=SENSOR_TYPES.get(ng_type), client_name=name
)
devices.append(new_sensor)
add_entities(devices)
@@ -81,7 +92,7 @@ class NZBGetSensor(Entity):
def __init__(self, api, sensor_type, client_name):
"""Initialize a new NZBGet sensor."""
self._name = '{} {}'.format(client_name, sensor_type[1])
self._name = "{} {}".format(client_name, sensor_type[1])
self.type = sensor_type[0]
self.client_name = client_name
self.api = api
@@ -114,8 +125,9 @@ class NZBGetSensor(Entity):
return
if self.api.status is None:
_LOGGER.debug("Update of %s requested, but no status is available",
self._name)
_LOGGER.debug(
"Update of %s requested, but no status is available", self._name
)
return
value = self.api.status.get(self.type)
@@ -125,7 +137,7 @@ class NZBGetSensor(Entity):
if "DownloadRate" in self.type and value > 0:
# Convert download rate from Bytes/s to MBytes/s
self._state = round(value / 2**20, 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)
@@ -150,22 +162,29 @@ class NZBGetAPI:
def post(self, method, params=None):
"""Send a POST request and return the response as a dict."""
payload = {'method': method}
payload = {"method": method}
if params:
payload['params'] = params
payload["params"] = params
try:
response = requests.post(
self.api_url, json=payload, auth=self.auth,
headers=self.headers, timeout=5)
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)
_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."""
self.status = self.post('status')['result']
self.status = self.post("status")["result"]