def setup(hass, config): """Set up the NZBGet sensors.""" host = config[DOMAIN][CONF_HOST] port = config[DOMAIN][CONF_PORT] ssl = "s" if config[DOMAIN][CONF_SSL] else "" name = config[DOMAIN][CONF_NAME] username = config[DOMAIN].get(CONF_USERNAME) password = config[DOMAIN].get(CONF_PASSWORD) scan_interval = config[DOMAIN][CONF_SCAN_INTERVAL] try: nzbget_api = pynzbgetapi.NZBGetAPI(host, username, password, ssl, ssl, port) nzbget_api.version() except pynzbgetapi.NZBGetAPIException as conn_err: _LOGGER.error("Error setting up NZBGet API: %s", conn_err) return False _LOGGER.debug("Successfully validated NZBGet API connection") nzbget_data = hass.data[DATA_NZBGET] = NZBGetData(hass, nzbget_api) nzbget_data.update() def refresh(event_time): """Get the latest data from NZBGet.""" nzbget_data.update() track_time_interval(hass, refresh, scan_interval) sensorconfig = {"client_name": name} hass.helpers.discovery.load_platform("sensor", DOMAIN, sensorconfig, config) return True
def setup(hass, config): """Set up the NZBGet sensors.""" host = config[DOMAIN][CONF_HOST] port = config[DOMAIN][CONF_PORT] ssl = "s" if config[DOMAIN][CONF_SSL] else "" name = config[DOMAIN][CONF_NAME] username = config[DOMAIN].get(CONF_USERNAME) password = config[DOMAIN].get(CONF_PASSWORD) scan_interval = config[DOMAIN][CONF_SCAN_INTERVAL] try: nzbget_api = pynzbgetapi.NZBGetAPI(host, username, password, ssl, ssl, port) nzbget_api.version() except pynzbgetapi.NZBGetAPIException as conn_err: _LOGGER.error("Error setting up NZBGet API: %s", conn_err) return False _LOGGER.debug("Successfully validated NZBGet API connection") nzbget_data = hass.data[DATA_NZBGET] = NZBGetData(hass, nzbget_api) nzbget_data.init_download_list() nzbget_data.update() def service_handler(service): """Handle service calls.""" if service.service == SERVICE_PAUSE: nzbget_data.pause_download() elif service.service == SERVICE_RESUME: nzbget_data.resume_download() elif service.service == SERVICE_SET_SPEED: limit = service.data[ATTR_SPEED] nzbget_data.rate(limit) hass.services.register( DOMAIN, SERVICE_PAUSE, service_handler, schema=vol.Schema({}) ) hass.services.register( DOMAIN, SERVICE_RESUME, service_handler, schema=vol.Schema({}) ) hass.services.register( DOMAIN, SERVICE_SET_SPEED, service_handler, schema=SPEED_LIMIT_SCHEMA ) def refresh(event_time): """Get the latest data from NZBGet.""" nzbget_data.update() track_time_interval(hass, refresh, scan_interval) sensorconfig = { "client_name": name, "monitored_variables": config[DOMAIN][CONF_MONITORED_VARIABLES], } hass.helpers.discovery.load_platform("sensor", DOMAIN, sensorconfig, config) return True
import os import pynzbgetapi import logging ng_api = pynzbgetapi.NZBGetAPI(os.environ["NZB_HOST"], os.environ["NZB_USERNAME"], os.environ["NZB_PASSWORD"]) def is_paused(): try: # RemainingSizeMB PostPaused status = ng_api.status() logging.debug("Current server status: {}".format(status)) return status["ServerStandBy"] except Exception as ex: logging.exception("Could not connect to NZBGet: {}".format(str(ex))) # if we can't connect to NZBGet, we asume it's paused. return True def queue(): try: listgroups = ng_api.listgroups(0) logging.debug("Queue size is: {}".format(len(listgroups))) return listgroups except Exception as ex: logging.exception("Could not connect to NZBGet: {}".format(str(ex))) # if we can't connect to NZBGet, we asume it's paused. return []