Files
home-assistant-core/homeassistant/components/nzbget/config_flow.py
T
Chris TalkingtonandJ. Nick Koston 7469f57a7b Add config flow to nzbget (#38938)
* work on config flow

* Update test_init.py

* work on config flow

* Update test_config_flow.py

* Update test_config_flow.py

* Update __init__.py

* Update test_config_flow.py

* Update __init__.py

* Update test_config_flow.py

* Update test_config_flow.py

* Update test_config_flow.py

* Update test_config_flow.py

* Update test_config_flow.py

* Update test_config_flow.py

* Update __init__.py

* Update __init__.py

* Update __init__.py

* Apply suggestions from code review

Co-authored-by: J. Nick Koston <nick@koston.org>

* Update __init__.py

* Update __init__.py

* Update __init__.py

* Update config_flow.py

* Update __init__.py

* Update __init__.py

* Create coordinator.py

* Update __init__.py

* Update sensor.py

* Update __init__.py

* Update .coveragerc

* Update coordinator.py

* Update __init__.py

* Update coordinator.py

* Update __init__.py

* Update coordinator.py

* Update config_flow.py

* Update __init__.py

* Update coordinator.py

* Update __init__.py

* Update test_config_flow.py

* Update coordinator.py

* Update test_config_flow.py

* Update test_init.py

* Update homeassistant/components/nzbget/coordinator.py

* Update test_config_flow.py

Co-authored-by: J. Nick Koston <nick@koston.org>
2020-08-29 16:47:00 -05:00

144 lines
4.4 KiB
Python

"""Config flow for NZBGet."""
import logging
from typing import Any, Dict, Optional
import voluptuous as vol
from homeassistant.config_entries import CONN_CLASS_LOCAL_POLL, ConfigFlow, OptionsFlow
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PORT,
CONF_SCAN_INTERVAL,
CONF_SSL,
CONF_USERNAME,
CONF_VERIFY_SSL,
)
from homeassistant.core import callback
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from .const import (
DEFAULT_NAME,
DEFAULT_PORT,
DEFAULT_SCAN_INTERVAL,
DEFAULT_SSL,
DEFAULT_VERIFY_SSL,
)
from .const import DOMAIN # pylint: disable=unused-import
from .coordinator import NZBGetAPI, NZBGetAPIException
_LOGGER = logging.getLogger(__name__)
def validate_input(hass: HomeAssistantType, data: dict) -> Dict[str, Any]:
"""Validate the user input allows us to connect.
Data has the keys from DATA_SCHEMA with values provided by the user.
"""
nzbget_api = NZBGetAPI(
data[CONF_HOST],
data[CONF_USERNAME] if data[CONF_USERNAME] != "" else None,
data[CONF_PASSWORD] if data[CONF_PASSWORD] != "" else None,
data[CONF_SSL],
data[CONF_VERIFY_SSL],
data[CONF_PORT],
)
nzbget_api.version()
return True
class NZBGetConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for NZBGet."""
VERSION = 1
CONNECTION_CLASS = CONN_CLASS_LOCAL_POLL
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return NZBGetOptionsFlowHandler(config_entry)
async def async_step_import(
self, user_input: Optional[ConfigType] = None
) -> Dict[str, Any]:
"""Handle a flow initiated by configuration file."""
if CONF_SCAN_INTERVAL in user_input:
user_input[CONF_SCAN_INTERVAL] = user_input[CONF_SCAN_INTERVAL].seconds
return await self.async_step_user(user_input)
async def async_step_user(
self, user_input: Optional[ConfigType] = None
) -> Dict[str, Any]:
"""Handle a flow initiated by the user."""
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
if user_input is None:
return self._show_setup_form()
if CONF_VERIFY_SSL not in user_input:
user_input[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
try:
await self.hass.async_add_executor_job(
validate_input, self.hass, user_input
)
except NZBGetAPIException:
return self._show_setup_form({"base": "cannot_connect"})
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
return self.async_abort(reason="unknown")
return self.async_create_entry(title=user_input[CONF_HOST], data=user_input)
def _show_setup_form(self, errors: Optional[Dict] = None) -> Dict[str, Any]:
"""Show the setup form to the user."""
data_schema = {
vol.Required(CONF_HOST): str,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): str,
vol.Optional(CONF_USERNAME): str,
vol.Optional(CONF_PASSWORD): str,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): bool,
}
if self.show_advanced_options:
data_schema[
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL)
] = bool
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(data_schema),
errors=errors or {},
)
class NZBGetOptionsFlowHandler(OptionsFlow):
"""Handle NZBGet client options."""
def __init__(self, config_entry):
"""Initialize options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input: Optional[ConfigType] = None):
"""Manage NZBGet options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)
options = {
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): int,
}
return self.async_show_form(step_id="init", data_schema=vol.Schema(options))