Abort nexia import if the username is already configured (#34863)

This commit is contained in:
J. Nick Koston 2020-04-29 16:02:59 -05:00 committed by GitHub
parent 6ce0819287
commit 97c82089b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 3 deletions

View file

@ -88,6 +88,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_import(self, user_input):
"""Handle import."""
for entry in self._async_current_entries():
if entry.data[CONF_USERNAME] == user_input[CONF_USERNAME]:
return self.async_abort(reason="already_configured")
return await self.async_step_user(user_input)

View file

@ -1,7 +1,7 @@
{
"domain": "nexia",
"name": "Nexia",
"requirements": ["nexia==0.9.2"],
"requirements": ["nexia==0.9.3"],
"codeowners": ["@ryannazaretian", "@bdraco"],
"documentation": "https://www.home-assistant.io/integrations/nexia",
"config_flow": true

View file

@ -931,7 +931,7 @@ netdisco==2.6.0
neurio==0.3.1
# homeassistant.components.nexia
nexia==0.9.2
nexia==0.9.3
# homeassistant.components.nextcloud
nextcloudmonitor==1.1.0

View file

@ -366,7 +366,7 @@ nessclient==0.9.15
netdisco==2.6.0
# homeassistant.components.nexia
nexia==0.9.2
nexia==0.9.3
# homeassistant.components.nsw_fuel_station
nsw-fuel-api-client==1.0.10

View file

@ -74,3 +74,44 @@ async def test_form_cannot_connect(hass):
assert result2["type"] == "form"
assert result2["errors"] == {"base": "cannot_connect"}
async def test_form_import(hass):
"""Test we get the form with import source."""
await setup.async_setup_component(hass, "persistent_notification", {})
with patch(
"homeassistant.components.nexia.config_flow.NexiaHome.get_name",
return_value="myhouse",
), patch(
"homeassistant.components.nexia.config_flow.NexiaHome.login",
side_effect=MagicMock(),
), patch(
"homeassistant.components.nexia.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.nexia.async_setup_entry", return_value=True,
) as mock_setup_entry:
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={CONF_USERNAME: "username", CONF_PASSWORD: "password"},
)
assert result["type"] == "create_entry"
assert result["title"] == "myhouse"
assert result["data"] == {
CONF_USERNAME: "username",
CONF_PASSWORD: "password",
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
result2 = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={CONF_USERNAME: "username", CONF_PASSWORD: "password"},
)
assert result2["type"] == "abort"
assert result2["reason"] == "already_configured"