2019-10-13 18:01:04 +00:00
|
|
|
"""Config flow for the Abode Security System component."""
|
|
|
|
from abodepy import Abode
|
|
|
|
from abodepy.exceptions import AbodeException
|
|
|
|
from requests.exceptions import ConnectTimeout, HTTPError
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2020-04-09 19:43:42 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, HTTP_BAD_REQUEST
|
2019-10-13 18:01:04 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
|
2020-03-04 22:54:28 +00:00
|
|
|
from .const import DEFAULT_CACHEDB, DOMAIN, LOGGER # pylint: disable=unused-import
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
CONF_POLLING = "polling"
|
|
|
|
|
|
|
|
|
|
|
|
class AbodeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Config flow for Abode."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize."""
|
|
|
|
self.data_schema = {
|
|
|
|
vol.Required(CONF_USERNAME): str,
|
|
|
|
vol.Required(CONF_PASSWORD): str,
|
|
|
|
}
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
if self._async_current_entries():
|
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
|
|
|
if not user_input:
|
|
|
|
return self._show_form()
|
|
|
|
|
|
|
|
username = user_input[CONF_USERNAME]
|
|
|
|
password = user_input[CONF_PASSWORD]
|
|
|
|
polling = user_input.get(CONF_POLLING, False)
|
2019-11-02 00:28:50 +00:00
|
|
|
cache = self.hass.config.path(DEFAULT_CACHEDB)
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
try:
|
2019-11-02 00:28:50 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
Abode, username, password, True, True, True, cache
|
|
|
|
)
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
except (AbodeException, ConnectTimeout, HTTPError) as ex:
|
2020-03-04 22:54:28 +00:00
|
|
|
LOGGER.error("Unable to connect to Abode: %s", str(ex))
|
2020-04-09 19:43:42 +00:00
|
|
|
if ex.errcode == HTTP_BAD_REQUEST:
|
2020-10-02 19:59:55 +00:00
|
|
|
return self._show_form({"base": "invalid_auth"})
|
|
|
|
return self._show_form({"base": "cannot_connect"})
|
2019-10-13 18:01:04 +00:00
|
|
|
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_USERNAME],
|
|
|
|
data={
|
|
|
|
CONF_USERNAME: username,
|
|
|
|
CONF_PASSWORD: password,
|
|
|
|
CONF_POLLING: polling,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _show_form(self, errors=None):
|
|
|
|
"""Show the form to the user."""
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(self.data_schema),
|
|
|
|
errors=errors if errors else {},
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_import(self, import_config):
|
|
|
|
"""Import a config entry from configuration.yaml."""
|
|
|
|
if self._async_current_entries():
|
2020-03-04 22:54:28 +00:00
|
|
|
LOGGER.warning("Only one configuration of abode is allowed.")
|
2019-10-13 18:01:04 +00:00
|
|
|
return self.async_abort(reason="single_instance_allowed")
|
|
|
|
|
|
|
|
return await self.async_step_user(import_config)
|