Skip to content

Commit

Permalink
fix(config_flow): catch connection errors in config_flow and inform user
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Sep 23, 2019
1 parent cb3a8fb commit ef06760
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 3 additions & 1 deletion custom_components/alexa_media/.translations/en.json
@@ -1,8 +1,10 @@
{
"config": {
"error": {
"connection_error": "Error connecting; check network and retry",
"identifier_exists": "Email for Alexa URL already registered",
"invalid_credentials": "Invalid credentials"
"invalid_credentials": "Invalid credentials",
"unknown_error": "Unknown error, please report with logs"
},
"step": {
"user": {
Expand Down
33 changes: 21 additions & 12 deletions custom_components/alexa_media/config_flow.py
Expand Up @@ -12,6 +12,7 @@
from typing import Text

import voluptuous as vol
from alexapy import AlexapyConnectionError
from homeassistant import config_entries
from homeassistant.const import (CONF_EMAIL, CONF_NAME, CONF_PASSWORD,
CONF_SCAN_INTERVAL, CONF_URL,
Expand Down Expand Up @@ -178,10 +179,9 @@ async def async_step_user(self, user_input=None):
)
else:
self.config[CONF_EXCLUDE_DEVICES] = user_input[CONF_EXCLUDE_DEVICES]

if not self.login:
_LOGGER.debug("Creating new login")
try:
try:
if not self.login:
_LOGGER.debug("Creating new login")
self.login = AlexaLogin(
self.config[CONF_URL],
self.config[CONF_EMAIL],
Expand All @@ -191,13 +191,15 @@ async def async_step_user(self, user_input=None):
)
await self.login.login_with_cookie()
return await self._test_login()
except BaseException:
raise
return await self._show_form(errors={"base": "invalid_credentials"})
else:
_LOGGER.debug("Using existing login")
await self.login.login(data=user_input)
return await self._test_login()
else:
_LOGGER.debug("Using existing login")
await self.login.login(data=user_input)
return await self._test_login()
except AlexapyConnectionError:
return await self._show_form(errors={"base": "connection_error"})
except BaseException as ex:
_LOGGER.warning("Unknown error: %s", ex)
return await self._show_form(errors={"base": "unknown_error"})

async def async_step_captcha(self, user_input=None):
"""Handle the input processing of the config flow."""
Expand Down Expand Up @@ -225,7 +227,14 @@ async def async_step_process(self, user_input=None):
if CONF_PASSWORD in user_input:
password = user_input[CONF_PASSWORD]
self.config[CONF_PASSWORD] = password
await self.login.login(data=user_input)
try:
await self.login.login(data=user_input)
except AlexapyConnectionError:
return await self._show_form(
errors={"base": "connection_error"})
except BaseException as ex:
_LOGGER.warning("Unknown error: %s", ex)
return await self._show_form(errors={"base": "unknown_error"})
return await self._test_login()

async def _test_login(self):
Expand Down
4 changes: 3 additions & 1 deletion custom_components/alexa_media/strings.json
@@ -1,8 +1,10 @@
{
"config": {
"error": {
"connection_error": "Error connecting; check network and retry",
"identifier_exists": "Email for Alexa URL already registered",
"invalid_credentials": "Invalid credentials"
"invalid_credentials": "Invalid credentials",
"unknown_error": "Unknown error, please report log info"
},
"step": {
"user": {
Expand Down

0 comments on commit ef06760

Please sign in to comment.