Exemplo n.º 1
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the SABnzbd sensors."""
    from pysabnzbd import SabnzbdApi, SabnzbdApiException

    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)
    base_url = "http://{}:{}/".format(host, port)

    sab_api = SabnzbdApi(base_url, api_key)

    try:
        sab_api.check_available()
    except SabnzbdApiException:
        _LOGGER.exception("Connection to SABnzbd API failed")
        return False

    # pylint: disable=global-statement
    global _THROTTLED_REFRESH
    _THROTTLED_REFRESH = Throttle(MIN_TIME_BETWEEN_UPDATES)(
        sab_api.refresh_queue)

    devices = []
    for variable in monitored_types:
        devices.append(SabnzbdSensor(variable, sab_api, name))

    add_devices(devices)
Exemplo n.º 2
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """ Sets up the SABnzbd sensors. """
    from pysabnzbd import SabnzbdApi, SabnzbdApiException

    api_key = config.get("api_key")
    base_url = config.get("base_url")
    name = config.get("name", "SABnzbd")
    if not base_url:
        _LOGGER.error('Missing config variable base_url')
        return False
    if not api_key:
        _LOGGER.error('Missing config variable api_key')
        return False

    sab_api = SabnzbdApi(base_url, api_key)

    try:
        sab_api.check_available()
    except SabnzbdApiException:
        _LOGGER.exception("Connection to SABnzbd API failed.")
        return False

    # pylint: disable=global-statement
    global _THROTTLED_REFRESH
    _THROTTLED_REFRESH = Throttle(timedelta(seconds=1))(sab_api.refresh_queue)

    dev = []
    for variable in config['monitored_variables']:
        if variable['type'] not in SENSOR_TYPES:
            _LOGGER.error('Sensor type: "%s" does not exist', variable['type'])
        else:
            dev.append(SabnzbdSensor(variable['type'], sab_api, name))

    add_devices(dev)
Exemplo n.º 3
0
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Setup the SABnzbd sensors."""
    from pysabnzbd import SabnzbdApi, SabnzbdApiException

    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)
    base_url = "http://{}:{}/".format(host, port)

    sab_api = SabnzbdApi(base_url, api_key)

    try:
        sab_api.check_available()
    except SabnzbdApiException:
        _LOGGER.exception("Connection to SABnzbd API failed")
        return False

    # pylint: disable=global-statement
    global _THROTTLED_REFRESH
    _THROTTLED_REFRESH = Throttle(
        MIN_TIME_BETWEEN_UPDATES)(sab_api.refresh_queue)

    devices = []
    for variable in monitored_types:
        devices.append(SabnzbdSensor(variable, sab_api, name))

    add_devices(devices)
Exemplo n.º 4
0
async def async_configure_sabnzbd(hass,
                                  config,
                                  use_ssl,
                                  name=DEFAULT_NAME,
                                  api_key=None):
    """Try to configure Sabnzbd and request api key if configuration fails."""

    host = config[CONF_HOST]
    port = config[CONF_PORT]
    web_root = config.get(CONF_PATH)
    uri_scheme = "https" if use_ssl else "http"
    base_url = BASE_URL_FORMAT.format(uri_scheme, host, port)
    if api_key is None:
        conf = await hass.async_add_executor_job(load_json,
                                                 hass.config.path(CONFIG_FILE))
        api_key = conf.get(base_url, {}).get(CONF_API_KEY, "")

    sab_api = SabnzbdApi(base_url,
                         api_key,
                         web_root=web_root,
                         session=async_get_clientsession(hass))
    if await async_check_sabnzbd(sab_api):
        async_setup_sabnzbd(hass, sab_api, config, name)
    else:
        async_request_configuration(hass, config, base_url, web_root)
Exemplo n.º 5
0
    async def async_configuration_callback(data):
        """Handle configuration changes."""
        api_key = data.get(CONF_API_KEY)
        sab_api = SabnzbdApi(host, api_key, session=async_get_clientsession(hass))
        if not await async_check_sabnzbd(sab_api):
            return

        def success():
            """Signal successful setup."""
            conf = load_json(hass.config.path(CONFIG_FILE))
            conf[host] = {CONF_API_KEY: api_key}
            save_json(hass.config.path(CONFIG_FILE), conf)
            req_config = _CONFIGURING.pop(host)
            configurator.request_done(req_config)

        hass.async_add_job(success)
        async_setup_sabnzbd(hass, sab_api, config, config.get(CONF_NAME, DEFAULT_NAME))
Exemplo n.º 6
0
async def get_client(hass: HomeAssistant, data):
    """Get Sabnzbd client."""
    web_root = data.get(CONF_PATH)
    api_key = data[CONF_API_KEY]
    url = data[CONF_URL]

    sab_api = SabnzbdApi(
        url,
        api_key,
        web_root=web_root,
        session=async_get_clientsession(hass, False),
    )
    try:
        await sab_api.check_available()
    except SabnzbdApiException as exception:
        _LOGGER.error("Connection to SABnzbd API failed: %s",
                      exception.message)
        return False

    return sab_api
Exemplo n.º 7
0
async def async_configure_sabnzbd(hass,
                                  config,
                                  use_ssl,
                                  name=DEFAULT_NAME,
                                  api_key=None):
    """Try to configure Sabnzbd and request api key if configuration fails."""
    from pysabnzbd import SabnzbdApi

    host = config[CONF_HOST]
    port = config[CONF_PORT]
    uri_scheme = 'https' if use_ssl else 'http'
    base_url = BASE_URL_FORMAT.format(uri_scheme, host, port)
    if api_key is None:
        conf = await hass.async_add_job(load_json,
                                        hass.config.path(CONFIG_FILE))
        api_key = conf.get(base_url, {}).get(CONF_API_KEY, '')

    sab_api = SabnzbdApi(base_url, api_key)
    if await async_check_sabnzbd(sab_api):
        async_setup_sabnzbd(hass, sab_api, config, name)
    else:
        async_request_configuration(hass, config, base_url)