diff --git a/README.md b/README.md index 341c8c9..38c363f 100644 --- a/README.md +++ b/README.md @@ -257,6 +257,14 @@ The WebSocket commands below can be used by custom cards/integrations along with | `type` | str | Yes | Must be `mass_queue/download_and_encode_image` | | `url` | str | Yes | URL of media to download. | +`mass_queue/get_user_info`: Returns a single image for a media item as a Base64 encoded string. Useful when avoiding mixed-content or when accessing local media outside of your network + +| Parameter | Type | Required | Description | +|-------------------|------|----------|---------|-----------------------------| +| `type` | str | Yes | Must be `mass_queue/get_user_info` | +| `entity_id` | str | Yes | URL of media to download. | +| `username` | str | Yes | Username of user to return info for. | + ## Installation 1. Download and install the integration by using the button above. diff --git a/custom_components/mass_queue/__init__.py b/custom_components/mass_queue/__init__.py index fd929ec..1546f8c 100644 --- a/custom_components/mass_queue/__init__.py +++ b/custom_components/mass_queue/__init__.py @@ -43,6 +43,7 @@ api_download_and_encode_image, api_download_images, api_get_entity_info, + api_get_user_info, ) if TYPE_CHECKING: @@ -146,6 +147,7 @@ async def on_hass_stop(event: Event) -> None: # noqa: ARG001 websocket_api.async_register_command(hass, api_download_images) websocket_api.async_register_command(hass, api_download_and_encode_image) websocket_api.async_register_command(hass, api_get_entity_info) + websocket_api.async_register_command(hass, api_get_user_info) # If the listen task is already failed, we need to raise ConfigEntryNotReady if listen_task.done() and (listen_error := listen_task.exception()) is not None: diff --git a/custom_components/mass_queue/utils.py b/custom_components/mass_queue/utils.py index b7402b0..7f1c2d8 100644 --- a/custom_components/mass_queue/utils.py +++ b/custom_components/mass_queue/utils.py @@ -295,6 +295,15 @@ async def download_and_encode_image(url: str, hass: HomeAssistant): return f"data:image;base64,{base64.b64encode(read).decode('utf-8')}" +async def get_user_info(hass: HomeAssistant, entity_id: str, username: str): + """Returns the user information for the given username.""" + client = get_mass_client(hass, entity_id) + users = await client.auth.list_users() + LOGGER.debug(f"Client: {client}") + LOGGER.debug(f"Users: {users}") + return [user.to_dict() for user in users if user.username == username][0] + + def get_entity_info(hass: HomeAssistant, entity_id: str): """Gets the server and client info for a given player.""" client = get_mass_client(hass, entity_id) diff --git a/custom_components/mass_queue/websocket_commands.py b/custom_components/mass_queue/websocket_commands.py index 5402b83..2f5236e 100644 --- a/custom_components/mass_queue/websocket_commands.py +++ b/custom_components/mass_queue/websocket_commands.py @@ -16,6 +16,7 @@ download_and_encode_image, download_single_image_from_image_data, get_entity_info, + get_user_info, ) @@ -101,3 +102,25 @@ async def get_playlist_items( msg: dict, ) -> None: """Retrieves all playlist items.""" + + +@websocket_api.websocket_command( + { + vol.Required("type"): "mass_queue/get_user_info", + vol.Required("entity_id"): str, + vol.Required("username"): str, + }, +) +@websocket_api.async_response +async def api_get_user_info( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict, +) -> None: + """Returns the information for a given user.""" + entity_id = msg["entity_id"] + username = msg["username"] + LOGGER.debug(f"Received message {msg}") + result = await get_user_info(hass, entity_id, username) + LOGGER.debug(f"Sending result {result}") + connection.send_result(msg["id"], result) diff --git a/docs/response_schemas.md b/docs/response_schemas.md index d7cd99f..50ff763 100644 --- a/docs/response_schemas.md +++ b/docs/response_schemas.md @@ -96,6 +96,10 @@ synced_to: str| None # Player(s) which are currently synced/grouped/joine type: str ``` +### GetUser +*See [music_assistant_models.auth.User](https://github.com/music-assistant/models/blob/0f2ad708ab26d2cc2ae008872afc00cd4a795380/music_assistant_models/auth.py#L29)* + + ### DownloadAndEncodeImageResponseSchema ```yaml