Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic user info API #120811

Closed
wants to merge 3 commits into from
Closed
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
22 changes: 22 additions & 0 deletions homeassistant/components/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
URL_API_STATES,
URL_API_STREAM,
URL_API_TEMPLATE,
URL_API_USER,
)
import homeassistant.core as ha
from homeassistant.core import Event, EventStateChangedData, HomeAssistant
Expand Down Expand Up @@ -74,6 +75,7 @@
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Register the API with the HTTP interface."""
hass.http.register_view(APIStatusView)
hass.http.register_view(APIUserView)
hass.http.register_view(APICoreStateView)
hass.http.register_view(APIEventStream)
hass.http.register_view(APIConfigView)
Expand Down Expand Up @@ -104,6 +106,26 @@ def get(self, request: web.Request) -> web.Response:
return self.json_message("API running.")


class APIUserView(HomeAssistantView):
"""View to handle User requests."""

url = URL_API_USER
name = "api:user"

@ha.callback
def get(self, request: web.Request) -> web.Response:
"""Retrieve user's info."""
user: User = request[KEY_HASS_USER]
return self.json(
{
"id": user.id,
"username": user.credentials[0].data["username"],
"name": user.name,
"is_admin": user.is_admin,
}
)


class APICoreStateView(HomeAssistantView):
"""View to handle core state requests."""

Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,7 @@ class UnitOfDataRate(StrEnum):

URL_ROOT: Final = "/"
URL_API: Final = "/api/"
URL_API_USER: Final = "/api/user"
URL_API_STREAM: Final = "/api/stream"
URL_API_CORE_STATE: Final = "/api/core/state"
URL_API_CONFIG: Final = "/api/config"
Expand Down
13 changes: 13 additions & 0 deletions tests/components/api/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,19 @@ async def test_event_stream_requires_admin(
assert resp.status == HTTPStatus.UNAUTHORIZED


async def test_api_user(
hass: HomeAssistant, mock_api_client: TestClient, hass_admin_user: MockUser
) -> None:
"""Test getting user information."""
resp = await mock_api_client.get("/api/user")
assert resp.status == HTTPStatus.OK
json = await resp.json()
assert json["id"] == hass_admin_user.id
assert json["username"] == hass_admin_user.credentials[0].data["username"]
assert json["name"] == hass_admin_user.name
assert json["is_admin"]


async def test_states(
hass: HomeAssistant, mock_api_client: TestClient, hass_admin_user: MockUser
) -> None:
Expand Down