diff --git a/homeassistant/auth/providers/homeassistant.py b/homeassistant/auth/providers/homeassistant.py index 4e38260dd2f..ec39bdbdcdc 100644 --- a/homeassistant/auth/providers/homeassistant.py +++ b/homeassistant/auth/providers/homeassistant.py @@ -55,13 +55,6 @@ class InvalidUser(HomeAssistantError): Will not be raised when validating authentication. """ - -class InvalidUsername(InvalidUser): - """Raised when invalid username is specified. - - Will not be raised when validating authentication. - """ - def __init__( self, *args: object, @@ -77,6 +70,13 @@ class InvalidUsername(InvalidUser): ) +class InvalidUsername(InvalidUser): + """Raised when invalid username is specified. + + Will not be raised when validating authentication. + """ + + class Data: """Hold the user data.""" @@ -216,7 +216,7 @@ class Data: break if index is None: - raise InvalidUser + raise InvalidUser(translation_key="user_not_found") self.users.pop(index) @@ -232,7 +232,7 @@ class Data: user["password"] = self.hash_password(new_password, True).decode() break else: - raise InvalidUser + raise InvalidUser(translation_key="user_not_found") @callback def _validate_new_username(self, new_username: str) -> None: @@ -275,7 +275,7 @@ class Data: self._async_check_for_not_normalized_usernames(self._data) break else: - raise InvalidUser + raise InvalidUser(translation_key="user_not_found") async def async_save(self) -> None: """Save data.""" diff --git a/homeassistant/components/auth/strings.json b/homeassistant/components/auth/strings.json index 0e4cede78a3..c8622880f0f 100644 --- a/homeassistant/components/auth/strings.json +++ b/homeassistant/components/auth/strings.json @@ -37,7 +37,10 @@ "message": "Username \"{username}\" already exists" }, "username_not_normalized": { - "message": "Username \"{new_username}\" is not normalized" + "message": "Username \"{new_username}\" is not normalized. Please make sure the username is lowercase and does not contain any whitespace." + }, + "user_not_found": { + "message": "User not found" } }, "issues": { diff --git a/homeassistant/components/config/auth_provider_homeassistant.py b/homeassistant/components/config/auth_provider_homeassistant.py index 1cfcda6d4b2..8513c53bd07 100644 --- a/homeassistant/components/config/auth_provider_homeassistant.py +++ b/homeassistant/components/config/auth_provider_homeassistant.py @@ -53,11 +53,7 @@ async def websocket_create( ) return - try: - await provider.async_add_auth(msg["username"], msg["password"]) - except auth_ha.InvalidUser: - connection.send_error(msg["id"], "username_exists", "Username already exists") - return + await provider.async_add_auth(msg["username"], msg["password"]) credentials = await provider.async_get_or_create_credentials( {"username": msg["username"]} @@ -94,13 +90,7 @@ async def websocket_delete( connection.send_result(msg["id"]) return - try: - await provider.async_remove_auth(msg["username"]) - except auth_ha.InvalidUser: - connection.send_error( - msg["id"], "auth_not_found", "Given username was not found." - ) - return + await provider.async_remove_auth(msg["username"]) connection.send_result(msg["id"]) @@ -187,14 +177,8 @@ async def websocket_admin_change_password( ) return - try: - await provider.async_change_password(username, msg["password"]) - connection.send_result(msg["id"]) - except auth_ha.InvalidUser: - connection.send_error( - msg["id"], "credentials_not_found", "Credentials not found" - ) - return + await provider.async_change_password(username, msg["password"]) + connection.send_result(msg["id"]) @websocket_api.websocket_command( diff --git a/tests/components/config/test_auth_provider_homeassistant.py b/tests/components/config/test_auth_provider_homeassistant.py index ffee88f91ec..6b580013968 100644 --- a/tests/components/config/test_auth_provider_homeassistant.py +++ b/tests/components/config/test_auth_provider_homeassistant.py @@ -183,7 +183,13 @@ async def test_create_auth_duplicate_username( result = await client.receive_json() assert not result["success"], result - assert result["error"]["code"] == "username_exists" + assert result["error"] == { + "code": "home_assistant_error", + "message": "username_already_exists", + "translation_key": "username_already_exists", + "translation_placeholders": {"username": "test-user"}, + "translation_domain": "auth", + } async def test_delete_removes_just_auth( @@ -282,7 +288,13 @@ async def test_delete_unknown_auth( result = await client.receive_json() assert not result["success"], result - assert result["error"]["code"] == "auth_not_found" + assert result["error"] == { + "code": "home_assistant_error", + "message": "user_not_found", + "translation_key": "user_not_found", + "translation_placeholders": None, + "translation_domain": "auth", + } async def test_change_password(