Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion homeassistant/components/unifi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
LOGGER,
)
from .controller import get_controller
from .errors import AlreadyConfigured, AuthenticationRequired, CannotConnect
from .errors import (
AlreadyConfigured,
AuthenticationRequired,
CannotConnect,
NoLocalUser,
)

DEFAULT_PORT = 8443
DEFAULT_SITE_ID = "default"
Expand Down Expand Up @@ -129,6 +134,8 @@ async def async_step_site(self, user_input=None):

for site in self.sites.values():
if desc == site["desc"]:
if "role" not in site:
raise NoLocalUser
self.config[CONF_SITE_ID] = site["name"]
break

Expand All @@ -147,6 +154,9 @@ async def async_step_site(self, user_input=None):
except AlreadyConfigured:
return self.async_abort(reason="already_configured")

except NoLocalUser:
return self.async_abort(reason="no_local_user")

if len(self.sites) == 1:
self.desc = next(iter(self.sites.values()))["desc"]
return await self.async_step_site(user_input={})
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/unifi/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ class LoginRequired(UnifiException):
"""Component got logged out."""


class NoLocalUser(UnifiException):
"""No local user."""


class UserLevel(UnifiException):
"""User level too low."""
1 change: 1 addition & 0 deletions homeassistant/components/unifi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"abort": {
"already_configured": "Controller site is already configured",
"no_local_user": "No local user found, configure a local account on controller and try again",
"user_privilege": "User needs to be administrator"
}
},
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/unifi/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"config": {
"abort": {
"already_configured": "Controller site is already configured",
"no_local_user": "No local user found, configure a local account on controller and try again",
"user_privilege": "User needs to be administrator"
},
"error": {
Expand Down
47 changes: 47 additions & 0 deletions tests/components/unifi/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,53 @@ async def test_flow_fails_site_already_configured(hass, aioclient_mock):
)

assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured"


async def test_flow_fails_site_has_no_local_user(hass, aioclient_mock):
"""Test config flow."""
entry = MockConfigEntry(
domain=UNIFI_DOMAIN, data={"controller": {"host": "1.2.3.4", "site": "site_id"}}
)
entry.add_to_hass(hass)

result = await hass.config_entries.flow.async_init(
UNIFI_DOMAIN, context={"source": "user"}
)

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "user"

aioclient_mock.get("https://1.2.3.4:1234", status=302)

aioclient_mock.post(
"https://1.2.3.4:1234/api/login",
json={"data": "login successful", "meta": {"rc": "ok"}},
headers={"content-type": "application/json"},
)

aioclient_mock.get(
"https://1.2.3.4:1234/api/self/sites",
json={
"data": [{"desc": "Site name", "name": "site_id"}],
"meta": {"rc": "ok"},
},
headers={"content-type": "application/json"},
)

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input={
CONF_HOST: "1.2.3.4",
CONF_USERNAME: "username",
CONF_PASSWORD: "password",
CONF_PORT: 1234,
CONF_VERIFY_SSL: True,
},
)

assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "no_local_user"


async def test_flow_fails_user_credentials_faulty(hass, aioclient_mock):
Expand Down