You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
GeoRideHA/custom_components/georide/config_flow.py

70 lines
2.7 KiB

""" Georide config flow """
5 years ago
import logging
5 years ago
from homeassistant import config_entries
5 years ago
import voluptuous as vol
import georideapilib.api as GeoRideApi
import georideapilib.exception as GeoRideException
5 years ago
from .const import CONF_EMAIL, CONF_PASSWORD, CONF_TOKEN, DOMAIN
_LOGGER = logging.getLogger(__name__)
@config_entries.HANDLERS.register(DOMAIN)
class GeoRideConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""GeoRide config flow """
async def async_step_user(self, user_input=None): #pylint: disable=W0613
""" handle info to help to configure GeoRide """
if self._async_current_entries():
return self.async_abort(reason="one_instance_allowed")
return self.async_show_form(step_id='georide_login', data_schema=vol.Schema({
vol.Required(CONF_EMAIL): vol.All(str, vol.Length(min=3)),
vol.Required(CONF_PASSWORD): vol.All(str)
}))
async def async_step_import(self, user_input=None): #pylint: disable=W0613
"""Import a config flow from configuration."""
if self._async_current_entries():
return self.async_abort(reason="one_instance_allowed")
return self.async_show_form(step_id='georide_login', data_schema=vol.Schema({
vol.Required(CONF_EMAIL): vol.All(str, vol.Length(min=3)),
vol.Required(CONF_PASSWORD): vol.All(str)
}))
async def async_step_georide_login(self, user_input):
""" try to seupt GeoRide Account """
schema = vol.Schema({
vol.Required(CONF_EMAIL): vol.All(str, vol.Length(min=3)),
vol.Required(CONF_PASSWORD): vol.All(str)
})
if user_input is None:
return self.async_show_form(step_id='georide_login', data_schema=schema)
email = user_input[CONF_EMAIL]
password = user_input[CONF_PASSWORD]
try:
account = await self.hass.async_add_executor_job(GeoRideApi.get_authorisation_token, email, password)
data = {
CONF_EMAIL: email,
CONF_PASSWORD: password,
CONF_TOKEN: account.auth_token
}
return self.async_create_entry(title=email, data=data)
except (GeoRideException.SeverException, GeoRideException.LoginException):
_LOGGER.error("Invalid credentials provided, config not created")
errors = {"base": "faulty_credentials"}
return self.async_show_form(step_id="georide_login", data_schema=schema, errors=errors)
except:
_LOGGER.error("Unknown error")
errors = {"base": "unkonwn"}
return self.async_show_form(step_id="georide_login", data_schema=schema, errors=errors)