Allow multiple Transmission clients and add unique_id to entities (#28136)

* Allow multiple clients + improvements

* remove commented code

* fixed test_init.py
This commit is contained in:
Rami Mosleh
2019-10-23 23:09:11 +03:00
committed by Paulus Schoutsen
parent 062ec8a7c2
commit 7cb6607b1f
10 changed files with 333 additions and 183 deletions

View File

@@ -29,32 +29,32 @@ class TransmissionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Get the options flow for this handler."""
return TransmissionOptionsFlowHandler(config_entry)
def __init__(self):
"""Initialize the Transmission flow."""
self.config = {}
self.errors = {}
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
if self.hass.config_entries.async_entries(DOMAIN):
return self.async_abort(reason="one_instance_allowed")
errors = {}
if user_input is not None:
self.config[CONF_NAME] = user_input.pop(CONF_NAME)
for entry in self.hass.config_entries.async_entries(DOMAIN):
if entry.data[CONF_HOST] == user_input[CONF_HOST]:
return self.async_abort(reason="already_configured")
if entry.data[CONF_NAME] == user_input[CONF_NAME]:
errors[CONF_NAME] = "name_exists"
break
try:
await get_api(self.hass, **user_input)
self.config.update(user_input)
if "options" not in self.config:
self.config["options"] = {CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL}
return self.async_create_entry(
title=self.config[CONF_NAME], data=self.config
)
await get_api(self.hass, user_input)
except AuthenticationError:
self.errors[CONF_USERNAME] = "wrong_credentials"
self.errors[CONF_PASSWORD] = "wrong_credentials"
errors[CONF_USERNAME] = "wrong_credentials"
errors[CONF_PASSWORD] = "wrong_credentials"
except (CannotConnect, UnknownError):
self.errors["base"] = "cannot_connect"
errors["base"] = "cannot_connect"
if not errors:
return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
return self.async_show_form(
step_id="user",
@@ -67,15 +67,12 @@ class TransmissionFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
}
),
errors=self.errors,
errors=errors,
)
async def async_step_import(self, import_config):
"""Import from Transmission client config."""
self.config["options"] = {
CONF_SCAN_INTERVAL: import_config.pop(CONF_SCAN_INTERVAL).seconds
}
import_config[CONF_SCAN_INTERVAL] = import_config[CONF_SCAN_INTERVAL].seconds
return await self.async_step_user(user_input=import_config)
@@ -95,8 +92,7 @@ class TransmissionOptionsFlowHandler(config_entries.OptionsFlow):
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL,
self.config_entry.data["options"][CONF_SCAN_INTERVAL],
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): int
}