Update Daikin config_flow with better error handling (#57069)
This commit is contained in:
parent
222a0c26e0
commit
a809f5fcf7
4 changed files with 27 additions and 13 deletions
|
@ -75,7 +75,8 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
uuid=uuid,
|
||||
password=password,
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
except (asyncio.TimeoutError, ClientError):
|
||||
self.host = None
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.schema,
|
||||
|
@ -87,13 +88,6 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
data_schema=self.schema,
|
||||
errors={"base": "invalid_auth"},
|
||||
)
|
||||
except ClientError:
|
||||
_LOGGER.exception("ClientError")
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.schema,
|
||||
errors={"base": "unknown"},
|
||||
)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected error creating device")
|
||||
return self.async_show_form(
|
||||
|
@ -109,6 +103,13 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""User initiated config flow."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(step_id="user", data_schema=self.schema)
|
||||
if user_input.get(CONF_API_KEY) and user_input.get(CONF_PASSWORD):
|
||||
self.host = user_input.get(CONF_HOST)
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=self.schema,
|
||||
errors={"base": "api_password"},
|
||||
)
|
||||
return await self._create_device(
|
||||
user_input[CONF_HOST],
|
||||
user_input.get(CONF_API_KEY),
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
"error": {
|
||||
"unknown": "[%key:common::config_flow::error::unknown%]",
|
||||
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
|
||||
"api_password": "[%key:common::config_flow::error::invalid_auth%], use either API Key or Password.",
|
||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
"cannot_connect": "Failed to connect"
|
||||
},
|
||||
"error": {
|
||||
"api_password": "Invalid authentication, use either API Key or Password.",
|
||||
"cannot_connect": "Failed to connect",
|
||||
"invalid_auth": "Invalid authentication",
|
||||
"unknown": "Unexpected error"
|
||||
|
|
|
@ -3,13 +3,12 @@
|
|||
import asyncio
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
from aiohttp import ClientError
|
||||
from aiohttp.web_exceptions import HTTPForbidden
|
||||
from aiohttp import ClientError, web_exceptions
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.daikin.const import KEY_MAC
|
||||
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PASSWORD
|
||||
from homeassistant.data_entry_flow import (
|
||||
RESULT_TYPE_ABORT,
|
||||
RESULT_TYPE_CREATE_ENTRY,
|
||||
|
@ -84,8 +83,8 @@ async def test_abort_if_already_setup(hass, mock_daikin):
|
|||
"s_effect,reason",
|
||||
[
|
||||
(asyncio.TimeoutError, "cannot_connect"),
|
||||
(HTTPForbidden, "invalid_auth"),
|
||||
(ClientError, "unknown"),
|
||||
(ClientError, "cannot_connect"),
|
||||
(web_exceptions.HTTPForbidden, "invalid_auth"),
|
||||
(Exception, "unknown"),
|
||||
],
|
||||
)
|
||||
|
@ -103,6 +102,18 @@ async def test_device_abort(hass, mock_daikin, s_effect, reason):
|
|||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
async def test_api_password_abort(hass):
|
||||
"""Test device abort."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
"daikin",
|
||||
context={"source": SOURCE_USER},
|
||||
data={CONF_HOST: HOST, CONF_API_KEY: "aa", CONF_PASSWORD: "aa"},
|
||||
)
|
||||
assert result["type"] == RESULT_TYPE_FORM
|
||||
assert result["errors"] == {"base": "api_password"}
|
||||
assert result["step_id"] == "user"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source, data, unique_id",
|
||||
[
|
||||
|
|
Loading…
Add table
Reference in a new issue